Skip to content

Commit

Permalink
Add address group resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jan committed Dec 26, 2021
1 parent d0fb307 commit d3a78d4
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 10 deletions.
1 change: 1 addition & 0 deletions nutanix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func Provider() terraform.ResourceProvider {
"nutanix_protection_rule": resourceNutanixProtectionRule(),
"nutanix_recovery_plan": resourceNutanixRecoveryPlan(),
"nutanix_service_group": resourceNutanixServiceGroup(),
"nutanix_address_group": resourceNutanixAddressGroup(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
217 changes: 217 additions & 0 deletions nutanix/resource_nutanix_address_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package nutanix

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
v3 "github.com/terraform-providers/terraform-provider-nutanix/client/v3"
"github.com/terraform-providers/terraform-provider-nutanix/utils"
"log"
"strings"
)

func resourceNutanixAddressGroup() *schema.Resource {
return &schema.Resource{
Create: resourceNutanixAddressGroupCreate,
Read: resourceNutanixAddressGroupRead,
Delete: resourceNutanixAddressGroupDelete,
Update: resourceNutanixAddressGroupUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"ip_address_block_list": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
},
"prefix_length": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"address_group_string": {
Type: schema.TypeString,
Computed: true,
},
},
}

}

func resourceNutanixAddressGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
id := d.Id()
response, err := conn.V3.GetAddressGroup(id)

request := &v3.AddressGroupInput{}

if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
d.SetId("")
}
return fmt.Errorf("error retrieving for address group id (%s) :%+v", id, err)
}

group := response.AddressGroup

if d.HasChange("name") {
group.Name = utils.StringPtr(d.Get("name").(string))
}

if d.HasChange("description") {
group.Description = utils.StringPtr(d.Get("description").(string))
}

if d.HasChange("ip_address_block_list") {
blockList, err := expandAddressEntry(d)

if err != nil {
return err
}

group.BlockList = blockList
}

request.Name = group.Name
request.Description = group.Description
request.BlockList = group.BlockList

errUpdate := conn.V3.UpdateAddressGroup(d.Id(), request)
if errUpdate != nil {
return fmt.Errorf("error updating address group id %s): %s", d.Id(), errUpdate)
}

return resourceNutanixAddressGroupRead(d, meta)

}

func resourceNutanixAddressGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

log.Printf("[Debug] Destroying the address group with the ID %s", d.Id())

if err := conn.V3.DeleteAddressGroup(d.Id()); err != nil {
return err
}

d.SetId("")
return nil
}

func resourceNutanixAddressGroupRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading AddressGroup: %s", d.Get("name").(string))

// Get client connection
conn := meta.(*Client).API

// Make request to the API
resp, err := conn.V3.GetAddressGroup(d.Id())

if err != nil {
if strings.Contains(fmt.Sprint(err), "ENTITY_NOT_FOUND") {
d.SetId("")
return nil
}
return err
}

if err := d.Set("ip_address_block_list", flattenAddressEntry(resp.AddressGroup)); err != nil {
return err
}

d.Set("name", utils.StringValue(resp.AddressGroup.Name))

return d.Set("description", utils.StringValue(resp.AddressGroup.Description))
}

func flattenAddressEntry(group *v3.AddressGroupInput) []map[string]interface{} {
groupList := make([]map[string]interface{}, 0)
for _, v := range group.BlockList {
groupItem := make(map[string]interface{})
groupItem["ip"] = v.IPAddress
groupItem["prefix_length"] = v.PrefixLength
groupList = append(groupList, groupItem)
}

return groupList
}

func resourceNutanixAddressGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

request := &v3.AddressGroupInput{}
request.BlockList = make([]*v3.IPAddressBlock, 0)

// Read Arguments and set request values

if name, ok := d.GetOk("name"); ok {
request.Name = utils.StringPtr(name.(string))
}

if desc, ok := d.GetOk("description"); ok {
request.Description = utils.StringPtr(desc.(string))
}
addressList, err := expandAddressEntry(d)

if err != nil {
return err
}

request.BlockList = addressList

resp, err := conn.V3.CreateAddressGroup(request)

if err != nil {
return err
}

n := *resp.UUID

// set terraform state
d.SetId(n)

return resourceNutanixAddressGroupRead(d, meta)
}

func expandAddressEntry(d *schema.ResourceData) ([]*v3.IPAddressBlock, error) {
if groups, ok := d.GetOk("ip_address_block_list"); ok {
set := groups.([]interface{})
outbound := make([]*v3.IPAddressBlock, len(set))

for k, v := range set {
entry := v.(map[string]interface{})

block := &v3.IPAddressBlock{}
if ip, ipok := entry["ip"]; ipok {
block.IPAddress = utils.StringPtr(ip.(string))
} else {
return nil, fmt.Errorf("error updating address group id %s): ip missing", d.Id())
}

if length, lengthok := entry["prefix_length"]; lengthok {
block.PrefixLength = utils.Int64Ptr(int64(length.(int)))
} else {
return nil, fmt.Errorf("error updating address group id %s): prefix_length missing", d.Id())
}

outbound[k] = block
}
return outbound, nil
}

return nil, nil
}
20 changes: 10 additions & 10 deletions nutanix/resource_nutanix_service_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

func resourceNutanixServiceGroup() *schema.Resource {
return &schema.Resource{
Create: resourceNutanixServicGroupCreate,
Read: resourceNutanixServicGroupRead,
Delete: resourceNutanixServicGroupDelete,
Update: resourceNutanixServicGroupUpdate,
Create: resourceNutanixServiceGroupCreate,
Read: resourceNutanixServiceGroupRead,
Delete: resourceNutanixServiceGroupDelete,
Update: resourceNutanixServiceGroupUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -111,7 +111,7 @@ func IsValidProtocol(category string) bool {
return false
}

func resourceNutanixServicGroupUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceNutanixServiceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API
id := d.Id()
response, err := conn.V3.GetServiceGroup(id)
Expand Down Expand Up @@ -159,7 +159,7 @@ func resourceNutanixServicGroupUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error updating service group id %s): %s", d.Id(), errUpdate)
}

return resourceNutanixServicGroupRead(d, meta)
return resourceNutanixServiceGroupRead(d, meta)
}

func flattenServiceEntry(group *v3.ServiceGroupInput) []map[string]interface{} {
Expand Down Expand Up @@ -210,7 +210,7 @@ func flattenServiceEntry(group *v3.ServiceGroupInput) []map[string]interface{} {
return groupList
}

func resourceNutanixServicGroupRead(d *schema.ResourceData, meta interface{}) error {
func resourceNutanixServiceGroupRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading ServiceGroup: %s", d.Get("name").(string))

// Get client connection
Expand Down Expand Up @@ -274,7 +274,7 @@ func expandServiceEntry(d *schema.ResourceData) ([]*v3.ServiceListEntry, error)
return nil, nil
}

func resourceNutanixServicGroupCreate(d *schema.ResourceData, meta interface{}) error {
func resourceNutanixServiceGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

request := &v3.ServiceGroupInput{}
Expand Down Expand Up @@ -321,10 +321,10 @@ func resourceNutanixServicGroupCreate(d *schema.ResourceData, meta interface{})
// set terraform state
d.SetId(n)

return resourceNutanixServicGroupRead(d, meta)
return resourceNutanixServiceGroupRead(d, meta)
}

func resourceNutanixServicGroupDelete(d *schema.ResourceData, meta interface{}) error {
func resourceNutanixServiceGroupDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Client).API

log.Printf("[Debug] Destroying the service group with the ID %s", d.Id())
Expand Down

0 comments on commit d3a78d4

Please sign in to comment.