Skip to content

Commit

Permalink
Add resource nsparam with examples
Browse files Browse the repository at this point in the history
Signed-off-by: George Nikolopoulos <[email protected]>
  • Loading branch information
George Nikolopoulos committed Jul 27, 2020
1 parent e60b0fb commit 0ce4f21
Show file tree
Hide file tree
Showing 5 changed files with 372 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 @@ -137,6 +137,7 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(),
"citrixadc_netprofile": resourceCitrixAdcNetprofile(),
"citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(),
"citrixadc_nsparam": resourceCitrixAdcNsparam(),
}
}

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

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

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

"log"
)

func resourceCitrixAdcNsparam() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createNsparamFunc,
Read: readNsparamFunc,
Delete: deleteNsparamFunc,
Schema: map[string]*schema.Schema{
"advancedanalyticsstats": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"aftpallowrandomsourceport": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"cip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"cipheader": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"cookieversion": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"crportrange": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"exclusivequotamaxclient": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"exclusivequotaspillover": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"ftpportrange": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"grantquotamaxclient": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"grantquotaspillover": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"internaluserlogin": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"maxconn": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"maxreq": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"mgmthttpport": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"mgmthttpsport": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"pmtumin": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"pmtutimeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"proxyprotocol": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"securecookie": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"servicepathingressvlan": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"tcpcip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"timezone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"useproxyport": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func createNsparamFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createNsparamFunc")
client := meta.(*NetScalerNitroClient).client
nsparamId := resource.PrefixedUniqueId("tf-nsparam-")
nsparam := ns.Nsparam{
Advancedanalyticsstats: d.Get("advancedanalyticsstats").(string),
Aftpallowrandomsourceport: d.Get("aftpallowrandomsourceport").(string),
Cip: d.Get("cip").(string),
Cipheader: d.Get("cipheader").(string),
Cookieversion: d.Get("cookieversion").(string),
Crportrange: d.Get("crportrange").(string),
Exclusivequotamaxclient: d.Get("exclusivequotamaxclient").(int),
Exclusivequotaspillover: d.Get("exclusivequotaspillover").(int),
Ftpportrange: d.Get("ftpportrange").(string),
Grantquotamaxclient: d.Get("grantquotamaxclient").(int),
Grantquotaspillover: d.Get("grantquotaspillover").(int),
Internaluserlogin: d.Get("internaluserlogin").(string),
Maxconn: d.Get("maxconn").(int),
Maxreq: d.Get("maxreq").(int),
Mgmthttpport: d.Get("mgmthttpport").(int),
Mgmthttpsport: d.Get("mgmthttpsport").(int),
Pmtumin: d.Get("pmtumin").(int),
Pmtutimeout: d.Get("pmtutimeout").(int),
Proxyprotocol: d.Get("proxyprotocol").(string),
Securecookie: d.Get("securecookie").(string),
Servicepathingressvlan: d.Get("servicepathingressvlan").(int),
Tcpcip: d.Get("tcpcip").(string),
Timezone: d.Get("timezone").(string),
Useproxyport: d.Get("useproxyport").(string),
}

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

d.SetId(nsparamId)

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

func readNsparamFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readNsparamFunc")
client := meta.(*NetScalerNitroClient).client
nsparamName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading nsparam state %s", nsparamName)
data, err := client.FindResource(netscaler.Nsparam.Type(), "")
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing nsparam state %s", nsparamName)
d.SetId("")
return nil
}
d.Set("name", data["name"])
d.Set("advancedanalyticsstats", data["advancedanalyticsstats"])
d.Set("aftpallowrandomsourceport", data["aftpallowrandomsourceport"])
d.Set("cip", data["cip"])
d.Set("cipheader", data["cipheader"])
d.Set("cookieversion", data["cookieversion"])
d.Set("crportrange", data["crportrange"])
d.Set("exclusivequotamaxclient", data["exclusivequotamaxclient"])
d.Set("exclusivequotaspillover", data["exclusivequotaspillover"])
d.Set("ftpportrange", data["ftpportrange"])
d.Set("grantquotamaxclient", data["grantquotamaxclient"])
d.Set("grantquotaspillover", data["grantquotaspillover"])
d.Set("internaluserlogin", data["internaluserlogin"])
d.Set("maxconn", data["maxconn"])
d.Set("maxreq", data["maxreq"])
d.Set("mgmthttpport", data["mgmthttpport"])
d.Set("mgmthttpsport", data["mgmthttpsport"])
d.Set("pmtumin", data["pmtumin"])
d.Set("pmtutimeout", data["pmtutimeout"])
d.Set("proxyprotocol", data["proxyprotocol"])
d.Set("securecookie", data["securecookie"])
d.Set("servicepathingressvlan", data["servicepathingressvlan"])
d.Set("tcpcip", data["tcpcip"])
d.Set("timezone", data["timezone"])
d.Set("useproxyport", data["useproxyport"])

return nil

}

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

d.SetId("")

return nil
}
103 changes: 103 additions & 0 deletions citrixadc/resource_citrixadc_nsparam_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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"
"github.com/chiradeep/go-nitro/netscaler"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"testing"
)

func TestAccNsparam_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccNsparam_basic_step1,
Check: resource.ComposeTestCheckFunc(
testAccCheckNsparamExist("citrixadc_nsparam.tf_nsparam", nil, map[string]interface{}{"maxconn": "10", "useproxyport": "DISABLED"}),
),
},
resource.TestStep{
Config: testAccNsparam_basic_step2,
Check: resource.ComposeTestCheckFunc(
testAccCheckNsparamExist("citrixadc_nsparam.tf_nsparam", nil, map[string]interface{}{"maxconn": "0", "useproxyport": "ENABLED"}),
),
},
},
})
}

func testAccCheckNsparamExist(n string, id *string, expectedValues map[string]interface{}) 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 lb vserver name is set")
}

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

*id = rs.Primary.ID
}

nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client
data, err := nsClient.FindResource(netscaler.Nsparam.Type(), "")

if err != nil {
return err
}

if data == nil {
return fmt.Errorf("NS parameters %s not found", n)
}

if data["proxyprotocol"] != expectedValues["proxyprotocol"] {
return fmt.Errorf("Expected value for \"proxyprotocol\" differs. Expected: \"%v\", Retrieved \"%v\"", expectedValues["proxyprotocol"], data["proxyprotocol"])
}

if data["maxconn"] != expectedValues["maxconn"] {
return fmt.Errorf("Expected value for \"maxconn\" differs. Expected: \"%v\", Retrieved \"%v\"", expectedValues["maxconn"], data["maxconn"])
}

return nil
}
}

const testAccNsparam_basic_step1 = `
resource "citrixadc_nsparam" "tf_nsparam" {
maxconn = 10
useproxyport = "DISABLED"
}
`

const testAccNsparam_basic_step2 = `
resource "citrixadc_nsparam" "tf_nsparam" {
maxconn = 0
useproxyport = "ENABLED"
}
`
3 changes: 3 additions & 0 deletions examples/nsparam/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "citrixadc" {
endpoint = "http://localhost:8080"
}
4 changes: 4 additions & 0 deletions examples/nsparam/resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "citrixadc_nsparam" "tf_nsparam" {
proxyprotocol = "DISABLED"
maxconn = 10
}

0 comments on commit 0ce4f21

Please sign in to comment.