Skip to content

Commit

Permalink
Merge pull request #1210 from citrix/systemgroup_systemcmdpolicy_bind…
Browse files Browse the repository at this point in the history
…ing-and-systemgroup_systemuser_binding

Added systemgroup_systemcmdpolicy_binding and systemgroup_systemuser_…
  • Loading branch information
rohit-myali authored Oct 15, 2024
2 parents 96de6ab + 7bbda00 commit 6cd2f04
Show file tree
Hide file tree
Showing 9 changed files with 850 additions and 33 deletions.
2 changes: 2 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,8 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_gslbservicegroup_gslbservicegroupmember_binding": resourceCitrixAdcGslbservicegroup_gslbservicegroupmember_binding(),
"citrixadc_gslbvserver_lbpolicy_binding": resourceCitrixAdcGslbvserver_lbpolicy_binding(),
"citrixadc_lbvserver_lbpolicy_binding": resourceCitrixAdcLbvserver_lbpolicy_binding(),
"citrixadc_systemgroup_systemcmdpolicy_binding": resourceCitrixAdcSystemgroup_systemcmdpolicy_binding(),
"citrixadc_systemgroup_systemuser_binding": resourceCitrixAdcSystemgroup_systemuser_binding(),
"citrixadc_sslprofile_ecccurve_binding": resourceCitrixAdcSslprofile_ecccurve_binding(),
"citrixadc_systemuser_systemcmdpolicy_binding": resourceCitrixAdcSystemuser_systemcmdpolicy_binding(),
}
Expand Down
35 changes: 22 additions & 13 deletions citrixadc/resource_citrixadc_systemgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ func createSystemgroupFunc(d *schema.ResourceData, meta interface{}) error {

d.SetId(systemgroupName)

err = updateSystemgroupCmdpolicyBindings(d, meta)
if err != nil {
return err
// Ignore bindings unless there is an explicit configuration for it
if _, ok := d.GetOk("cmdpolicybinding"); ok {
err = updateSystemgroupCmdpolicyBindings(d, meta)
if err != nil {
return err
}
}

err = updateSystemgroupSystemuserBindings(d, meta)
if err != nil {
return err
// Ignore bindings unless there is an explicit configuration for it
if _, ok := d.GetOk("systemusers"); ok {
err = updateSystemgroupSystemuserBindings(d, meta)
if err != nil {
return err
}
}

err = readSystemgroupFunc(d, meta)
Expand All @@ -122,15 +128,18 @@ func readSystemgroupFunc(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

err = readSystemgroupCmdpolicybindings(d, meta)
if err != nil {
return err
if _, ok := d.GetOk("cmdpolicybinding"); ok {
err = readSystemgroupCmdpolicybindings(d, meta)
if err != nil {
return err
}
}

err = readSystemgroupSystemuserbindings(d, meta)
if err != nil {
return err
if _, ok := d.GetOk("systemusers"); ok {
err = readSystemgroupSystemuserbindings(d, meta)
if err != nil {
return err
}
}

d.Set("name", data["name"])
Expand Down
152 changes: 152 additions & 0 deletions citrixadc/resource_citrixadc_systemgroup_systemcmdpolicy_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package citrixadc

import (
"net/url"

"github.com/citrix/adc-nitro-go/resource/config/system"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"fmt"
"log"
"strings"
)

func resourceCitrixAdcSystemgroup_systemcmdpolicy_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createSystemgroup_systemcmdpolicy_bindingFunc,
Read: readSystemgroup_systemcmdpolicy_bindingFunc,
Delete: deleteSystemgroup_systemcmdpolicy_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"groupname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policyname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"priority": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}

func createSystemgroup_systemcmdpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createSystemgroup_systemcmdpolicy_bindingFunc")
client := meta.(*NetScalerNitroClient).client
groupname := d.Get("groupname")
policyname := d.Get("policyname")
bindingId := fmt.Sprintf("%s,%s", groupname, policyname)
systemgroup_systemcmdpolicy_binding := system.Systemgroupsystemcmdpolicybinding{
Groupname: d.Get("groupname").(string),
Policyname: d.Get("policyname").(string),
Priority: d.Get("priority").(int),
}

_, err := client.AddResource(service.Systemgroup_systemcmdpolicy_binding.Type(), bindingId, &systemgroup_systemcmdpolicy_binding)
if err != nil {
return err
}

d.SetId(bindingId)

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

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

groupname := idSlice[0]
policyname := idSlice[1]

log.Printf("[DEBUG] citrixadc-provider: Reading systemgroup_systemcmdpolicy_binding state %s", bindingId)

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

// Iterate through results to find the one with the right id
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 secondIdComponent not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing systemgroup_systemcmdpolicy_binding state %s", bindingId)
d.SetId("")
return nil
}
// Fallthrough

data := dataArr[foundIndex]

d.Set("groupname", data["groupname"])
d.Set("policyname", data["policyname"])
setToInt("priority", d, data["priority"])

return nil

}

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

bindingId := d.Id()
idSlice := strings.SplitN(bindingId, ",", 2)

name := idSlice[0]
policyname := idSlice[1]

args := make([]string, 0)
args = append(args, fmt.Sprintf("policyname:%s", url.QueryEscape(policyname)))

err := client.DeleteResourceWithArgs(service.Systemgroup_systemcmdpolicy_binding.Type(), name, args)
if err != nil {
return err
}

d.SetId("")

return nil
}
Loading

0 comments on commit 6cd2f04

Please sign in to comment.