Skip to content

Commit 9074bf9

Browse files
committed
more acceptance tests coverage and bug fixes from tests
1 parent c71d142 commit 9074bf9

20 files changed

+697
-40
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/swagger_flat.json
22
/swagger_go.json
33
/.env
4-
plan.out
4+
*.out
55

66
*.dll
77
*.exe

GNUmakefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ openapi_generate: ## Generate the go code from the OpenAPI spec.
2424
-g go \
2525
-o /local/internal/netlifyapi ; \
2626
sed -i '' 's/int32/int64/g' internal/netlifyapi/model_*.go ; \
27-
sed -i '' 's/int32/int64/g' internal/netlifyapi/api_*.go
27+
sed -i '' 's/int32/int64/g' internal/netlifyapi/api_*.go ; \
28+
sed -i '' 's/return e.error/return fmt.Sprintf("%s: %q", e.error, e.body)/g' internal/netlifyapi/client.go
2829

2930
test: ## Test the go code.
3031
go test -v ./...
3132

32-
testacc:
33+
testacc: ## Test the go code and run acceptance tests.
3334
TF_ACC=1 go test ./... -v $(TESTARGS)
3435
# -timeout 120m

docs/resources/team_firewall_traffic_rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Netlify team-level firewall traffic rules. [Read more](https://docs.netlify.com/
1414

1515
```terraform
1616
resource "netlify_team_firewall_traffic_rules" "team" {
17-
site_id = data.netlify_team.team.id
17+
team_id = data.netlify_team.team.id
1818
published = {
1919
default_action = "allow"
2020
ip_restrictions = [

examples/resources/netlify_team_firewall_traffic_rules/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "netlify_team_firewall_traffic_rules" "team" {
2-
site_id = data.netlify_team.team.id
2+
team_id = data.netlify_team.team.id
33
published = {
44
default_action = "allow"
55
ip_restrictions = [

internal/netlifyapi/client.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/common_tf_models.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package provider
22

3-
import "github.com/hashicorp/terraform-plugin-framework/types"
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/attr"
5+
"github.com/hashicorp/terraform-plugin-framework/types"
6+
)
47

58
type netlifyDomainModel struct {
69
ID types.String `tfsdk:"id"`
@@ -11,3 +14,15 @@ type netlifyDomainModel struct {
1114
AutoRenew types.Bool `tfsdk:"auto_renew"`
1215
AutoRenewAt types.String `tfsdk:"auto_renew_at"`
1316
}
17+
18+
func (m netlifyDomainModel) AttributeTypes() map[string]attr.Type {
19+
return map[string]attr.Type{
20+
"id": types.StringType,
21+
"name": types.StringType,
22+
"registered_at": types.StringType,
23+
"expires_at": types.StringType,
24+
"renewal_price": types.StringType,
25+
"auto_renew": types.BoolType,
26+
"auto_renew_at": types.StringType,
27+
}
28+
}

internal/provider/dns_record_resource_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import (
66
"testing"
77

88
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
910
"github.com/hashicorp/terraform-plugin-testing/terraform"
1011
)
1112

12-
var zoneId = "66afdbce3cf2b4f0fab520d9"
13-
1413
func TestAccDnsRecordA(t *testing.T) {
14+
var zoneId = "66afdbce3cf2b4f0fab520d9"
1515
accTest(t, []resource.TestStep{
1616
{
17-
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
17+
Config: `resource "netlify_dns_record" "example" {
1818
type = "A"
19-
zone_id = "%s"
19+
zone_id = "66afdbce3cf2b4f0fab520d9"
2020
hostname = "testacc.examplepetstore.com"
2121
value = "10.0.0.0"
22-
}`, zoneId),
22+
}`,
2323
Check: resource.ComposeTestCheckFunc(
2424
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
2525
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
@@ -42,33 +42,36 @@ func TestAccDnsRecordA(t *testing.T) {
4242
ImportStateVerifyIgnore: []string{"last_updated"},
4343
},
4444
{
45-
Config: fmt.Sprintf(`resource "netlify_dns_record" "example" {
45+
Config: `resource "netlify_dns_record" "example" {
4646
type = "A"
47-
zone_id = "%s"
47+
zone_id = "66afdbce3cf2b4f0fab520d9"
4848
hostname = "testacc.examplepetstore.com"
4949
value = "10.0.0.1"
50-
}`, zoneId),
50+
}`,
51+
ConfigPlanChecks: resource.ConfigPlanChecks{
52+
PreApply: []plancheck.PlanCheck{
53+
plancheck.ExpectResourceAction("netlify_dns_record.example", plancheck.ResourceActionReplace),
54+
},
55+
},
5156
Check: resource.ComposeAggregateTestCheckFunc(
5257
resource.TestCheckResourceAttr("netlify_dns_record.example", "type", "A"),
5358
resource.TestCheckResourceAttr("netlify_dns_record.example", "zone_id", zoneId),
5459
resource.TestCheckResourceAttr("netlify_dns_record.example", "hostname", "testacc.examplepetstore.com"),
5560
resource.TestCheckResourceAttr("netlify_dns_record.example", "value", "10.0.0.1"),
5661
),
5762
},
58-
}, testAccDnsRecordCheckDestroy("testacc.examplepetstore.com"))
63+
}, testAccDnsRecordCheckDestroy)
5964
}
6065

