-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_data_source.go
227 lines (206 loc) · 7.86 KB
/
group_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package provider
import (
"context"
"fmt"
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &GroupDataSource{}
func NewGroupDataSource() datasource.DataSource {
return &GroupDataSource{}
}
// GroupDataSource defines the data source implementation.
type GroupDataSource struct {
*CoderdProviderData
}
// GroupDataSourceModel describes the data source data model.
type GroupDataSourceModel struct {
// ID or name and organization ID must be set
ID UUID `tfsdk:"id"`
Name types.String `tfsdk:"name"`
OrganizationID UUID `tfsdk:"organization_id"`
DisplayName types.String `tfsdk:"display_name"`
AvatarURL types.String `tfsdk:"avatar_url"`
QuotaAllowance types.Int32 `tfsdk:"quota_allowance"`
Source types.String `tfsdk:"source"`
Members []Member `tfsdk:"members"`
}
type Member struct {
ID UUID `tfsdk:"id"`
Username types.String `tfsdk:"username"`
Email types.String `tfsdk:"email"`
CreatedAt types.Int64 `tfsdk:"created_at"`
LastSeenAt types.Int64 `tfsdk:"last_seen_at"`
Status types.String `tfsdk:"status"`
LoginType types.String `tfsdk:"login_type"`
ThemePreference types.String `tfsdk:"theme_preference"`
}
func (d *GroupDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_group"
}
func (d *GroupDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "An existing group on the Coder deployment.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the group to retrieve. This field will be populated if a name and organization ID is supplied.",
Optional: true,
Computed: true,
CustomType: UUIDType,
Validators: []validator.String{
stringvalidator.AtLeastOneOf(path.Expressions{
path.MatchRoot("name"),
}...),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the group to retrieve. This field will be populated if an ID is supplied.",
Optional: true,
Computed: true,
Validators: []validator.String{},
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "The organization ID that the group belongs to. This field will be populated if an ID is supplied. Defaults to the provider default organization ID.",
CustomType: UUIDType,
Optional: true,
Computed: true,
},
"display_name": schema.StringAttribute{
Computed: true,
},
"avatar_url": schema.StringAttribute{
Computed: true,
},
"quota_allowance": schema.Int32Attribute{
MarkdownDescription: "The number of quota credits to allocate to each user in the group.",
Computed: true,
},
"source": schema.StringAttribute{
MarkdownDescription: "The source of the group. Either `oidc` or `user`.",
Computed: true,
},
"members": schema.SetNestedAttribute{
MarkdownDescription: "Members of the group.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
CustomType: UUIDType,
Computed: true,
},
"username": schema.StringAttribute{
Computed: true,
},
"email": schema.StringAttribute{
Computed: true,
},
"created_at": schema.Int64Attribute{
MarkdownDescription: "Unix timestamp of when the member was created.",
Computed: true,
},
"last_seen_at": schema.Int64Attribute{
MarkdownDescription: "Unix timestamp of when the member was last seen.",
Computed: true,
},
"status": schema.StringAttribute{
MarkdownDescription: "The status of the member. Can be `active`, `dormant` or `suspended`.",
Computed: true,
},
"login_type": schema.StringAttribute{
MarkdownDescription: "The login type of the member. Can be `oidc`, `token`, `password`, `github` or `none`.",
Computed: true,
},
"theme_preference": schema.StringAttribute{
Computed: true,
},
// TODO: Upgrade requested user type if required
},
},
},
},
}
}
func (d *GroupDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*CoderdProviderData)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.CoderdProviderData = data
}
func (d *GroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Read Terraform configuration data into the model
var data GroupDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(CheckGroupEntitlements(ctx, d.Features)...)
if resp.Diagnostics.HasError() {
return
}
var (
group codersdk.Group
err error
)
if !data.ID.IsNull() {
groupID := data.ID.ValueUUID()
group, err = d.Client.Group(ctx, groupID)
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with ID %s not found. Marking as deleted.", groupID.String()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get group by ID, got error: %s", err))
return
}
data.Name = types.StringValue(group.Name)
data.OrganizationID = UUIDValue(group.OrganizationID)
} else {
group, err = d.Client.GroupByOrgAndName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", fmt.Sprintf("Group with name %s not found in organization with ID %s. Marking as deleted.", data.Name.ValueString(), data.OrganizationID.ValueString()))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Failed to get group by name and org ID", err.Error())
return
}
data.ID = UUIDValue(group.ID)
}
data.DisplayName = types.StringValue(group.DisplayName)
data.AvatarURL = types.StringValue(group.AvatarURL)
data.QuotaAllowance = types.Int32Value(int32(group.QuotaAllowance))
members := make([]Member, 0, len(group.Members))
for _, member := range group.Members {
members = append(members, Member{
ID: UUIDValue(member.ID),
Username: types.StringValue(member.Username),
Email: types.StringValue(member.Email),
CreatedAt: types.Int64Value(member.CreatedAt.Unix()),
LastSeenAt: types.Int64Value(member.LastSeenAt.Unix()),
Status: types.StringValue(string(member.Status)),
LoginType: types.StringValue(string(member.LoginType)),
ThemePreference: types.StringValue(member.ThemePreference),
})
}
data.Members = members
data.Source = types.StringValue(string(group.Source))
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}