From 9b6bca5b4326f9a50795f2457bee8f716765ed8c Mon Sep 17 00:00:00 2001 From: Alan Platt Date: Thu, 29 Oct 2020 18:06:41 +0000 Subject: [PATCH 1/7] Add zone resource --- infoblox/provider.go | 1 + infoblox/resource_infoblox_zone_auth.go | 151 ++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100755 infoblox/resource_infoblox_zone_auth.go diff --git a/infoblox/provider.go b/infoblox/provider.go index 541ba59..6075633 100755 --- a/infoblox/provider.go +++ b/infoblox/provider.go @@ -70,6 +70,7 @@ func Provider() terraform.ResourceProvider { "infoblox_a_record": resourceARecord(), "infoblox_cname_record": resourceCNAMERecord(), "infoblox_ptr_record": resourcePTRRecord(), + "infoblox_zone_auth": resourceZoneAuth(), }, DataSourcesMap: map[string]*schema.Resource{ "infoblox_network": dataSourceNetwork(), diff --git a/infoblox/resource_infoblox_zone_auth.go b/infoblox/resource_infoblox_zone_auth.go new file mode 100755 index 0000000..1535ac9 --- /dev/null +++ b/infoblox/resource_infoblox_zone_auth.go @@ -0,0 +1,151 @@ +package infoblox + +import ( + "fmt" + "log" + "strings" + + "github.com/hashicorp/terraform/helper/schema" + ibclient "github.com/infobloxopen/infoblox-go-client" +) + +func resourceZoneAuth() *schema.Resource { + return &schema.Resource{ + Create: resourceZoneAuthCreate, + Read: resourceZoneAuthGet, + Update: resourceZoneAuthUpdate, + Delete: resourceZoneAuthDelete, + + Schema: map[string]*schema.Schema{ + + "fqdn": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "The fqdn of the auth zone to create.", + }, + + "dns_view": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "default", + Description: "Dns View under which the zone has been created.", + }, + + "tenant_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Description: "Unique identifier of your tenant in cloud.", + }, + }, + } +} + +func resourceZoneAuthCreate(d *schema.ResourceData, m interface{}) error { + log.Printf("[DEBUG] %s: Beginning to create auth zone from required network block", resourceZoneAuthIDString(d)) + + fqdn := d.Get("fqdn").(string) + tenantID := d.Get("tenant_id").(string) + connector := m.(*ibclient.Connector) + + ea := make(ibclient.EA) + + objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) + + ZoneAuth, err := objMgr.CreateZoneAuth(fqdn, ea) + + if err != nil { + return fmt.Errorf("Error creating auth zone (%s): %s", fqdn, err) + } + + d.SetId(ZoneAuth.Ref) + + log.Printf("[DEBUG] %s: Creation of auth zone complete", resourceZoneAuthIDString(d)) + + return nil + return resourceZoneAuthGet(d, m) +} + +func resourceZoneAuthGet(d *schema.ResourceData, m interface{}) error { + + log.Printf("[DEBUG] %s: Beginning to Get auth zone", resourceZoneAuthIDString(d)) + + fqdn := d.Get("fqdn").(string) + tenantID := d.Get("tenant_id").(string) + connector := m.(*ibclient.Connector) + + objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) + + obj, err := objMgr.GetZoneAuthByRef(d.Id()) + if err != nil { + return fmt.Errorf("Getting auth zone failed from dns view (%s) : %s", fqdn, err) + } + d.SetId(obj.Ref) + log.Printf("[DEBUG] %s: Completed reading required auth zone ", resourceZoneAuthIDString(d)) + return nil +} + +func resourceZoneAuthUpdate(d *schema.ResourceData, m interface{}) error { + + return fmt.Errorf("updating a auth zone is not supported") + + // log.Printf("[DEBUG] %s: Beginning to Update auth zone", resourceZoneAuthIDString(d)) + + // dnsView := d.Get("dns_view").(string) + // port := uint(d.Get("port").(int)) + // tenantID := d.Get("tenant_id").(string) + // connector := m.(*ibclient.Connector) + + // objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) + + // obj, err := objMgr.UpdateZoneAuth(d.Id(), dnsView, port) + // if err != nil { + // return fmt.Errorf("Updating auth zone failed from dns view (%s) : %s", dnsView, err) + // } + // d.SetId(obj.Ref) + // log.Printf("[DEBUG] %s: Completed updating required auth zone", resourceZoneAuthIDString(d)) + // return nil +} + +func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { + + log.Printf("[DEBUG] %s: Beginning Deletion of auth zone", resourceZoneAuthIDString(d)) + + fqdn := d.Get("fqdn").(string) + tenantID := d.Get("tenant_id").(string) + connector := m.(*ibclient.Connector) + + domain := strings.SplitAfterN(fqdn, ".", 2) + + log.Printf("++++++++++++++++++[DEBUG] %s: ++++++++++++++++++++++++", domain[1]) + + objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) + + foo, bar := objMgr.GetZoneAuthByFQDN(domain[1]) + + if foo != nil { + log.Printf("++++++++++++++++++[DEBUG] %s: ++++++++++++++++++++++++ %s", foo.Ref, bar) + } + + return fmt.Errorf("deleting a auth zone is not supported") + + _, err := objMgr.DeleteZoneAuth(d.Id()) + if err != nil { + return fmt.Errorf("Deletion of auth zone failed from dns view(%s) : %s", fqdn, err) + } + d.SetId("") + + log.Printf("[DEBUG] %s: Deletion of auth zone complete", resourceZoneAuthIDString(d)) + return nil +} + +type resourceZoneAuthIDStringInterface interface { + Id() string +} + +func resourceZoneAuthIDString(d resourceZoneAuthIDStringInterface) string { + id := d.Id() + if id == "" { + id = "" + } + return fmt.Sprintf("infoblox_auth_zone (ID = %s)", id) +} From f19cf1ee7d24a94b3041d16ae00491ac0cb0224d Mon Sep 17 00:00:00 2001 From: Alan Platt <2979908+alanplatt@users.noreply.github.com> Date: Wed, 4 Nov 2020 17:36:27 +0000 Subject: [PATCH 2/7] Add acceptance tests for zone_auth --- infoblox/resource_infoblox_zone_auth.go | 15 +--- infoblox/resource_infoblox_zone_auth_test.go | 86 ++++++++++++++++++++ 2 files changed, 87 insertions(+), 14 deletions(-) create mode 100755 infoblox/resource_infoblox_zone_auth_test.go diff --git a/infoblox/resource_infoblox_zone_auth.go b/infoblox/resource_infoblox_zone_auth.go index 1535ac9..f39d5e9 100755 --- a/infoblox/resource_infoblox_zone_auth.go +++ b/infoblox/resource_infoblox_zone_auth.go @@ -3,7 +3,6 @@ package infoblox import ( "fmt" "log" - "strings" "github.com/hashicorp/terraform/helper/schema" ibclient "github.com/infobloxopen/infoblox-go-client" @@ -114,19 +113,7 @@ func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { tenantID := d.Get("tenant_id").(string) connector := m.(*ibclient.Connector) - domain := strings.SplitAfterN(fqdn, ".", 2) - - log.Printf("++++++++++++++++++[DEBUG] %s: ++++++++++++++++++++++++", domain[1]) - - objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) - - foo, bar := objMgr.GetZoneAuthByFQDN(domain[1]) - - if foo != nil { - log.Printf("++++++++++++++++++[DEBUG] %s: ++++++++++++++++++++++++ %s", foo.Ref, bar) - } - - return fmt.Errorf("deleting a auth zone is not supported") + objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) _, err := objMgr.DeleteZoneAuth(d.Id()) if err != nil { diff --git a/infoblox/resource_infoblox_zone_auth_test.go b/infoblox/resource_infoblox_zone_auth_test.go new file mode 100755 index 0000000..285814d --- /dev/null +++ b/infoblox/resource_infoblox_zone_auth_test.go @@ -0,0 +1,86 @@ +package infoblox + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + ibclient "github.com/infobloxopen/infoblox-go-client" +) + +func TestAccResourceZoneAuth(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckZoneAuthDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccresourceZoneAuthCreate, + Check: resource.ComposeTestCheckFunc( + testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), + ), + }, + resource.TestStep{ + Config: testAccresourceZoneAuthUpdate, + Check: resource.ComposeTestCheckFunc( + testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), + ), + }, + }, + }) +} + +func testAccCheckZoneAuthDestroy(s *terraform.State) error { + meta := testAccProvider.Meta() + + for _, rs := range s.RootModule().Resources { + if rs.Type != "resource_a_record" { + continue + } + Connector := meta.(*ibclient.Connector) + objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "test") + recordName, _ := objMgr.GetZoneAuthByRef(rs.Primary.ID) + if recordName != nil { + return fmt.Errorf("record not found") + } + + } + return nil +} +func testAccZoneAuthExists(t *testing.T, n string, fqdn string, dns_view string, tenant_id string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found:%s", n) + } + if rs.Primary.ID == "" { + return fmt.Errorf("No ID i set") + } + meta := testAccProvider.Meta() + Connector := meta.(*ibclient.Connector) + objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "test") + + recordName, _ := objMgr.GetZoneAuthByRef(rs.Primary.ID) + if recordName == nil { + return fmt.Errorf("record not found") + } + + return nil + } +} + +var testAccresourceZoneAuthCreate = fmt.Sprintf(` +resource "infoblox_zone_auth" "zone_auth"{ + fqdn = "acctest.com" + dns_view="default" + tenant_id="test" + }`) + +var testAccresourceZoneAuthUpdate = fmt.Sprintf(` +resource "infoblox_zone_auth" "zone_auth"{ + fqdn = "acctest.com" + dns_view="default" + tenant_id="test" + }`) \ No newline at end of file From 2adf4f10377ccf5df3231d076035a23e0f536a14 Mon Sep 17 00:00:00 2001 From: Alan Date: Tue, 1 Dec 2020 17:04:00 +0000 Subject: [PATCH 3/7] go fmt code --- infoblox/resource_infoblox_zone_auth.go | 2 +- infoblox/resource_infoblox_zone_auth_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/infoblox/resource_infoblox_zone_auth.go b/infoblox/resource_infoblox_zone_auth.go index f39d5e9..1e6c731 100755 --- a/infoblox/resource_infoblox_zone_auth.go +++ b/infoblox/resource_infoblox_zone_auth.go @@ -113,7 +113,7 @@ func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { tenantID := d.Get("tenant_id").(string) connector := m.(*ibclient.Connector) - objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) + objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) _, err := objMgr.DeleteZoneAuth(d.Id()) if err != nil { diff --git a/infoblox/resource_infoblox_zone_auth_test.go b/infoblox/resource_infoblox_zone_auth_test.go index 285814d..e4b9867 100755 --- a/infoblox/resource_infoblox_zone_auth_test.go +++ b/infoblox/resource_infoblox_zone_auth_test.go @@ -25,7 +25,7 @@ func TestAccResourceZoneAuth(t *testing.T) { resource.TestStep{ Config: testAccresourceZoneAuthUpdate, Check: resource.ComposeTestCheckFunc( - testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), + testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), ), }, }, @@ -77,10 +77,10 @@ resource "infoblox_zone_auth" "zone_auth"{ dns_view="default" tenant_id="test" }`) - + var testAccresourceZoneAuthUpdate = fmt.Sprintf(` resource "infoblox_zone_auth" "zone_auth"{ fqdn = "acctest.com" dns_view="default" tenant_id="test" - }`) \ No newline at end of file + }`) From 3bf9440ec0bef0be44cac6d64f0998a78f64f545 Mon Sep 17 00:00:00 2001 From: Alan Date: Tue, 1 Dec 2020 17:22:49 +0000 Subject: [PATCH 4/7] Upgrade infoblox-go-client to v1.1.1-0.20201120132302-107472b6d379 --- go.mod | 2 +- go.sum | 4 ++++ infoblox/resource_infoblox_ip_association.go | 2 +- infoblox/resource_infoblox_zone_auth_test.go | 12 ++++++------ 4 files changed, 12 insertions(+), 8 deletions(-) mode change 100755 => 100644 go.mod diff --git a/go.mod b/go.mod old mode 100755 new mode 100644 index bd2ee86..d3cf006 --- a/go.mod +++ b/go.mod @@ -4,5 +4,5 @@ go 1.12 require ( github.com/hashicorp/terraform v0.12.9 - github.com/infobloxopen/infoblox-go-client v0.8.1-0.20190830062100-dd50c409ab6d + github.com/infobloxopen/infoblox-go-client v1.1.1-0.20201120132302-107472b6d379 ) diff --git a/go.sum b/go.sum index dd75c2d..543c681 100644 --- a/go.sum +++ b/go.sum @@ -176,6 +176,10 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/infobloxopen/infoblox-go-client v0.8.1-0.20190830062100-dd50c409ab6d h1:oFAo63W76GKVRa6AWa5AFKFsJlUhkBtoXV89WDHPj3A= github.com/infobloxopen/infoblox-go-client v0.8.1-0.20190830062100-dd50c409ab6d/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI= +github.com/infobloxopen/infoblox-go-client v1.1.0 h1:fw8q8USnngsoZxLploJ0LomBN+1SAhSyEjUZrSibKX4= +github.com/infobloxopen/infoblox-go-client v1.1.0/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI= +github.com/infobloxopen/infoblox-go-client v1.1.1-0.20201120132302-107472b6d379 h1:KgMxhxbwMHyb7rjeFVCCxP98ipFmJ75bdKSb7OMnxpk= +github.com/infobloxopen/infoblox-go-client v1.1.1-0.20201120132302-107472b6d379/go.mod h1:BXiw7S2b9qJoM8MS40vfgCNB2NLHGusk1DtO16BD9zI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= diff --git a/infoblox/resource_infoblox_ip_association.go b/infoblox/resource_infoblox_ip_association.go index 3d35897..ba9437a 100755 --- a/infoblox/resource_infoblox_ip_association.go +++ b/infoblox/resource_infoblox_ip_association.go @@ -191,7 +191,7 @@ func Resource(d *schema.ResourceData, m interface{}) error { name := Name + "." + zone if (zone != "" || len(zone) != 0) && (dnsView != "" || len(dnsView) != 0) { - hostRecordObj, err := objMgr.GetHostRecord(name, networkViewName, cidr, ipAddr) + hostRecordObj, err := objMgr.GetHostRecord(name) if err != nil { return fmt.Errorf("GetHostRecord failed from network block(%s):%s", cidr, err) } diff --git a/infoblox/resource_infoblox_zone_auth_test.go b/infoblox/resource_infoblox_zone_auth_test.go index e4b9867..501e367 100755 --- a/infoblox/resource_infoblox_zone_auth_test.go +++ b/infoblox/resource_infoblox_zone_auth_test.go @@ -41,9 +41,9 @@ func testAccCheckZoneAuthDestroy(s *terraform.State) error { } Connector := meta.(*ibclient.Connector) objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "test") - recordName, _ := objMgr.GetZoneAuthByRef(rs.Primary.ID) - if recordName != nil { - return fmt.Errorf("record not found") + _, err := objMgr.GetZoneAuthByRef(rs.Primary.ID) + if err != nil { + return fmt.Errorf("Error:%s - record not found", err) } } @@ -62,9 +62,9 @@ func testAccZoneAuthExists(t *testing.T, n string, fqdn string, dns_view string, Connector := meta.(*ibclient.Connector) objMgr := ibclient.NewObjectManager(Connector, "terraform_test", "test") - recordName, _ := objMgr.GetZoneAuthByRef(rs.Primary.ID) - if recordName == nil { - return fmt.Errorf("record not found") + _, err := objMgr.GetZoneAuthByRef(rs.Primary.ID) + if err != nil { + return fmt.Errorf("Error:%s - record not found", err) } return nil From 2b027321e30779a6aa7f017943fd4285de816361 Mon Sep 17 00:00:00 2001 From: Alan Platt <2979908+alanplatt@users.noreply.github.com> Date: Wed, 2 Dec 2020 15:23:19 +0000 Subject: [PATCH 5/7] Remove unused comments --- infoblox/resource_infoblox_zone_auth.go | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/infoblox/resource_infoblox_zone_auth.go b/infoblox/resource_infoblox_zone_auth.go index 1e6c731..0e710ec 100755 --- a/infoblox/resource_infoblox_zone_auth.go +++ b/infoblox/resource_infoblox_zone_auth.go @@ -85,24 +85,8 @@ func resourceZoneAuthGet(d *schema.ResourceData, m interface{}) error { func resourceZoneAuthUpdate(d *schema.ResourceData, m interface{}) error { - return fmt.Errorf("updating a auth zone is not supported") + return fmt.Errorf("Updating an auth zone is not supported") - // log.Printf("[DEBUG] %s: Beginning to Update auth zone", resourceZoneAuthIDString(d)) - - // dnsView := d.Get("dns_view").(string) - // port := uint(d.Get("port").(int)) - // tenantID := d.Get("tenant_id").(string) - // connector := m.(*ibclient.Connector) - - // objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) - - // obj, err := objMgr.UpdateZoneAuth(d.Id(), dnsView, port) - // if err != nil { - // return fmt.Errorf("Updating auth zone failed from dns view (%s) : %s", dnsView, err) - // } - // d.SetId(obj.Ref) - // log.Printf("[DEBUG] %s: Completed updating required auth zone", resourceZoneAuthIDString(d)) - // return nil } func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { From 8b74e82e08c035a2e207837785c8641df8b1f4d6 Mon Sep 17 00:00:00 2001 From: Alan Platt <2979908+alanplatt@users.noreply.github.com> Date: Wed, 2 Dec 2020 15:33:52 +0000 Subject: [PATCH 6/7] Add zones to README --- README.md | 2 +- infoblox/resource_infoblox_ip_association.go | 2 +- .../infobloxopen/infoblox-go-client/README.md | 23 +- .../infoblox-go-client/object_manager.go | 293 +++++++++++++++--- .../infoblox-go-client/objects.go | 66 +++- 5 files changed, 333 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index b524d89..2d3fd73 100755 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ $ make testacc * Creation & Deletion of Network in NIOS appliance * Allocation & Deallocation of IP from a Network * Association & Disassociation of IP Address for a VM -* Creation and Deletion of A, CNAME, Host, and Ptr records +* Creation and Deletion of A, CNAME, Host, Zones and Ptr records ### Data Source * Supports Data Source for Network diff --git a/infoblox/resource_infoblox_ip_association.go b/infoblox/resource_infoblox_ip_association.go index ba9437a..3d35897 100755 --- a/infoblox/resource_infoblox_ip_association.go +++ b/infoblox/resource_infoblox_ip_association.go @@ -191,7 +191,7 @@ func Resource(d *schema.ResourceData, m interface{}) error { name := Name + "." + zone if (zone != "" || len(zone) != 0) && (dnsView != "" || len(dnsView) != 0) { - hostRecordObj, err := objMgr.GetHostRecord(name) + hostRecordObj, err := objMgr.GetHostRecord(name, networkViewName, cidr, ipAddr) if err != nil { return fmt.Errorf("GetHostRecord failed from network block(%s):%s", cidr, err) } diff --git a/vendor/github.com/infobloxopen/infoblox-go-client/README.md b/vendor/github.com/infobloxopen/infoblox-go-client/README.md index de74e9c..d39cc07 100644 --- a/vendor/github.com/infobloxopen/infoblox-go-client/README.md +++ b/vendor/github.com/infobloxopen/infoblox-go-client/README.md @@ -48,21 +48,22 @@ This library is compatible with Go 1.2+ ## Supported NIOS operations - * CreateNetworkView + * AllocateNetwork * CreateDefaultNetviews + * CreateEADefinition * CreateNetwork * CreateNetworkContainer - * GetNetworkView + * CreateNetworkView + * DeleteNetwork + * DeleteNetworkView + * GetAllMembers + * GetCapacityReport + * GetEADefinition + * GetFixedAddress * GetNetwork * GetNetworkContainer - * AllocateNetwork - * UpdateFixedAddress - * GetFixedAddress + * GetNetworkView + * GetUpgradeStatus (2.7 or above) * ReleaseIP - * DeleteNetwork - * GetEADefinition - * CreateEADefinition + * UpdateFixedAddress * UpdateNetworkViewEA - * GetCapacityReport - * GetAllMembers - * GetUpgradeStatus (2.7 or above) diff --git a/vendor/github.com/infobloxopen/infoblox-go-client/object_manager.go b/vendor/github.com/infobloxopen/infoblox-go-client/object_manager.go index e2a565e..d2b1d2b 100644 --- a/vendor/github.com/infobloxopen/infoblox-go-client/object_manager.go +++ b/vendor/github.com/infobloxopen/infoblox-go-client/object_manager.go @@ -8,39 +8,40 @@ import ( ) type IBObjectManager interface { - CreateNetworkView(name string) (*NetworkView, error) + AllocateIP(netview string, cidr string, ipAddr string, macAddress string, name string, ea EA) (*FixedAddress, error) + AllocateNetwork(netview string, cidr string, prefixLen uint, name string) (network *Network, err error) + CreateARecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordA, error) + CreateCNAMERecord(canonical string, recordname string, dnsview string, ea EA) (*RecordCNAME, error) CreateDefaultNetviews(globalNetview string, localNetview string) (globalNetviewRef string, localNetviewRef string, err error) + CreateEADefinition(eadef EADefinition) (*EADefinition, error) + CreateHostRecord(enabledns bool, recordName string, netview string, dnsview string, cidr string, ipAddr string, macAddress string, ea EA) (*HostRecord, error) CreateNetwork(netview string, cidr string, name string) (*Network, error) CreateNetworkContainer(netview string, cidr string) (*NetworkContainer, error) - GetNetworkView(name string) (*NetworkView, error) - GetNetwork(netview string, cidr string, ea EA) (*Network, error) - GetNetworkContainer(netview string, cidr string) (*NetworkContainer, error) - AllocateIP(netview string, cidr string, ipAddr string, macAddress string, name string, ea EA) (*FixedAddress, error) - AllocateNetwork(netview string, cidr string, prefixLen uint, name string) (network *Network, err error) - UpdateFixedAddress(fixedAddrRef string, matchclient string, macAddress string, vmID string, vmName string) (*FixedAddress, error) - GetFixedAddress(netview string, cidr string, ipAddr string, macAddr string) (*FixedAddress, error) - GetFixedAddressByRef(ref string) (*FixedAddress, error) + CreateNetworkView(name string) (*NetworkView, error) + CreatePTRRecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordPTR, error) + DeleteARecord(ref string) (string, error) + DeleteCNAMERecord(ref string) (string, error) DeleteFixedAddress(ref string) (string, error) - ReleaseIP(netview string, cidr string, ipAddr string, macAddr string) (string, error) + DeleteHostRecord(ref string) (string, error) DeleteNetwork(ref string, netview string) (string, error) + DeleteNetworkView(ref string) (string, error) + DeletePTRRecord(ref string) (string, error) + GetARecordByRef(ref string) (*RecordA, error) + GetCNAMERecordByRef(ref string) (*RecordA, error) GetEADefinition(name string) (*EADefinition, error) - CreateEADefinition(eadef EADefinition) (*EADefinition, error) - UpdateNetworkViewEA(ref string, addEA EA, removeEA EA) error - CreateHostRecord(enabledns bool, recordName string, netview string, dnsview string, cidr string, ipAddr string, macAddress string, ea EA) (*HostRecord, error) - GetHostRecordByRef(ref string) (*HostRecord, error) + GetFixedAddress(netview string, cidr string, ipAddr string, macAddr string) (*FixedAddress, error) + GetFixedAddressByRef(ref string) (*FixedAddress, error) GetHostRecord(recordName string, netview string, cidr string, ipAddr string) (*HostRecord, error) + GetHostRecordByRef(ref string) (*HostRecord, error) GetIpAddressFromHostRecord(host HostRecord) (string, error) - UpdateHostRecord(hostRref string, ipAddr string, macAddress string, vmID string, vmName string) (string, error) - DeleteHostRecord(ref string) (string, error) - CreateARecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordA, error) - GetARecordByRef(ref string) (*RecordA, error) - DeleteARecord(ref string) (string, error) - CreateCNAMERecord(canonical string, recordname string, dnsview string, ea EA) (*RecordCNAME, error) - GetCNAMERecordByRef(ref string) (*RecordA, error) - DeleteCNAMERecord(ref string) (string, error) - CreatePTRRecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordPTR, error) + GetNetwork(netview string, cidr string, ea EA) (*Network, error) + GetNetworkContainer(netview string, cidr string) (*NetworkContainer, error) + GetNetworkView(name string) (*NetworkView, error) GetPTRRecordByRef(ref string) (*RecordPTR, error) - DeletePTRRecord(ref string) (string, error) + ReleaseIP(netview string, cidr string, ipAddr string, macAddr string) (string, error) + UpdateFixedAddress(fixedAddrRef string, matchclient string, macAddress string, vmID string, vmName string) (*FixedAddress, error) + UpdateHostRecord(hostRref string, ipAddr string, macAddress string, vmID string, vmName string) (string, error) + UpdateNetworkViewEA(ref string, addEA EA, removeEA EA) error } type ObjectManager struct { @@ -49,7 +50,6 @@ type ObjectManager struct { tenantID string } - func NewObjectManager(connector IBConnector, cmpType string, tenantID string) *ObjectManager { objMgr := new(ObjectManager) @@ -68,10 +68,10 @@ func (objMgr *ObjectManager) getBasicEA(cloudAPIOwned Bool) EA { return ea } -func (objMgr *ObjectManager) extendEA(ea EA) EA{ +func (objMgr *ObjectManager) extendEA(ea EA) EA { eas := objMgr.getBasicEA(true) - for k,v :=range ea{ - eas[k]=v + for k, v := range ea { + eas[k] = v } return eas } @@ -284,7 +284,7 @@ func (objMgr *ObjectManager) AllocateIP(netview string, cidr string, ipAddr stri Cidr: cidr, Mac: macAddress, Name: name, - Ea: eas}) + Ea: eas}) if ipAddr == "" { fixedAddr.IPAddress = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview) @@ -381,7 +381,7 @@ func (objMgr *ObjectManager) UpdateFixedAddress(fixedAddrRef string, matchClient if validateMatchClient(matchClient) { updateFixedAddr.MatchClient = matchClient } else { - return nil , fmt.Errorf("wrong value for match_client passed %s \n ", matchClient) + return nil, fmt.Errorf("wrong value for match_client passed %s \n ", matchClient) } } @@ -407,6 +407,10 @@ func (objMgr *ObjectManager) DeleteNetwork(ref string, netview string) (string, return "", nil } +func (objMgr *ObjectManager) DeleteNetworkView(ref string) (string, error) { + return objMgr.connector.DeleteObject(ref) +} + func (objMgr *ObjectManager) GetEADefinition(name string) (*EADefinition, error) { var res []EADefinition @@ -453,6 +457,9 @@ func (objMgr *ObjectManager) CreateHostRecord(enabledns bool, recordName string, Ea: eas}) ref, err := objMgr.connector.CreateObject(recordHost) + if err != nil { + return nil,err + } recordHost.Ref = ref err = objMgr.connector.GetObject(recordHost, ref, &recordHost) return recordHost, err @@ -510,10 +517,60 @@ func (objMgr *ObjectManager) DeleteHostRecord(ref string) (string, error) { return objMgr.connector.DeleteObject(ref) } -func (objMgr *ObjectManager) CreateARecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordA, error) { +func (objMgr *ObjectManager) CreateSRVRecord(dnsview string, recordname string, port uint, priority uint, target string, weight uint, ea EA) (*RecordSRV, error) { eas := objMgr.extendEA(ea) + recordSRV := NewRecordSRV(RecordSRV{ + Name: recordname, + Port: port, + Priority: priority, + Target: target, + View: dnsview, + Weight: weight, + Ea: eas}) + + ref, err := objMgr.connector.CreateObject(recordSRV) + recordSRV.Ref = ref + return recordSRV, err +} + +func (objMgr *ObjectManager) GetSRVRecordByRef(ref string) (*RecordSRV, error) { + recordSRV := NewRecordSRV(RecordSRV{}) + err := objMgr.connector.GetObject(recordSRV, ref, &recordSRV) + return recordSRV, err +} + +func (objMgr *ObjectManager) DeleteSRVRecord(ref string) (string, error) { + return objMgr.connector.DeleteObject(ref) +} + + +func (objMgr *ObjectManager) UpdateSRVRecord(ref string, recordname string, port uint) (*RecordSRV, error) { + + recordSRV := NewRecordSRV(RecordSRV{Name: recordname}) + + err := objMgr.connector.GetObject(recordSRV, ref, &recordSRV) + + recordSRV.Port = port + + recordSRV.View = "" + + + _, err = objMgr.connector.UpdateObject(recordSRV, ref) + + if err != nil { + return nil, err + } + + return recordSRV, nil +} + + + +func (objMgr *ObjectManager) CreateARecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordA, error) { + + eas := objMgr.extendEA(ea) recordA := NewRecordA(RecordA{ View: dnsview, @@ -539,7 +596,9 @@ func (objMgr *ObjectManager) DeleteARecord(ref string) (string, error) { return objMgr.connector.DeleteObject(ref) } -func (objMgr *ObjectManager) CreateCNAMERecord(canonical string, recordname string, dnsview string, ea EA)(*RecordCNAME, error) { + + +func (objMgr *ObjectManager) CreateCNAMERecord(canonical string, recordname string, dnsview string, ea EA) (*RecordCNAME, error) { eas := objMgr.extendEA(ea) @@ -547,7 +606,7 @@ func (objMgr *ObjectManager) CreateCNAMERecord(canonical string, recordname stri View: dnsview, Name: recordname, Canonical: canonical, - Ea: eas}) + Ea: eas}) ref, err := objMgr.connector.CreateObject(recordCNAME) recordCNAME.Ref = ref @@ -564,11 +623,75 @@ func (objMgr *ObjectManager) DeleteCNAMERecord(ref string) (string, error) { return objMgr.connector.DeleteObject(ref) } -func (objMgr *ObjectManager) CreatePTRRecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordPTR, error) { +// Creates TXT Record. Use TTL of 0 to inherit TTL from the Zone +func (objMgr *ObjectManager) CreateTXTRecord(recordname string, text string, ttl int, dnsview string) (*RecordTXT, error) { + recordTXT := NewRecordTXT(RecordTXT{ + View: dnsview, + Name: recordname, + Text: text, + TTL: ttl, + }) - eas := objMgr.extendEA(ea) + ref, err := objMgr.connector.CreateObject(recordTXT) + recordTXT.Ref = ref + return recordTXT, err +} + +func (objMgr *ObjectManager) GetTXTRecordByRef(ref string) (*RecordTXT, error) { + recordTXT := NewRecordTXT(RecordTXT{}) + err := objMgr.connector.GetObject(recordTXT, ref, &recordTXT) + return recordTXT, err +} + +func (objMgr *ObjectManager) GetTXTRecord(name string) (*RecordTXT, error) { + if name == "" { + return nil, fmt.Errorf("name can not be empty") + } + var res []RecordTXT + + recordTXT := NewRecordTXT(RecordTXT{Name: name}) + + err := objMgr.connector.GetObject(recordTXT, "", &res) + + if err != nil || res == nil || len(res) == 0 { + return nil, err + } + + return &res[0], nil +} + +func (objMgr *ObjectManager) UpdateTXTRecord(recordname string, text string) (*RecordTXT, error) { + var res []RecordTXT + + recordTXT := NewRecordTXT(RecordTXT{Name: recordname}) + + err := objMgr.connector.GetObject(recordTXT, "", &res) + + if len(res) == 0 { + return nil, nil + } + + res[0].Text = text + res[0].Zone = "" // set the Zone value to "" as its a non writable field + + _, err = objMgr.connector.UpdateObject(&res[0], res[0].Ref) + + if err != nil || res == nil || len(res) == 0 { + return nil, err + } + + return &res[0], nil +} + +func (objMgr *ObjectManager) DeleteTXTRecord(ref string) (string, error) { + return objMgr.connector.DeleteObject(ref) +} + +func (objMgr *ObjectManager) CreatePTRRecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordPTR, error) { + + eas := objMgr.extendEA(ea) recordPTR := NewRecordPTR(RecordPTR{ View: dnsview, @@ -677,3 +800,103 @@ func (objMgr *ObjectManager) GetGridInfo() ([]Grid, error) { err := objMgr.connector.GetObject(gridObj, "", &res) return res, err } + +// CreateZoneAuth creates zones and subs by passing fqdn +func (objMgr *ObjectManager) CreateZoneAuth(fqdn string, ea EA) (*ZoneAuth, error) { + + eas := objMgr.extendEA(ea) + + zoneAuth := NewZoneAuth(ZoneAuth{ + Fqdn: fqdn, + Ea: eas}) + + + ref, err := objMgr.connector.CreateObject(zoneAuth) + zoneAuth.Ref = ref + return zoneAuth, err +} + +// Retreive a authortative zone by ref +func (objMgr *ObjectManager) GetZoneAuthByRef(ref string) (*ZoneAuth, error) { + zoneAuth := NewZoneAuth(ZoneAuth{}) + err := objMgr.connector.GetObject(zoneAuth, ref, &zoneAuth) + return zoneAuth, err +} + +func (objMgr *ObjectManager) GetZoneAuthByFQDN(fqdn string) (*ZoneAuth, error) { + if fqdn == "" { + return nil, fmt.Errorf("name can not be empty") + } + var res []ZoneAuth + + zoneAuth := NewZoneAuth(ZoneAuth{Fqdn: fqdn}) + + err := objMgr.connector.GetObject(zoneAuth, "", &res) + + if err != nil || res == nil || len(res) == 0 { + return nil, err + } + + return &res[0], nil +} + +// DeleteZoneAuth deletes an auth zone +func (objMgr *ObjectManager) DeleteZoneAuth(ref string) (string, error) { + return objMgr.connector.DeleteObject(ref) +} + +// GetZoneAuth returns the authoritatives zones +func (objMgr *ObjectManager) GetZoneAuth() ([]ZoneAuth, error) { + var res []ZoneAuth + + zoneAuth := NewZoneAuth(ZoneAuth{}) + err := objMgr.connector.GetObject(zoneAuth, "", &res) + + return res, err +} + +// GetZoneDelegated returns the delegated zone +func (objMgr *ObjectManager) GetZoneDelegated(fqdn string) (*ZoneDelegated, error) { + if len(fqdn) == 0 { + return nil, nil + } + var res []ZoneDelegated + + zoneDelegated := NewZoneDelegated(ZoneDelegated{Fqdn: fqdn}) + + err := objMgr.connector.GetObject(zoneDelegated, "", &res) + + if err != nil || res == nil || len(res) == 0 { + return nil, err + } + + return &res[0], nil +} + +// CreateZoneDelegated creates delegated zone +func (objMgr *ObjectManager) CreateZoneDelegated(fqdn string, delegate_to []NameServer) (*ZoneDelegated, error) { + zoneDelegated := NewZoneDelegated(ZoneDelegated{ + Fqdn: fqdn, + DelegateTo: delegate_to}) + + ref, err := objMgr.connector.CreateObject(zoneDelegated) + zoneDelegated.Ref = ref + + return zoneDelegated, err +} + +// UpdateZoneDelegated updates delegated zone +func (objMgr *ObjectManager) UpdateZoneDelegated(ref string, delegate_to []NameServer) (*ZoneDelegated, error) { + zoneDelegated := NewZoneDelegated(ZoneDelegated{ + Ref: ref, + DelegateTo: delegate_to}) + + refResp, err := objMgr.connector.UpdateObject(zoneDelegated, ref) + zoneDelegated.Ref = refResp + return zoneDelegated, err +} + +// DeleteZoneDelegated deletes delegated zone +func (objMgr *ObjectManager) DeleteZoneDelegated(ref string) (string, error) { + return objMgr.connector.DeleteObject(ref) +} diff --git a/vendor/github.com/infobloxopen/infoblox-go-client/objects.go b/vendor/github.com/infobloxopen/infoblox-go-client/objects.go index 297ad1b..c7de3e1 100644 --- a/vendor/github.com/infobloxopen/infoblox-go-client/objects.go +++ b/vendor/github.com/infobloxopen/infoblox-go-client/objects.go @@ -3,6 +3,7 @@ package ibclient import ( "bytes" "encoding/json" + "fmt" "reflect" ) @@ -350,6 +351,26 @@ func NewUserProfile(userprofile UserProfile) *UserProfile { return &res } +type RecordSRV struct { + IBBase `json:"-"` + Ref string `json:"_ref,omitempty"` + Name string `json:"name,omitempty"` + Port uint `json:"port,omitempty"` + Priority uint `json:"priority,omitempty"` + Target string `json:"target,omitempty"` + View string `json:"view,omitempty"` + Weight uint `json:"weight,omitempty"` + Ea EA `json:"extattrs,omitempty"` +} + +func NewRecordSRV(ra RecordSRV) *RecordSRV { + res := ra + res.objectType = "record:srv" + res.returnFields = []string{"extattrs", "name", "port", "priority", "target", "view", "weight"} + + return &res +} + type RecordA struct { IBBase `json:"-"` Ref string `json:"_ref,omitempty"` @@ -446,6 +467,7 @@ type RecordTXT struct { Ref string `json:"_ref,omitempty"` Name string `json:"name,omitempty"` Text string `json:"text,omitempty"` + TTL int `json:"ttl,omitempty"` View string `json:"view,omitempty"` Zone string `json:"zone,omitempty"` Ea EA `json:"extattrs,omitempty"` @@ -475,6 +497,28 @@ func NewZoneAuth(za ZoneAuth) *ZoneAuth { return &res } +type NameServer struct { + Address string `json:"address,omitempty"` + Name string `json:"name,omitempty"` +} + +type ZoneDelegated struct { + IBBase `json:"-"` + Ref string `json:"_ref,omitempty"` + Fqdn string `json:"fqdn,omitempty"` + DelegateTo []NameServer `json:"delegate_to,omitempty"` + View string `json:"view,omitempty"` + Ea EA `json:"extattrs,omitempty"` +} + +func NewZoneDelegated(za ZoneDelegated) *ZoneDelegated { + res := za + res.objectType = "zone_delegated" + res.returnFields = []string{"extattrs", "fqdn", "view", "delegate_to"} + + return &res +} + func (ea EA) MarshalJSON() ([]byte, error) { m := make(map[string]interface{}) for k, v := range ea { @@ -523,14 +567,26 @@ func (ea *EA) UnmarshalJSON(b []byte) (err error) { *ea = make(EA) for k, v := range m { val := v["value"] - if reflect.TypeOf(val).String() == "json.Number" { + switch valType := reflect.TypeOf(val).String(); valType { + case "json.Number": var i64 int64 i64, err = val.(json.Number).Int64() val = int(i64) - } else if val.(string) == "True" { - val = Bool(true) - } else if val.(string) == "False" { - val = Bool(false) + case "string": + if val.(string) == "True" { + val = Bool(true) + } else if val.(string) == "False" { + val = Bool(false) + } + case "[]interface {}": + nval := val.([]interface{}) + nVals := make([]string, len(nval)) + for i, v := range nval { + nVals[i] = fmt.Sprintf("%v", v) + } + val = nVals + default: + val = fmt.Sprintf("%v", val) } (*ea)[k] = val From b672bd1fc035b48fc3c0149027a2cbe9401a4884 Mon Sep 17 00:00:00 2001 From: Darren Oakley Date: Fri, 4 Dec 2020 15:43:36 +0000 Subject: [PATCH 7/7] Prevent the deletion of domains with sub-domains This stops the accidental deletion of sub-domains within Infoblox when the parent domain is deleted. In order for a parent domain to be deleted now, the sub-domains will need to be removed first. This can be protected against with the `depends_on` directive within the users terraform code (sub-domains referencing parent domains) but this is easily forgotten. --- infoblox/resource_infoblox_zone_auth.go | 24 ++++- infoblox/resource_infoblox_zone_auth_test.go | 97 ++++++++++++++++---- 2 files changed, 99 insertions(+), 22 deletions(-) diff --git a/infoblox/resource_infoblox_zone_auth.go b/infoblox/resource_infoblox_zone_auth.go index 0e710ec..6788140 100755 --- a/infoblox/resource_infoblox_zone_auth.go +++ b/infoblox/resource_infoblox_zone_auth.go @@ -3,6 +3,7 @@ package infoblox import ( "fmt" "log" + "strings" "github.com/hashicorp/terraform/helper/schema" ibclient "github.com/infobloxopen/infoblox-go-client" @@ -79,14 +80,13 @@ func resourceZoneAuthGet(d *schema.ResourceData, m interface{}) error { return fmt.Errorf("Getting auth zone failed from dns view (%s) : %s", fqdn, err) } d.SetId(obj.Ref) + log.Printf("[DEBUG] %s: Completed reading required auth zone ", resourceZoneAuthIDString(d)) return nil } func resourceZoneAuthUpdate(d *schema.ResourceData, m interface{}) error { - return fmt.Errorf("Updating an auth zone is not supported") - } func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { @@ -99,7 +99,16 @@ func resourceZoneAuthDelete(d *schema.ResourceData, m interface{}) error { objMgr := ibclient.NewObjectManager(connector, "Terraform", tenantID) - _, err := objMgr.DeleteZoneAuth(d.Id()) + zaList, err := objMgr.GetZoneAuth() + if err != nil { + return fmt.Errorf("Getting a list of all current AuthZones failed") + } + + if hasSubdomain(ibclient.ZoneAuth{Fqdn: fqdn}, zaList) { + return fmt.Errorf("Cannot delete an AuthZone that has a sub-domain: %s", fqdn) + } + + _, err = objMgr.DeleteZoneAuth(d.Id()) if err != nil { return fmt.Errorf("Deletion of auth zone failed from dns view(%s) : %s", fqdn, err) } @@ -120,3 +129,12 @@ func resourceZoneAuthIDString(d resourceZoneAuthIDStringInterface) string { } return fmt.Sprintf("infoblox_auth_zone (ID = %s)", id) } + +func hasSubdomain(target ibclient.ZoneAuth, list []ibclient.ZoneAuth) bool { + for _, za := range list { + if za.Fqdn != target.Fqdn && strings.Contains(za.Fqdn, target.Fqdn) { + return true + } + } + return false +} diff --git a/infoblox/resource_infoblox_zone_auth_test.go b/infoblox/resource_infoblox_zone_auth_test.go index 501e367..27a1d45 100755 --- a/infoblox/resource_infoblox_zone_auth_test.go +++ b/infoblox/resource_infoblox_zone_auth_test.go @@ -2,6 +2,7 @@ package infoblox import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -10,28 +11,82 @@ import ( ) func TestAccResourceZoneAuth(t *testing.T) { - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckZoneAuthDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccresourceZoneAuthCreate, + Config: testStep1CreateSingleZone, + Check: resource.ComposeTestCheckFunc( + testAccZoneAuthExists(t, "infoblox_zone_auth.acctest", "aaa.com", "default", "test"), + ), + }, + resource.TestStep{ + Config: testStep2CreateASubDomain, + Check: resource.ComposeTestCheckFunc( + testAccZoneAuthExists(t, "infoblox_zone_auth.acctest", "aaa.com", "default", "test"), + testAccZoneAuthExists(t, "infoblox_zone_auth.sub_acctest", "sub.aaa.com", "default", "test"), + ), + }, + // We expect this step to fail as you can't delete a domain with sub-domains + resource.TestStep{ + Config: testStep3DeleteParentZone, + ExpectError: regexp.MustCompile("Cannot delete an AuthZone that has a sub-domain"), Check: resource.ComposeTestCheckFunc( - testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), + testAccZoneAuthExists(t, "infoblox_zone_auth.acctest", "aaa.com", "default", "test"), + testAccZoneAuthExists(t, "infoblox_zone_auth.sub_acctest", "sub.aaa.com", "default", "test"), ), }, + // This final step is to remove the sub-domain so that the state can be cleaned properly resource.TestStep{ - Config: testAccresourceZoneAuthUpdate, + Config: testStep4DeleteSubDomain, Check: resource.ComposeTestCheckFunc( - testAccZoneAuthExists(t, "infoblox_zone_auth.zone_auth", "aaa.com", "default", "test"), + testAccZoneAuthExists(t, "infoblox_zone_auth.acctest", "aaa.com", "default", "test"), ), }, }, }) } +var testStep1CreateSingleZone = fmt.Sprintf(` + resource "infoblox_zone_auth" "acctest" { + fqdn = "acctest.com" + dns_view="default" + tenant_id="test" + } +`) + +var testStep2CreateASubDomain = fmt.Sprintf(` + resource "infoblox_zone_auth" "acctest" { + fqdn = "acctest.com" + dns_view="default" + tenant_id="test" + } + + resource "infoblox_zone_auth" "sub_acctest" { + fqdn = "sub.acctest.com" + dns_view="default" + tenant_id="test" + } +`) + +var testStep3DeleteParentZone = fmt.Sprintf(` + resource "infoblox_zone_auth" "sub_acctest" { + fqdn = "sub.acctest.com" + dns_view="default" + tenant_id="test" + } +`) + +var testStep4DeleteSubDomain = fmt.Sprintf(` + resource "infoblox_zone_auth" "acctest" { + fqdn = "acctest.com" + dns_view="default" + tenant_id="test" + } +`) + func testAccCheckZoneAuthDestroy(s *terraform.State) error { meta := testAccProvider.Meta() @@ -45,10 +100,10 @@ func testAccCheckZoneAuthDestroy(s *terraform.State) error { if err != nil { return fmt.Errorf("Error:%s - record not found", err) } - } return nil } + func testAccZoneAuthExists(t *testing.T, n string, fqdn string, dns_view string, tenant_id string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -71,16 +126,20 @@ func testAccZoneAuthExists(t *testing.T, n string, fqdn string, dns_view string, } } -var testAccresourceZoneAuthCreate = fmt.Sprintf(` -resource "infoblox_zone_auth" "zone_auth"{ - fqdn = "acctest.com" - dns_view="default" - tenant_id="test" - }`) - -var testAccresourceZoneAuthUpdate = fmt.Sprintf(` -resource "infoblox_zone_auth" "zone_auth"{ - fqdn = "acctest.com" - dns_view="default" - tenant_id="test" - }`) +func TestHasSubdomain(t *testing.T) { + main := ibclient.ZoneAuth{Fqdn: "aaa.com"} + subdomain := ibclient.ZoneAuth{Fqdn: "test.aaa.com"} + other := ibclient.ZoneAuth{Fqdn: "foo.com"} + + list := []ibclient.ZoneAuth{main, subdomain, other} + + if hasSubdomain(main, list) == false { + fmt.Printf("'%s' has not been identified as having a subdomain", main.Fqdn) + t.Fail() + } + + if hasSubdomain(other, list) == true { + fmt.Printf("'%s' has been identified incorrectly as having a subdomain", other.Fqdn) + t.Fail() + } +}