Skip to content

Commit

Permalink
Implement resource servicegroup_lbmonitor_binding with examples
Browse files Browse the repository at this point in the history
Signed-off-by: George Nikolopoulos <[email protected]>
  • Loading branch information
George Nikolopoulos committed Jul 14, 2020
1 parent dcc0dd5 commit 1c9987e
Show file tree
Hide file tree
Showing 5 changed files with 592 additions and 58 deletions.
117 changes: 59 additions & 58 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,64 +78,65 @@ func providerSchema() map[string]*schema.Schema {

func providerResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"citrixadc_lbvserver": resourceCitrixAdcLbvserver(),
"citrixadc_service": resourceCitrixAdcService(),
"citrixadc_csvserver": resourceCitrixAdcCsvserver(),
"citrixadc_cspolicy": resourceCitrixAdcCspolicy(),
"citrixadc_csaction": resourceCitrixAdcCsaction(),
"citrixadc_sslaction": resourceCitrixAdcSslaction(),
"citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(),
"citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(),
"citrixadc_sslprofile": resourceCitrixAdcSslprofile(),
"citrixadc_sslparameter": resourceCitrixAdcSslparameter(),
"citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(),
"citrixadc_sslcipher": resourceCitrixAdcSslcipher(),
"citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(),
"citrixadc_servicegroup": resourceCitrixAdcServicegroup(),
"citrixadc_nsacl": resourceCitrixAdcNsacl(),
"citrixadc_nsacls": resourceCitrixAdcNsacls(),
"citrixadc_inat": resourceCitrixAdcInat(),
"citrixadc_rnat": resourceCitrixAdcRnats(),
"citrixadc_gslbsite": resourceCitrixAdcGslbsite(),
"citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(),
"citrixadc_gslbservice": resourceCitrixAdcGslbservice(),
"citrixadc_server": resourceCitrixAdcServer(),
"citrixadc_responderaction": resourceCitrixAdcResponderaction(),
"citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(),
"citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(),
"citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(),
"citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(),
"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(),
"citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(),
"citrixadc_interface": resourceCitrixAdcInterface(),
"citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(),
"citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(),
"citrixadc_nslicense": resourceCitrixAdcNslicense(),
"citrixadc_cluster": resourceCitrixAdcCluster(),
"citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(),
"citrixadc_systemfile": resourceCitrixAdcSystemfile(),
"citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(),
"citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(),
"citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(),
"citrixadc_rebooter": resourceCitrixAdcRebooter(),
"citrixadc_installer": resourceCitrixAdcInstaller(),
"citrixadc_pinger": resourceCitrixAdcPinger(),
"citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(),
"citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(),
"citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(),
"citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(),
"citrixadc_netprofile": resourceCitrixAdcNetprofile(),
"citrixadc_lbvserver": resourceCitrixAdcLbvserver(),
"citrixadc_service": resourceCitrixAdcService(),
"citrixadc_csvserver": resourceCitrixAdcCsvserver(),
"citrixadc_cspolicy": resourceCitrixAdcCspolicy(),
"citrixadc_csaction": resourceCitrixAdcCsaction(),
"citrixadc_sslaction": resourceCitrixAdcSslaction(),
"citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(),
"citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(),
"citrixadc_sslprofile": resourceCitrixAdcSslprofile(),
"citrixadc_sslparameter": resourceCitrixAdcSslparameter(),
"citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(),
"citrixadc_sslcipher": resourceCitrixAdcSslcipher(),
"citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(),
"citrixadc_servicegroup": resourceCitrixAdcServicegroup(),
"citrixadc_nsacl": resourceCitrixAdcNsacl(),
"citrixadc_nsacls": resourceCitrixAdcNsacls(),
"citrixadc_inat": resourceCitrixAdcInat(),
"citrixadc_rnat": resourceCitrixAdcRnats(),
"citrixadc_gslbsite": resourceCitrixAdcGslbsite(),
"citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(),
"citrixadc_gslbservice": resourceCitrixAdcGslbservice(),
"citrixadc_server": resourceCitrixAdcServer(),
"citrixadc_responderaction": resourceCitrixAdcResponderaction(),
"citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(),
"citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(),
"citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(),
"citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(),
"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(),
"citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(),
"citrixadc_interface": resourceCitrixAdcInterface(),
"citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(),
"citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(),
"citrixadc_nslicense": resourceCitrixAdcNslicense(),
"citrixadc_cluster": resourceCitrixAdcCluster(),
"citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(),
"citrixadc_systemfile": resourceCitrixAdcSystemfile(),
"citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(),
"citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(),
"citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(),
"citrixadc_rebooter": resourceCitrixAdcRebooter(),
"citrixadc_installer": resourceCitrixAdcInstaller(),
"citrixadc_pinger": resourceCitrixAdcPinger(),
"citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(),
"citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(),
"citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(),
"citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(),
"citrixadc_netprofile": resourceCitrixAdcNetprofile(),
"citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(),
}
}

