/
scim_subattribute_resource.go
603 lines (542 loc) · 25.4 KB
/
scim_subattribute_resource.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package scimsubattribute
import (
"context"
"strings"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"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-log/tflog"
client "github.com/pingidentity/pingdirectory-go-client/v10000/configurationapi"
"github.com/pingidentity/terraform-provider-pingdirectory/internal/operations"
"github.com/pingidentity/terraform-provider-pingdirectory/internal/resource/config"
internaltypes "github.com/pingidentity/terraform-provider-pingdirectory/internal/types"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &scimSubattributeResource{}
_ resource.ResourceWithConfigure = &scimSubattributeResource{}
_ resource.ResourceWithImportState = &scimSubattributeResource{}
_ resource.Resource = &defaultScimSubattributeResource{}
_ resource.ResourceWithConfigure = &defaultScimSubattributeResource{}
_ resource.ResourceWithImportState = &defaultScimSubattributeResource{}
)
// Create a Scim Subattribute resource
func NewScimSubattributeResource() resource.Resource {
return &scimSubattributeResource{}
}
func NewDefaultScimSubattributeResource() resource.Resource {
return &defaultScimSubattributeResource{}
}
// scimSubattributeResource is the resource implementation.
type scimSubattributeResource struct {
providerConfig internaltypes.ProviderConfiguration
apiClient *client.APIClient
}
// defaultScimSubattributeResource is the resource implementation.
type defaultScimSubattributeResource struct {
providerConfig internaltypes.ProviderConfiguration
apiClient *client.APIClient
}
// Metadata returns the resource type name.
func (r *scimSubattributeResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_scim_subattribute"
}
func (r *defaultScimSubattributeResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_default_scim_subattribute"
}
// Configure adds the provider configured client to the resource.
func (r *scimSubattributeResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
providerCfg := req.ProviderData.(internaltypes.ResourceConfiguration)
r.providerConfig = providerCfg.ProviderConfig
r.apiClient = providerCfg.ApiClient
}
func (r *defaultScimSubattributeResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
providerCfg := req.ProviderData.(internaltypes.ResourceConfiguration)
r.providerConfig = providerCfg.ProviderConfig
r.apiClient = providerCfg.ApiClient
}
type scimSubattributeResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Notifications types.Set `tfsdk:"notifications"`
RequiredActions types.Set `tfsdk:"required_actions"`
ResourceType types.String `tfsdk:"resource_type"`
ScimAttributeName types.String `tfsdk:"scim_attribute_name"`
ScimSchemaName types.String `tfsdk:"scim_schema_name"`
Description types.String `tfsdk:"description"`
Type types.String `tfsdk:"type"`
Required types.Bool `tfsdk:"required"`
CaseExact types.Bool `tfsdk:"case_exact"`
MultiValued types.Bool `tfsdk:"multi_valued"`
CanonicalValue types.Set `tfsdk:"canonical_value"`
Mutability types.String `tfsdk:"mutability"`
Returned types.String `tfsdk:"returned"`
ReferenceType types.Set `tfsdk:"reference_type"`
}
// GetSchema defines the schema for the resource.
func (r *scimSubattributeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
scimSubattributeSchema(ctx, req, resp, false)
}
func (r *defaultScimSubattributeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
scimSubattributeSchema(ctx, req, resp, true)
}
func scimSubattributeSchema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse, isDefault bool) {
schemaDef := schema.Schema{
Description: "Manages a Scim Subattribute.",
Attributes: map[string]schema.Attribute{
"resource_type": schema.StringAttribute{
Description: "The type of SCIM Subattribute resource. Options are ['scim-subattribute']",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("scim-subattribute"),
Validators: []validator.String{
stringvalidator.OneOf([]string{"scim-subattribute"}...),
},
},
"scim_attribute_name": schema.StringAttribute{
Description: "Name of the parent SCIM Attribute",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"scim_schema_name": schema.StringAttribute{
Description: "Name of the parent SCIM Schema",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"description": schema.StringAttribute{
Description: "A description for this SCIM Subattribute",
Optional: true,
},
"type": schema.StringAttribute{
Description: "Specifies the data type for this sub-attribute.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("string"),
Validators: []validator.String{
stringvalidator.OneOf([]string{"string", "boolean", "datetime", "decimal", "integer", "binary", "reference"}...),
},
},
"required": schema.BoolAttribute{
Description: "Specifies whether this sub-attribute is required.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
"case_exact": schema.BoolAttribute{
Description: "Specifies whether the sub-attribute values are case sensitive.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
"multi_valued": schema.BoolAttribute{
Description: "Specifies whether this attribute may have multiple values.",
Optional: true,
Computed: true,
Default: booldefault.StaticBool(false),
},
"canonical_value": schema.SetAttribute{
Description: "Specifies the suggested canonical type values for the sub-attribute.",
Optional: true,
Computed: true,
Default: internaltypes.EmptySetDefault(types.StringType),
ElementType: types.StringType,
},
"mutability": schema.StringAttribute{
Description: "Specifies the circumstances under which the values of the sub-attribute can be written.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("read-write"),
Validators: []validator.String{
stringvalidator.OneOf([]string{"read-only", "read-write", "immutable", "write-only"}...),
},
},
"returned": schema.StringAttribute{
Description: "Specifies the circumstances under which the values of the sub-attribute are returned in response to a request.",
Optional: true,
Computed: true,
Default: stringdefault.StaticString("by-default"),
Validators: []validator.String{
stringvalidator.OneOf([]string{"by-default", "upon-request", "always", "never"}...),
},
},
"reference_type": schema.SetAttribute{
Description: "Specifies the SCIM resource types that may be referenced. This property is only applicable for sub-attributes that are of type 'reference'. Valid values are: A SCIM resource type (e.g., 'User' or 'Group'), 'external' - indicating the resource is an external resource (e.g., such as a photo), or 'uri' - indicating that the reference is to a service endpoint or an identifier (such as a schema urn).",
Optional: true,
Computed: true,
Default: internaltypes.EmptySetDefault(types.StringType),
ElementType: types.StringType,
},
},
}
if isDefault {
typeAttr := schemaDef.Attributes["resource_type"].(schema.StringAttribute)
typeAttr.Optional = false
typeAttr.Required = false
typeAttr.Computed = true
typeAttr.PlanModifiers = []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
}
schemaDef.Attributes["resource_type"] = typeAttr
// Add any default properties and set optional properties to computed where necessary
config.SetAttributesToOptionalAndComputedAndRemoveDefaults(&schemaDef, []string{"resource_type", "scim_attribute_name", "scim_schema_name"})
}
config.AddCommonResourceSchema(&schemaDef, true)
resp.Schema = schemaDef
}
// Add optional fields to create request for scim-subattribute scim-subattribute
func addOptionalScimSubattributeFields(ctx context.Context, addRequest *client.AddScimSubattributeRequest, plan scimSubattributeResourceModel) error {
// Empty strings are treated as equivalent to null
if internaltypes.IsNonEmptyString(plan.Description) {
addRequest.Description = plan.Description.ValueStringPointer()
}
// Empty strings are treated as equivalent to null
if internaltypes.IsNonEmptyString(plan.Type) {
typeValue, err := client.NewEnumscimSubattributeTypePropFromValue(plan.Type.ValueString())
if err != nil {
return err
}
addRequest.Type = typeValue
}
if internaltypes.IsDefined(plan.Required) {
addRequest.Required = plan.Required.ValueBoolPointer()
}
if internaltypes.IsDefined(plan.CaseExact) {
addRequest.CaseExact = plan.CaseExact.ValueBoolPointer()
}
if internaltypes.IsDefined(plan.MultiValued) {
addRequest.MultiValued = plan.MultiValued.ValueBoolPointer()
}
if internaltypes.IsDefined(plan.CanonicalValue) {
var slice []string
plan.CanonicalValue.ElementsAs(ctx, &slice, false)
addRequest.CanonicalValue = slice
}
// Empty strings are treated as equivalent to null
if internaltypes.IsNonEmptyString(plan.Mutability) {
mutability, err := client.NewEnumscimSubattributeMutabilityPropFromValue(plan.Mutability.ValueString())
if err != nil {
return err
}
addRequest.Mutability = mutability
}
// Empty strings are treated as equivalent to null
if internaltypes.IsNonEmptyString(plan.Returned) {
returned, err := client.NewEnumscimSubattributeReturnedPropFromValue(plan.Returned.ValueString())
if err != nil {
return err
}
addRequest.Returned = returned
}
if internaltypes.IsDefined(plan.ReferenceType) {
var slice []string
plan.ReferenceType.ElementsAs(ctx, &slice, false)
addRequest.ReferenceType = slice
}
return nil
}
// Populate any computed string values with empty strings, since that is equivalent to null to PD. This will reduce noise in plan output
func (model *scimSubattributeResourceModel) populateAllComputedStringAttributes() {
if model.Returned.IsUnknown() || model.Returned.IsNull() {
model.Returned = types.StringValue("")
}
if model.Type.IsUnknown() || model.Type.IsNull() {
model.Type = types.StringValue("")
}
if model.Description.IsUnknown() || model.Description.IsNull() {
model.Description = types.StringValue("")
}
if model.Mutability.IsUnknown() || model.Mutability.IsNull() {
model.Mutability = types.StringValue("")
}
}
// Read a ScimSubattributeResponse object into the model struct
func readScimSubattributeResponse(ctx context.Context, r *client.ScimSubattributeResponse, state *scimSubattributeResourceModel, expectedValues *scimSubattributeResourceModel, diagnostics *diag.Diagnostics) {
state.ResourceType = types.StringValue("scim-subattribute")
state.Id = types.StringValue(r.Id)
state.Name = types.StringValue(r.Id)
state.Description = internaltypes.StringTypeOrNil(r.Description, internaltypes.IsEmptyString(expectedValues.Description))
state.Type = types.StringValue(r.Type.String())
state.Required = types.BoolValue(r.Required)
state.CaseExact = types.BoolValue(r.CaseExact)
state.MultiValued = types.BoolValue(r.MultiValued)
state.CanonicalValue = internaltypes.GetStringSet(r.CanonicalValue)
state.Mutability = types.StringValue(r.Mutability.String())
state.Returned = types.StringValue(r.Returned.String())
state.ReferenceType = internaltypes.GetStringSet(r.ReferenceType)
state.Notifications, state.RequiredActions = config.ReadMessages(ctx, r.Urnpingidentityschemasconfigurationmessages20, diagnostics)
}
// Set any properties that aren't returned by the API in the state, based on some expected value (usually the plan value)
// This will include any parent endpoint names and any obscured (sensitive) attributes
func (state *scimSubattributeResourceModel) setStateValuesNotReturnedByAPI(expectedValues *scimSubattributeResourceModel) {
if !expectedValues.ScimAttributeName.IsUnknown() {
state.ScimAttributeName = expectedValues.ScimAttributeName
}
if !expectedValues.ScimSchemaName.IsUnknown() {
state.ScimSchemaName = expectedValues.ScimSchemaName
}
}
// Create any update operations necessary to make the state match the plan
func createScimSubattributeOperations(plan scimSubattributeResourceModel, state scimSubattributeResourceModel) []client.Operation {
var ops []client.Operation
operations.AddStringOperationIfNecessary(&ops, plan.Description, state.Description, "description")
operations.AddStringOperationIfNecessary(&ops, plan.Type, state.Type, "type")
operations.AddBoolOperationIfNecessary(&ops, plan.Required, state.Required, "required")
operations.AddBoolOperationIfNecessary(&ops, plan.CaseExact, state.CaseExact, "case-exact")
operations.AddBoolOperationIfNecessary(&ops, plan.MultiValued, state.MultiValued, "multi-valued")
operations.AddStringSetOperationsIfNecessary(&ops, plan.CanonicalValue, state.CanonicalValue, "canonical-value")
operations.AddStringOperationIfNecessary(&ops, plan.Mutability, state.Mutability, "mutability")
operations.AddStringOperationIfNecessary(&ops, plan.Returned, state.Returned, "returned")
operations.AddStringSetOperationsIfNecessary(&ops, plan.ReferenceType, state.ReferenceType, "reference-type")
return ops
}
// Create a scim-subattribute scim-subattribute
func (r *scimSubattributeResource) CreateScimSubattribute(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse, plan scimSubattributeResourceModel) (*scimSubattributeResourceModel, error) {
addRequest := client.NewAddScimSubattributeRequest(plan.Name.ValueString())
err := addOptionalScimSubattributeFields(ctx, addRequest, plan)
if err != nil {
resp.Diagnostics.AddError("Failed to add optional properties to add request for Scim Subattribute", err.Error())
return nil, err
}
// Log request JSON
requestJson, err := addRequest.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Add request: "+string(requestJson))
}
apiAddRequest := r.apiClient.ScimSubattributeAPI.AddScimSubattribute(
config.ProviderBasicAuthContext(ctx, r.providerConfig), plan.ScimAttributeName.ValueString(), plan.ScimSchemaName.ValueString())
apiAddRequest = apiAddRequest.AddScimSubattributeRequest(*addRequest)
addResponse, httpResp, err := r.apiClient.ScimSubattributeAPI.AddScimSubattributeExecute(apiAddRequest)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while creating the Scim Subattribute", err, httpResp)
return nil, err
}
// Log response JSON
responseJson, err := addResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Add response: "+string(responseJson))
}
// Read the response into the state
var state scimSubattributeResourceModel
readScimSubattributeResponse(ctx, addResponse, &state, &plan, &resp.Diagnostics)
return &state, nil
}
// Create a new resource
func (r *scimSubattributeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan scimSubattributeResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
state, err := r.CreateScimSubattribute(ctx, req, resp, plan)
if err != nil {
return
}
// Populate Computed attribute values
state.setStateValuesNotReturnedByAPI(&plan)
// Set state to fully populated data
diags = resp.State.Set(ctx, *state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Create a new resource
// For edit only resources like this, create doesn't actually "create" anything - it "adopts" the existing
// config object into management by terraform. This method reads the existing config object
// and makes any changes needed to make it match the plan - similar to the Update method.
func (r *defaultScimSubattributeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Retrieve values from plan
var plan scimSubattributeResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
readResponse, httpResp, err := r.apiClient.ScimSubattributeAPI.GetScimSubattribute(
config.ProviderBasicAuthContext(ctx, r.providerConfig), plan.Name.ValueString(), plan.ScimAttributeName.ValueString(), plan.ScimSchemaName.ValueString()).Execute()
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while getting the Scim Subattribute", err, httpResp)
return
}
// Log response JSON
responseJson, err := readResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Read response: "+string(responseJson))
}
// Read the existing configuration
var state scimSubattributeResourceModel
readScimSubattributeResponse(ctx, readResponse, &state, &state, &resp.Diagnostics)
// Determine what changes are needed to match the plan
updateRequest := r.apiClient.ScimSubattributeAPI.UpdateScimSubattribute(config.ProviderBasicAuthContext(ctx, r.providerConfig), plan.Name.ValueString(), plan.ScimAttributeName.ValueString(), plan.ScimSchemaName.ValueString())
ops := createScimSubattributeOperations(plan, state)
if len(ops) > 0 {
updateRequest = updateRequest.UpdateRequest(*client.NewUpdateRequest(ops))
// Log operations
operations.LogUpdateOperations(ctx, ops)
updateResponse, httpResp, err := r.apiClient.ScimSubattributeAPI.UpdateScimSubattributeExecute(updateRequest)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while updating the Scim Subattribute", err, httpResp)
return
}
// Log response JSON
responseJson, err := updateResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Update response: "+string(responseJson))
}
// Read the response
readScimSubattributeResponse(ctx, updateResponse, &state, &plan, &resp.Diagnostics)
}
state.setStateValuesNotReturnedByAPI(&plan)
state.populateAllComputedStringAttributes()
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Read resource information
func (r *scimSubattributeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
readScimSubattribute(ctx, req, resp, r.apiClient, r.providerConfig, false)
}
func (r *defaultScimSubattributeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
readScimSubattribute(ctx, req, resp, r.apiClient, r.providerConfig, true)
}
func readScimSubattribute(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse, apiClient *client.APIClient, providerConfig internaltypes.ProviderConfiguration, isDefault bool) {
// Get current state
var state scimSubattributeResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
readResponse, httpResp, err := apiClient.ScimSubattributeAPI.GetScimSubattribute(
config.ProviderBasicAuthContext(ctx, providerConfig), state.Name.ValueString(), state.ScimAttributeName.ValueString(), state.ScimSchemaName.ValueString()).Execute()
if err != nil {
if httpResp != nil && httpResp.StatusCode == 404 && !isDefault {
config.ReportHttpErrorAsWarning(ctx, &resp.Diagnostics, "An error occurred while getting the Scim Subattribute", err, httpResp)
resp.State.RemoveResource(ctx)
} else {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while getting the Scim Subattribute", err, httpResp)
}
return
}
// Log response JSON
responseJson, err := readResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Read response: "+string(responseJson))
}
// Read the response into the state
readScimSubattributeResponse(ctx, readResponse, &state, &state, &resp.Diagnostics)
if isDefault {
state.populateAllComputedStringAttributes()
}
// Set refreshed state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
// Update a resource
func (r *scimSubattributeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
updateScimSubattribute(ctx, req, resp, r.apiClient, r.providerConfig)
}
func (r *defaultScimSubattributeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
updateScimSubattribute(ctx, req, resp, r.apiClient, r.providerConfig)
}
func updateScimSubattribute(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse, apiClient *client.APIClient, providerConfig internaltypes.ProviderConfiguration) {
// Retrieve values from plan
var plan scimSubattributeResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get the current state to see how any attributes are changing
var state scimSubattributeResourceModel
req.State.Get(ctx, &state)
updateRequest := apiClient.ScimSubattributeAPI.UpdateScimSubattribute(
config.ProviderBasicAuthContext(ctx, providerConfig), plan.Name.ValueString(), plan.ScimAttributeName.ValueString(), plan.ScimSchemaName.ValueString())
// Determine what update operations are necessary
ops := createScimSubattributeOperations(plan, state)
if len(ops) > 0 {
updateRequest = updateRequest.UpdateRequest(*client.NewUpdateRequest(ops))
// Log operations
operations.LogUpdateOperations(ctx, ops)
updateResponse, httpResp, err := apiClient.ScimSubattributeAPI.UpdateScimSubattributeExecute(updateRequest)
if err != nil {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while updating the Scim Subattribute", err, httpResp)
return
}
// Log response JSON
responseJson, err := updateResponse.MarshalJSON()
if err == nil {
tflog.Debug(ctx, "Update response: "+string(responseJson))
}
// Read the response
readScimSubattributeResponse(ctx, updateResponse, &state, &plan, &resp.Diagnostics)
} else {
tflog.Warn(ctx, "No configuration API operations created for update")
}
state.setStateValuesNotReturnedByAPI(&plan)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Delete deletes the resource and removes the Terraform state on success.
// This config object is edit-only, so Terraform can't delete it.
// After running a delete, Terraform will just "forget" about this object and it can be managed elsewhere.
func (r *defaultScimSubattributeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// No implementation necessary
}
func (r *scimSubattributeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state scimSubattributeResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
httpResp, err := r.apiClient.ScimSubattributeAPI.DeleteScimSubattributeExecute(r.apiClient.ScimSubattributeAPI.DeleteScimSubattribute(
config.ProviderBasicAuthContext(ctx, r.providerConfig), state.Name.ValueString(), state.ScimAttributeName.ValueString(), state.ScimSchemaName.ValueString()))
if err != nil && (httpResp == nil || httpResp.StatusCode != 404) {
config.ReportHttpError(ctx, &resp.Diagnostics, "An error occurred while deleting the Scim Subattribute", err, httpResp)
return
}
}
func (r *scimSubattributeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
importScimSubattribute(ctx, req, resp)
}
func (r *defaultScimSubattributeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
importScimSubattribute(ctx, req, resp)
}
func importScimSubattribute(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
split := strings.Split(req.ID, "/")
if len(split) != 3 {
resp.Diagnostics.AddError("Invalid import id for resource", "Expected [scim-schema-name]/[scim-attribute-name]/[scim-subattribute-name]. Got: "+req.ID)
return
}
// Set the required attributes to read the resource
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("scim_schema_name"), split[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("scim_attribute_name"), split[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), split[2])...)
}