From 655fbceffcb3b9245819527d5886f5ff2f8b0d4c Mon Sep 17 00:00:00 2001 From: Douglas Parsons Date: Mon, 4 Jul 2022 10:04:05 +0100 Subject: [PATCH] Use terraform types for all fields Turns out using `string` or `int64` directly causes issues when the value passed comes from somewhere else (is Unknown). This causes the provider to panic internally, as the `Unknown` value is unhandled. Closes #48 --- vercel/resource_dns_record.go | 17 +++++---- vercel/resource_dns_record_model.go | 56 ++++++++++++++--------------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/vercel/resource_dns_record.go b/vercel/resource_dns_record.go index 574a665f..942741bd 100644 --- a/vercel/resource_dns_record.go +++ b/vercel/resource_dns_record.go @@ -139,36 +139,35 @@ func (r resourceDNSRecord) ValidateConfig(ctx context.Context, req tfsdk.Validat if resp.Diagnostics.HasError() { return } - - if config.Type == "SRV" && config.SRV == nil { + if config.Type.Value == "SRV" && config.SRV == nil { resp.Diagnostics.AddError( "DNS Record Invalid", "A DNS Record type of 'SRV' requires the `srv` attribute to be set", ) } - if config.Type == "SRV" && config.Value.Value != "" { + if config.Type.Value == "SRV" && !config.Value.Null { resp.Diagnostics.AddError( "DNS Record Invalid", "The `value` attribute should not be set on records of `type` 'SRV'", ) } - if config.Type != "SRV" && config.SRV != nil { + if config.Type.Value != "SRV" && config.SRV != nil { resp.Diagnostics.AddError( "DNS Record Invalid", "The `srv` attribute should only be set on records of `type` 'SRV'", ) } - if config.Type != "MX" && !config.MXPriority.Null { + if config.Type.Value != "MX" && !config.MXPriority.Null { resp.Diagnostics.AddError( "DNS Record Invalid", "The `mx_priority` attribute should only be set on records of `type` 'MX'", ) } - if config.Type == "MX" && config.MXPriority.Null { + if config.Type.Value == "MX" && config.MXPriority.Null { resp.Diagnostics.AddError( "DNS Record Invalid", "A DNS Record type of 'MX' requires the `mx_priority` attribute to be set", @@ -301,7 +300,7 @@ func (r resourceDNSRecord) Update(ctx context.Context, req tfsdk.UpdateResourceR fmt.Sprintf( "Could not update DNS Record %s for domain %s, unexpected error: %s", state.ID.Value, - state.Domain, + state.Domain.Value, err, ), ) @@ -338,7 +337,7 @@ func (r resourceDNSRecord) Delete(ctx context.Context, req tfsdk.DeleteResourceR return } - err := r.p.client.DeleteDNSRecord(ctx, state.Domain, state.ID.Value, state.TeamID.Value) + err := r.p.client.DeleteDNSRecord(ctx, state.Domain.Value, state.ID.Value, state.TeamID.Value) var apiErr client.APIError if err != nil && errors.As(err, &apiErr) && apiErr.StatusCode == 404 { // The DNS Record is already gone - do nothing. @@ -350,7 +349,7 @@ func (r resourceDNSRecord) Delete(ctx context.Context, req tfsdk.DeleteResourceR fmt.Sprintf( "Could not delete DNS Record %s for domain %s, unexpected error: %s", state.ID.Value, - state.Domain, + state.Domain.Value, err, ), ) diff --git a/vercel/resource_dns_record_model.go b/vercel/resource_dns_record_model.go index eef715e5..d263fba2 100644 --- a/vercel/resource_dns_record_model.go +++ b/vercel/resource_dns_record_model.go @@ -11,41 +11,41 @@ import ( // SRV reflect the state terraform stores internally for a nested SRV Record. type SRV struct { - Port int64 `tfsdk:"port"` - Priority int64 `tfsdk:"priority"` - Target string `tfsdk:"target"` - Weight int64 `tfsdk:"weight"` + Port types.Int64 `tfsdk:"port"` + Priority types.Int64 `tfsdk:"priority"` + Target types.String `tfsdk:"target"` + Weight types.Int64 `tfsdk:"weight"` } // DNSRecord reflects the state terraform stores internally for a DNS Record. type DNSRecord struct { ID types.String `tfsdk:"id"` - Domain string `tfsdk:"domain"` + Domain types.String `tfsdk:"domain"` MXPriority types.Int64 `tfsdk:"mx_priority"` - Name string `tfsdk:"name"` + Name types.String `tfsdk:"name"` SRV *SRV `tfsdk:"srv"` TTL types.Int64 `tfsdk:"ttl"` TeamID types.String `tfsdk:"team_id"` - Type string `tfsdk:"type"` + Type types.String `tfsdk:"type"` Value types.String `tfsdk:"value"` } func (d DNSRecord) toCreateDNSRecordRequest() client.CreateDNSRecordRequest { var srv *client.SRV = nil - if d.Type == "SRV" { + if d.Type.Value == "SRV" { srv = &client.SRV{ - Port: d.SRV.Port, - Priority: d.SRV.Priority, - Target: d.SRV.Target, - Weight: d.SRV.Weight, + Port: d.SRV.Port.Value, + Priority: d.SRV.Priority.Value, + Target: d.SRV.Target.Value, + Weight: d.SRV.Weight.Value, } } return client.CreateDNSRecordRequest{ - Domain: d.Domain, + Domain: d.Domain.Value, MXPriority: d.MXPriority.Value, - Name: d.Name, + Name: d.Name.Value, TTL: d.TTL.Value, - Type: d.Type, + Type: d.Type.Value, Value: d.Value.Value, SRV: srv, } @@ -55,15 +55,15 @@ func (d DNSRecord) toUpdateRequest() client.UpdateDNSRecordRequest { var srv *client.SRVUpdate = nil if d.SRV != nil { srv = &client.SRVUpdate{ - Port: &d.SRV.Port, - Priority: &d.SRV.Priority, - Target: &d.SRV.Target, - Weight: &d.SRV.Weight, + Port: &d.SRV.Port.Value, + Priority: &d.SRV.Priority.Value, + Target: &d.SRV.Target.Value, + Weight: &d.SRV.Weight.Value, } } return client.UpdateDNSRecordRequest{ MXPriority: toInt64Pointer(d.MXPriority), - Name: &d.Name, + Name: &d.Name.Value, SRV: srv, TTL: toInt64Pointer(d.TTL), Value: toStrPointer(d.Value), @@ -77,13 +77,13 @@ func convertResponseToDNSRecord(r client.DNSRecord, tid types.String, value type } record = DNSRecord{ - Domain: r.Domain, + Domain: types.String{Value: r.Domain}, ID: types.String{Value: r.ID}, MXPriority: types.Int64{Null: true}, - Name: r.Name, + Name: types.String{Value: r.Name}, TTL: types.Int64{Value: r.TTL}, TeamID: teamID, - Type: r.RecordType, + Type: types.String{Value: r.RecordType}, } if r.RecordType == "SRV" { @@ -110,14 +110,14 @@ func convertResponseToDNSRecord(r client.DNSRecord, tid types.String, value type target = split[3] } record.SRV = &SRV{ - Weight: int64(weight), - Port: int64(port), - Priority: int64(priority), - Target: target, + Weight: types.Int64{Value: int64(weight)}, + Port: types.Int64{Value: int64(port)}, + Priority: types.Int64{Value: int64(priority)}, + Target: types.String{Value: target}, } // SRV records have no value record.Value = types.String{Null: true} - if srv != nil && fmt.Sprintf("%s.", srv.Target) == record.SRV.Target { + if srv != nil && fmt.Sprintf("%s.", srv.Target.Value) == record.SRV.Target.Value { record.SRV.Target = srv.Target } return record, nil