Skip to content

Commit

Permalink
Merge pull request #207 from citrix/lbroute6
Browse files Browse the repository at this point in the history
Add Lbroute6 resource
  • Loading branch information
George Nikolopoulos authored Sep 10, 2021
2 parents af4519b + 659ea86 commit b10474d
Show file tree
Hide file tree
Showing 6 changed files with 542 additions and 0 deletions.
1 change: 1 addition & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func providerSchema() map[string]*schema.Schema {
func providerResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"citrixadc_lbmetrictable": resourceCitrixAdcLbmetrictable(),
"citrixadc_lbroute6": resourceCitrixAdcLbroute6(),
"citrixadc_lbvserver_botpolicy_binding": resourceCitrixAdcLbvserver_botpolicy_binding(),
"citrixadc_lbvserver_auditsyslogpolicy_binding": resourceCitrixAdcLbvserver_auditsyslogpolicy_binding(),
"citrixadc_sslcacertgroup": resourceCitrixAdcSslcacertgroup(),
Expand Down
130 changes: 130 additions & 0 deletions citrixadc/resource_citrixadc_lbroute6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package citrixadc

import (
"net/url"

"github.com/citrix/adc-nitro-go/resource/config/lb"

"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform/helper/schema"

"log"
)

func resourceCitrixAdcLbroute6() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createLbroute6Func,
Read: readLbroute6Func,
Delete: deleteLbroute6Func,
Schema: map[string]*schema.Schema{
"gatewayname": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"td": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func createLbroute6Func(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createLbroute6Func")
client := meta.(*NetScalerNitroClient).client
var network = d.Get("network").(string)
lbroute6 := lb.Lbroute6{
Gatewayname: d.Get("gatewayname").(string),
Network: d.Get("network").(string),
Td: d.Get("td").(int),
}

_, err := client.AddResource(service.Lbroute6.Type(), "", &lbroute6)
if err != nil {
return err
}

d.SetId(network)

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

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

log.Printf("[DEBUG] citrixadc-provider: Reading lbroute6 state %s", network)
findParams := service.FindParams{
ResourceType: service.Lbroute6.Type(),
}

dataArray, err := client.FindResourceArrayWithParams(findParams)

if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing lb route6 state %s", network)
d.SetId("")
return nil
}

if len(dataArray) == 0 {
log.Printf("[WARN] citrixadc-provider: lb route6 does not exist. Clearing state.")
d.SetId("")
return nil
}

foundIndex := -1
for i, lbroute6 := range dataArray {
if lbroute6["network"] == network {
foundIndex = i
break
}
}
if foundIndex == -1 {
log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams route6 not found in array")
log.Printf("[WARN] citrixadc-provider: Clearing lb route6 state %s", network)
d.SetId("")
return nil
}

data := dataArray[foundIndex]

d.Set("gatewayname", data["gatewayname"])
d.Set("network", data["network"])
d.Set("td", data["td"])

return nil

}

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

argsMap := make(map[string]string)
argsMap["network"] = url.QueryEscape(d.Get("network").(string))

err := client.DeleteResourceWithArgsMap(service.Lbroute6.Type(), "", argsMap)

if err != nil {
return err
}

d.SetId("")

return nil
}
206 changes: 206 additions & 0 deletions citrixadc/resource_citrixadc_lbroute6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
Copyright 2016 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"
"log"
"testing"

"github.com/citrix/adc-nitro-go/service"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

const testAccLbroute6_basic = `
resource "citrixadc_nsip6" "tf1_nsip6" {
ipv6address = "22::1/64"
vserver = "DISABLED"
}
resource "citrixadc_nsip6" "tf2_nsip6" {
ipv6address = "33::1/64"
vserver = "DISABLED"
}
resource "citrixadc_nsip6" "tf3_nsip6" {
ipv6address = "44::1/64"
vserver = "DISABLED"
}
resource "citrixadc_lbvserver" "llb6" {
name = "llb6"
servicetype = "ANY"
persistencetype = "NONE"
lbmethod = "ROUNDROBIN"
}
resource "citrixadc_service" "r4" {
name = "r4"
ip = "22::2"
servicetype = "ANY"
port = 65535
depends_on = [citrixadc_nsip6.tf1_nsip6]
}
resource "citrixadc_service" "r5" {
name = "r5"
ip = "33::2"
servicetype = "ANY"
port = 65535
depends_on = [citrixadc_nsip6.tf2_nsip6]
}
resource "citrixadc_service" "r6" {
name = "r6"
ip = "44::2"
servicetype = "ANY"
port = 65535
depends_on = [citrixadc_nsip6.tf3_nsip6]
}
resource "citrixadc_lbvserver_service_binding" "tf_binding4" {
name = citrixadc_lbvserver.llb6.name
servicename = citrixadc_service.r4.name
weight = 10
}
resource "citrixadc_lbvserver_service_binding" "tf_binding5" {
name = citrixadc_lbvserver.llb6.name
servicename = citrixadc_service.r5.name
weight = 10
}
resource "citrixadc_lbvserver_service_binding" "tf_binding6" {
name = citrixadc_lbvserver.llb6.name
servicename = citrixadc_service.r6.name
weight = 10
}
resource "citrixadc_lbroute6" "demo_route6" {
network = "66::/64"
gatewayname = citrixadc_lbvserver.llb6.name
}`

func TestAccLbroute6_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLbroute6Destroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccLbroute6_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckLbroute6Exist("citrixadc_lbroute6.demo_route6", nil),
),
},
},
})
}

func testAccCheckLbroute6Exist(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 lbroute6 name is set")
}

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

*id = rs.Primary.ID
}

network := rs.Primary.ID
nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client

findParams := service.FindParams{
ResourceType: service.Lbroute6.Type(),
}

dataArray, err := nsClient.FindResourceArrayWithParams(findParams)

foundIndex := -1
for i, lbroute6 := range dataArray {
if lbroute6["network"] == network {
foundIndex = i
break
}
}

if err != nil {
log.Printf("[WARN] citrix-provider: acceptance test: Clearing lbroute6 state %s", network)
return err
}

if foundIndex == -1 {
return fmt.Errorf("Could not find lbroute6 with network %v", network)
}

return nil
}
}

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

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

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

network := rs.Primary.ID

findParams := service.FindParams{
ResourceType: service.Lbroute6.Type(),
}
dataArray, err := nsClient.FindResourceArrayWithParams(findParams)

if err != nil {
return err
}

foundIndex := -1
for i, lbroute6 := range dataArray {
if lbroute6["network"] == network {
foundIndex = i
break
}
}

if foundIndex != -1 {
return fmt.Errorf("LB route6 still exists with network %v", network)
}

}

return nil
}
Loading

0 comments on commit b10474d

Please sign in to comment.