Skip to content

Commit

Permalink
Merge pull request #1145 from citrix/lbpolicy_binding
Browse files Browse the repository at this point in the history
Added gslbvserver_lbpolicy_binding and lbvserver_lbpolicy_binding
  • Loading branch information
sumanth-lingappa authored Apr 19, 2024
2 parents fe15112 + f500664 commit 8127135
Show file tree
Hide file tree
Showing 18 changed files with 1,142 additions and 4 deletions.
2 changes: 2 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_lbaction": resourceCitrixAdcLbaction(),
"citrixadc_lbpolicy": resourceCitrixAdcLbpolicy(),
"citrixadc_sslcrl": resourceCitrixAdcSslcrl(),
"citrixadc_gslbvserver_lbpolicy_binding": resourceCitrixAdcGslbvserver_lbpolicy_binding(),
"citrixadc_lbvserver_lbpolicy_binding": resourceCitrixAdcLbvserver_lbpolicy_binding(),
}
}

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

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

"fmt"
"log"
"strings"
)

func resourceCitrixAdcGslbvserver_lbpolicy_binding() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createGslbvserver_lbpolicy_bindingFunc,
Read: readGslbvserver_lbpolicy_bindingFunc,
Delete: deleteGslbvserver_lbpolicy_bindingFunc,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"gotopriorityexpression": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"order": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"policyname": {
Type: schema.TypeString,
Required: true,
Computed: false,
ForceNew: true,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func createGslbvserver_lbpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createGslbvserver_lbpolicy_bindingFunc")
client := meta.(*NetScalerNitroClient).client
name := d.Get("name")
policyname := d.Get("policyname")
bindingId := fmt.Sprintf("%s,%s", name, policyname)
gslbvserver_lbpolicy_binding := gslb.Gslbvserverlbpolicybinding{
Gotopriorityexpression: d.Get("gotopriorityexpression").(string),
Name: d.Get("name").(string),
Order: d.Get("order").(int),
Policyname: d.Get("policyname").(string),
Priority: d.Get("priority").(int),
Type: d.Get("type").(string),
}

_, err := client.AddResource("gslbvserver_lbpolicy_binding", bindingId, &gslbvserver_lbpolicy_binding)
if err != nil {
return err
}

d.SetId(bindingId)

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

func readGslbvserver_lbpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readGslbvserver_lbpolicy_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 gslbvserver_lbpolicy_binding state %s", bindingId)

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

data := dataArr[foundIndex]

d.Set("gotopriorityexpression", data["gotopriorityexpression"])
d.Set("name", data["name"])
setToInt("order", d, data["order"])
d.Set("policyname", data["policyname"])
setToInt("priority", d, data["priority"])
d.Set("type", data["type"])

return nil

}

func deleteGslbvserver_lbpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deleteGslbvserver_lbpolicy_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", policyname))

err := client.DeleteResourceWithArgs("gslbvserver_lbpolicy_binding", name, args)
if err != nil {
return err
}

d.SetId("")

return nil
}
200 changes: 200 additions & 0 deletions citrixadc/resource_citrixadc_gslbvserver_lbpolicy_binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
Copyright 2024 Citrix Systems, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package citrixadc

import (
"fmt"
"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"strings"
"testing"
)

const testAccGslbvserver_lbpolicy_binding_basic = `
resource "citrixadc_gslbvserver" "tf_gslbvserver" {
name = "tf_gslbvserver"
servicetype = "HTTP"
}
resource "citrixadc_lbpolicy" "tf_pol" {
name = "tf_pol"
rule = "true"
action = "NOLBACTION"
}
resource "citrixadc_gslbvserver_lbpolicy_binding" "tf_bind" {
policyname = citrixadc_lbpolicy.tf_pol.name
name = citrixadc_gslbvserver.tf_gslbvserver.name
priority = 10
}
`

const testAccGslbvserver_lbpolicy_binding_basic_step2 = `
# Keep the above bound resources without the actual binding to check proper deletion
resource "citrixadc_gslbvserver" "tf_gslbvserver" {
name = "tf_gslbvserver"
servicetype = "HTTP"
}
resource "citrixadc_lbpolicy" "tf_pol" {
name = "tf_pol"
rule = "true"
action = "NOLBACTION"
}
`

func TestAccGslbvserver_lbpolicy_binding_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGslbvserver_lbpolicy_bindingDestroy,
Steps: []resource.TestStep{
{
Config: testAccGslbvserver_lbpolicy_binding_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckGslbvserver_lbpolicy_bindingExist("citrixadc_gslbvserver_lbpolicy_binding.tf_bind", nil),
),
},
{
Config: testAccGslbvserver_lbpolicy_binding_basic_step2,
Check: resource.ComposeTestCheckFunc(
testAccCheckGslbvserver_lbpolicy_bindingNotExist("citrixadc_gslbvserver_lbpolicy_binding.tf_bind", "tf_gslbvserver,tf_pol"),
),
},
},
})
}

func testAccCheckGslbvserver_lbpolicy_bindingExist(n string, id *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No gslbvserver_lbpolicy_binding id is set")
}

if id != nil {
if *id != "" && *id != rs.Primary.ID {
return fmt.Errorf("Resource ID has changed!")
}

*id = rs.Primary.ID
}

client := testAccProvider.Meta().(*NetScalerNitroClient).client

bindingId := rs.Primary.ID

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

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

findParams := service.FindParams{
ResourceType: "gslbvserver_lbpolicy_binding",
ResourceName: name,
ResourceMissingErrorCode: 258,
}
dataArr, err := client.FindResourceArrayWithParams(findParams)

// Unexpected error
if err != nil {
return err
}

// Iterate through results to find the one with the matching policyname
found := false
for _, v := range dataArr {
if v["policyname"].(string) == policyname {
found = true
break
}
}

if !found {
return fmt.Errorf("gslbvserver_lbpolicy_binding %s not found", n)
}

return nil
}
}

func testAccCheckGslbvserver_lbpolicy_bindingNotExist(n string, id string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*NetScalerNitroClient).client

if !strings.Contains(id, ",") {
return fmt.Errorf("Invalid id string %v. The id string must contain a comma.", id)
}
idSlice := strings.SplitN(id, ",", 2)

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

findParams := service.FindParams{
ResourceType: "gslbvserver_lbpolicy_binding",
ResourceName: name,
ResourceMissingErrorCode: 258,
}
dataArr, err := client.FindResourceArrayWithParams(findParams)

// Unexpected error
if err != nil {
return err
}

// Iterate through results to hopefully not find the one with the matching policyname
found := false
for _, v := range dataArr {
if v["policyname"].(string) == policyname {
found = true
break
}
}

if found {
return fmt.Errorf("gslbvserver_lbpolicy_binding %s was found, but it should have been destroyed", n)
}

return nil
}
}

func testAccCheckGslbvserver_lbpolicy_bindingDestroy(s *terraform.State) error {
nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client

for _, rs := range s.RootModule().Resources {
if rs.Type != "citrixadc_gslbvserver_lbpolicy_binding" {
continue
}

if rs.Primary.ID == "" {
return fmt.Errorf("No name is set")
}

_, err := nsClient.FindResource("gslbvserver_lbpolicy_binding", rs.Primary.ID)
if err == nil {
return fmt.Errorf("gslbvserver_lbpolicy_binding %s still exists", rs.Primary.ID)
}

}

return nil
}
Loading

0 comments on commit 8127135

Please sign in to comment.