From 65b08e9ee718431879d3dce64edbc56991ed269f Mon Sep 17 00:00:00 2001 From: Sumanth Lingappa Date: Tue, 14 Apr 2020 17:33:25 +0530 Subject: [PATCH] ipset_nsip6_binding resource support added Signed-off-by: Sumanth Lingappa --- citrixadc/resource_citrixadc_ipset.go | 94 ++++++++++++++++++++++ citrixadc/resource_citrixadc_ipset_test.go | 1 + examples/ipset/resources.tf | 12 +++ 3 files changed, 107 insertions(+) diff --git a/citrixadc/resource_citrixadc_ipset.go b/citrixadc/resource_citrixadc_ipset.go index b78cad22f..00289d262 100644 --- a/citrixadc/resource_citrixadc_ipset.go +++ b/citrixadc/resource_citrixadc_ipset.go @@ -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}, + }, }, } } @@ -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) @@ -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"]) @@ -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 +} diff --git a/citrixadc/resource_citrixadc_ipset_test.go b/citrixadc/resource_citrixadc_ipset_test.go index c52007966..0e94854d6 100644 --- a/citrixadc/resource_citrixadc_ipset_test.go +++ b/citrixadc/resource_citrixadc_ipset_test.go @@ -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" diff --git a/examples/ipset/resources.tf b/examples/ipset/resources.tf index a56e5f261..f9f21f5a3 100644 --- a/examples/ipset/resources.tf +++ b/examples/ipset/resources.tf @@ -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" @@ -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" +} \ No newline at end of file