Skip to content

Commit

Permalink
Merge pull request #94 from citrix/ip6-sslciph-linkset-nsconfig
Browse files Browse the repository at this point in the history
Ipset_nsip6 sslcipher linkset nsconfig
  • Loading branch information
George Nikolopoulos authored Apr 14, 2020
2 parents 6c80c96 + 24524b5 commit 42c56ec
Show file tree
Hide file tree
Showing 21 changed files with 1,377 additions and 0 deletions.
5 changes: 5 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_sslprofile": resourceCitrixAdcSslprofile(),
"citrixadc_sslparameter": resourceCitrixAdcSslparameter(),
"citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(),
"citrixadc_sslcipher": resourceCitrixAdcSslcipher(),
"citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(),
"citrixadc_servicegroup": resourceCitrixAdcServicegroup(),
"citrixadc_nsacl": resourceCitrixAdcNsacl(),
Expand All @@ -107,8 +108,12 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_rewritepolicylabel": resourceCitrixAdcRewritepolicylabel(),
"citrixadc_nsip": resourceCitrixAdcNsip(),
"citrixadc_nsip6": resourceCitrixAdcNsip6(),
"citrixadc_nsconfig_save": resourceCitrixAdcNsconfigSave(),
"citrixadc_nsconfig_clear": resourceCitrixAdcNsconfigClear(),
"citrixadc_nsconfig_update": resourceCitrixAdcNsconfigUpdate(),
"citrixadc_ipset": resourceCitrixAdcIpset(),
"citrixadc_route": resourceCitrixAdcRoute(),
"citrixadc_linkset": resourceCitrixAdcLinkset(),
"citrixadc_nsfeature": resourceCitrixAdcNsfeature(),
"citrixadc_systemuser": resourceCitrixAdcSystemuser(),
"citrixadc_systemgroup": resourceCitrixAdcSystemgroup(),
Expand Down
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
181 changes: 181 additions & 0 deletions citrixadc/resource_citrixadc_linkset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package citrixadc

import (
"fmt"
"net/url"

"github.com/chiradeep/go-nitro/config/network"

"github.com/chiradeep/go-nitro/netscaler"
"github.com/hashicorp/terraform/helper/schema"

"log"
)

func resourceCitrixAdcLinkset() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createLinksetFunc,
Read: readLinksetFunc,
Delete: deleteLinksetFunc,
Schema: map[string]*schema.Schema{
"linkset_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"interfacebinding": &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},
},
},
}
}

func createLinksetFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createLinksetFunc")
client := meta.(*NetScalerNitroClient).client
linksetName := d.Get("linkset_id").(string)
linkset := network.Linkset{
Id: linksetName,
}

_, err := client.AddResource(netscaler.Linkset.Type(), "", &linkset)
if err != nil {
return err
}

d.SetId(linksetName)

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

err = readLinksetFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this linkset but we can't read it ?? %s", linksetName)
return nil
}
return nil
}

func readLinksetFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readLinksetFunc")
client := meta.(*NetScalerNitroClient).client
linksetName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading linkset state %s", linksetName)
// double encode value part as it contains `/`
linksetNameEscaped := url.QueryEscape(url.QueryEscape(linksetName))
data, err := client.FindResource(netscaler.Linkset.Type(), linksetNameEscaped)

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

d.Set("linkset_id", data["id"])

return nil

}

func deleteLinksetFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteLinksetFunc")
client := meta.(*NetScalerNitroClient).client
linksetName := d.Id()
// double encode value part as it contains `/`
linksetNameEscaped := url.QueryEscape(url.QueryEscape(linksetName))
err := client.DeleteResource(netscaler.Linkset.Type(), linksetNameEscaped)
if err != nil {
return err
}

d.SetId("")

return nil
}

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

linksetName := d.Get("linkset_id").(string)
args := make([]string, 0, 1)
// double encode value part as it contains `/`
ifnumEscaped := url.QueryEscape(url.QueryEscape(ifnum))

s := fmt.Sprintf("ifnum:%s", ifnumEscaped)
args = append(args, s)

log.Printf("args is %v", args)
linksetNameEscaped := url.QueryEscape(url.QueryEscape(linksetName))

if err := client.DeleteResourceWithArgs(netscaler.Linkset_interface_binding.Type(), linksetNameEscaped, args); err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error deleting interface binding %v\n", ifnum)
return err
}

return nil
}

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

bindingStruct := network.Linksetinterfacebinding{}
bindingStruct.Id = d.Get("linkset_id").(string)
bindingStruct.Ifnum = ifnum

// We need to do a HTTP PUT hence the UpdateResource
if err := client.UpdateUnnamedResource(netscaler.Linkset_interface_binding.Type(), bindingStruct); err != nil {
return err
}
return nil
}

func updateLinksetInterfaceBindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updateLinksetInterfaceBindings")
oldSet, newSet := d.GetChange("interfacebinding")
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 _, ifnum := range remove.List() {
if err := deleteSingleLinksetInterfaceBinding(d, meta, ifnum.(string)); err != nil {
return err
}
}

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

func readLinksetInterfaceBindings(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readLinksetInterfaceBindings")
client := meta.(*NetScalerNitroClient).client
linksetName := d.Get("linkset_id").(string)
// double encode value part as it contains `/`
linksetNameEscaped := url.QueryEscape(url.QueryEscape(linksetName))

bindings, _ := client.FindResourceArray(netscaler.Linkset_interface_binding.Type(), linksetNameEscaped)
log.Printf("bindings %v\n", bindings)

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

updatedSet := processedBindings
log.Printf("updatedSet %v\n", updatedSet)
if err := d.Set("interfacebinding", updatedSet); err != nil {
return err
}
return nil
}
Loading

0 comments on commit 42c56ec

Please sign in to comment.