-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 1127-feature-request-add-attribute-support…
…-to-gslbservicegroup-resource
- Loading branch information
Showing
18 changed files
with
1,142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
citrixadc/resource_citrixadc_gslbvserver_lbpolicy_binding.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
200
citrixadc/resource_citrixadc_gslbvserver_lbpolicy_binding_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.