Skip to content

Commit

Permalink
Bug_fix_dnsSec (#55)
Browse files Browse the repository at this point in the history
* bug: DNSSec bug fix #minor

* bug: DNSSec docGen #minor
  • Loading branch information
diegoazion authored May 22, 2023
1 parent ae575fd commit 93347d0
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 60 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ get-gosec-deps:
https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(GOBIN) v2.15.0 ;\
fi

docs: tools
docGen: tools
chmod u+r+x ./scripts/generate-docs.sh
@sh -c "'$(CURDIR)/scripts/generate-docs.sh'"

tools:
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/dnssec.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Required:

- `is_enabled` (Boolean) Zone DNSSEC flags for enabled.

Optional:
Read-Only:

- `delegation_signer` (Attributes) Zone DNSSEC delegation-signer. (see [below for nested schema](#nestedatt--dns_sec--delegation_signer))
- `status` (String) The status of the Zone DNSSEC.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/aziontech/azionapi-go-sdk v0.38.0
github.com/hashicorp/go-changelog v0.0.0-20221013053416-ba40b3a8c7ff
github.com/hashicorp/terraform-plugin-docs v0.14.1
github.com/hashicorp/terraform-plugin-framework v1.1.1
github.com/hashicorp/terraform-plugin-framework v1.2.0
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-log v0.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ github.com/hashicorp/terraform-json v0.15.0 h1:/gIyNtR6SFw6h5yzlbDbACyGvIhKtQi8m
github.com/hashicorp/terraform-json v0.15.0/go.mod h1:+L1RNzjDU5leLFZkHTFTbJXaoqUC6TqXlFgDoOXrtvk=
github.com/hashicorp/terraform-plugin-docs v0.14.1 h1:MikFi59KxrP/ewrZoaowrB9he5Vu4FtvhamZFustiA4=
github.com/hashicorp/terraform-plugin-docs v0.14.1/go.mod h1:k2NW8+t113jAus6bb5tQYQgEAX/KueE/u8X2Z45V1GM=
github.com/hashicorp/terraform-plugin-framework v1.1.1 h1:PbnEKHsIU8KTTzoztHQGgjZUWx7Kk8uGtpGMMc1p+oI=
github.com/hashicorp/terraform-plugin-framework v1.1.1/go.mod h1:DyZPxQA+4OKK5ELxFIIcqggcszqdWWUpTLPHAhS/tkY=
github.com/hashicorp/terraform-plugin-framework v1.2.0 h1:MZjFFfULnFq8fh04FqrKPcJ/nGpHOvX4buIygT3MSNY=
github.com/hashicorp/terraform-plugin-framework v1.2.0/go.mod h1:nToI62JylqXDq84weLJ/U3umUsBhZAaTmU0HXIVUOcw=
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0 h1:4L0tmy/8esP6OcvocVymw52lY0HyQ5OxB7VNl7k4bS0=
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0/go.mod h1:qdQJCdimB9JeX2YwOpItEu+IrfoJjWQ5PhLpAOMDQAE=
github.com/hashicorp/terraform-plugin-go v0.14.3 h1:nlnJ1GXKdMwsC8g1Nh05tK2wsC3+3BL/DBBxFEki+j0=
Expand Down
3 changes: 1 addition & 2 deletions internal/data_source_dnssec.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ func (d *dnsSecDataSource) Read(ctx context.Context, req datasource.ReadRequest,
)
return
}
dnsSecEnabled := *getDnsSec.Results.IsEnabled
if dnsSecEnabled {
if getDnsSec.Results.DelegationSigner != nil {
dnsSecState := &dnsSecDataSourceModel{
SchemaVersion: types.Int64Value(int64(*getDnsSec.SchemaVersion)),
ZoneId: getZoneId,
Expand Down
13 changes: 10 additions & 3 deletions internal/data_source_dnssec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestAccDNSSecDataSource(t *testing.T) {
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `data "azion_dnssec" "test" { zone_id = "2580" }`,
Config: testAccDNSSecDataSourceConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.azion_dnssec.test", "schema_version", "3"),
resource.TestCheckResourceAttr("data.azion_dnssec.test", "zone_id", "2580"),
Expand All @@ -23,10 +23,17 @@ func TestAccDNSSecDataSource(t *testing.T) {
resource.TestCheckResourceAttr("data.azion_dnssec.test", "dns_sec.delegation_signer.algorithmtype.slug", "ECDSAP256SHA256"),
resource.TestCheckResourceAttr("data.azion_dnssec.test", "dns_sec.delegation_signer.digest", "3b7d6073c98645707d84e497a9263590c1ab00c494c3980305076b1add5fe781"),
resource.TestCheckResourceAttr("data.azion_dnssec.test", "dns_sec.delegation_signer.keytag", "42528"),
// Verify placeholder id attribute
resource.TestCheckResourceAttr("data.azion_dnssec.test", "id", "Get DNSSEC"),
),
},
},
})
}

func testAccDNSSecDataSourceConfig() string {
return `
provider "azion" {
api_token = "token"
}
data "azion_dnssec" "test" { zone_id = "2580" }
`
}
5 changes: 2 additions & 3 deletions internal/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ provider "azion" {

var (
testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"azionProvider": func() (tfprotov6.ProviderServer, error) {
ctx := context.Background()
"azion": func() (tfprotov6.ProviderServer, error) {
providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(New("test")),
}

return tf6muxserver.NewMuxServer(ctx, providers...)
return tf6muxserver.NewMuxServer(context.Background(), providers...)
},
}
)
86 changes: 39 additions & 47 deletions internal/resource_dnssec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ func (r *dnssecResource) Schema(_ context.Context, _ resource.SchemaRequest, res
Description: "Zone DNSSEC flags for enabled.",
},
"status": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The status of the Zone DNSSEC.",
},
"delegation_signer": schema.SingleNestedAttribute{
Description: "Zone DNSSEC delegation-signer.",
Optional: true,
Computed: true,
Attributes: DnsDelegationSigner(),
},
},
Expand Down Expand Up @@ -142,14 +142,14 @@ func (r *dnssecResource) Configure(_ context.Context, req resource.ConfigureRequ
}