61-
func testAccDnsRecordCheckDestroy(hostname string) func(s *terraform.State) error {
62-
return func(s *terraform.State) error {
63-
records, _, err := testAccProvider.client.DNSZonesAPI.GetDnsRecords(context.Background(), zoneId).Execute()
64-
if err != nil {
65-
return err
66-
}
67-
for _, record := range records {
68-
if record.Hostname == hostname {
69-
return fmt.Errorf("DNS record still exists")
70-
}
66+
func testAccDnsRecordCheckDestroy(s *terraform.State) error {
67+
records, _, err := testAccProvider.client.DNSZonesAPI.GetDnsRecords(context.Background(), "66afdbce3cf2b4f0fab520d9").Execute()
68+
if err != nil {
69+
return err
70+
}
71+
for _, record := range records {
72+
if record.Hostname == "testacc.examplepetstore.com" {
73+
return fmt.Errorf("DNS record still exists")
7174
}
72-
return nil
7375
}
76+
return nil
7477
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-testing/terraform"
8+
)
9+
10+
func TestAccDataDnsZone(t *testing.T) {
11+
accTest(t, []resource.TestStep{
12+
{
13+
Config: `data "netlify_dns_zone" "example" {
14+
name = "examplepetstore.com"
15+
}`,
16+
Check: resource.ComposeTestCheckFunc(
17+
resource.TestCheckResourceAttr("data.netlify_dns_zone.example", "id", "66afdbce3cf2b4f0fab520d9"),
18+
),
19+
},
20+
}, func(s *terraform.State) error { return nil })
21+
}

internal/provider/dns_zone_resource.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ type dnsZoneResource struct {
3232
}
3333

3434
type dnsZoneResourceModel struct {
35-
ID types.String `tfsdk:"id"`
36-
LastUpdated types.String `tfsdk:"last_updated"`
37-
Name types.String `tfsdk:"name"`
38-
TeamID types.String `tfsdk:"team_id"`
39-
TeamSlug types.String `tfsdk:"team_slug"`
40-
DnsServers types.List `tfsdk:"dns_servers"`
41-
Domain *netlifyDomainModel `tfsdk:"domain"`
35+
ID types.String `tfsdk:"id"`
36+
LastUpdated types.String `tfsdk:"last_updated"`
37+
Name types.String `tfsdk:"name"`
38+
TeamID types.String `tfsdk:"team_id"`
39+
TeamSlug types.String `tfsdk:"team_slug"`
40+
DnsServers types.List `tfsdk:"dns_servers"`
41+
Domain types.Object `tfsdk:"domain"`
4242
}
4343

4444
func (r *dnsZoneResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
@@ -183,17 +183,18 @@ func (r *dnsZoneResource) Create(ctx context.Context, req resource.CreateRequest
183183
plan.DnsServers, diags = types.ListValueFrom(ctx, types.StringType, dnsServers)
184184
resp.Diagnostics.Append(diags...)
185185
if dnsZone.Domain == nil {
186-
plan.Domain = nil
186+
plan.Domain = types.ObjectNull(netlifyDomainModel{}.AttributeTypes())
187187
} else {
188-
plan.Domain = &netlifyDomainModel{
188+
plan.Domain, diags = types.ObjectValueFrom(ctx, netlifyDomainModel{}.AttributeTypes(), netlifyDomainModel{
189189
ID: types.StringValue(dnsZone.Domain.Id),
190190
Name: types.StringValue(dnsZone.Domain.Name),
191191
RegisteredAt: types.StringValue(dnsZone.Domain.RegisteredAt.Format(time.RFC3339)),
192192
ExpiresAt: types.StringValue(dnsZone.Domain.ExpiresAt.Format(time.RFC3339)),
193193
RenewalPrice: types.StringValue(dnsZone.Domain.RenewalPrice),
194194
AutoRenew: types.BoolValue(dnsZone.Domain.AutoRenew),
195195
AutoRenewAt: types.StringValue(dnsZone.Domain.AutoRenewAt.Format(time.RFC3339)),
196-
}
196+
})
197+
resp.Diagnostics.Append(diags...)
197198
}
198199

199200
_, _, err = r.data.client.DNSZonesAPI.EnableDnsZoneIpv6(ctx, plan.ID.ValueString()).Execute()
@@ -244,17 +245,18 @@ func (r *dnsZoneResource) Read(ctx context.Context, req resource.ReadRequest, re
244245
state.DnsServers, diags = types.ListValueFrom(ctx, types.StringType, dnsServers)
245246
resp.Diagnostics.Append(diags...)
246247
if dnsZone.Domain == nil {
247-
state.Domain = nil
248+
state.Domain = types.ObjectNull(netlifyDomainModel{}.AttributeTypes())
248249
} else {
249-
state.Domain = &netlifyDomainModel{
250+
state.Domain, diags = types.ObjectValueFrom(ctx, netlifyDomainModel{}.AttributeTypes(), netlifyDomainModel{
250251
ID: types.StringValue(dnsZone.Domain.Id),
251252
Name: types.StringValue(dnsZone.Domain.Name),
252253
RegisteredAt: types.StringValue(dnsZone.Domain.RegisteredAt.Format(time.RFC3339)),
253254
ExpiresAt: types.StringValue(dnsZone.Domain.ExpiresAt.Format(time.RFC3339)),
254255
RenewalPrice: types.StringValue(dnsZone.Domain.RenewalPrice),
255256
AutoRenew: types.BoolValue(dnsZone.Domain.AutoRenew),
256257
AutoRenewAt: types.StringValue(dnsZone.Domain.AutoRenewAt.Format(time.RFC3339)),
257-
}
258+
})
259+
resp.Diagnostics.Append(diags...)
258260
}
259261

260262
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
10+
"github.com/hashicorp/terraform-plugin-testing/terraform"
11+
)
12+
13+
func TestAccDnsZone(t *testing.T) {
14+
accTest(t, []resource.TestStep{
15+
{
16+
Config: `resource "netlify_dns_zone" "example" {
17+
name = "tfsoftsecretnetlifytestingexamplestore.com"
18+
team_slug = "netlify-terraform-test"
19+
}`,
20+
Check: resource.ComposeAggregateTestCheckFunc(
21+
resource.TestCheckResourceAttrSet("netlify_dns_zone.example", "id"),
22+
resource.TestCheckResourceAttr("netlify_dns_zone.example", "team_id", "66ae34e11a567e9092e3850f"),
23+
),
24+
},
25+
{
26+
ResourceName: "netlify_dns_zone.example",
27+
ImportState: true,
28+
ImportStateVerify: true,
29+
ImportStateVerifyIgnore: []string{"last_updated"},
30+
},
31+
32+
{
33+
Config: `resource "netlify_dns_zone" "example" {
34+
name = "tfsoftsecretnetlifytestingexamplestore2.com"
35+
team_slug = "netlify-terraform-test"
36+
}`,
37+
ConfigPlanChecks: resource.ConfigPlanChecks{
38+
PreApply: []plancheck.PlanCheck{
39+
plancheck.ExpectResourceAction("netlify_dns_zone.example", plancheck.ResourceActionReplace),
40+
},
41+
},
42+
},
43+
}, testAccDnsZoneDestroy)
44+
}
45+
46+
func testAccDnsZoneDestroy(s *terraform.State) error {
47+
for _, m := range s.Modules {
48+
if v, ok := m.Resources["netlify_dns_zone.example"]; ok {
49+
key, _, err := testAccProvider.client.DNSZonesAPI.GetDnsZone(context.Background(), v.Primary.Attributes["id"]).Execute()
50+
if err != nil {
51+
//lint:ignore nilerr we expect an error to know it was not found
52+
return nil
53+
}
54+
return fmt.Errorf("DNS zone still exists: %s", key.Id)
55+
}
56+
}
57+
return fmt.Errorf("not found in testAccDnsZoneDestroy check destroy")
58+
}

0 commit comments

Comments
 (0)