/
team_data_source.go
143 lines (127 loc) · 4.67 KB
/
team_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package provider
import (
"context"
"fmt"
"strconv"
"github.com/ctfer-io/go-ctfd/api"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
var (
_ datasource.DataSource = (*teamDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*teamDataSource)(nil)
)
func NewTeamDataSource() datasource.DataSource {
return &teamDataSource{}
}
type teamDataSource struct {
client *api.Client
}
type teamsDataSourceModel struct {
ID types.String `tfsdk:"id"`
Teams []teamResourceModel `tfsdk:"teams"`
}
func (team *teamDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_teams"
}
func (team *teamDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Identifier of the user.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the team.",
Computed: true,
},
"email": schema.StringAttribute{
MarkdownDescription: "Email of the team.",
Computed: true,
},
"password": schema.StringAttribute{
MarkdownDescription: "Password of the team. Notice that during a CTF you may not want to update those to avoid defaulting team accesses.",
Computed: true,
},
"website": schema.StringAttribute{
MarkdownDescription: "Website, blog, or anything similar (displayed to other participants).",
Computed: true,
},
"affiliation": schema.StringAttribute{
MarkdownDescription: "Affiliation to a company or agency.",
Computed: true,
},
"country": schema.StringAttribute{
MarkdownDescription: "Country the team represent or is hail from.",
Computed: true,
},
"hidden": schema.BoolAttribute{
MarkdownDescription: "Is true if the team is hidden to the participants.",
Computed: true,
},
"banned": schema.BoolAttribute{
MarkdownDescription: "Is true if the team is banned from the CTF.",
Computed: true,
},
"members": schema.ListAttribute{
MarkdownDescription: "List of members (User), defined by their IDs.",
ElementType: types.StringType,
Computed: true,
},
"captain": schema.StringAttribute{
MarkdownDescription: "Member who is captain of the team. Must be part of the members too. Note it could cause a fatal error in case of resource import with an inconsistent CTFd configuration i.e. if a team has no captain yet (should not be possible).",
Computed: true,
},
},
}
}
func (team *teamDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*api.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *github.com/ctfer-io/go-ctfd/api.Client, got: %T. Please open an issue at https://github.com/ctfer-io/terraform-provider-ctfd", req.ProviderData),
)
return
}
team.client = client
}
func (team *teamDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state teamsDataSourceModel
teams, err := team.client.GetTeams(&api.GetTeamsParams{}, api.WithContext(ctx))
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read CTFd Teams",
err.Error(),
)
return
}
state.Teams = make([]teamResourceModel, 0, len(teams))
for _, t := range teams {
// Flatten response
members := make([]basetypes.StringValue, 0, len(t.Members))
for _, tm := range t.Members {
members = append(members, types.StringValue(strconv.Itoa(tm)))
}
state.Teams = append(state.Teams, teamResourceModel{
ID: types.StringValue(strconv.Itoa(t.ID)),
Name: types.StringValue(t.Name),
Email: types.StringValue(*t.Email),
Password: types.StringValue("placeholder"),
Website: types.StringValue(*t.Website),
Affiliation: types.StringValue(*t.Affiliation),
Country: types.StringValue(*t.Country),
Hidden: types.BoolValue(t.Hidden),
Banned: types.BoolValue(t.Banned),
Members: members,
Captain: types.StringValue(strconv.Itoa(*t.CaptainID)),
})
}
state.ID = types.StringValue("placeholder")
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}