func (r *dnssecResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var getZoneId types.String
diags := req.Plan.GetAttribute(ctx, path.Root("zone_id"), &getZoneId)
var plan dnssecResourceModel
diags := req.Config.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

zoneId, err := strconv.ParseUint(getZoneId.ValueString(), 10, 16)
zoneId, err := strconv.ParseUint(plan.ZoneId.ValueString(), 10, 16)
if err != nil {
resp.Diagnostics.AddError(
"Value Conversion error ",
Expand All @@ -158,7 +158,7 @@ func (r *dnssecResource) Create(ctx context.Context, req resource.CreateRequest,
return
}
dnsSec := idns.DnsSec{
IsEnabled: idns.PtrBool(true),
IsEnabled: idns.PtrBool(plan.DnsSec.IsEnabled.ValueBool()),
}

enableDnsSec, response, err := r.client.idnsApi.DNSSECApi.PutZoneDnsSec(ctx, int32(zoneId)).DnsSec(dnsSec).Execute()
Expand All @@ -177,24 +177,29 @@ func (r *dnssecResource) Create(ctx context.Context, req resource.CreateRequest,
)
return
}
var plan dnssecResourceModel
plan.ZoneId = getZoneId
plan.SchemaVersion = types.Int64Value(int64(*enableDnsSec.SchemaVersion))
plan.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*enableDnsSec.Results.IsEnabled),
Status: types.StringValue(*enableDnsSec.Results.Status),
DelegationSigner: &DnsDelegationSignerModel{
DigestType: &DnsDelegationSignerDigestType{
Id: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.DigestType.Id)),
Slug: types.StringValue(*enableDnsSec.Results.DelegationSigner.DigestType.Slug),
},
AlgorithmType: &DnsDelegationSignerDigestType{
Id: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.AlgorithmType.Id)),
Slug: types.StringValue(*enableDnsSec.Results.DelegationSigner.AlgorithmType.Slug),
if enableDnsSec.Results.DelegationSigner != nil {
plan.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*enableDnsSec.Results.IsEnabled),
Status: types.StringValue(*enableDnsSec.Results.Status),
DelegationSigner: &DnsDelegationSignerModel{
DigestType: &DnsDelegationSignerDigestType{
Id: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.DigestType.Id)),
Slug: types.StringValue(*enableDnsSec.Results.DelegationSigner.DigestType.Slug),
},
AlgorithmType: &DnsDelegationSignerDigestType{
Id: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.AlgorithmType.Id)),
Slug: types.StringValue(*enableDnsSec.Results.DelegationSigner.AlgorithmType.Slug),
},
Digest: types.StringValue(*enableDnsSec.Results.DelegationSigner.Digest),
KeyTag: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.KeyTag)),
},
Digest: types.StringValue(*enableDnsSec.Results.DelegationSigner.Digest),
KeyTag: types.Int64Value(int64(*enableDnsSec.Results.DelegationSigner.KeyTag)),
},
}
} else {
plan.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*enableDnsSec.Results.IsEnabled),
Status: types.StringValue(*enableDnsSec.Results.Status),
}
}
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

