Skip to content

Commit

Permalink
Add IP allocation import support
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jul 30, 2023
1 parent ca20b09 commit 3284736
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions nsxt/resource_nsxt_policy_ip_address_allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nsxt

import (
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -227,6 +228,14 @@ func resourceNsxtPolicyIPAddressAllocationDelete(d *schema.ResourceData, m inter
func resourceNsxtPolicyIPAddressAllocationImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
importID := d.Id()
s := strings.Split(importID, "/")
rd, err := nsxtPolicyPathResourceImporterHelper(d, m)
if err == nil {
d.Set("pool_path", importID[0:strings.Index(importID, "/ip-allocations/")])
return rd, nil
} else if !errors.Is(err, ErrNotAPolicyPath) {
return rd, err
}

if len(s) != 2 {
return nil, fmt.Errorf("Please provide <ip-pool-id>/<allocation-id> as an input")
}
Expand Down
26 changes: 26 additions & 0 deletions nsxt/resource_nsxt_policy_ip_address_allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ func TestAccResourceNsxtPolicyIPAddressAllocation_importBasic(t *testing.T) {
})
}

func TestAccResourceNsxtPolicyIPAddressAllocation_importBasic_multitenancy(t *testing.T) {
name := accTestPolicyIPAddressAllocationUpdateAttributes["display_name"]
testResourceName := "nsxt_policy_ip_address_allocation.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccOnlyMultitenancy(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyIPAddressAllocationCheckDestroy(state, name)
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyIPAddressAllocationTemplate(true, true),
},
{
ResourceName: testResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccResourceNsxtPolicyImportIDRetriever(testResourceName),
},
{
Config: testAccNsxtPolicyIPAddressAllocationDependenciesTemplate(true),
},
},
})
}
func testAccNSXPolicyIPAddressAllocationImporterGetID(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources["nsxt_policy_ip_address_allocation.test"]
if !ok {
Expand Down
7 changes: 6 additions & 1 deletion website/docs/r/policy_ip_address_allocation.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,10 @@ An existing IP Allocation can be [imported][docs-import] into this resource, via
```
terraform import nsxt_policy_ip_address_allocation.test POOL-ID/ID
```

The above command imports IpAddressAllocation named `test` with the NSX IpAddressAllocation ID `ID` in IP Pool `POOL-ID`.

```
terraform import nsxt_policy_ip_address_allocation.test POLICY_PATH
```
The above command imports IpAddressAllocation named `test` with the NSX policy path `POLICY_PATH`.
Note: for multitenancy projects only the later form is usable.

0 comments on commit 3284736

Please sign in to comment.