Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add correct importer for ip pool allocation resource #319

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_ip_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 13 additions & 1 deletion nsxt/resource_nsxt_ip_pool_allocation_ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/vmware/go-vmware-nsxt/manager"
"log"
"net/http"
"strings"
)

func resourceNsxtIPPoolAllocationIPAddress() *schema.Resource {
Expand All @@ -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{
Expand Down Expand Up @@ -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 <pool-id>/<ip-address-id> as an input")
}
d.SetId(s[1])
d.Set("ip_pool_id", s[0])
return []*schema.ResourceData{d}, nil
}
93 changes: 63 additions & 30 deletions nsxt/resource_nsxt_ip_pool_allocation_ip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -124,13 +141,29 @@ 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"
}

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
}
12 changes: 12 additions & 0 deletions website/docs/r/ip_pool_allocation_ip_address.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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`.