Expand Down Expand Up @@ -236,8 +241,7 @@ func (r *dnssecResource) Read(ctx context.Context, req resource.ReadRequest, res
)
return
}
dnsSecEnabled := *getDnsSec.Results.IsEnabled
if dnsSecEnabled {
if getDnsSec.Results.DelegationSigner != nil {
state.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*getDnsSec.Results.IsEnabled),
Status: types.StringValue(*getDnsSec.Results.Status),
Expand All @@ -254,22 +258,16 @@ func (r *dnssecResource) Read(ctx context.Context, req resource.ReadRequest, res
KeyTag: types.Int64Value(int64(*getDnsSec.Results.DelegationSigner.KeyTag)),
},
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
state.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*getDnsSec.Results.IsEnabled),
Status: types.StringValue(*getDnsSec.Results.Status),
}

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

Expand Down Expand Up @@ -312,8 +310,7 @@ func (r *dnssecResource) Update(ctx context.Context, req resource.UpdateRequest,
}
plan.SchemaVersion = types.Int64Value(int64(*enableDnsSec.SchemaVersion))
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
dnsSecEnabled := *enableDnsSec.Results.IsEnabled
if dnsSecEnabled {
if enableDnsSec.Results.DelegationSigner != nil {
plan.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*enableDnsSec.Results.IsEnabled),
Status: types.StringValue(*enableDnsSec.Results.Status),
Expand All @@ -331,21 +328,16 @@ func (r *dnssecResource) Update(ctx context.Context, req resource.UpdateRequest,
},
}

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
plan.DnsSec = &dnsSecModel{
IsEnabled: types.BoolValue(*enableDnsSec.Results.IsEnabled),
Status: types.StringValue(*enableDnsSec.Results.Status),
}
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

Expand Down
73 changes: 73 additions & 0 deletions internal/resource_dnssec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package provider

//func TestAccResourceDnsSec(t *testing.T) {
// resourceName := "azion_dnssec.examples"
// resource.ParallelTest(t, resource.TestCase{
// ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// CheckDestroy: testAccDNSSecResourceDestroy,
// Steps: []resource.TestStep{
// {
// Config: testAccDNSSecResourceConfig(),
// Check: resource.ComposeTestCheckFunc(
// resource.TestCheckResourceAttr(resourceName, "schema_version", "3"),
// resource.TestCheckResourceAttr(resourceName, "id", "2595"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.is_enabled", "true"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.status", "ready"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.digesttype.id", "2"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.digesttype.slug", "SHA256"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.algorithmtype.id", "13"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.algorithmtype.slug", "ECDSAP256SHA256"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.digest", "35dbd2f5cd43d191d6f7c61f9c8d79149254186761a188b667f5ca78d0a3cc27"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.delegation_signer.keytag", "32597"),
// ),
// },
// {
// Config: testAccDNSSecResourceConfigUpdate(),
// Check: resource.ComposeTestCheckFunc(
// resource.TestCheckResourceAttr(resourceName, "schema_version", "3"),
// resource.TestCheckResourceAttr(resourceName, "id", "2595"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.is_enabled", "false"),
// resource.TestCheckResourceAttr(resourceName, "dns_sec.status", "ready"),
// ),
// },
// {
// ResourceName: resourceName,
// ImportState: true,
// ImportStateVerify: true,
// ImportStateVerifyIgnore: []string{"last_updated", "schema_version"},
// },
// },
// })
//}
//
//func testAccDNSSecResourceDestroy(s *terraform.State) error {
// return nil
//}
//
//func testAccDNSSecResourceConfig() string {
// return `
//provider "azion" {
// api_token = "token"
//}
//resource "azion_dnssec" "examples" {
// id = "2595"
// dns_sec = {
// is_enabled = true
// }
//}
//`
//}
//
//func testAccDNSSecResourceConfigUpdate() string {
// return `
//provider "azion" {
// api_token = "token"
//}
//resource "azion_dnssec" "examples" {
// id = "2595"
// dns_sec = {
// is_enabled = false
// }
//}
//`
//}
Empty file modified scripts/generate-docs.sh
100644 → 100755
Empty file.

0 comments on commit 93347d0

Please sign in to comment.