这是indexloc提供的服务,不要输入任何密码
Skip to content

Use terraform types for all fields #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions vercel/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
),
)
Expand Down Expand Up @@ -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.
Expand All @@ -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,
),
)
Expand Down
56 changes: 28 additions & 28 deletions vercel/resource_dns_record_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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),
Expand All @@ -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" {
Expand All @@ -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
Expand Down