Skip to content

Commit 7e5b2f9

Browse files
Merge pull request #364 from ns1-terraform/fix/zone-networks-empty-array
fix zone creation with empty networks field
2 parents e5a8d18 + b2dfe15 commit 7e5b2f9

6 files changed

Lines changed: 162 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.7.3 (November 25, 2025)
2+
BUGFIX
3+
* Fix zone create with empty networks field
4+
15
## 2.7.2 (October 21, 2025)
26
BUGFIX
37
* Headers were not added to webhook notifiers

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-retryablehttp v0.7.7
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
99
github.com/stretchr/testify v1.8.1
10-
gopkg.in/ns1/ns1-go.v2 v2.15.1
10+
gopkg.in/ns1/ns1-go.v2 v2.15.2
1111
)
1212

1313
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
257257
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
258258
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
259259
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
260-
gopkg.in/ns1/ns1-go.v2 v2.15.1 h1:8rri2TzAPYcVbBGXn48+dz1Xg30PzHfZ4k8A9JOS0Z0=
261-
gopkg.in/ns1/ns1-go.v2 v2.15.1/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
260+
gopkg.in/ns1/ns1-go.v2 v2.15.2 h1:aBVyKeEH3rBFWwX72xPPjEuRL4+Lp5P9GlAcrzu0Y5M=
261+
gopkg.in/ns1/ns1-go.v2 v2.15.2/go.mod h1:pfaU0vECVP7DIOr453z03HXS6dFJpXdNRwOyRzwmPSc=
262262
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
263263
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
264264
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ns1/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
clientVersion = "2.7.2"
22+
clientVersion = "2.7.3"
2323
providerUserAgent = "tf-ns1" + "/" + clientVersion
2424
defaultRetryMax = 3
2525
)

ns1/resource_zone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func resourceDataToZone(z *dns.Zone, d *schema.ResourceData) {
391391
if v, ok := d.GetOk("link"); ok {
392392
z.LinkTo(v.(string))
393393
}
394-
if v, ok := d.GetOk("networks"); ok {
394+
if v, ok := d.GetOkExists("networks"); ok {
395395
networkIDSet := v.(*schema.Set)
396396
z.NetworkIDs = setToInts(networkIDSet)
397397
}

ns1/resource_zone_test.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,144 @@ func TestAccZone_ManualDelete(t *testing.T) {
506506
})
507507
}
508508

