/
user_data_source.go
142 lines (126 loc) · 4.52 KB
/
user_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
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"
)
var (
_ datasource.DataSource = (*userDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*userDataSource)(nil)
)
func NewUserDataSource() datasource.DataSource {
return &userDataSource{}
}
type userDataSource struct {
client *api.Client
}
type usersDataSourceModel struct {
ID types.String `tfsdk:"id"`
Users []userResourceModel `tfsdk:"users"`
}
func (usr *userDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_users"
}
func (usr *userDataSource) 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 or pseudo of the user.",
Computed: true,
},
"email": schema.StringAttribute{
MarkdownDescription: "Email of the user, may be used to verify the account.",
Computed: true,
},
"password": schema.StringAttribute{
MarkdownDescription: "Password of the user. Notice that during a CTF you may not want to update those to avoid defaulting user 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 team, company or agency.",
Computed: true,
},
"country": schema.StringAttribute{
MarkdownDescription: "Country the user represent or is native from.",
Computed: true,
},
"language": schema.StringAttribute{
MarkdownDescription: "Language the user is fluent in.",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "Generic type for RBAC purposes.",
Computed: true,
},
"verified": schema.BoolAttribute{
MarkdownDescription: "Is true if the user has verified its account by email, or if set by an admin.",
Computed: true,
},
"hidden": schema.BoolAttribute{
MarkdownDescription: "Is true if the user is hidden to the participants.",
Computed: true,
},
"banned": schema.BoolAttribute{
MarkdownDescription: "Is true if the user is banned from the CTF.",
Computed: true,
},
},
}
}
func (usr *userDataSource) 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
}
usr.client = client
}
func (usr *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state usersDataSourceModel
users, err := usr.client.GetUsers(&api.GetUsersParams{}, api.WithContext(ctx))
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read CTFd Users",
err.Error(),
)
return
}
state.Users = make([]userResourceModel, 0, len(users))
for _, u := range users {
// Flatten response
state.Users = append(state.Users, userResourceModel{
ID: types.StringValue(strconv.Itoa(u.ID)),
Name: types.StringValue(u.Name),
Email: types.StringPointerValue(u.Email),
Password: types.StringValue("placeholder"),
Website: types.StringPointerValue(u.Website),
Affiliation: types.StringPointerValue(u.Affiliation),
Country: types.StringPointerValue(u.Country),
Language: types.StringPointerValue(u.Language),
Type: types.StringPointerValue(u.Type),
Verified: types.BoolPointerValue(u.Verified),
Hidden: types.BoolPointerValue(u.Hidden),
Banned: types.BoolPointerValue(u.Banned),
})
}
state.ID = types.StringValue("placeholder")
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}