From 5dda684f994259cd409771e810e0de979bf1bcf1 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Wed, 20 May 2020 17:51:44 -0700 Subject: [PATCH 1/2] Add correct importer for ip pool allocation resource --- nsxt/data_source_nsxt_ip_pool.go | 2 +- ...urce_nsxt_ip_pool_allocation_ip_address.go | 14 ++- ...nsxt_ip_pool_allocation_ip_address_test.go | 93 +++++++++++++------ 3 files changed, 77 insertions(+), 32 deletions(-) diff --git a/nsxt/data_source_nsxt_ip_pool.go b/nsxt/data_source_nsxt_ip_pool.go index 005cff8cd..b72053f57 100644 --- a/nsxt/data_source_nsxt_ip_pool.go +++ b/nsxt/data_source_nsxt_ip_pool.go @@ -75,7 +75,7 @@ func dataSourceNsxtIPPoolRead(d *schema.ResourceData, m interface{}) error { } } if !found { - return fmt.Errorf("IP pool '%s' was not found out of %d services", objName, len(objList.Results)) + return fmt.Errorf("IP pool '%s' was not found out of %d objects", objName, len(objList.Results)) } } else { return fmt.Errorf("Error obtaining IP pool ID or name during read") diff --git a/nsxt/resource_nsxt_ip_pool_allocation_ip_address.go b/nsxt/resource_nsxt_ip_pool_allocation_ip_address.go index d56082a51..0ac9ec406 100644 --- a/nsxt/resource_nsxt_ip_pool_allocation_ip_address.go +++ b/nsxt/resource_nsxt_ip_pool_allocation_ip_address.go @@ -9,6 +9,7 @@ import ( "github.com/vmware/go-vmware-nsxt/manager" "log" "net/http" + "strings" ) func resourceNsxtIPPoolAllocationIPAddress() *schema.Resource { @@ -17,7 +18,7 @@ func resourceNsxtIPPoolAllocationIPAddress() *schema.Resource { Read: resourceNsxtIPPoolAllocationIPAddressRead, Delete: resourceNsxtIPPoolAllocationIPAddressDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceNsxtIPPoolAllocationIPAddressImport, }, Schema: map[string]*schema.Schema{ @@ -132,3 +133,14 @@ func resourceNsxtIPPoolAllocationIPAddressDelete(d *schema.ResourceData, m inter return nil } + +func resourceNsxtIPPoolAllocationIPAddressImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { + importID := d.Id() + s := strings.Split(importID, "/") + if len(s) != 2 { + return nil, fmt.Errorf("Please provide / as an input") + } + d.SetId(s[1]) + d.Set("ip_pool_id", s[0]) + return []*schema.ResourceData{d}, nil +} diff --git a/nsxt/resource_nsxt_ip_pool_allocation_ip_address_test.go b/nsxt/resource_nsxt_ip_pool_allocation_ip_address_test.go index 074720285..0a96975ac 100644 --- a/nsxt/resource_nsxt_ip_pool_allocation_ip_address_test.go +++ b/nsxt/resource_nsxt_ip_pool_allocation_ip_address_test.go @@ -17,51 +17,68 @@ const ( waitSeconds = 150 ) -func TestAccResourceNsxtIPPoolAllocationIPAddress_basic(t *testing.T) { - poolName := getIPPoolName() - if poolName == "" { - t.Skipf("No NSXT_TEST_IP_POOL set - skipping test") - } - - poolResourceName := "data.nsxt_ip_pool.acceptance_test" - resourceName := "nsxt_ip_pool_allocation_ip_address.test" +var testAccIPAllocationName = "nsxt_ip_pool_allocation_ip_address.test" +var testAccIPPoolName = "data.nsxt_ip_pool.acceptance_test" +func TestAccResourceNsxtIPPoolAllocationIPAddress_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, + PreCheck: func() { testAccPreCheck(t); testAccEnvDefined(t, "NSXT_TEST_IP_POOL") }, Providers: testAccProviders, CheckDestroy: func(state *terraform.State) error { - return testAccNSXIPPoolAllocationIPAddressCheckDestroy(state, poolResourceName, resourceName) + return testAccNSXIPPoolAllocationIPAddressCheckDestroy(state) }, Steps: []resource.TestStep{ { - Config: testAccNSXIPPoolAllocationIPAddressCreateTemplate(poolName), + Config: testAccNSXIPPoolAllocationIPAddressCreateTemplate(), Check: resource.ComposeTestCheckFunc( - testAccNSXIPPoolAllocationIPAddressExists(poolResourceName, resourceName), - resource.TestCheckResourceAttrSet(resourceName, "ip_pool_id"), - resource.TestCheckResourceAttrSet(resourceName, "allocation_id"), + testAccNSXIPPoolAllocationIPAddressExists(), + resource.TestCheckResourceAttrSet(testAccIPAllocationName, "ip_pool_id"), + resource.TestCheckResourceAttrSet(testAccIPAllocationName, "allocation_id"), ), }, }, }) } -func testAccNSXIPPoolAllocationIPAddressExists(poolResourceName string, resourceName string) resource.TestCheckFunc { +func TestAccResourceNsxtIPPoolAllocationIPAddress_import(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccEnvDefined(t, "NSXT_TEST_IP_POOL") }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNSXIPPoolAllocationIPAddressCheckDestroy(state) + }, + Steps: []resource.TestStep{ + { + Config: testAccNSXIPPoolAllocationIPAddressCreateTemplate(), + }, + { + ResourceName: testAccIPAllocationName, + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: testAccNSXIPPoolAllocationIPAddressImporterGetID, + }, + }, + }) +} + +func testAccNSXIPPoolAllocationIPAddressExists() resource.TestCheckFunc { return func(state *terraform.State) error { - exists, err := checkAllocationIPAddressExists(state, poolResourceName, resourceName) + exists, err := checkAllocationIPAddressExists(state) if err != nil { return err } if !*exists { - return fmt.Errorf("Allocation %s in IP Pool %s is not existing", resourceName, poolResourceName) + return fmt.Errorf("Allocation %s in IP Pool %s does not exist", testAccIPAllocationName, testAccIPPoolName) } return nil } } -func getIPPoolIDByResourceName(state *terraform.State, poolResourceName string) (string, error) { - rsPool, ok := state.RootModule().Resources[poolResourceName] +func getIPPoolIDByResourceName(state *terraform.State) (string, error) { + rsPool, ok := state.RootModule().Resources[testAccIPPoolName] if !ok { - return "", fmt.Errorf("IP Pool resource %s not found in resources", getIPPoolName()) + return "", fmt.Errorf("IP Pool resource %s not found in resources", testAccIPPoolName) } poolID := rsPool.Primary.ID @@ -71,15 +88,16 @@ func getIPPoolIDByResourceName(state *terraform.State, poolResourceName string) return poolID, nil } -func checkAllocationIPAddressExists(state *terraform.State, poolResourceName string, resourceName string) (*bool, error) { - poolID, err := getIPPoolIDByResourceName(state, poolResourceName) +func checkAllocationIPAddressExists(state *terraform.State) (*bool, error) { + exists := false + poolID, err := getIPPoolIDByResourceName(state) if err != nil { - return nil, err + return &exists, nil } - rs, ok := state.RootModule().Resources[resourceName] + rs, ok := state.RootModule().Resources[testAccIPAllocationName] if !ok { - return nil, fmt.Errorf("IP Pool allocation_ip_address resource %s not found in resources", resourceName) + return nil, fmt.Errorf("IP Pool allocation_ip_address resource %s not found in resources", testAccIPAllocationName) } nsxClient := testAccProvider.Meta().(nsxtClients).NsxtClient @@ -95,7 +113,6 @@ func checkAllocationIPAddressExists(state *terraform.State, poolResourceName str return nil, fmt.Errorf("Error while checking if allocations in IP Pool %s exists. HTTP return code was %d", poolID, responseCode.StatusCode) } - exists := false for _, allocationIPAddress := range listResult.Results { if allocationIPAddress.AllocationId == rs.Primary.ID { exists = true @@ -106,13 +123,13 @@ func checkAllocationIPAddressExists(state *terraform.State, poolResourceName str return &exists, nil } -func testAccNSXIPPoolAllocationIPAddressCheckDestroy(state *terraform.State, poolResourceName, resourceName string) error { +func testAccNSXIPPoolAllocationIPAddressCheckDestroy(state *terraform.State) error { // PoolManagementApi.ListIpPoolAllocations() call does not return updated list within two minutes. Need to wait... fmt.Printf("testAccNSXIPPoolAllocationIPAddressCheckDestroy: waiting up to %d seconds\n", waitSeconds) timeout := time.Now().Add(waitSeconds * time.Second) for time.Now().Before(timeout) { - exists, err := checkAllocationIPAddressExists(state, poolResourceName, resourceName) + exists, err := checkAllocationIPAddressExists(state) if err != nil { return err } @@ -124,7 +141,7 @@ func testAccNSXIPPoolAllocationIPAddressCheckDestroy(state *terraform.State, poo return fmt.Errorf("Timeout on check destroy IP address allocation") } -func testAccNSXIPPoolAllocationIPAddressCreateTemplate(poolName string) string { +func testAccNSXIPPoolAllocationIPAddressCreateTemplate() string { return fmt.Sprintf(` data "nsxt_ip_pool" "acceptance_test" { display_name = "%s" @@ -132,5 +149,21 @@ data "nsxt_ip_pool" "acceptance_test" { resource "nsxt_ip_pool_allocation_ip_address" "test" { ip_pool_id = "${data.nsxt_ip_pool.acceptance_test.id}" -}`, poolName) +}`, getIPPoolName()) +} + +func testAccNSXIPPoolAllocationIPAddressImporterGetID(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[testAccIPAllocationName] + if !ok { + return "", fmt.Errorf("NSX IP allocation resource %s not found in resources", testAccIPAllocationName) + } + resourceID := rs.Primary.ID + if resourceID == "" { + return "", fmt.Errorf("NSX IP allocation resource ID not set in resources ") + } + poolID := rs.Primary.Attributes["ip_pool_id"] + if poolID == "" { + return "", fmt.Errorf("NSX IP Pool ID not set in resources ") + } + return fmt.Sprintf("%s/%s", poolID, resourceID), nil } From 31231072a7daf7addbd5653a505c8053b71a8610 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Wed, 20 May 2020 20:36:31 -0700 Subject: [PATCH 2/2] Update IP pool allocation doc with import command --- .../r/ip_pool_allocation_ip_address.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/docs/r/ip_pool_allocation_ip_address.html.markdown b/website/docs/r/ip_pool_allocation_ip_address.html.markdown index ba917b5a9..603b09609 100644 --- a/website/docs/r/ip_pool_allocation_ip_address.html.markdown +++ b/website/docs/r/ip_pool_allocation_ip_address.html.markdown @@ -34,3 +34,15 @@ In addition to arguments listed above, the following attributes are exported: * `id` - ID of the IP pool allocation IP address (currently identical to `allocation_ip`). * `allocation_ip` - Allocation IP address. + +## Importing + +An existing IP pool allocation address can be [imported][docs-import] into this resource, via the following command: + +[docs-import]: /docs/import/index.html + +``` +terraform import nsxt_ip_pool_allocation_ip_address.ip1 POOL-UUID/UUID +``` + +The above would import the IP pool allocation address named `ip_pool` with the nsx ID `UUID`, from IP Pool with nsx ID `POOL-UUID`.