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 resource nsxt_ip_pool_allocation_ip_address #190

Merged
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
87 changes: 87 additions & 0 deletions nsxt/data_source_nsxt_ip_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
api "github.com/vmware/go-vmware-nsxt"
"github.com/vmware/go-vmware-nsxt/manager"
"net/http"
)

func dataSourceNsxtIPPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtIPPoolRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique ID of this resource",
Optional: true,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of this resource",
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of this resource",
Optional: true,
Computed: true,
},
},
}
}

func dataSourceNsxtIPPoolRead(d *schema.ResourceData, m interface{}) error {
// Read IP Pool by name or id
nsxClient := m.(*api.APIClient)
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj manager.IpPool
if objID != "" {
// Get by id
objGet, resp, err := nsxClient.PoolManagementApi.ReadIpPool(nsxClient.Context, objID)

if resp != nil && resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("IP pool %s was not found", objID)
}
if err != nil {
MartinWeindel marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Error while reading ns service %s: %v", objID, err)
}
obj = objGet
} else if objName != "" {
// Get by full name
objList, _, err := nsxClient.PoolManagementApi.ListIpPools(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading IP pool: %v", err)
}
// go over the list to find the correct one
found := false
for _, objInList := range objList.Results {
if objInList.DisplayName == objName {
if found {
return fmt.Errorf("Found multiple IP pool with name '%s'", objName)
}
obj = objInList
found = true
}
}
if !found {
return fmt.Errorf("IP pool '%s' was not found out of %d services", objName, len(objList.Results))
}
} else {
return fmt.Errorf("Error obtaining IP pool ID or name during read")
}

d.SetId(obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)

return nil
}
39 changes: 39 additions & 0 deletions nsxt/data_source_nsxt_ip_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccDataSourceNsxtIPPool_basic(t *testing.T) {
ipPoolName := getIPPoolName()
if ipPoolName == "" {
t.Skipf("No NSXT_TEST_IP_POOL set - skipping test")
}
testResourceName := "data.nsxt_ip_pool.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNSXIPPoolReadTemplate(ipPoolName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", ipPoolName),
resource.TestCheckResourceAttrSet(testResourceName, "id"),
),
},
},
})
}

func testAccNSXIPPoolReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_ip_pool" "test" {
display_name = "%s"
}`, name)
}
2 changes: 2 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Provider() terraform.ResourceProvider {
"nsxt_ns_service": dataSourceNsxtNsService(),
"nsxt_edge_cluster": dataSourceNsxtEdgeCluster(),
"nsxt_certificate": dataSourceNsxtCertificate(),
"nsxt_ip_pool": dataSourceNsxtIPPool(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -140,6 +141,7 @@ func Provider() terraform.ResourceProvider {
"nsxt_ip_block": resourceNsxtIPBlock(),
"nsxt_ip_block_subnet": resourceNsxtIPBlockSubnet(),
"nsxt_ip_pool": resourceNsxtIPPool(),
"nsxt_ip_pool_allocation_ip_address": resourceNsxtIPPoolAllocationIPAddress(),
"nsxt_ip_set": resourceNsxtIPSet(),
"nsxt_static_route": resourceNsxtStaticRoute(),
"nsxt_vm_tags": resourceNsxtVMTags(),
Expand Down
128 changes: 128 additions & 0 deletions nsxt/resource_nsxt_ip_pool_allocation_ip_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* Copyright © 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
api "github.com/vmware/go-vmware-nsxt"
"github.com/vmware/go-vmware-nsxt/manager"
"log"
"net/http"
)

func resourceNsxtIPPoolAllocationIPAddress() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtIPPoolAllocationIPAddressCreate,
Read: resourceNsxtIPPoolAllocationIPAddressRead,
Update: resourceNsxtIPPoolAllocationIPAddressUpdate,
Delete: resourceNsxtIPPoolAllocationIPAddressDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"ip_pool_id": {
Type: schema.TypeString,
Description: "ID of IP pool that allocation belongs to",
Required: true,
},
"allocation_id": {
Type: schema.TypeString,
Description: "IP Address that is allocated from the pool",
Optional: true,
Computed: true,
},
},
}
}

func resourceNsxtIPPoolAllocationIPAddressCreate(d *schema.ResourceData, m interface{}) error {
nsxClient := m.(*api.APIClient)
poolID := d.Get("ip_pool_id").(string)
allocationID := d.Get("allocation_id").(string)
allocationIPAddress := manager.AllocationIpAddress{
AllocationId: allocationID,
}

allocationIPAddress, resp, err := nsxClient.PoolManagementApi.AllocateOrReleaseFromIpPool(nsxClient.Context, poolID, allocationIPAddress, "ALLOCATE")

if err != nil {
return fmt.Errorf("Error during IPPoolAllocationIPAddress create: %v", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during IPPoolAllocationIPAddress create: %v", resp.StatusCode)
}
d.SetId(allocationIPAddress.AllocationId)

return resourceNsxtIPPoolAllocationIPAddressRead(d, m)
}

func resourceNsxtIPPoolAllocationIPAddressRead(d *schema.ResourceData, m interface{}) error {
nsxClient := m.(*api.APIClient)
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining logical object id")
}
poolID := d.Get("ip_pool_id").(string)
if poolID == "" {
return fmt.Errorf("Error obtaining pool id")
}

resultList, resp, err := nsxClient.PoolManagementApi.ListIpPoolAllocations(nsxClient.Context, poolID)
if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Printf("[DEBUG] IP pool %s not found", poolID)
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("Error during IPPoolAllocationIPAddress read: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during IPPoolAllocationIPAddress read: %v", resp.StatusCode)
}

for _, address := range resultList.Results {
if address.AllocationId == id {
d.Set("ip_pool_id", poolID)
d.Set("allocation_id", address.AllocationId)
return nil
}
}

log.Printf("[DEBUG] IPPoolAllocationIPAddress list%s not found", id)
d.SetId("")
return nil
}

func resourceNsxtIPPoolAllocationIPAddressUpdate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Updating IPPoolAllocationIPAddress is not supported")
}

func resourceNsxtIPPoolAllocationIPAddressDelete(d *schema.ResourceData, m interface{}) error {
nsxClient := m.(*api.APIClient)
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining logical object id")
}
poolID := d.Get("ip_pool_id").(string)
if id == "" {
return fmt.Errorf("Error obtaining pool id")
}

allocationIPAddress := manager.AllocationIpAddress{
AllocationId: d.Id(),
}
_, resp, err := nsxClient.PoolManagementApi.AllocateOrReleaseFromIpPool(nsxClient.Context, poolID, allocationIPAddress, "RELEASE")
if resp != nil && resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error during IPPoolAllocationIPAddress delete: status=%s", resp.Status)
}
if err != nil {
return fmt.Errorf("Error during IPPoolAllocationIPAddress delete: %v", err)
}

return nil
}
Loading