509+
func TestAccZone_Networks(t *testing.T) {
510+
var zone dns.Zone
511+
zoneName := fmt.Sprintf("terraform-test-%s.io",
512+
acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum),
513+
)
514+
resource.Test(t, resource.TestCase{
515+
PreCheck: func() { testAccPreCheck(t) },
516+
Providers: testAccProviders,
517+
CheckDestroy: testAccCheckZoneDestroy,
518+
Steps: []resource.TestStep{
519+
// Create with nil networks (field omitted) - should default to [0]
520+
{
521+
Config: testAccZoneDefaultNetworks(zoneName),
522+
Check: resource.ComposeTestCheckFunc(
523+
testAccCheckZoneExists("ns1_zone.it", &zone),
524+
testAccCheckZoneNetworks(&zone, []int{0}, "initial create with nil networks"),
525+
),
526+
},
527+
// Update with nil networks (no change) - should remain [0]
528+
{
529+
Config: testAccZoneDefaultNetworks(zoneName),
530+
Check: resource.ComposeTestCheckFunc(
531+
testAccCheckZoneExists("ns1_zone.it", &zone),
532+
testAccCheckZoneNetworks(&zone, []int{0}, "update with nil networks"),
533+
),
534+
},
535+
// Update with empty networks [] - should become []
536+
{
537+
Config: testAccZoneEmptyNetworks(zoneName),
538+
Check: resource.ComposeTestCheckFunc(
539+
testAccCheckZoneExists("ns1_zone.it", &zone),
540+
testAccCheckZoneNetworks(&zone, []int{}, "update with networks=[]"),
541+
),
542+
},
543+
// Update back to nil networks - should become [0]
544+
{
545+
Config: testAccZoneDefaultNetworks(zoneName),
546+
Check: resource.ComposeTestCheckFunc(
547+
testAccCheckZoneExists("ns1_zone.it", &zone),
548+
testAccCheckZoneNetworks(&zone, []int{0}, "update back to nil networks"),
549+
),
550+
},
551+
// Update with specific networks [1, 2]
552+
{
553+
Config: testAccZoneSpecificNetworks(zoneName, []int{1, 2}),
554+
Check: resource.ComposeTestCheckFunc(
555+
testAccCheckZoneExists("ns1_zone.it", &zone),
556+
testAccCheckZoneNetworks(&zone, []int{1, 2}, "update with networks=[1,2]"),
557+
),
558+
},
559+
// Import verification
560+
{
561+
ResourceName: "ns1_zone.it",
562+
ImportState: true,
563+
ImportStateId: zoneName,
564+
ImportStateVerify: true,
565+
},
566+
},
567+
})
568+
}
569+
570+
func TestAccZone_CreateWithEmptyNetworks(t *testing.T) {
571+
var zone dns.Zone
572+
zoneName := fmt.Sprintf("terraform-test-%s.io",
573+
acctest.RandStringFromCharSet(15, acctest.CharSetAlphaNum),
574+
)
575+
resource.Test(t, resource.TestCase{
576+
PreCheck: func() { testAccPreCheck(t) },
577+
Providers: testAccProviders,
578+
CheckDestroy: testAccCheckZoneDestroy,
579+
Steps: []resource.TestStep{
580+
// Create with empty networks [] - should be []
581+
{
582+
Config: testAccZoneEmptyNetworks(zoneName),
583+
Check: resource.ComposeTestCheckFunc(
584+
testAccCheckZoneExists("ns1_zone.it", &zone),
585+
testAccCheckZoneNetworks(&zone, []int{}, "create with networks=[]"),
586+
),
587+
},
588+
// Update to specific networks
589+
{
590+
Config: testAccZoneSpecificNetworks(zoneName, []int{1}),
591+
Check: resource.ComposeTestCheckFunc(
592+
testAccCheckZoneExists("ns1_zone.it", &zone),
593+
testAccCheckZoneNetworks(&zone, []int{1}, "update to networks=[1]"),
594+
),
595+
},
596+
// Update back to empty networks []
597+
{
598+
Config: testAccZoneEmptyNetworks(zoneName),
599+
Check: resource.ComposeTestCheckFunc(
600+
testAccCheckZoneExists("ns1_zone.it", &zone),
601+
testAccCheckZoneNetworks(&zone, []int{}, "update back to networks=[]"),
602+
),
603+
},
604+
// Import verification
605+
{
606+
ResourceName: "ns1_zone.it",
607+
ImportState: true,
608+
ImportStateId: zoneName,
609+
ImportStateVerify: true,
610+
},
611+
},
612+
})
613+
}
614+
615+
func testAccZoneEmptyNetworks(zoneName string) string {
616+
return fmt.Sprintf(`resource "ns1_zone" "it" {
617+
zone="%s"
618+
networks=[]
619+
}
620+
`, zoneName)
621+
}
622+
623+
func testAccZoneSpecificNetworks(zoneName string, networks []int) string {
624+
networksStr := "["
625+
for i, n := range networks {
626+
if i > 0 {
627+
networksStr += ", "
628+
}
629+
networksStr += fmt.Sprintf("%d", n)
630+
}
631+
networksStr += "]"
632+
633+
return fmt.Sprintf(`resource "ns1_zone" "it" {
634+
zone="%s"
635+
networks=%s
636+
}
637+
`, zoneName, networksStr)
638+
}
639+
640+
func testAccZoneDefaultNetworks(zoneName string) string {
641+
return fmt.Sprintf(`resource "ns1_zone" "it" {
642+
zone="%s"
643+
}
644+
`, zoneName)
645+
}
646+
509647
// A Client instance we can use outside of a TestStep
510648
func sharedClient() (*ns1.Client, error) {
511649
var ignoreSSL bool
@@ -809,6 +947,21 @@ func testAccCheckZoneNotPrimary(z *dns.Zone) resource.TestCheckFunc {
809947
}
810948
}
811949

950+
func testAccCheckZoneNetworks(zone *dns.Zone, expected []int, context string) resource.TestCheckFunc {
951+
return func(s *terraform.State) error {
952+
var actual []int
953+
if zone.NetworkIDs != nil {
954+
actual = zone.NetworkIDs
955+
}
956+
957+
if !reflect.DeepEqual(actual, expected) {
958+
return fmt.Errorf("Networks check failed for %s: got: %v, want: %v",
959+
context, actual, expected)
960+
}
961+
return nil
962+
}
963+
}
964+
812965
func testAccCheckZoneNotSecondary(z *dns.Zone) resource.TestCheckFunc {
813966
return func(s *terraform.State) error {
814967
if z.Secondary != nil {

0 commit comments

Comments
 (0)