Skip to content

Commit

Permalink
ipset_nsip6_binding resource support added
Browse files Browse the repository at this point in the history
Signed-off-by: Sumanth Lingappa <[email protected]>
  • Loading branch information
sumanth-lingappa committed Apr 14, 2020
1 parent a4c375e commit 65b08e9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
94 changes: 94 additions & 0 deletions citrixadc/resource_citrixadc_ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func resourceCitrixAdcIpset() *schema.Resource {
ForceNew: true, // to avoid this error: https://github.com/hashicorp/terraform/blob/master/helper/schema/resource.go#L635
Elem: &schema.Schema{Type: schema.TypeString},
},
"nsip6binding": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: true, // to avoid this error: https://github.com/hashicorp/terraform/blob/master/helper/schema/resource.go#L635
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -59,6 +65,11 @@ func createIpsetFunc(d *schema.ResourceData, meta interface{}) error {
return err
}

err = updateIpsetNsip6Bindings(d, meta)
if err != nil {
return err
}

err = readIpsetFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this ipset but we can't read it ?? %s", ipsetName)
Expand All @@ -84,6 +95,11 @@ func readIpsetFunc(d *schema.ResourceData, meta interface{}) error {
return err
}

err = readIpsetNsip6Bindings(d, meta)
if err != nil {
return err
}

d.Set("name", data["name"])
d.Set("td", data["td"])

Expand Down Expand Up @@ -180,3 +196,81 @@ func readIpsetNsipBindings(d *schema.ResourceData, meta interface{}) error {
}
return nil
}

func deleteSingleIpsetNsip6Binding(d *schema.ResourceData, meta interface{}, nsip6 string) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteSingleIpsetNsip6Binding")
client := meta.(*NetScalerNitroClient).client

name := d.Get("name").(string)
args := make([]string, 0, 1)

s := fmt.Sprintf("ipaddress:%s", nsip6)
args = append(args, s)

log.Printf("args is %v", args)

if err := client.DeleteResourceWithArgs("ipset_nsip6_binding", name, args); err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error deleting nsip6 binding %v\n", nsip6)
return err
}

return nil
}

func addSingleIpsetNsip6Binding(d *schema.ResourceData, meta interface{}, nsip6 string) error {
log.Printf("[DEBUG] citrixadc-provider: In addSingleIpsetNsip6Binding")
client := meta.(*NetScalerNitroClient).client

bindingStruct := network.Ipsetnsip6binding{}
bindingStruct.Name = d.Get("name").(string)
bindingStruct.Ipaddress = nsip6 //strings.SplitN(nsip6, "/", -1)[0]

log.Printf("[DEBUG] bindingStruct: %v\n", bindingStruct)

// We need to do a HTTP PUT hence the UpdateResource
if _, err := client.UpdateResource("ipset_nsip6_binding", bindingStruct.Name, bindingStruct); err != nil {
return err
}
return nil
}

func updateIpsetNsip6Bindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateIpsetNsip6Bindings")
oldSet, newSet := d.GetChange("nsip6binding")
log.Printf("[DEBUG] citrixadc-provider: oldSet %v\n", oldSet)
log.Printf("[DEBUG] citrixadc-provider: newSet %v\n", newSet)
remove := oldSet.(*schema.Set).Difference(newSet.(*schema.Set))
add := newSet.(*schema.Set).Difference(oldSet.(*schema.Set))
for _, nsip6 := range remove.List() {
if err := deleteSingleIpsetNsip6Binding(d, meta, nsip6.(string)); err != nil {
return err
}
}

for _, nsip6 := range add.List() {
if err := addSingleIpsetNsip6Binding(d, meta, nsip6.(string)); err != nil {
return err
}
}
return nil
}

func readIpsetNsip6Bindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readIpsetNsip6Bindings")
client := meta.(*NetScalerNitroClient).client
name := d.Get("name").(string)
bindings, _ := client.FindResourceArray("ipset_nsip6_binding", name)
log.Printf("bindings %v\n", bindings)

processedBindings := make([]interface{}, len(bindings))
for i, val := range bindings {
processedBindings[i] = val["ipaddress"].(string)
}

updatedSet := processedBindings
log.Printf("updatedSet %v\n", updatedSet)
if err := d.Set("nsip6binding", updatedSet); err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions citrixadc/resource_citrixadc_ipset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

// TODO: add ipset_nsip6_binding testcase
const testAccIpset_no_bindings = `
resource "citrixadc_ipset" "foo" {
name = "tf_test_ipset"
Expand Down
12 changes: 12 additions & 0 deletions examples/ipset/resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ resource "citrixadc_ipset" "tf_ipset" {
citrixadc_nsip.nsip1.ipaddress,
citrixadc_nsip.nsip2.ipaddress,
]

nsip6binding = [
citrixadc_nsip6.nsip6_1.ipv6address,
]
}

# ipv4
resource "citrixadc_nsip" "nsip1" {
ipaddress = "10.1.1.1"
type = "VIP"
Expand All @@ -17,3 +22,10 @@ resource "citrixadc_nsip" "nsip2" {
type = "SNIP"
netmask = "255.255.255.0"
}

# ipv6
resource "citrixadc_nsip6" "nsip6_1" {
ipv6address = "2009::2/64"
type = "VIP"
icmp = "DISABLED"
}

0 comments on commit 65b08e9

Please sign in to comment.