Expand Down
236 changes: 236 additions & 0 deletions citrixadc/resource_citrixadc_servicegroup_lbmonitor_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package citrixadc

import (
"github.com/chiradeep/go-nitro/config/basic"

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

"fmt"
"log"
"strings"
)

func resourceCitrixAdcServicegroup_lbmonitor_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createServicegroup_lbmonitor_bindingFunc,
Read: readServicegroup_lbmonitor_bindingFunc,
Delete: deleteServicegroup_lbmonitor_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"customserverid": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"dbsttl": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"hashid": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"monitorname": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"monstate": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"nameserver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"passive": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"serverid": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"servicegroupname": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"state": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func createServicegroup_lbmonitor_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createServicegroup_lbmonitor_bindingFunc")
client := meta.(*NetScalerNitroClient).client
servicegroupName := d.Get("servicegroupname")
monitorName := d.Get("monitorname")
// Use `,` as the separator since it is invalid character for servicegroup and monitor name
servicegroupLbmonitorBindingId := fmt.Sprintf("%s,%s", servicegroupName, monitorName)

servicegroup_lbmonitor_binding := basic.Servicegrouplbmonitorbinding{
Customserverid: d.Get("customserverid").(string),
Dbsttl: d.Get("dbsttl").(int),
Hashid: d.Get("hashid").(int),
Monitorname: d.Get("monitorname").(string),
Monstate: d.Get("monstate").(string),
Nameserver: d.Get("nameserver").(string),
Passive: d.Get("passive").(bool),
Port: d.Get("port").(int),
Serverid: d.Get("serverid").(int),
Servicegroupname: d.Get("servicegroupname").(string),
State: d.Get("state").(string),
Weight: d.Get("weight").(int),
}

err := client.UpdateUnnamedResource(netscaler.Servicegroup_lbmonitor_binding.Type(), &servicegroup_lbmonitor_binding)
if err != nil {
return err
}

d.SetId(servicegroupLbmonitorBindingId)

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

func readServicegroup_lbmonitor_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readServicegroup_lbmonitor_bindingFunc")
client := meta.(*NetScalerNitroClient).client
servicegroupLbmonitorBindingId := d.Id()
idSlice := strings.Split(servicegroupLbmonitorBindingId, ",")

if len(idSlice) < 2 {
return fmt.Errorf("Cannot deduce monitorname from id string")
}

if len(idSlice) > 2 {
return fmt.Errorf("Too many separators \",\" in id string")
}

servicegroupName := idSlice[0]
monitorName := idSlice[1]

log.Printf("[DEBUG] citrixadc-provider: Reading servicegroup_lbmonitor_binding state %s", servicegroupLbmonitorBindingId)
findParams := netscaler.FindParams{
ResourceType: "servicegroup_lbmonitor_binding",
ResourceName: servicegroupName,
ResourceMissingErrorCode: 258,
}
dataArr, err := client.FindResourceArrayWithParams(findParams)

// Unexpected error
if err != nil {
log.Printf("[DEBUG] citrixadc-provider: Error during FindResourceArrayWithParams %s", err.Error())
return err
}

// Resource is missing
if len(dataArr) == 0 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams returned empty array")
log.Printf("[WARN] citrixadc-provider: Clearing servicegroup_lbmonitor_binding state %s", servicegroupLbmonitorBindingId)
d.SetId("")
return nil
}

// Iterate through results to find the one with the right monitor name
foundIndex := -1
for i, v := range dataArr {
if v["monitor_name"].(string) == monitorName {
foundIndex = i
break
}
}

// Resource is missing
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams monitor name not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing servicegroup_lbmonitor_binding state %s", servicegroupLbmonitorBindingId)
d.SetId("")
return nil
}
// Fallthrough

data := dataArr[foundIndex]

d.Set("name", data["name"])
d.Set("customserverid", data["customserverid"])
d.Set("dbsttl", data["dbsttl"])
d.Set("hashid", data["hashid"])
d.Set("monitorname", data["monitor_name"])
d.Set("monstate", data["monstate"])
d.Set("nameserver", data["nameserver"])
d.Set("passive", data["passive"])
d.Set("port", data["port"])
d.Set("serverid", data["serverid"])
d.Set("servicegroupname", data["servicegroupname"])
d.Set("state", data["state"])
d.Set("weight", data["weight"])

return nil

}

func deleteServicegroup_lbmonitor_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteServicegroup_lbmonitor_bindingFunc")
client := meta.(*NetScalerNitroClient).client
servicegroupLbmonitorBindingId := d.Id()
idSlice := strings.Split(servicegroupLbmonitorBindingId, ",")

servicegroupName := idSlice[0]
monitorName := idSlice[1]

args := make([]string, 0)
args = append(args, fmt.Sprintf("monitor_name:%s", monitorName))
if v, ok := d.GetOk("port"); ok {
args = append(args, fmt.Sprintf("port:%v", v))
}

err := client.DeleteResourceWithArgs(netscaler.Servicegroup_lbmonitor_binding.Type(), servicegroupName, args)
if err != nil {
return err
}

d.SetId("")

return nil
}
Loading

0 comments on commit 1c9987e

Please sign in to comment.