Skip to content

Commit

Permalink
Merge pull request #143 from citrix/new_resources
Browse files Browse the repository at this point in the history
New resources
  • Loading branch information
George Nikolopoulos authored Mar 5, 2021
2 parents 0f0f795 + 1a73a9d commit c1bbf05
Show file tree
Hide file tree
Showing 19 changed files with 1,741 additions and 39 deletions.
4 changes: 4 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_cmppolicy": resourceCitrixAdcCmppolicy(),
"citrixadc_lbvserver_cmppolicy_binding": resourceCitrixAdcLbvserver_cmppolicy_binding(),
"citrixadc_csvserver_cmppolicy_binding": resourceCitrixAdcCsvserver_cmppolicy_binding(),
"citrixadc_lbvserver_responderpolicy_binding": resourceCitrixAdcLbvserver_responderpolicy_binding(),
"citrixadc_csvserver_responderpolicy_binding": resourceCitrixAdcCsvserver_responderpolicy_binding(),
"citrixadc_csvserver_rewritepolicy_binding": resourceCitrixAdcCsvserver_rewritepolicy_binding(),
"citrixadc_lbvserver_rewritepolicy_binding": resourceCitrixAdcLbvserver_rewritepolicy_binding(),
}
}

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

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

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

"fmt"
"log"
"net/url"
"strings"
)

func resourceCitrixAdcCsvserver_responderpolicy_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createCsvserver_responderpolicy_bindingFunc,
Read: readCsvserver_responderpolicy_bindingFunc,
Delete: deleteCsvserver_responderpolicy_bindingFunc,
Schema: map[string]*schema.Schema{
"bindpoint": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"gotopriorityexpression": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"invoke": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"labelname": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"labeltype": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policyname": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"priority": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"targetlbvserver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func createCsvserver_responderpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createCsvserver_responderpolicy_bindingFunc")
client := meta.(*NetScalerNitroClient).client
name := d.Get("name").(string)
policyname := d.Get("policyname").(string)
bindingId := fmt.Sprintf("%s,%s", name, policyname)
csvserver_responderpolicy_binding := cs.Csvserverresponderpolicybinding{
Bindpoint: d.Get("bindpoint").(string),
Gotopriorityexpression: d.Get("gotopriorityexpression").(string),
Invoke: d.Get("invoke").(bool),
Labelname: d.Get("labelname").(string),
Labeltype: d.Get("labeltype").(string),
Name: d.Get("name").(string),
Policyname: d.Get("policyname").(string),
Priority: d.Get("priority").(int),
Targetlbvserver: d.Get("targetlbvserver").(string),
}

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

d.SetId(bindingId)

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

func readCsvserver_responderpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readCsvserver_responderpolicy_bindingFunc")
client := meta.(*NetScalerNitroClient).client
bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)
name := idSlice[0]
policyname := idSlice[1]
log.Printf("[DEBUG] citrixadc-provider: Reading csvserver_responderpolicy_binding state %s", bindingId)

findParams := netscaler.FindParams{
ResourceType: "csvserver_responderpolicy_binding",
ResourceName: name,
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 csvserver_responderpolicy_binding state %s", bindingId)
d.SetId("")
return nil
}

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

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

data := dataArr[foundIndex]

d.Set("name", data["name"])
d.Set("bindpoint", data["bindpoint"])
d.Set("gotopriorityexpression", data["gotopriorityexpression"])
d.Set("invoke", data["invoke"])
d.Set("labelname", data["labelname"])
d.Set("labeltype", data["labeltype"])
d.Set("name", data["name"])
d.Set("policyname", data["policyname"])
d.Set("priority", data["priority"])
d.Set("targetlbvserver", data["targetlbvserver"])

return nil

}

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

bindingId := d.Id()

idSlice := strings.SplitN(bindingId, ",", 2)
name := idSlice[0]
policyname := idSlice[1]

argsMap := make(map[string]string)
argsMap["policyname"] = url.QueryEscape(policyname)

if v, ok := d.GetOk("bindpoint"); ok {
argsMap["bindpoint"] = url.QueryEscape(v.(string))
}

if v, ok := d.GetOk("priority"); ok {
argsMap["priority"] = url.QueryEscape(fmt.Sprintf("%v", v))
}
err := client.DeleteResourceWithArgsMap(netscaler.Csvserver_responderpolicy_binding.Type(), name, argsMap)
if err != nil {
return err
}

d.SetId("")

return nil
}
Loading

0 comments on commit c1bbf05

Please sign in to comment.