Skip to content

Commit a42753a

Browse files
committed
Clean up PR #3195: fix error handling, remove dead code, prevent panics │
│ │ │ - Fix error handling: properly propagate conversion errors instead of ignoring them │ │ - Fix recursive diagnostics appending (diags.Append(diags...)) │ │ - Prevent potential nil pointer panic in convertApiParseTestOptions │ │ - Remove unused struct definitions (dead code) │ │ - Update updateState method to return diagnostics properly │ │ - Fix API method name (NewSyntheticsVariableParserWithDefaults)
1 parent f6877b9 commit a42753a

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

datadog/fwprovider/resource_datadog_synthetics_global_variable.go

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,6 @@ type syntheticsGlobalVariableModel struct {
7979
IsFido types.Bool `tfsdk:"is_fido"`
8080
}
8181

82-
type syntheticsGlobalVariableParseTestOptionsModel struct {
83-
Field types.String `tfsdk:"field"`
84-
Type types.String `tfsdk:"type"`
85-
Parser []syntheticsGlobalVariableParserModel `tfsdk:"parser"`
86-
LocalVariableName types.String `tfsdk:"local_variable_name"`
87-
}
88-
type syntheticsGlobalVariableParserModel struct {
89-
Type types.String `tfsdk:"type"`
90-
Value types.String `tfsdk:"value"`
91-
}
92-
93-
type syntheticsGlobalVariableOptionsModel struct {
94-
TotpParameters []syntheticsGlobalVariableTotpParametersModel `tfsdk:"totp_parameters"`
95-
}
96-
type syntheticsGlobalVariableTotpParametersModel struct {
97-
Digits types.Int64 `tfsdk:"digits"`
98-
RefreshInterval types.Int64 `tfsdk:"refresh_interval"`
99-
}
100-
10182
func NewSyntheticsGlobalVariableResource() resource.Resource {
10283
return &syntheticsGlobalVariableResource{}
10384
}
@@ -307,7 +288,7 @@ func convertParseTestOptions(ctx context.Context, opts types.List) ([]datadogV1.
307288
parserObj := parserElements[0].(types.Object)
308289
parserAttrs := parserObj.Attributes()
309290

310-
parser := datadogV1.NewSyntheticsGlobalVariableParserWithDefaults()
291+
parser := datadogV1.NewSyntheticsVariableParserWithDefaults()
311292

312293
if pType, ok := parserAttrs["type"].(types.String); ok && !pType.IsNull() && !pType.IsUnknown() {
313294
parser.SetType(datadogV1.SyntheticsGlobalVariableParserType(pType.ValueString()))
@@ -329,7 +310,10 @@ func convertParseTestOptions(ctx context.Context, opts types.List) ([]datadogV1.
329310
// convertApiParseTestOptions converts API type to Terraform List
330311
func convertApiParseTestOptions(ctx context.Context, opts *[]datadogV1.SyntheticsGlobalVariableParseTestOptions) (types.List, diag.Diagnostics) {
331312
var diags diag.Diagnostics
332-
if opts == nil || len(*opts) == 0 {
313+
if opts == nil {
314+
return types.ListNull(parseTestOptionsAttrType), diags
315+
}
316+
if len(*opts) == 0 {
333317
return types.ListNull(parseTestOptionsAttrType), diags
334318
}
335319

@@ -379,7 +363,7 @@ func convertApiParseTestOptions(ctx context.Context, opts *[]datadogV1.Synthetic
379363
}
380364

381365
if localVarName, ok := opt.GetLocalVariableNameOk(); ok {
382-
attrs["local_variable_name"] = types.StringValue(localVarName)
366+
attrs["local_variable_name"] = types.StringValue(*localVarName)
383367
}
384368

385369
obj, d := types.ObjectValue(parseTestOptionsAttrType.AttrTypes, attrs)
@@ -397,7 +381,7 @@ func convertApiParseTestOptions(ctx context.Context, opts *[]datadogV1.Synthetic
397381
// convertOptions converts Terraform List to API type
398382
func convertOptions(ctx context.Context, opts types.List) (*datadogV1.SyntheticsGlobalVariableOptions, diag.Diagnostics) {
399383
var diags diag.Diagnostics
400-
if opts.IsNull() || opts.IsUnknown() || opts.IsFullyNull() || opts.IsFullyUnknown() {
384+
if opts.IsNull() || opts.IsUnknown() {
401385
return nil, diags
402386
}
403387

@@ -524,7 +508,7 @@ func (r *syntheticsGlobalVariableResource) Read(ctx context.Context, request res
524508
return
525509
}
526510

527-
r.updateState(ctx, &state, &resp)
511+
response.Diagnostics.Append(r.updateState(ctx, &state, &resp)...)
528512

529513
// Save data into Terraform state
530514
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
@@ -552,7 +536,7 @@ func (r *syntheticsGlobalVariableResource) Create(ctx context.Context, request r
552536
response.Diagnostics.AddError("response contains unparsedObject", err.Error())
553537
return
554538
}
555-
r.updateState(ctx, &state, &resp)
539+
response.Diagnostics.Append(r.updateState(ctx, &state, &resp)...)
556540

557541
// Save data into Terraform state
558542
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
@@ -582,7 +566,7 @@ func (r *syntheticsGlobalVariableResource) Update(ctx context.Context, request r
582566
response.Diagnostics.AddError("response contains unparsedObject", err.Error())
583567
return
584568
}
585-
r.updateState(ctx, &state, &resp)
569+
response.Diagnostics.Append(r.updateState(ctx, &state, &resp)...)
586570

587571
// Save data into Terraform state
588572
response.Diagnostics.Append(response.State.Set(ctx, &state)...)
@@ -609,7 +593,8 @@ func (r *syntheticsGlobalVariableResource) Delete(ctx context.Context, request r
609593
}
610594
}
611595

612-
func (r *syntheticsGlobalVariableResource) updateState(ctx context.Context, state *syntheticsGlobalVariableModel, resp *datadogV1.SyntheticsGlobalVariable) {
596+
func (r *syntheticsGlobalVariableResource) updateState(ctx context.Context, state *syntheticsGlobalVariableModel, resp *datadogV1.SyntheticsGlobalVariable) diag.Diagnostics {
597+
var diags diag.Diagnostics
613598
state.Id = types.StringValue(resp.GetId())
614599

615600
state.Name = types.StringValue(resp.GetName())
@@ -628,7 +613,11 @@ func (r *syntheticsGlobalVariableResource) updateState(ctx context.Context, stat
628613
state.Secure = types.BoolValue(*secure)
629614
}
630615
if options, ok := value.GetOptionsOk(); ok {
631-
state.Options, _ = convertApiOptions(ctx, options)
616+
opts, convertDiags := convertApiOptions(ctx, options)
617+
diags.Append(convertDiags...)
618+
if !convertDiags.HasError() {
619+
state.Options = opts
620+
}
632621
}
633622
}
634623

@@ -640,9 +629,14 @@ func (r *syntheticsGlobalVariableResource) updateState(ctx context.Context, stat
640629
state.ParseTestId = types.StringValue(*parseTestId)
641630

642631
if parseTestOptions, ok := resp.GetParseTestOptionsOk(); ok {
643-
state.ParseTestOptions, _ = convertApiParseTestOptions(ctx, &[]datadogV1.SyntheticsGlobalVariableParseTestOptions{*parseTestOptions})
632+
opts, convertDiags := convertApiParseTestOptions(ctx, &[]datadogV1.SyntheticsGlobalVariableParseTestOptions{*parseTestOptions})
633+
diags.Append(convertDiags...)
634+
if !convertDiags.HasError() {
635+
state.ParseTestOptions = opts
636+
}
644637
}
645638
}
639+
return diags
646640
}
647641

648642
func (r *syntheticsGlobalVariableResource) buildSyntheticsGlobalVariableRequestBody(ctx context.Context, state *syntheticsGlobalVariableModel) (*datadogV1.SyntheticsGlobalVariableRequest, diag.Diagnostics) {
@@ -682,11 +676,11 @@ func (r *syntheticsGlobalVariableResource) buildSyntheticsGlobalVariableRequestB
682676
syntheticsGlobalVariableRequest.SetParseTestPublicId(state.ParseTestId.ValueString())
683677

684678
if !state.ParseTestOptions.IsNull() && !state.ParseTestOptions.IsUnknown() {
685-
parseTestOptions, diags := convertParseTestOptions(ctx, state.ParseTestOptions)
679+
parseTestOptions, convertDiags := convertParseTestOptions(ctx, state.ParseTestOptions)
680+
diags.Append(convertDiags...)
686681
if len(parseTestOptions) > 0 {
687682
syntheticsGlobalVariableRequest.ParseTestOptions = &parseTestOptions[0]
688683
}
689-
diags.Append(diags...)
690684
}
691685
}
692686

@@ -699,8 +693,8 @@ func (r *syntheticsGlobalVariableResource) buildSyntheticsGlobalVariableRequestB
699693
}
700694

701695
if !state.Options.IsNull() && !state.Options.IsUnknown() {
702-
options, diags := convertOptions(ctx, state.Options)
703-
diags.Append(diags...)
696+
options, convertDiags := convertOptions(ctx, state.Options)
697+
diags.Append(convertDiags...)
704698
if options != nil {
705699
value.SetOptions(*options)
706700
}

0 commit comments

Comments
 (0)