-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_workers_service.go
506 lines (433 loc) · 15.2 KB
/
resource_workers_service.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package platform
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/jfrog/terraform-provider-shared/util"
utilfw "github.com/jfrog/terraform-provider-shared/util/fw"
"github.com/samber/lo"
)
const WorkersServiceEndpoint = "worker/api/v1/workers"
var validActions = []string{
"BEFORE_DOWNLOAD",
"AFTER_DOWNLOAD",
"BEFORE_UPLOAD",
"AFTER_CREATE",
"AFTER_BUILD_INFO_SAVE",
"AFTER_MOVE",
}
var _ resource.Resource = (*workersServiceResource)(nil)
type workersServiceResource struct {
ProviderData util.ProviderMetadata
TypeName string
}
func NewWorkerServiceResource() resource.Resource {
return &workersServiceResource{
TypeName: "platform_workers_service",
}
}
func (r *workersServiceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = r.TypeName
}
func (r *workersServiceResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"key": schema.StringAttribute{
Required: true,
Description: "The unique ID of the worker.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Optional: true,
Description: "Description of the worker.",
},
"enabled": schema.BoolAttribute{
Required: true,
Description: "Whether to enable the worker immediately after creation.",
},
"source_code": schema.StringAttribute{
Required: true,
Description: "The worker script in TypeScript or JavaScript.",
},
"action": schema.StringAttribute{
Required: true,
Description: fmt.Sprintf("The worker action with which the worker is associated. Valid values: %s", strings.Join(validActions, ", ")),
Validators: []validator.String{stringvalidator.OneOf(validActions...)},
},
"filter_criteria": schema.SingleNestedAttribute{
Required: true,
Description: "Defines the repositories to be used or excluded.",
Attributes: map[string]schema.Attribute{
"artifact_filter_criteria": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"repo_keys": schema.SetAttribute{
ElementType: types.StringType,
Required: true,
Description: "Defines which repositories are used when an action event occurs to trigger the worker.",
},
"include_patterns": schema.SetAttribute{
ElementType: types.StringType,
Optional: true,
Description: "Define patterns to match all repository paths for repositories identified in the repoKeys. Defines those repositories that trigger the worker.",
},
"exclude_patterns": schema.SetAttribute{
ElementType: types.StringType,
Optional: true,
Description: "Define patterns to for all repository paths for repositories to be excluded in the repoKeys. Defines those repositories that do not trigger the worker.",
},
},
},
},
},
"secrets": schema.SetNestedAttribute{
Optional: true,
Description: "The secrets to be added to the worker.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"key": schema.StringAttribute{
Required: true,
Description: "The name of the secret.",
},
"value": schema.StringAttribute{
Required: true,
Description: "The name of the secret.",
},
},
},
},
},
Description: "Provides a JFrog [Workers Service](https://jfrog.com/help/r/jfrog-platform-administration-documentation/workers-service) resource. This can be used to create and manage Workers Service.\n\n" +
"->From Artifactory 7.94 the Workers service will be available in a general availability release to Enterprise X and Enterprise+ licenses.",
}
}
type workersServiceResourceModel struct {
Key types.String `tfsdk:"key"`
Description types.String `tfsdk:"description"`
SourceCode types.String `tfsdk:"source_code"`
Action types.String `tfsdk:"action"`
FilterCriteria types.Object `tfsdk:"filter_criteria"`
Enabled types.Bool `tfsdk:"enabled"`
Secrets types.Set `tfsdk:"secrets"`
}
type filterCriteriaResourceModel struct {
ArtifactFilterCriteria types.Object `tfsdk:"artifact_filter_criteria"`
}
type artifactFilterCriteriaResourceModel struct {
RepoKeys types.Set `tfsdk:"repo_keys"`
IncludePatterns types.Set `tfsdk:"include_patterns"`
ExcludePatterns types.Set `tfsdk:"exclude_patterns"`
}
func (r *workersServiceResourceModel) toAPIModel(ctx context.Context, apiModel *WorkersServiceAPIModel, secretKeysToBeRemoved []string) (ds diag.Diagnostics) {
var filterCriteria filterCriteriaResourceModel
ds.Append(r.FilterCriteria.As(ctx, &filterCriteria, basetypes.ObjectAsOptions{})...)
if ds.HasError() {
return
}
var artifactFilterCriteria artifactFilterCriteriaResourceModel
ds.Append(filterCriteria.ArtifactFilterCriteria.As(ctx, &artifactFilterCriteria, basetypes.ObjectAsOptions{})...)
if ds.HasError() {
return
}
var repoKeys []string
artifactFilterCriteria.RepoKeys.ElementsAs(ctx, &repoKeys, false)
var includePatterns []string
artifactFilterCriteria.IncludePatterns.ElementsAs(ctx, &includePatterns, false)
var excludePatterns []string
artifactFilterCriteria.ExcludePatterns.ElementsAs(ctx, &excludePatterns, false)
secrets := lo.Map[attr.Value](
r.Secrets.Elements(),
func(elem attr.Value, index int) secretAPIModel {
attr := elem.(types.Object).Attributes()
return secretAPIModel{
Key: attr["key"].(types.String).ValueString(),
Value: attr["value"].(types.String).ValueString(),
}
},
)
for _, secretKeyToBeRemoved := range secretKeysToBeRemoved {
s := secretAPIModel{
Key: secretKeyToBeRemoved,
MarkedForRemoval: true,
}
secrets = append(secrets, s)
}
*apiModel = WorkersServiceAPIModel{
Key: r.Key.ValueString(),
Description: r.Description.ValueString(),
SourceCode: r.SourceCode.ValueString(),
Action: r.Action.ValueString(),
FilterCriteria: filterCriteriaAPIModel{
ArtifactFilterCriteria: artifactFilterCriteriaAPIModel{
RepoKeys: repoKeys,
IncludePatterns: includePatterns,
ExcludePatterns: excludePatterns,
},
},
Enabled: r.Enabled.ValueBool(),
Secrets: secrets,
}
return nil
}
var filterCriteriaResourceModelAttributeTypes map[string]attr.Type = map[string]attr.Type{
"artifact_filter_criteria": types.ObjectType{
AttrTypes: artifactFilterCriteriaResourceModelAttributeTypes,
},
}
var artifactFilterCriteriaResourceModelAttributeTypes map[string]attr.Type = map[string]attr.Type{
"repo_keys": types.SetType{ElemType: types.StringType},
"include_patterns": types.SetType{ElemType: types.StringType},
"exclude_patterns": types.SetType{ElemType: types.StringType},
}
func (r *workersServiceResourceModel) fromAPIModel(ctx context.Context, apiModel *WorkersServiceAPIModel) (ds diag.Diagnostics) {
r.Key = types.StringValue(apiModel.Key)
r.Description = types.StringValue(apiModel.Description)
r.SourceCode = types.StringValue(apiModel.SourceCode)
r.Action = types.StringValue(apiModel.Action)
repoKeys, d := types.SetValueFrom(
ctx,
types.StringType,
apiModel.FilterCriteria.ArtifactFilterCriteria.RepoKeys,
)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
includePatterns, d := types.SetValueFrom(
ctx,
types.StringType,
apiModel.FilterCriteria.ArtifactFilterCriteria.IncludePatterns,
)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
excludePatterns, d := types.SetValueFrom(
ctx,
types.StringType,
apiModel.FilterCriteria.ArtifactFilterCriteria.ExcludePatterns,
)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
artifactFilterCriteriaValue := artifactFilterCriteriaResourceModel{
RepoKeys: repoKeys,
IncludePatterns: includePatterns,
ExcludePatterns: excludePatterns,
}
atrifactFilterCriteria, d := types.ObjectValueFrom(
ctx,
artifactFilterCriteriaResourceModelAttributeTypes,
artifactFilterCriteriaValue,
)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
filterCriteria, d := types.ObjectValue(
filterCriteriaResourceModelAttributeTypes,
map[string]attr.Value{
"artifact_filter_criteria": atrifactFilterCriteria,
},
)
if d != nil {
ds = append(ds, d...)
}
if ds.HasError() {
return
}
r.FilterCriteria = filterCriteria
r.Enabled = types.BoolValue(apiModel.Enabled)
return
}
type WorkersServiceAPIModel struct {
Key string `json:"key"`
Description string `json:"description"`
SourceCode string `json:"sourceCode"`
Action string `json:"action"`
FilterCriteria filterCriteriaAPIModel `json:"filterCriteria"`
Enabled bool `json:"enabled"`
Secrets []secretAPIModel `json:"secrets"`
}
type filterCriteriaAPIModel struct {
ArtifactFilterCriteria artifactFilterCriteriaAPIModel `json:"artifactFilterCriteria"`
}
type artifactFilterCriteriaAPIModel struct {
RepoKeys []string `json:"repoKeys"`
IncludePatterns []string `json:"includePatterns,omitempty"`
ExcludePatterns []string `json:"excludePatterns,omitempty"`
}
type secretAPIModel struct {
Key string `json:"key"`
Value string `json:"value"`
MarkedForRemoval bool `json:"markedForRemoval,omitempty"`
}
func (r *workersServiceResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
r.ProviderData = req.ProviderData.(util.ProviderMetadata)
}
func (r *workersServiceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
go util.SendUsageResourceCreate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan workersServiceResourceModel
diags := req.Config.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
var workersService WorkersServiceAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &workersService, []string{})...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetBody(&workersService).
Post(WorkersServiceEndpoint)
if err != nil {
utilfw.UnableToCreateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToCreateResourceError(resp, response.String())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *workersServiceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
go util.SendUsageResourceRead(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var state workersServiceResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
var workersService WorkersServiceAPIModel
response, err := r.ProviderData.Client.R().
SetPathParam("key", state.Key.ValueString()).
SetResult(&workersService).
Get(WorkersServiceEndpoint + "/{key}")
if err != nil {
utilfw.UnableToRefreshResourceError(resp, err.Error())
return
}
// Treat HTTP 404 Not Found status as a signal to recreate resource
// and return early
if response.StatusCode() == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
if response.IsError() {
utilfw.UnableToRefreshResourceError(resp, response.String())
return
}
// Convert from the API data model to the Terraform data model
// and refresh any attribute values.
resp.Diagnostics.Append(state.fromAPIModel(ctx, &workersService)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *workersServiceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
go util.SendUsageResourceUpdate(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var plan workersServiceResourceModel
var state workersServiceResourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
planSecrets := lo.Map(
plan.Secrets.Elements(),
func(elem attr.Value, index int) secretAPIModel {
attrs := elem.(types.Object).Attributes()
return secretAPIModel{
Key: attrs["key"].(types.String).ValueString(),
}
},
)
stateSecrets := lo.Map(state.Secrets.Elements(), func(elem attr.Value, index int) secretAPIModel {
attrs := elem.(types.Object).Attributes()
return secretAPIModel{
Key: attrs["key"].(types.String).ValueString(),
}
})
_, secretsToBeRemoved := lo.Difference(planSecrets, stateSecrets)
secretKeysToBeRemovedKeys := lo.Map(
secretsToBeRemoved,
func(x secretAPIModel, index int) string {
return x.Key
},
)
var workersService WorkersServiceAPIModel
resp.Diagnostics.Append(plan.toAPIModel(ctx, &workersService, secretKeysToBeRemovedKeys)...)
if resp.Diagnostics.HasError() {
return
}
response, err := r.ProviderData.Client.R().
SetBody(&workersService).
Put(WorkersServiceEndpoint)
if err != nil {
utilfw.UnableToUpdateResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToUpdateResourceError(resp, response.String())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *workersServiceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
go util.SendUsageResourceDelete(ctx, r.ProviderData.Client.R(), r.ProviderData.ProductId, r.TypeName)
var data workersServiceResourceModel
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
key := data.Key.ValueString()
response, err := r.ProviderData.Client.R().
SetPathParam("key", key).
Delete(WorkersServiceEndpoint + "/{key}")
if err != nil {
utilfw.UnableToDeleteResourceError(resp, err.Error())
return
}
if response.IsError() {
utilfw.UnableToDeleteResourceError(resp, response.String())
return
}
// If the logic reaches here, it implicitly succeeded and will remove
// the resource from state if there are no other errors.
}
func (r *workersServiceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("key"), req, resp)
}