@@ -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
510648func 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+
812965func testAccCheckZoneNotSecondary (z * dns.Zone ) resource.TestCheckFunc {
813966 return func (s * terraform.State ) error {
814967 if z .Secondary != nil {
0 commit comments