From 0ce4f21fbeb7df010cbc5af9b1dbd8437074d111 Mon Sep 17 00:00:00 2001 From: George Nikolopoulos Date: Thu, 23 Jul 2020 11:36:03 +0300 Subject: [PATCH] Add resource nsparam with examples Signed-off-by: George Nikolopoulos --- citrixadc/provider.go | 1 + citrixadc/resource_citrixadc_nsparam.go | 261 +++++++++++++++++++ citrixadc/resource_citrixadc_nsparam_test.go | 103 ++++++++ examples/nsparam/provider.tf | 3 + examples/nsparam/resources.tf | 4 + 5 files changed, 372 insertions(+) create mode 100644 citrixadc/resource_citrixadc_nsparam.go create mode 100644 citrixadc/resource_citrixadc_nsparam_test.go create mode 100644 examples/nsparam/provider.tf create mode 100644 examples/nsparam/resources.tf diff --git a/citrixadc/provider.go b/citrixadc/provider.go index bc4fd51ca..33d9d1be2 100644 --- a/citrixadc/provider.go +++ b/citrixadc/provider.go @@ -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(), } } diff --git a/citrixadc/resource_citrixadc_nsparam.go b/citrixadc/resource_citrixadc_nsparam.go new file mode 100644 index 000000000..6dfbb02b3 --- /dev/null +++ b/citrixadc/resource_citrixadc_nsparam.go @@ -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 +} diff --git a/citrixadc/resource_citrixadc_nsparam_test.go b/citrixadc/resource_citrixadc_nsparam_test.go new file mode 100644 index 000000000..6e2671e09 --- /dev/null +++ b/citrixadc/resource_citrixadc_nsparam_test.go @@ -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" +} +` diff --git a/examples/nsparam/provider.tf b/examples/nsparam/provider.tf new file mode 100644 index 000000000..3d4508593 --- /dev/null +++ b/examples/nsparam/provider.tf @@ -0,0 +1,3 @@ +provider "citrixadc" { + endpoint = "http://localhost:8080" +} diff --git a/examples/nsparam/resources.tf b/examples/nsparam/resources.tf new file mode 100644 index 000000000..7d111fbf2 --- /dev/null +++ b/examples/nsparam/resources.tf @@ -0,0 +1,4 @@ +resource "citrixadc_nsparam" "tf_nsparam" { + proxyprotocol = "DISABLED" + maxconn = 10 +}