-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtemplate_data_source.go
More file actions
347 lines (319 loc) · 14.1 KB
/
template_data_source.go
File metadata and controls
347 lines (319 loc) · 14.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package provider
import (
"context"
"fmt"
"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"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 = &TemplateDataSource{}
func NewTemplateDataSource() datasource.DataSource {
return &TemplateDataSource{}
}
// TemplateDataSource defines the data source implementation.
type TemplateDataSource struct {
*CoderdProviderData
}
// TemplateDataSourceModel describes the data source data model.
type TemplateDataSourceModel struct {
// ((Organization and Name) or ID) must be set
OrganizationID UUID `tfsdk:"organization_id"`
ID UUID `tfsdk:"id"`
Name types.String `tfsdk:"name"`
DisplayName types.String `tfsdk:"display_name"`
// TODO: Provisioner
Description types.String `tfsdk:"description"`
ActiveVersionID UUID `tfsdk:"active_version_id"`
ActiveUserCount types.Int64 `tfsdk:"active_user_count"`
Deprecated types.Bool `tfsdk:"deprecated"`
DeprecationMessage types.String `tfsdk:"deprecation_message"`
Icon types.String `tfsdk:"icon"`
DefaultTTLMillis types.Int64 `tfsdk:"default_ttl_ms"`
ActivityBumpMillis types.Int64 `tfsdk:"activity_bump_ms"`
AutostopRequirement types.Object `tfsdk:"auto_stop_requirement"`
AutostartPermittedDaysOfWeek types.Set `tfsdk:"auto_start_permitted_days_of_week"`
AllowUserAutostart types.Bool `tfsdk:"allow_user_autostart"`
AllowUserAutostop types.Bool `tfsdk:"allow_user_autostop"`
AllowUserCancelWorkspaceJobs types.Bool `tfsdk:"allow_user_cancel_workspace_jobs"`
FailureTTLMillis types.Int64 `tfsdk:"failure_ttl_ms"`
TimeTilDormantMillis types.Int64 `tfsdk:"time_til_dormant_ms"`
TimeTilDormantAutoDeleteMillis types.Int64 `tfsdk:"time_til_dormant_autodelete_ms"`
RequireActiveVersion types.Bool `tfsdk:"require_active_version"`
MaxPortShareLevel types.String `tfsdk:"max_port_share_level"`
CreatedByUserID UUID `tfsdk:"created_by_user_id"`
CreatedAt types.Int64 `tfsdk:"created_at"` // Unix timestamp
UpdatedAt types.Int64 `tfsdk:"updated_at"` // Unix timestamp
ACL types.Object `tfsdk:"acl"`
}
func (d *TemplateDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_template"
}
func (d *TemplateDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "An existing template on the Coder deployment.",
Attributes: map[string]schema.Attribute{
"organization_id": schema.StringAttribute{
MarkdownDescription: "ID of the organization the template is associated with. This field will be populated if an ID is supplied. Defaults to the provider default organization ID.",
CustomType: UUIDType,
Optional: true,
Computed: true,
},
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the template to retrieve. This field will be populated if a template name is supplied.",
CustomType: UUIDType,
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.AtLeastOneOf(path.Expressions{
path.MatchRoot("name"),
}...),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the template to retrieve. This field will be populated if an ID is supplied.",
Optional: true,
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: "Display name of the template.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the template.",
Computed: true,
},
"active_version_id": schema.StringAttribute{
MarkdownDescription: "ID of the active version of the template.",
CustomType: UUIDType,
Computed: true,
},
"active_user_count": schema.Int64Attribute{
MarkdownDescription: "Number of active users using the template.",
Computed: true,
},
"deprecated": schema.BoolAttribute{
MarkdownDescription: "Whether the template is deprecated.",
Computed: true,
},
"deprecation_message": schema.StringAttribute{
MarkdownDescription: "Message to display when the template is deprecated.",
Computed: true,
},
"icon": schema.StringAttribute{
MarkdownDescription: "URL of the template's icon.",
Computed: true,
},
"default_ttl_ms": schema.Int64Attribute{
MarkdownDescription: "Default time-to-live for workspaces created from the template.",
Computed: true,
},
"activity_bump_ms": schema.Int64Attribute{
MarkdownDescription: "Duration to bump the deadline of a workspace when it receives activity.",
Computed: true,
},
"auto_stop_requirement": schema.SingleNestedAttribute{
MarkdownDescription: "The auto-stop requirement for all workspaces created from this template.",
Computed: true,
Attributes: map[string]schema.Attribute{
"days_of_week": schema.SetAttribute{
MarkdownDescription: "List of days of the week on which restarts are required. Restarts happen within the user's quiet hours (in their configured timezone). If no days are specified, restarts are not required.",
Computed: true,
ElementType: types.StringType,
},
"weeks": schema.Int64Attribute{
MarkdownDescription: "Weeks is the number of weeks between required restarts. Weeks are synced across all workspaces (and Coder deployments) using modulo math on a hardcoded epoch week of January 2nd, 2023 (the first Monday of 2023). Values of 0 or 1 indicate weekly restarts. Values of 2 indicate fortnightly restarts, etc.",
Optional: true,
Computed: true,
},
},
},
"auto_start_permitted_days_of_week": schema.SetAttribute{
MarkdownDescription: "List of days of the week in which autostart is allowed to happen, for all workspaces created from this template. Defaults to all days. If no days are specified, autostart is not allowed.",
Computed: true,
ElementType: types.StringType,
},
"allow_user_autostart": schema.BoolAttribute{
MarkdownDescription: "Whether users can autostart workspaces created from the template.",
Computed: true,
},
"allow_user_autostop": schema.BoolAttribute{
MarkdownDescription: "Whether users can customize autostop behavior for workspaces created from the template.",
Computed: true,
},
"allow_user_cancel_workspace_jobs": schema.BoolAttribute{
MarkdownDescription: "Whether users can cancel jobs in workspaces created from the template.",
Computed: true,
},
"failure_ttl_ms": schema.Int64Attribute{
MarkdownDescription: "Automatic cleanup TTL for failed workspace builds.",
Computed: true,
},
"time_til_dormant_ms": schema.Int64Attribute{
MarkdownDescription: "Duration of inactivity before a workspace is considered dormant.",
Computed: true,
},
"time_til_dormant_autodelete_ms": schema.Int64Attribute{
MarkdownDescription: "Duration of inactivity after the workspace becomes dormant before a workspace is automatically deleted.",
Computed: true,
},
"require_active_version": schema.BoolAttribute{
MarkdownDescription: "Whether workspaces created from the template must be up-to-date on the latest active version.",
Computed: true,
},
"max_port_share_level": schema.StringAttribute{
MarkdownDescription: "The maximum port share level for workspaces created from the template.",
Computed: true,
},
"created_by_user_id": schema.StringAttribute{
MarkdownDescription: "ID of the user who created the template.",
CustomType: UUIDType,
Computed: true,
},
"created_at": schema.Int64Attribute{
MarkdownDescription: "Unix timestamp of when the template was created.",
Computed: true,
},
"updated_at": schema.Int64Attribute{
MarkdownDescription: "Unix timestamp of when the template was last updated.",
Computed: true,
},
"acl": schema.SingleNestedAttribute{
MarkdownDescription: "(Enterprise) Access control list for the template.",
Computed: true,
Attributes: map[string]schema.Attribute{
"users": computedPermissionAttribute,
"groups": computedPermissionAttribute,
},
},
},
}
}
func (d *TemplateDataSource) 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 *TemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// Read Terraform configuration data into the model
var data TemplateDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var (
template codersdk.Template
err error
)
if data.ID.ValueUUID() != uuid.Nil {
template, err = d.Client.Template(ctx, data.ID.ValueUUID())
} else {
if data.OrganizationID.ValueUUID() == uuid.Nil {
resp.Diagnostics.AddError("Client Error", "name requires organization_id to be set")
return
}
template, err = d.Client.TemplateByName(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString())
}
if err != nil {
if isNotFound(err) {
resp.Diagnostics.AddWarning("Client Warning", "Template not found. Marking as deleted.")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get template, got error: %s", err))
return
}
if !data.ID.IsNull() && template.ID.String() != data.ID.ValueString() {
resp.Diagnostics.AddError("Client Error", "Retrieved Template's ID does not match the provided ID")
return
}
if !data.Name.IsNull() && template.Name != data.Name.ValueString() {
resp.Diagnostics.AddError("Client Error", "Retrieved Template's name does not match the provided name")
return
}
if !data.OrganizationID.IsNull() && template.OrganizationID.String() != data.OrganizationID.ValueString() {
resp.Diagnostics.AddError("Client Error", "Retrieved Template's organization ID does not match the provided organization ID")
return
}
acl, err := d.Client.TemplateACL(ctx, template.ID)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to get template ACL: %s", err))
return
}
tfACL := convertResponseToACL(acl)
aclObj, diag := types.ObjectValueFrom(ctx, aclTypeAttr, tfACL)
if diag.HasError() {
resp.Diagnostics.Append(diag...)
return
}
asrObj, diag := types.ObjectValueFrom(ctx, autostopRequirementTypeAttr, AutostopRequirement{
DaysOfWeek: template.AutostopRequirement.DaysOfWeek,
Weeks: template.AutostopRequirement.Weeks,
})
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
autoStartDays := make([]attr.Value, 0, len(template.AutostartRequirement.DaysOfWeek))
for _, day := range template.AutostartRequirement.DaysOfWeek {
autoStartDays = append(autoStartDays, types.StringValue(day))
}
data.ACL = aclObj
data.AutostartPermittedDaysOfWeek = types.SetValueMust(types.StringType, autoStartDays)
data.AutostopRequirement = asrObj
data.OrganizationID = UUIDValue(template.OrganizationID)
data.ID = UUIDValue(template.ID)
data.Name = types.StringValue(template.Name)
data.DisplayName = types.StringValue(template.DisplayName)
data.Description = types.StringValue(template.Description)
data.ActiveVersionID = UUIDValue(template.ActiveVersionID)
data.ActiveUserCount = types.Int64Value(int64(template.ActiveUserCount))
data.Deprecated = types.BoolValue(template.Deprecated)
data.DeprecationMessage = types.StringValue(template.DeprecationMessage)
data.Icon = types.StringValue(template.Icon)
data.DefaultTTLMillis = types.Int64Value(template.DefaultTTLMillis)
data.ActivityBumpMillis = types.Int64Value(template.ActivityBumpMillis)
data.AllowUserAutostart = types.BoolValue(template.AllowUserAutostart)
data.AllowUserAutostop = types.BoolValue(template.AllowUserAutostop)
data.AllowUserCancelWorkspaceJobs = types.BoolValue(template.AllowUserCancelWorkspaceJobs)
data.FailureTTLMillis = types.Int64Value(template.FailureTTLMillis)
data.TimeTilDormantMillis = types.Int64Value(template.TimeTilDormantMillis)
data.TimeTilDormantAutoDeleteMillis = types.Int64Value(template.TimeTilDormantAutoDeleteMillis)
data.RequireActiveVersion = types.BoolValue(template.RequireActiveVersion)
data.MaxPortShareLevel = types.StringValue(string(template.MaxPortShareLevel))
data.CreatedByUserID = UUIDValue(template.CreatedByID)
data.CreatedAt = types.Int64Value(template.CreatedAt.Unix())
data.UpdatedAt = types.Int64Value(template.UpdatedAt.Unix())
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
// computedPermissionAttribute is the attribute schema for a computed instance of `[]Permission`.
var computedPermissionAttribute = schema.SetNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"role": schema.StringAttribute{
Computed: true,
},
},
},
}