From 9bf3a130fb0fb665cee3bb03f1585654831cd726 Mon Sep 17 00:00:00 2001 From: George Nikolopoulos Date: Mon, 15 Feb 2021 14:38:53 +0200 Subject: [PATCH 1/3] Add policy stringmap with pattern bindings Signed-off-by: George Nikolopoulos --- citrixadc/provider.go | 156 +++++++++-------- .../resource_citrixadc_policystringmap.go | 119 +++++++++++++ ...trixadc_policystringmap_pattern_binding.go | 149 ++++++++++++++++ ...dc_policystringmap_pattern_binding_test.go | 164 ++++++++++++++++++ ...resource_citrixadc_policystringmap_test.go | 118 +++++++++++++ examples/policystringmap/provider.tf | 10 ++ examples/policystringmap/resources.tf | 16 ++ 7 files changed, 655 insertions(+), 77 deletions(-) create mode 100644 citrixadc/resource_citrixadc_policystringmap.go create mode 100644 citrixadc/resource_citrixadc_policystringmap_pattern_binding.go create mode 100644 citrixadc/resource_citrixadc_policystringmap_pattern_binding_test.go create mode 100644 citrixadc/resource_citrixadc_policystringmap_test.go create mode 100644 examples/policystringmap/provider.tf create mode 100644 examples/policystringmap/resources.tf diff --git a/citrixadc/provider.go b/citrixadc/provider.go index bb2ac15cc..911f8e2a8 100644 --- a/citrixadc/provider.go +++ b/citrixadc/provider.go @@ -78,83 +78,85 @@ func providerSchema() map[string]*schema.Schema { func providerResources() map[string]*schema.Resource { return map[string]*schema.Resource{ - "citrixadc_lbvserver": resourceCitrixAdcLbvserver(), - "citrixadc_service": resourceCitrixAdcService(), - "citrixadc_csvserver": resourceCitrixAdcCsvserver(), - "citrixadc_cspolicy": resourceCitrixAdcCspolicy(), - "citrixadc_csaction": resourceCitrixAdcCsaction(), - "citrixadc_sslaction": resourceCitrixAdcSslaction(), - "citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(), - "citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(), - "citrixadc_sslprofile": resourceCitrixAdcSslprofile(), - "citrixadc_sslparameter": resourceCitrixAdcSslparameter(), - "citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(), - "citrixadc_sslcipher": resourceCitrixAdcSslcipher(), - "citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(), - "citrixadc_servicegroup": resourceCitrixAdcServicegroup(), - "citrixadc_nsacl": resourceCitrixAdcNsacl(), - "citrixadc_nsacls": resourceCitrixAdcNsacls(), - "citrixadc_inat": resourceCitrixAdcInat(), - "citrixadc_rnat": resourceCitrixAdcRnats(), - "citrixadc_gslbsite": resourceCitrixAdcGslbsite(), - "citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(), - "citrixadc_gslbservice": resourceCitrixAdcGslbservice(), - "citrixadc_server": resourceCitrixAdcServer(), - "citrixadc_responderaction": resourceCitrixAdcResponderaction(), - "citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(), - "citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(), - "citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(), - "citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(), - "citrixadc_rewritepolicylabel": resourceCitrixAdcRewritepolicylabel(), - "citrixadc_nsip": resourceCitrixAdcNsip(), - "citrixadc_nsip6": resourceCitrixAdcNsip6(), - "citrixadc_nsconfig_save": resourceCitrixAdcNsconfigSave(), - "citrixadc_nsconfig_clear": resourceCitrixAdcNsconfigClear(), - "citrixadc_nsconfig_update": resourceCitrixAdcNsconfigUpdate(), - "citrixadc_ipset": resourceCitrixAdcIpset(), - "citrixadc_route": resourceCitrixAdcRoute(), - "citrixadc_linkset": resourceCitrixAdcLinkset(), - "citrixadc_nsfeature": resourceCitrixAdcNsfeature(), - "citrixadc_systemuser": resourceCitrixAdcSystemuser(), - "citrixadc_systemgroup": resourceCitrixAdcSystemgroup(), - "citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(), - "citrixadc_interface": resourceCitrixAdcInterface(), - "citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(), - "citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(), - "citrixadc_nslicense": resourceCitrixAdcNslicense(), - "citrixadc_cluster": resourceCitrixAdcCluster(), - "citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(), - "citrixadc_systemfile": resourceCitrixAdcSystemfile(), - "citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(), - "citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(), - "citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(), - "citrixadc_rebooter": resourceCitrixAdcRebooter(), - "citrixadc_installer": resourceCitrixAdcInstaller(), - "citrixadc_pinger": resourceCitrixAdcPinger(), - "citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(), - "citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(), - "citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(), - "citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(), - "citrixadc_netprofile": resourceCitrixAdcNetprofile(), - "citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(), - "citrixadc_nsparam": resourceCitrixAdcNsparam(), - "citrixadc_sslvserver_sslpolicy_binding": resourceCitrixAdcSslvserver_sslpolicy_binding(), - "citrixadc_sslprofile_sslcipher_binding": resourceCitrixAdcSslprofile_sslcipher_binding(), - "citrixadc_policydataset": resourceCitrixAdcPolicydataset(), - "citrixadc_policydataset_value_binding": resourceCitrixAdcPolicydataset_value_binding(), - "citrixadc_password_resetter": resourceCitrixAdcPasswordResetter(), - "citrixadc_csvserver_cspolicy_binding": resourceCitrixAdcCsvserver_cspolicy_binding(), - "citrixadc_appfwprofile": resourceCitrixAdcAppfwprofile(), - "citrixadc_appfwpolicy": resourceCitrixAdcAppfwpolicy(), - "citrixadc_appfwfieldtype": resourceCitrixAdcAppfwfieldtype(), - "citrixadc_appfwpolicylabel": resourceCitrixAdcAppfwpolicylabel(), - "citrixadc_appfwjsoncontenttype": resourceCitrixAdcAppfwjsoncontenttype(), - "citrixadc_appfwxmlcontenttype": resourceCitrixAdcAppfwxmlcontenttype(), - "citrixadc_appfwprofile_starturl_binding": resourceCitrixAdcAppfwprofileStarturlBinding(), - "citrixadc_appfwprofile_denyurl_binding": resourceCitrixAdcAppfwprofileDenyurlBinding(), - "citrixadc_nslicenseserver": resourceCitrixAdcNslicenseserver(), - "citrixadc_nscapacity": resourceCitrixAdcNscapacity(), - "citrixadc_lbvserver_service_binding": resourceCitrixAdcLbvserver_service_binding(), + "citrixadc_lbvserver": resourceCitrixAdcLbvserver(), + "citrixadc_service": resourceCitrixAdcService(), + "citrixadc_csvserver": resourceCitrixAdcCsvserver(), + "citrixadc_cspolicy": resourceCitrixAdcCspolicy(), + "citrixadc_csaction": resourceCitrixAdcCsaction(), + "citrixadc_sslaction": resourceCitrixAdcSslaction(), + "citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(), + "citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(), + "citrixadc_sslprofile": resourceCitrixAdcSslprofile(), + "citrixadc_sslparameter": resourceCitrixAdcSslparameter(), + "citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(), + "citrixadc_sslcipher": resourceCitrixAdcSslcipher(), + "citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(), + "citrixadc_servicegroup": resourceCitrixAdcServicegroup(), + "citrixadc_nsacl": resourceCitrixAdcNsacl(), + "citrixadc_nsacls": resourceCitrixAdcNsacls(), + "citrixadc_inat": resourceCitrixAdcInat(), + "citrixadc_rnat": resourceCitrixAdcRnats(), + "citrixadc_gslbsite": resourceCitrixAdcGslbsite(), + "citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(), + "citrixadc_gslbservice": resourceCitrixAdcGslbservice(), + "citrixadc_server": resourceCitrixAdcServer(), + "citrixadc_responderaction": resourceCitrixAdcResponderaction(), + "citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(), + "citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(), + "citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(), + "citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(), + "citrixadc_rewritepolicylabel": resourceCitrixAdcRewritepolicylabel(), + "citrixadc_nsip": resourceCitrixAdcNsip(), + "citrixadc_nsip6": resourceCitrixAdcNsip6(), + "citrixadc_nsconfig_save": resourceCitrixAdcNsconfigSave(), + "citrixadc_nsconfig_clear": resourceCitrixAdcNsconfigClear(), + "citrixadc_nsconfig_update": resourceCitrixAdcNsconfigUpdate(), + "citrixadc_ipset": resourceCitrixAdcIpset(), + "citrixadc_route": resourceCitrixAdcRoute(), + "citrixadc_linkset": resourceCitrixAdcLinkset(), + "citrixadc_nsfeature": resourceCitrixAdcNsfeature(), + "citrixadc_systemuser": resourceCitrixAdcSystemuser(), + "citrixadc_systemgroup": resourceCitrixAdcSystemgroup(), + "citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(), + "citrixadc_interface": resourceCitrixAdcInterface(), + "citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(), + "citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(), + "citrixadc_nslicense": resourceCitrixAdcNslicense(), + "citrixadc_cluster": resourceCitrixAdcCluster(), + "citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(), + "citrixadc_systemfile": resourceCitrixAdcSystemfile(), + "citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(), + "citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(), + "citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(), + "citrixadc_rebooter": resourceCitrixAdcRebooter(), + "citrixadc_installer": resourceCitrixAdcInstaller(), + "citrixadc_pinger": resourceCitrixAdcPinger(), + "citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(), + "citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(), + "citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(), + "citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(), + "citrixadc_netprofile": resourceCitrixAdcNetprofile(), + "citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(), + "citrixadc_nsparam": resourceCitrixAdcNsparam(), + "citrixadc_sslvserver_sslpolicy_binding": resourceCitrixAdcSslvserver_sslpolicy_binding(), + "citrixadc_sslprofile_sslcipher_binding": resourceCitrixAdcSslprofile_sslcipher_binding(), + "citrixadc_policydataset": resourceCitrixAdcPolicydataset(), + "citrixadc_policydataset_value_binding": resourceCitrixAdcPolicydataset_value_binding(), + "citrixadc_password_resetter": resourceCitrixAdcPasswordResetter(), + "citrixadc_csvserver_cspolicy_binding": resourceCitrixAdcCsvserver_cspolicy_binding(), + "citrixadc_appfwprofile": resourceCitrixAdcAppfwprofile(), + "citrixadc_appfwpolicy": resourceCitrixAdcAppfwpolicy(), + "citrixadc_appfwfieldtype": resourceCitrixAdcAppfwfieldtype(), + "citrixadc_appfwpolicylabel": resourceCitrixAdcAppfwpolicylabel(), + "citrixadc_appfwjsoncontenttype": resourceCitrixAdcAppfwjsoncontenttype(), + "citrixadc_appfwxmlcontenttype": resourceCitrixAdcAppfwxmlcontenttype(), + "citrixadc_appfwprofile_starturl_binding": resourceCitrixAdcAppfwprofileStarturlBinding(), + "citrixadc_appfwprofile_denyurl_binding": resourceCitrixAdcAppfwprofileDenyurlBinding(), + "citrixadc_nslicenseserver": resourceCitrixAdcNslicenseserver(), + "citrixadc_nscapacity": resourceCitrixAdcNscapacity(), + "citrixadc_lbvserver_service_binding": resourceCitrixAdcLbvserver_service_binding(), + "citrixadc_policystringmap": resourceCitrixAdcPolicystringmap(), + "citrixadc_policystringmap_pattern_binding": resourceCitrixAdcPolicystringmap_pattern_binding(), } } diff --git a/citrixadc/resource_citrixadc_policystringmap.go b/citrixadc/resource_citrixadc_policystringmap.go new file mode 100644 index 000000000..25289c2ad --- /dev/null +++ b/citrixadc/resource_citrixadc_policystringmap.go @@ -0,0 +1,119 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/policy" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcPolicystringmap() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createPolicystringmapFunc, + Read: readPolicystringmapFunc, + Update: updatePolicystringmapFunc, + Delete: deletePolicystringmapFunc, + Schema: map[string]*schema.Schema{ + "comment": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func createPolicystringmapFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createPolicystringmapFunc") + client := meta.(*NetScalerNitroClient).client + + policystringmapName := d.Get("name").(string) + + policystringmap := policy.Policystringmap{ + Comment: d.Get("comment").(string), + Name: d.Get("name").(string), + } + + _, err := client.AddResource(netscaler.Policystringmap.Type(), policystringmapName, &policystringmap) + if err != nil { + return err + } + + d.SetId(policystringmapName) + + err = readPolicystringmapFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this policystringmap but we can't read it ?? %s", policystringmapName) + return nil + } + return nil +} + +func readPolicystringmapFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readPolicystringmapFunc") + client := meta.(*NetScalerNitroClient).client + policystringmapName := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading policystringmap state %s", policystringmapName) + data, err := client.FindResource(netscaler.Policystringmap.Type(), policystringmapName) + if err != nil { + log.Printf("[WARN] citrixadc-provider: Clearing policystringmap state %s", policystringmapName) + d.SetId("") + return nil + } + d.Set("comment", data["comment"]) + d.Set("name", data["name"]) + + return nil + +} + +func updatePolicystringmapFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updatePolicystringmapFunc") + client := meta.(*NetScalerNitroClient).client + policystringmapName := d.Get("name").(string) + + policystringmap := policy.Policystringmap{ + Name: d.Get("name").(string), + } + hasChange := false + if d.HasChange("comment") { + log.Printf("[DEBUG] citrixadc-provider: Comment has changed for policystringmap %s, starting update", policystringmapName) + policystringmap.Comment = d.Get("comment").(string) + hasChange = true + } + if d.HasChange("name") { + log.Printf("[DEBUG] citrixadc-provider: Name has changed for policystringmap %s, starting update", policystringmapName) + policystringmap.Name = d.Get("name").(string) + hasChange = true + } + + if hasChange { + _, err := client.UpdateResource(netscaler.Policystringmap.Type(), policystringmapName, &policystringmap) + if err != nil { + return fmt.Errorf("Error updating policystringmap %s", policystringmapName) + } + } + return readPolicystringmapFunc(d, meta) +} + +func deletePolicystringmapFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deletePolicystringmapFunc") + client := meta.(*NetScalerNitroClient).client + policystringmapName := d.Id() + err := client.DeleteResource(netscaler.Policystringmap.Type(), policystringmapName) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_policystringmap_pattern_binding.go b/citrixadc/resource_citrixadc_policystringmap_pattern_binding.go new file mode 100644 index 000000000..d7a08f6dd --- /dev/null +++ b/citrixadc/resource_citrixadc_policystringmap_pattern_binding.go @@ -0,0 +1,149 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/policy" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" + "strings" +) + +func resourceCitrixAdcPolicystringmap_pattern_binding() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createPolicystringmap_pattern_bindingFunc, + Read: readPolicystringmap_pattern_bindingFunc, + Delete: deletePolicystringmap_pattern_bindingFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "key": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "value": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func createPolicystringmap_pattern_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createPolicystringmap_pattern_bindingFunc") + client := meta.(*NetScalerNitroClient).client + name := d.Get("name").(string) + key := d.Get("key").(string) + bindingId := fmt.Sprintf("%s,%s", name, key) + policystringmap_pattern_binding := policy.Policystringmappatternbinding{ + Key: d.Get("key").(string), + Name: d.Get("name").(string), + Value: d.Get("value").(string), + } + + _, err := client.AddResource(netscaler.Policystringmap_pattern_binding.Type(), name, &policystringmap_pattern_binding) + if err != nil { + return err + } + + d.SetId(bindingId) + + err = readPolicystringmap_pattern_bindingFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this policystringmap_pattern_binding but we can't read it ?? %s", bindingId) + return nil + } + return nil +} + +func readPolicystringmap_pattern_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readPolicystringmap_pattern_bindingFunc") + client := meta.(*NetScalerNitroClient).client + bindingId := d.Id() + idSlice := strings.SplitN(bindingId, ",", 2) + name := idSlice[0] + key := idSlice[1] + log.Printf("[DEBUG] citrixadc-provider: Reading policystringmap_pattern_binding state %s", bindingId) + + findParams := netscaler.FindParams{ + ResourceType: "policystringmap_pattern_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 policystringmap_pattern_binding state %s", bindingId) + d.SetId("") + return nil + } + + // Iterate through results to find the one with the right monitor name + foundIndex := -1 + for i, v := range dataArr { + if v["key"].(string) == key { + foundIndex = i + break + } + } + + // Resource is missing + if foundIndex == -1 { + log.Printf("[DEBUG] citrixadc-provider: FindResourceArrayWithParams monitor name not found in array") + log.Printf("[WARN] citrixadc-provider: Clearing policystringmap_pattern_binding state %s", bindingId) + d.SetId("") + return nil + } + // Fallthrough + + data := dataArr[foundIndex] + + d.Set("name", data["name"]) + d.Set("key", data["key"]) + d.Set("value", data["value"]) + + return nil + +} + +func deletePolicystringmap_pattern_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deletePolicystringmap_pattern_bindingFunc") + client := meta.(*NetScalerNitroClient).client + + bindingId := d.Id() + idSlice := strings.SplitN(bindingId, ",", 2) + + name := idSlice[0] + key := idSlice[1] + + args := make([]string, 0) + args = append(args, fmt.Sprintf("key:%s", key)) + + err := client.DeleteResourceWithArgs("policystringmap_pattern_binding", name, args) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_policystringmap_pattern_binding_test.go b/citrixadc/resource_citrixadc_policystringmap_pattern_binding_test.go new file mode 100644 index 000000000..822e76d34 --- /dev/null +++ b/citrixadc/resource_citrixadc_policystringmap_pattern_binding_test.go @@ -0,0 +1,164 @@ +/* +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" + "strings" + "testing" +) + +const testAccPolicystringmap_pattern_binding_basic_step1 = ` + +resource "citrixadc_policystringmap" "tf_policystringmap" { + name = "tf_policystringmap" + comment = "Some comment" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind1" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key1" + value = "value1" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind2" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key2" + value = "value2" +} +` + +const testAccPolicystringmap_pattern_binding_basic_step2 = ` + +resource "citrixadc_policystringmap" "tf_policystringmap" { + name = "tf_policystringmap" + comment = "Some comment" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind1" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key1" + value = "value1_new" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind2" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key2" + value = "value2" +} +` + +func TestAccPolicystringmap_pattern_binding_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPolicystringmap_pattern_bindingDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccPolicystringmap_pattern_binding_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckPolicystringmap_pattern_bindingExist("citrixadc_policystringmap_pattern_binding.tf_bind1", nil), + testAccCheckPolicystringmap_pattern_bindingExist("citrixadc_policystringmap_pattern_binding.tf_bind2", nil), + ), + }, + resource.TestStep{ + Config: testAccPolicystringmap_pattern_binding_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckPolicystringmap_pattern_bindingExist("citrixadc_policystringmap_pattern_binding.tf_bind1", nil), + testAccCheckPolicystringmap_pattern_bindingExist("citrixadc_policystringmap_pattern_binding.tf_bind2", nil), + ), + }, + }, + }) +} + +func testAccCheckPolicystringmap_pattern_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 policystringmap_pattern_binding name 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] + key := idSlice[1] + + findParams := netscaler.FindParams{ + ResourceType: "policystringmap_pattern_binding", + ResourceName: name, + ResourceMissingErrorCode: 258, + } + dataArr, err := client.FindResourceArrayWithParams(findParams) + + // Unexpected error + if err != nil { + return err + } + + foundIndex := -1 + for i, v := range dataArr { + if v["key"].(string) == key { + foundIndex = i + break + } + } + if foundIndex == -1 { + return fmt.Errorf("Could not find binding %s", bindingId) + } + + return nil + } +} + +func testAccCheckPolicystringmap_pattern_bindingDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_policystringmap_pattern_binding" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Policystringmap_pattern_binding.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("policystringmap_pattern_binding %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/citrixadc/resource_citrixadc_policystringmap_test.go b/citrixadc/resource_citrixadc_policystringmap_test.go new file mode 100644 index 000000000..0f72ff50e --- /dev/null +++ b/citrixadc/resource_citrixadc_policystringmap_test.go @@ -0,0 +1,118 @@ +/* +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" +) + +const testAccPolicystringmap_basic_step1 = ` + +resource "citrixadc_policystringmap" "tf_policystringmap" { + name = "tf_policystringmap" + comment = "Some comment" +} +` + +const testAccPolicystringmap_basic_step2 = ` + +resource "citrixadc_policystringmap" "tf_policystringmap" { + name = "tf_policystringmap" + comment = "Some other comment" +} +` + +func TestAccPolicystringmap_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPolicystringmapDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccPolicystringmap_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckPolicystringmapExist("citrixadc_policystringmap.tf_policystringmap", nil), + ), + }, + resource.TestStep{ + Config: testAccPolicystringmap_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckPolicystringmapExist("citrixadc_policystringmap.tf_policystringmap", nil), + ), + }, + }, + }) +} + +func testAccCheckPolicystringmapExist(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 policystringmap 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.Policystringmap.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("policystringmap %s not found", n) + } + + return nil + } +} + +func testAccCheckPolicystringmapDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_policystringmap" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Policystringmap.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("policystringmap %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/examples/policystringmap/provider.tf b/examples/policystringmap/provider.tf new file mode 100644 index 000000000..e96880608 --- /dev/null +++ b/examples/policystringmap/provider.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + citrixadc = { + source = "citrix/citrixadc" + } + } +} +provider "citrixadc" { + endpoint = "http://localhost:8000" +} diff --git a/examples/policystringmap/resources.tf b/examples/policystringmap/resources.tf new file mode 100644 index 000000000..112031846 --- /dev/null +++ b/examples/policystringmap/resources.tf @@ -0,0 +1,16 @@ +resource "citrixadc_policystringmap" "tf_policystringmap" { + name = "tf_policystringmap" + comment = "Some comment" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind1" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key1" + value = "value1" +} + +resource "citrixadc_policystringmap_pattern_binding" "tf_bind2" { + name = citrixadc_policystringmap.tf_policystringmap.name + key = "key2" + value = "value2" +} From 508b6ea51464511753d1003d03656ea3a54a7ea9 Mon Sep 17 00:00:00 2001 From: George Nikolopoulos Date: Tue, 16 Feb 2021 15:39:01 +0200 Subject: [PATCH 2/3] Add transform resources Signed-off-by: George Nikolopoulos --- citrixadc/provider.go | 163 ++++++------ ...ixadc_csvserver_transformpolicy_binding.go | 205 +++++++++++++++ ..._csvserver_transformpolicy_binding_test.go | 182 +++++++++++++ ...ixadc_lbvserver_transformpolicy_binding.go | 199 ++++++++++++++ ..._lbvserver_transformpolicy_binding_test.go | 180 +++++++++++++ .../resource_citrixadc_transformaction.go | 245 ++++++++++++++++++ ...resource_citrixadc_transformaction_test.go | 168 ++++++++++++ .../resource_citrixadc_transformpolicy.go | 157 +++++++++++ ...resource_citrixadc_transformpolicy_test.go | 134 ++++++++++ .../resource_citrixadc_transformprofile.go | 170 ++++++++++++ ...esource_citrixadc_transformprofile_test.go | 140 ++++++++++ 11 files changed, 1864 insertions(+), 79 deletions(-) create mode 100644 citrixadc/resource_citrixadc_csvserver_transformpolicy_binding.go create mode 100644 citrixadc/resource_citrixadc_csvserver_transformpolicy_binding_test.go create mode 100644 citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding.go create mode 100644 citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding_test.go create mode 100644 citrixadc/resource_citrixadc_transformaction.go create mode 100644 citrixadc/resource_citrixadc_transformaction_test.go create mode 100644 citrixadc/resource_citrixadc_transformpolicy.go create mode 100644 citrixadc/resource_citrixadc_transformpolicy_test.go create mode 100644 citrixadc/resource_citrixadc_transformprofile.go create mode 100644 citrixadc/resource_citrixadc_transformprofile_test.go diff --git a/citrixadc/provider.go b/citrixadc/provider.go index 911f8e2a8..a08a6738d 100644 --- a/citrixadc/provider.go +++ b/citrixadc/provider.go @@ -78,85 +78,90 @@ func providerSchema() map[string]*schema.Schema { func providerResources() map[string]*schema.Resource { return map[string]*schema.Resource{ - "citrixadc_lbvserver": resourceCitrixAdcLbvserver(), - "citrixadc_service": resourceCitrixAdcService(), - "citrixadc_csvserver": resourceCitrixAdcCsvserver(), - "citrixadc_cspolicy": resourceCitrixAdcCspolicy(), - "citrixadc_csaction": resourceCitrixAdcCsaction(), - "citrixadc_sslaction": resourceCitrixAdcSslaction(), - "citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(), - "citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(), - "citrixadc_sslprofile": resourceCitrixAdcSslprofile(), - "citrixadc_sslparameter": resourceCitrixAdcSslparameter(), - "citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(), - "citrixadc_sslcipher": resourceCitrixAdcSslcipher(), - "citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(), - "citrixadc_servicegroup": resourceCitrixAdcServicegroup(), - "citrixadc_nsacl": resourceCitrixAdcNsacl(), - "citrixadc_nsacls": resourceCitrixAdcNsacls(), - "citrixadc_inat": resourceCitrixAdcInat(), - "citrixadc_rnat": resourceCitrixAdcRnats(), - "citrixadc_gslbsite": resourceCitrixAdcGslbsite(), - "citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(), - "citrixadc_gslbservice": resourceCitrixAdcGslbservice(), - "citrixadc_server": resourceCitrixAdcServer(), - "citrixadc_responderaction": resourceCitrixAdcResponderaction(), - "citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(), - "citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(), - "citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(), - "citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(), - "citrixadc_rewritepolicylabel": resourceCitrixAdcRewritepolicylabel(), - "citrixadc_nsip": resourceCitrixAdcNsip(), - "citrixadc_nsip6": resourceCitrixAdcNsip6(), - "citrixadc_nsconfig_save": resourceCitrixAdcNsconfigSave(), - "citrixadc_nsconfig_clear": resourceCitrixAdcNsconfigClear(), - "citrixadc_nsconfig_update": resourceCitrixAdcNsconfigUpdate(), - "citrixadc_ipset": resourceCitrixAdcIpset(), - "citrixadc_route": resourceCitrixAdcRoute(), - "citrixadc_linkset": resourceCitrixAdcLinkset(), - "citrixadc_nsfeature": resourceCitrixAdcNsfeature(), - "citrixadc_systemuser": resourceCitrixAdcSystemuser(), - "citrixadc_systemgroup": resourceCitrixAdcSystemgroup(), - "citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(), - "citrixadc_interface": resourceCitrixAdcInterface(), - "citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(), - "citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(), - "citrixadc_nslicense": resourceCitrixAdcNslicense(), - "citrixadc_cluster": resourceCitrixAdcCluster(), - "citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(), - "citrixadc_systemfile": resourceCitrixAdcSystemfile(), - "citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(), - "citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(), - "citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(), - "citrixadc_rebooter": resourceCitrixAdcRebooter(), - "citrixadc_installer": resourceCitrixAdcInstaller(), - "citrixadc_pinger": resourceCitrixAdcPinger(), - "citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(), - "citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(), - "citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(), - "citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(), - "citrixadc_netprofile": resourceCitrixAdcNetprofile(), - "citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(), - "citrixadc_nsparam": resourceCitrixAdcNsparam(), - "citrixadc_sslvserver_sslpolicy_binding": resourceCitrixAdcSslvserver_sslpolicy_binding(), - "citrixadc_sslprofile_sslcipher_binding": resourceCitrixAdcSslprofile_sslcipher_binding(), - "citrixadc_policydataset": resourceCitrixAdcPolicydataset(), - "citrixadc_policydataset_value_binding": resourceCitrixAdcPolicydataset_value_binding(), - "citrixadc_password_resetter": resourceCitrixAdcPasswordResetter(), - "citrixadc_csvserver_cspolicy_binding": resourceCitrixAdcCsvserver_cspolicy_binding(), - "citrixadc_appfwprofile": resourceCitrixAdcAppfwprofile(), - "citrixadc_appfwpolicy": resourceCitrixAdcAppfwpolicy(), - "citrixadc_appfwfieldtype": resourceCitrixAdcAppfwfieldtype(), - "citrixadc_appfwpolicylabel": resourceCitrixAdcAppfwpolicylabel(), - "citrixadc_appfwjsoncontenttype": resourceCitrixAdcAppfwjsoncontenttype(), - "citrixadc_appfwxmlcontenttype": resourceCitrixAdcAppfwxmlcontenttype(), - "citrixadc_appfwprofile_starturl_binding": resourceCitrixAdcAppfwprofileStarturlBinding(), - "citrixadc_appfwprofile_denyurl_binding": resourceCitrixAdcAppfwprofileDenyurlBinding(), - "citrixadc_nslicenseserver": resourceCitrixAdcNslicenseserver(), - "citrixadc_nscapacity": resourceCitrixAdcNscapacity(), - "citrixadc_lbvserver_service_binding": resourceCitrixAdcLbvserver_service_binding(), - "citrixadc_policystringmap": resourceCitrixAdcPolicystringmap(), - "citrixadc_policystringmap_pattern_binding": resourceCitrixAdcPolicystringmap_pattern_binding(), + "citrixadc_lbvserver": resourceCitrixAdcLbvserver(), + "citrixadc_service": resourceCitrixAdcService(), + "citrixadc_csvserver": resourceCitrixAdcCsvserver(), + "citrixadc_cspolicy": resourceCitrixAdcCspolicy(), + "citrixadc_csaction": resourceCitrixAdcCsaction(), + "citrixadc_sslaction": resourceCitrixAdcSslaction(), + "citrixadc_sslpolicy": resourceCitrixAdcSslpolicy(), + "citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(), + "citrixadc_sslprofile": resourceCitrixAdcSslprofile(), + "citrixadc_sslparameter": resourceCitrixAdcSslparameter(), + "citrixadc_ssldhparam": resourceCitrixAdcSsldhparam(), + "citrixadc_sslcipher": resourceCitrixAdcSslcipher(), + "citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(), + "citrixadc_servicegroup": resourceCitrixAdcServicegroup(), + "citrixadc_nsacl": resourceCitrixAdcNsacl(), + "citrixadc_nsacls": resourceCitrixAdcNsacls(), + "citrixadc_inat": resourceCitrixAdcInat(), + "citrixadc_rnat": resourceCitrixAdcRnats(), + "citrixadc_gslbsite": resourceCitrixAdcGslbsite(), + "citrixadc_gslbvserver": resourceCitrixAdcGslbvserver(), + "citrixadc_gslbservice": resourceCitrixAdcGslbservice(), + "citrixadc_server": resourceCitrixAdcServer(), + "citrixadc_responderaction": resourceCitrixAdcResponderaction(), + "citrixadc_responderpolicy": resourceCitrixAdcResponderpolicy(), + "citrixadc_responderpolicylabel": resourceCitrixAdcResponderpolicylabel(), + "citrixadc_rewriteaction": resourceCitrixAdcRewriteaction(), + "citrixadc_rewritepolicy": resourceCitrixAdcRewritepolicy(), + "citrixadc_rewritepolicylabel": resourceCitrixAdcRewritepolicylabel(), + "citrixadc_nsip": resourceCitrixAdcNsip(), + "citrixadc_nsip6": resourceCitrixAdcNsip6(), + "citrixadc_nsconfig_save": resourceCitrixAdcNsconfigSave(), + "citrixadc_nsconfig_clear": resourceCitrixAdcNsconfigClear(), + "citrixadc_nsconfig_update": resourceCitrixAdcNsconfigUpdate(), + "citrixadc_ipset": resourceCitrixAdcIpset(), + "citrixadc_route": resourceCitrixAdcRoute(), + "citrixadc_linkset": resourceCitrixAdcLinkset(), + "citrixadc_nsfeature": resourceCitrixAdcNsfeature(), + "citrixadc_systemuser": resourceCitrixAdcSystemuser(), + "citrixadc_systemgroup": resourceCitrixAdcSystemgroup(), + "citrixadc_systemcmdpolicy": resourceCitrixAdcSystemcmdpolicy(), + "citrixadc_interface": resourceCitrixAdcInterface(), + "citrixadc_nstcpprofile": resourceCitrixAdcNstcpprofile(), + "citrixadc_nshttpprofile": resourceCitrixAdcNshttpprofile(), + "citrixadc_nslicense": resourceCitrixAdcNslicense(), + "citrixadc_cluster": resourceCitrixAdcCluster(), + "citrixadc_clusterfiles_syncer": resourceCitrixAdcClusterfilesSyncer(), + "citrixadc_systemfile": resourceCitrixAdcSystemfile(), + "citrixadc_auditmessageaction": resourceCitrixAdcAuditmessageaction(), + "citrixadc_auditsyslogaction": resourceCitrixAdcAuditsyslogaction(), + "citrixadc_auditsyslogpolicy": resourceCitrixAdcAuditsyslogpolicy(), + "citrixadc_rebooter": resourceCitrixAdcRebooter(), + "citrixadc_installer": resourceCitrixAdcInstaller(), + "citrixadc_pinger": resourceCitrixAdcPinger(), + "citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(), + "citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(), + "citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(), + "citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(), + "citrixadc_netprofile": resourceCitrixAdcNetprofile(), + "citrixadc_servicegroup_lbmonitor_binding": resourceCitrixAdcServicegroup_lbmonitor_binding(), + "citrixadc_nsparam": resourceCitrixAdcNsparam(), + "citrixadc_sslvserver_sslpolicy_binding": resourceCitrixAdcSslvserver_sslpolicy_binding(), + "citrixadc_sslprofile_sslcipher_binding": resourceCitrixAdcSslprofile_sslcipher_binding(), + "citrixadc_policydataset": resourceCitrixAdcPolicydataset(), + "citrixadc_policydataset_value_binding": resourceCitrixAdcPolicydataset_value_binding(), + "citrixadc_password_resetter": resourceCitrixAdcPasswordResetter(), + "citrixadc_csvserver_cspolicy_binding": resourceCitrixAdcCsvserver_cspolicy_binding(), + "citrixadc_appfwprofile": resourceCitrixAdcAppfwprofile(), + "citrixadc_appfwpolicy": resourceCitrixAdcAppfwpolicy(), + "citrixadc_appfwfieldtype": resourceCitrixAdcAppfwfieldtype(), + "citrixadc_appfwpolicylabel": resourceCitrixAdcAppfwpolicylabel(), + "citrixadc_appfwjsoncontenttype": resourceCitrixAdcAppfwjsoncontenttype(), + "citrixadc_appfwxmlcontenttype": resourceCitrixAdcAppfwxmlcontenttype(), + "citrixadc_appfwprofile_starturl_binding": resourceCitrixAdcAppfwprofileStarturlBinding(), + "citrixadc_appfwprofile_denyurl_binding": resourceCitrixAdcAppfwprofileDenyurlBinding(), + "citrixadc_nslicenseserver": resourceCitrixAdcNslicenseserver(), + "citrixadc_nscapacity": resourceCitrixAdcNscapacity(), + "citrixadc_lbvserver_service_binding": resourceCitrixAdcLbvserver_service_binding(), + "citrixadc_policystringmap": resourceCitrixAdcPolicystringmap(), + "citrixadc_policystringmap_pattern_binding": resourceCitrixAdcPolicystringmap_pattern_binding(), + "citrixadc_transformprofile": resourceCitrixAdcTransformprofile(), + "citrixadc_transformaction": resourceCitrixAdcTransformaction(), + "citrixadc_transformpolicy": resourceCitrixAdcTransformpolicy(), + "citrixadc_lbvserver_transformpolicy_binding": resourceCitrixAdcLbvserver_transformpolicy_binding(), + "citrixadc_csvserver_transformpolicy_binding": resourceCitrixAdcCsvserver_transformpolicy_binding(), } } diff --git a/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding.go b/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding.go new file mode 100644 index 000000000..24f9e592f --- /dev/null +++ b/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding.go @@ -0,0 +1,205 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/cs" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" + "strings" +) + +func resourceCitrixAdcCsvserver_transformpolicy_binding() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createCsvserver_transformpolicy_bindingFunc, + Read: readCsvserver_transformpolicy_bindingFunc, + Delete: deleteCsvserver_transformpolicy_bindingFunc, + Schema: map[string]*schema.Schema{ + "bindpoint": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "gotopriorityexpression": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "invoke": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Computed: true, + ForceNew: true, + }, + "labelname": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "labeltype": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "policyname": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "priority": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + "targetlbvserver": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func createCsvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createCsvserver_transformpolicy_bindingFunc") + client := meta.(*NetScalerNitroClient).client + name := d.Get("name").(string) + policyname := d.Get("policyname").(string) + bindingId := fmt.Sprintf("%s,%s", name, policyname) + csvserver_transformpolicy_binding := cs.Csvservertransformpolicybinding{ + Bindpoint: d.Get("bindpoint").(string), + Gotopriorityexpression: d.Get("gotopriorityexpression").(string), + Invoke: d.Get("invoke").(bool), + Labelname: d.Get("labelname").(string), + Labeltype: d.Get("labeltype").(string), + Name: d.Get("name").(string), + Policyname: d.Get("policyname").(string), + Priority: d.Get("priority").(int), + Targetlbvserver: d.Get("targetlbvserver").(string), + } + + err := client.UpdateUnnamedResource(netscaler.Csvserver_transformpolicy_binding.Type(), &csvserver_transformpolicy_binding) + if err != nil { + return err + } + + d.SetId(bindingId) + + err = readCsvserver_transformpolicy_bindingFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this csvserver_transformpolicy_binding but we can't read it ?? %s", bindingId) + return nil + } + return nil +} + +func readCsvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readCsvserver_transformpolicy_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 csvserver_transformpolicy_binding state %v", bindingId) + + findParams := netscaler.FindParams{ + ResourceType: "csvserver_transformpolicy_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 csvserver_transformpolicy_binding state %s", bindingId) + d.SetId("") + return nil + } + + // Iterate through results to find the one with the right policy name + 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 monitor name not found in array") + log.Printf("[WARN] citrixadc-provider: Clearing csvserver_transformpolicy_binding state %s", bindingId) + d.SetId("") + return nil + } + // Fallthrough + + data := dataArr[foundIndex] + + d.Set("name", data["name"]) + d.Set("bindpoint", data["bindpoint"]) + d.Set("gotopriorityexpression", data["gotopriorityexpression"]) + d.Set("invoke", data["invoke"]) + d.Set("labelname", data["labelname"]) + d.Set("labeltype", data["labeltype"]) + d.Set("name", data["name"]) + d.Set("policyname", data["policyname"]) + d.Set("priority", data["priority"]) + d.Set("targetlbvserver", data["targetlbvserver"]) + + return nil + +} + +func deleteCsvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteCsvserver_transformpolicy_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)) + + if bindpoint, ok := d.GetOk("bindpoint"); ok { + args = append(args, fmt.Sprintf("bindpoint:%v", bindpoint)) + } + + if priority, ok := d.GetOk("priority"); ok { + args = append(args, fmt.Sprintf("priority:%v", priority)) + } + + err := client.DeleteResourceWithArgs("csvserver_transformpolicy_binding", name, args) + if err != nil { + return err + } + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding_test.go b/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding_test.go new file mode 100644 index 000000000..2ef23633c --- /dev/null +++ b/citrixadc/resource_citrixadc_csvserver_transformpolicy_binding_test.go @@ -0,0 +1,182 @@ +/* +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" + "strings" + "testing" +) + +const testAccCsvserver_transformpolicy_binding_basic_step1 = ` +resource "citrixadc_csvserver" "tf_csvserver" { + ipv46 = "10.10.10.34" + name = "tf_csvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_csvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_csvserver.tf_csvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 100 + bindpoint = "REQUEST" + gotopriorityexpression = "END" +} +` + +const testAccCsvserver_transformpolicy_binding_basic_step2 = ` +resource "citrixadc_csvserver" "tf_csvserver" { + ipv46 = "10.10.10.34" + name = "tf_csvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_csvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_csvserver.tf_csvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 110 + bindpoint = "REQUEST" + gotopriorityexpression = "NEXT" +} +` + +func TestAccCsvserver_transformpolicy_binding_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckCsvserver_transformpolicy_bindingDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCsvserver_transformpolicy_binding_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckCsvserver_transformpolicy_bindingExist("citrixadc_csvserver_transformpolicy_binding.tf_binding", nil), + ), + }, + resource.TestStep{ + Config: testAccCsvserver_transformpolicy_binding_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckCsvserver_transformpolicy_bindingExist("citrixadc_csvserver_transformpolicy_binding.tf_binding", nil), + ), + }, + }, + }) +} + +func testAccCheckCsvserver_transformpolicy_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 csvserver_transformpolicy_binding name 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 := netscaler.FindParams{ + ResourceType: "csvserver_transformpolicy_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 right policy name + foundIndex := -1 + for i, v := range dataArr { + if v["policyname"].(string) == policyname { + foundIndex = i + break + } + } + + // Resource is missing + if foundIndex == -1 { + return fmt.Errorf("csvserver_transformpolicy_binding %s not found", bindingId) + } + + return nil + } +} + +func testAccCheckCsvserver_transformpolicy_bindingDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_csvserver_transformpolicy_binding" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Csvserver_transformpolicy_binding.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("csvserver_transformpolicy_binding %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding.go b/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding.go new file mode 100644 index 000000000..67a64e262 --- /dev/null +++ b/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding.go @@ -0,0 +1,199 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/lb" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" + "strings" +) + +func resourceCitrixAdcLbvserver_transformpolicy_binding() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createLbvserver_transformpolicy_bindingFunc, + Read: readLbvserver_transformpolicy_bindingFunc, + Delete: deleteLbvserver_transformpolicy_bindingFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "bindpoint": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "gotopriorityexpression": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "invoke": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Computed: true, + ForceNew: true, + }, + "labelname": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "labeltype": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "policyname": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "priority": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func createLbvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createLbvserver_transformpolicy_bindingFunc") + client := meta.(*NetScalerNitroClient).client + name := d.Get("name").(string) + policyname := d.Get("policyname").(string) + bindingId := fmt.Sprintf("%s,%s", name, policyname) + lbvserver_transformpolicy_binding := lb.Lbvservertransformpolicybinding{ + Bindpoint: d.Get("bindpoint").(string), + Gotopriorityexpression: d.Get("gotopriorityexpression").(string), + Invoke: d.Get("invoke").(bool), + Labelname: d.Get("labelname").(string), + Labeltype: d.Get("labeltype").(string), + Name: d.Get("name").(string), + Policyname: d.Get("policyname").(string), + Priority: d.Get("priority").(int), + } + + err := client.UpdateUnnamedResource(netscaler.Lbvserver_transformpolicy_binding.Type(), &lbvserver_transformpolicy_binding) + if err != nil { + return err + } + + d.SetId(bindingId) + + err = readLbvserver_transformpolicy_bindingFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this lbvserver_transformpolicy_binding but we can't read it ?? %s", bindingId) + return nil + } + return nil +} + +func readLbvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readLbvserver_transformpolicy_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 lbvserver_transformpolicy_binding state %v", bindingId) + + findParams := netscaler.FindParams{ + ResourceType: "lbvserver_transformpolicy_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 lbvserver_transformpolicy_binding state %s", bindingId) + d.SetId("") + return nil + } + + // Iterate through results to find the one with the right policy name + 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 monitor name not found in array") + log.Printf("[WARN] citrixadc-provider: Clearing lbvserver_transformpolicy_binding state %s", bindingId) + d.SetId("") + return nil + } + // Fallthrough + + data := dataArr[foundIndex] + + d.Set("bindpoint", data["bindpoint"]) + d.Set("gotopriorityexpression", data["gotopriorityexpression"]) + d.Set("invoke", data["invoke"]) + d.Set("labelname", data["labelname"]) + d.Set("labeltype", data["labeltype"]) + d.Set("name", data["name"]) + d.Set("policyname", data["policyname"]) + d.Set("priority", data["priority"]) + + return nil + +} + +func deleteLbvserver_transformpolicy_bindingFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteLbvserver_transformpolicy_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)) + + if bindpoint, ok := d.GetOk("bindpoint"); ok { + args = append(args, fmt.Sprintf("bindpoint:%v", bindpoint)) + } + + if priority, ok := d.GetOk("priority"); ok { + args = append(args, fmt.Sprintf("priority:%v", priority)) + } + + err := client.DeleteResourceWithArgs("lbvserver_transformpolicy_binding", name, args) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding_test.go b/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding_test.go new file mode 100644 index 000000000..5e697e506 --- /dev/null +++ b/citrixadc/resource_citrixadc_lbvserver_transformpolicy_binding_test.go @@ -0,0 +1,180 @@ +/* +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" + "strings" + "testing" +) + +const testAccLbvserver_transformpolicy_binding_basic_step1 = ` +resource "citrixadc_lbvserver" "tf_lbvserver" { + ipv46 = "10.10.10.33" + name = "tf_lbvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_lbvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_lbvserver.tf_lbvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 100 + bindpoint = "REQUEST" + gotopriorityexpression = "END" +} +` + +const testAccLbvserver_transformpolicy_binding_basic_step2 = ` +resource "citrixadc_lbvserver" "tf_lbvserver" { + ipv46 = "10.10.10.33" + name = "tf_lbvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_lbvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_lbvserver.tf_lbvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 110 + bindpoint = "REQUEST" + gotopriorityexpression = "NEXT" +} +` + +func TestAccLbvserver_transformpolicy_binding_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLbvserver_transformpolicy_bindingDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccLbvserver_transformpolicy_binding_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckLbvserver_transformpolicy_bindingExist("citrixadc_lbvserver_transformpolicy_binding.tf_binding", nil), + ), + }, + resource.TestStep{ + Config: testAccLbvserver_transformpolicy_binding_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckLbvserver_transformpolicy_bindingExist("citrixadc_lbvserver_transformpolicy_binding.tf_binding", nil), + ), + }, + }, + }) +} + +func testAccCheckLbvserver_transformpolicy_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 lbvserver_transformpolicy_binding name 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 := netscaler.FindParams{ + ResourceType: "lbvserver_transformpolicy_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 right policy name + foundIndex := -1 + for i, v := range dataArr { + if v["policyname"].(string) == policyname { + foundIndex = i + break + } + } + + // Resource is missing + if foundIndex == -1 { + return fmt.Errorf("lbvserver_transformpolicy_binding %s not found", bindingId) + } + return nil + } +} + +func testAccCheckLbvserver_transformpolicy_bindingDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_lbvserver_transformpolicy_binding" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Lbvserver_transformpolicy_binding.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("lbvserver_transformpolicy_binding %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformaction.go b/citrixadc/resource_citrixadc_transformaction.go new file mode 100644 index 000000000..615bfe5e1 --- /dev/null +++ b/citrixadc/resource_citrixadc_transformaction.go @@ -0,0 +1,245 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/transform" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcTransformaction() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createTransformactionFunc, + Read: readTransformactionFunc, + Update: updateTransformactionFunc, + Delete: deleteTransformactionFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "comment": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "cookiedomainfrom": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "cookiedomaininto": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "priority": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "profilename": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "requrlfrom": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "requrlinto": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resurlfrom": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "resurlinto": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "state": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func createTransformactionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createTransformactionFunc") + client := meta.(*NetScalerNitroClient).client + transformactionName := d.Get("name").(string) + + // Create does not support all attributes + transformactionNew := transform.Transformaction{ + Name: d.Get("name").(string), + Priority: d.Get("priority").(int), + Profilename: d.Get("profilename").(string), + State: d.Get("state").(string), + } + + _, err := client.AddResource(netscaler.Transformaction.Type(), transformactionName, &transformactionNew) + if err != nil { + return err + } + + // Need to update with full set of attributes + transformaction := transform.Transformaction{ + Comment: d.Get("comment").(string), + Cookiedomainfrom: d.Get("cookiedomainfrom").(string), + Cookiedomaininto: d.Get("cookiedomaininto").(string), + Name: d.Get("name").(string), + Priority: d.Get("priority").(int), + Requrlfrom: d.Get("requrlfrom").(string), + Requrlinto: d.Get("requrlinto").(string), + Resurlfrom: d.Get("resurlfrom").(string), + Resurlinto: d.Get("resurlinto").(string), + State: d.Get("state").(string), + } + + _, err = client.UpdateResource(netscaler.Transformaction.Type(), transformactionName, &transformaction) + if err != nil { + return err + } + + d.SetId(transformactionName) + + err = readTransformactionFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this transformaction but we can't read it ?? %s", transformactionName) + return nil + } + return nil +} + +func readTransformactionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readTransformactionFunc") + client := meta.(*NetScalerNitroClient).client + transformactionName := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading transformaction state %s", transformactionName) + data, err := client.FindResource(netscaler.Transformaction.Type(), transformactionName) + if err != nil { + log.Printf("[WARN] citrixadc-provider: Clearing transformaction state %s", transformactionName) + d.SetId("") + return nil + } + d.Set("name", data["name"]) + d.Set("comment", data["comment"]) + d.Set("cookiedomainfrom", data["cookiedomainfrom"]) + d.Set("cookiedomaininto", data["cookiedomaininto"]) + d.Set("name", data["name"]) + d.Set("priority", data["priority"]) + d.Set("profilename", data["profilename"]) + d.Set("requrlfrom", data["requrlfrom"]) + d.Set("requrlinto", data["requrlinto"]) + d.Set("resurlfrom", data["resurlfrom"]) + d.Set("resurlinto", data["resurlinto"]) + d.Set("state", data["state"]) + + return nil + +} + +func updateTransformactionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updateTransformactionFunc") + client := meta.(*NetScalerNitroClient).client + transformactionName := d.Get("name").(string) + + transformaction := transform.Transformaction{ + Name: d.Get("name").(string), + } + hasChange := false + if d.HasChange("comment") { + log.Printf("[DEBUG] citrixadc-provider: Comment has changed for transformaction %s, starting update", transformactionName) + transformaction.Comment = d.Get("comment").(string) + hasChange = true + } + if d.HasChange("cookiedomainfrom") { + log.Printf("[DEBUG] citrixadc-provider: Cookiedomainfrom has changed for transformaction %s, starting update", transformactionName) + transformaction.Cookiedomainfrom = d.Get("cookiedomainfrom").(string) + hasChange = true + } + if d.HasChange("cookiedomaininto") { + log.Printf("[DEBUG] citrixadc-provider: Cookiedomaininto has changed for transformaction %s, starting update", transformactionName) + transformaction.Cookiedomaininto = d.Get("cookiedomaininto").(string) + hasChange = true + } + if d.HasChange("name") { + log.Printf("[DEBUG] citrixadc-provider: Name has changed for transformaction %s, starting update", transformactionName) + transformaction.Name = d.Get("name").(string) + hasChange = true + } + if d.HasChange("priority") { + log.Printf("[DEBUG] citrixadc-provider: Priority has changed for transformaction %s, starting update", transformactionName) + transformaction.Priority = d.Get("priority").(int) + hasChange = true + } + if d.HasChange("profilename") { + log.Printf("[DEBUG] citrixadc-provider: Profilename has changed for transformaction %s, starting update", transformactionName) + transformaction.Profilename = d.Get("profilename").(string) + hasChange = true + } + if d.HasChange("requrlfrom") { + log.Printf("[DEBUG] citrixadc-provider: Requrlfrom has changed for transformaction %s, starting update", transformactionName) + transformaction.Requrlfrom = d.Get("requrlfrom").(string) + hasChange = true + } + if d.HasChange("requrlinto") { + log.Printf("[DEBUG] citrixadc-provider: Requrlinto has changed for transformaction %s, starting update", transformactionName) + transformaction.Requrlinto = d.Get("requrlinto").(string) + hasChange = true + } + if d.HasChange("resurlfrom") { + log.Printf("[DEBUG] citrixadc-provider: Resurlfrom has changed for transformaction %s, starting update", transformactionName) + transformaction.Resurlfrom = d.Get("resurlfrom").(string) + hasChange = true + } + if d.HasChange("resurlinto") { + log.Printf("[DEBUG] citrixadc-provider: Resurlinto has changed for transformaction %s, starting update", transformactionName) + transformaction.Resurlinto = d.Get("resurlinto").(string) + hasChange = true + } + if d.HasChange("state") { + log.Printf("[DEBUG] citrixadc-provider: State has changed for transformaction %s, starting update", transformactionName) + transformaction.State = d.Get("state").(string) + hasChange = true + } + + if hasChange { + _, err := client.UpdateResource(netscaler.Transformaction.Type(), transformactionName, &transformaction) + if err != nil { + return fmt.Errorf("Error updating transformaction %s", transformactionName) + } + } + return readTransformactionFunc(d, meta) +} + +func deleteTransformactionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteTransformactionFunc") + client := meta.(*NetScalerNitroClient).client + transformactionName := d.Id() + err := client.DeleteResource(netscaler.Transformaction.Type(), transformactionName) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformaction_test.go b/citrixadc/resource_citrixadc_transformaction_test.go new file mode 100644 index 000000000..2eab6eb94 --- /dev/null +++ b/citrixadc/resource_citrixadc_transformaction_test.go @@ -0,0 +1,168 @@ +/* +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" +) + +const testAccTransformaction_basic_step1 = ` +resource "citrixadc_transformprofile" "tf_trans_profile1" { + name = "tf_trans_profile1" +} + +resource "citrixadc_transformprofile" "tf_trans_profile2" { + name = "tf_trans_profile2" +} + +resource "citrixadc_transformaction" "tf_trans_action" { + name = "tf_trans_action" + profilename = citrixadc_transformprofile.tf_trans_profile1.name + priority = 100 + requrlfrom = "http://m3.mydomain.com/(.*)" + requrlinto = "https://exp-proxy-v1.api.mydomain.com/$1" + resurlfrom = "https://exp-proxy-v1.api.mydomain.com/(.*)" + resurlinto = "https://m3.mydomain.com/$1" +} +` + +const testAccTransformaction_basic_step2 = ` +resource "citrixadc_transformprofile" "tf_trans_profile1" { + name = "tf_trans_profile1" +} + +resource "citrixadc_transformprofile" "tf_trans_profile2" { + name = "tf_trans_profile2" +} + +resource "citrixadc_transformaction" "tf_trans_action" { + name = "tf_trans_action" + profilename = citrixadc_transformprofile.tf_trans_profile2.name + priority = 100 + requrlfrom = "http://m4.mydomain.com/(.*)" + requrlinto = "https://exp-proxy-v1.api.mydomain.com/$1" + resurlfrom = "https://exp-proxy-v1.api.mydomain.com/(.*)" + resurlinto = "https://m4.mydomain.com/$1" +} +` + +const testAccTransformaction_basic_step3 = ` +resource "citrixadc_transformprofile" "tf_trans_profile1" { + name = "tf_trans_profile1" +} + +resource "citrixadc_transformprofile" "tf_trans_profile2" { + name = "tf_trans_profile2" +} + +resource "citrixadc_transformaction" "tf_trans_action" { + name = "tf_trans_action" + profilename = citrixadc_transformprofile.tf_trans_profile2.name + priority = 110 + requrlfrom = "http://m5.mydomain.com/(.*)" + requrlinto = "https://exp-proxy-v1.api.mydomain.com/$1" + resurlfrom = "https://exp-proxy-v1.api.mydomain.com/(.*)" + resurlinto = "https://m5.mydomain.com/$1" +} +` + +func TestAccTransformaction_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckTransformactionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTransformaction_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformactionExist("citrixadc_transformaction.tf_trans_action", nil), + ), + }, + resource.TestStep{ + Config: testAccTransformaction_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformactionExist("citrixadc_transformaction.tf_trans_action", nil), + ), + }, + resource.TestStep{ + Config: testAccTransformaction_basic_step3, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformactionExist("citrixadc_transformaction.tf_trans_action", nil), + ), + }, + }, + }) +} + +func testAccCheckTransformactionExist(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 transformaction 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.Transformaction.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("transformaction %s not found", n) + } + + return nil + } +} + +func testAccCheckTransformactionDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_transformaction" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Transformaction.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("transformaction %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformpolicy.go b/citrixadc/resource_citrixadc_transformpolicy.go new file mode 100644 index 000000000..570160d70 --- /dev/null +++ b/citrixadc/resource_citrixadc_transformpolicy.go @@ -0,0 +1,157 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/transform" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcTransformpolicy() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createTransformpolicyFunc, + Read: readTransformpolicyFunc, + Update: updateTransformpolicyFunc, + Delete: deleteTransformpolicyFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "comment": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "logaction": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "profilename": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "rule": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func createTransformpolicyFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createTransformpolicyFunc") + client := meta.(*NetScalerNitroClient).client + transformpolicyName := d.Get("name").(string) + transformpolicy := transform.Transformpolicy{ + Comment: d.Get("comment").(string), + Logaction: d.Get("logaction").(string), + Name: d.Get("name").(string), + Profilename: d.Get("profilename").(string), + Rule: d.Get("rule").(string), + } + + _, err := client.AddResource(netscaler.Transformpolicy.Type(), transformpolicyName, &transformpolicy) + if err != nil { + return err + } + + d.SetId(transformpolicyName) + + err = readTransformpolicyFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this transformpolicy but we can't read it ?? %s", transformpolicyName) + return nil + } + return nil +} + +func readTransformpolicyFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readTransformpolicyFunc") + client := meta.(*NetScalerNitroClient).client + transformpolicyName := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading transformpolicy state %s", transformpolicyName) + data, err := client.FindResource(netscaler.Transformpolicy.Type(), transformpolicyName) + if err != nil { + log.Printf("[WARN] citrixadc-provider: Clearing transformpolicy state %s", transformpolicyName) + d.SetId("") + return nil + } + d.Set("comment", data["comment"]) + d.Set("logaction", data["logaction"]) + d.Set("name", data["name"]) + d.Set("profilename", data["profilename"]) + d.Set("rule", data["rule"]) + + return nil + +} + +func updateTransformpolicyFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updateTransformpolicyFunc") + client := meta.(*NetScalerNitroClient).client + transformpolicyName := d.Get("name").(string) + + transformpolicy := transform.Transformpolicy{ + Name: d.Get("name").(string), + } + hasChange := false + if d.HasChange("comment") { + log.Printf("[DEBUG] citrixadc-provider: Comment has changed for transformpolicy %s, starting update", transformpolicyName) + transformpolicy.Comment = d.Get("comment").(string) + hasChange = true + } + if d.HasChange("logaction") { + log.Printf("[DEBUG] citrixadc-provider: Logaction has changed for transformpolicy %s, starting update", transformpolicyName) + transformpolicy.Logaction = d.Get("logaction").(string) + hasChange = true + } + if d.HasChange("name") { + log.Printf("[DEBUG] citrixadc-provider: Name has changed for transformpolicy %s, starting update", transformpolicyName) + transformpolicy.Name = d.Get("name").(string) + hasChange = true + } + if d.HasChange("profilename") { + log.Printf("[DEBUG] citrixadc-provider: Profilename has changed for transformpolicy %s, starting update", transformpolicyName) + transformpolicy.Profilename = d.Get("profilename").(string) + hasChange = true + } + if d.HasChange("rule") { + log.Printf("[DEBUG] citrixadc-provider: Rule has changed for transformpolicy %s, starting update", transformpolicyName) + transformpolicy.Rule = d.Get("rule").(string) + hasChange = true + } + + if hasChange { + _, err := client.UpdateResource(netscaler.Transformpolicy.Type(), transformpolicyName, &transformpolicy) + if err != nil { + return fmt.Errorf("Error updating transformpolicy %s", transformpolicyName) + } + } + return readTransformpolicyFunc(d, meta) +} + +func deleteTransformpolicyFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteTransformpolicyFunc") + client := meta.(*NetScalerNitroClient).client + transformpolicyName := d.Id() + err := client.DeleteResource(netscaler.Transformpolicy.Type(), transformpolicyName) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformpolicy_test.go b/citrixadc/resource_citrixadc_transformpolicy_test.go new file mode 100644 index 000000000..33f0c45ac --- /dev/null +++ b/citrixadc/resource_citrixadc_transformpolicy_test.go @@ -0,0 +1,134 @@ +/* +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" +) + +const testAccTransformpolicy_basic_step1 = ` +resource "citrixadc_transformprofile" "tf_trans_profile1" { + name = "tf_trans_profile1" +} + +resource "citrixadc_transformprofile" "tf_trans_profile2" { + name = "tf_trans_profile2" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile1.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} +` + +const testAccTransformpolicy_basic_step2 = ` +resource "citrixadc_transformprofile" "tf_trans_profile1" { + name = "tf_trans_profile1" +} + +resource "citrixadc_transformprofile" "tf_trans_profile2" { + name = "tf_trans_profile2" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile2.name + rule = "http.REQ.URL.CONTAINS(\"test_url_other\")" +} +` + +func TestAccTransformpolicy_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckTransformpolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTransformpolicy_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformpolicyExist("citrixadc_transformpolicy.tf_trans_policy", nil), + ), + }, + resource.TestStep{ + Config: testAccTransformpolicy_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformpolicyExist("citrixadc_transformpolicy.tf_trans_policy", nil), + ), + }, + }, + }) +} + +func testAccCheckTransformpolicyExist(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 transformpolicy 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.Transformpolicy.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("transformpolicy %s not found", n) + } + + return nil + } +} + +func testAccCheckTransformpolicyDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_transformpolicy" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Transformpolicy.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("transformpolicy %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformprofile.go b/citrixadc/resource_citrixadc_transformprofile.go new file mode 100644 index 000000000..76eeb0e76 --- /dev/null +++ b/citrixadc/resource_citrixadc_transformprofile.go @@ -0,0 +1,170 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/transform" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcTransformprofile() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createTransformprofileFunc, + Read: readTransformprofileFunc, + Update: updateTransformprofileFunc, + Delete: deleteTransformprofileFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "comment": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "onlytransformabsurlinbody": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func createTransformprofileFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createTransformprofileFunc") + client := meta.(*NetScalerNitroClient).client + transformprofileName := d.Get("name").(string) + + transformprofileNew := transform.Transformprofile{ + Name: d.Get("name").(string), + Type: d.Get("type").(string), + } + + _, err := client.AddResource(netscaler.Transformprofile.Type(), transformprofileName, &transformprofileNew) + if err != nil { + return err + } + + // Need to also update to include the parameters that are + // invalid for the create operation + transformprofile := transform.Transformprofile{ + Comment: d.Get("comment").(string), + Name: d.Get("name").(string), + Onlytransformabsurlinbody: d.Get("onlytransformabsurlinbody").(string), + Type: d.Get("type").(string), + } + + // Update will fail if only the name attribute is present + doUpdate := false + if transformprofile.Comment != "" { + doUpdate = true + } + if transformprofile.Onlytransformabsurlinbody != "" { + doUpdate = true + } + if transformprofile.Type != "" { + doUpdate = true + } + if doUpdate { + _, err := client.UpdateResource(netscaler.Transformprofile.Type(), transformprofileName, &transformprofile) + if err != nil { + return err + } + } + + d.SetId(transformprofileName) + + err = readTransformprofileFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this transformprofile but we can't read it ?? %s", transformprofileName) + return nil + } + return nil +} + +func readTransformprofileFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readTransformprofileFunc") + client := meta.(*NetScalerNitroClient).client + transformprofileName := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading transformprofile state %s", transformprofileName) + data, err := client.FindResource(netscaler.Transformprofile.Type(), transformprofileName) + if err != nil { + log.Printf("[WARN] citrixadc-provider: Clearing transformprofile state %s", transformprofileName) + d.SetId("") + return nil + } + d.Set("comment", data["comment"]) + d.Set("name", data["name"]) + d.Set("onlytransformabsurlinbody", data["onlytransformabsurlinbody"]) + d.Set("type", data["type"]) + + return nil + +} + +func updateTransformprofileFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updateTransformprofileFunc") + client := meta.(*NetScalerNitroClient).client + transformprofileName := d.Get("name").(string) + + transformprofile := transform.Transformprofile{ + Name: d.Get("name").(string), + } + hasChange := false + if d.HasChange("comment") { + log.Printf("[DEBUG] citrixadc-provider: Comment has changed for transformprofile %s, starting update", transformprofileName) + transformprofile.Comment = d.Get("comment").(string) + hasChange = true + } + if d.HasChange("name") { + log.Printf("[DEBUG] citrixadc-provider: Name has changed for transformprofile %s, starting update", transformprofileName) + transformprofile.Name = d.Get("name").(string) + hasChange = true + } + if d.HasChange("onlytransformabsurlinbody") { + log.Printf("[DEBUG] citrixadc-provider: Onlytransformabsurlinbody has changed for transformprofile %s, starting update", transformprofileName) + transformprofile.Onlytransformabsurlinbody = d.Get("onlytransformabsurlinbody").(string) + hasChange = true + } + if d.HasChange("type") { + log.Printf("[DEBUG] citrixadc-provider: Type has changed for transformprofile %s, starting update", transformprofileName) + transformprofile.Type = d.Get("type").(string) + hasChange = true + } + + if hasChange { + _, err := client.UpdateResource(netscaler.Transformprofile.Type(), transformprofileName, &transformprofile) + if err != nil { + return fmt.Errorf("Error updating transformprofile %s", transformprofileName) + } + } + return readTransformprofileFunc(d, meta) +} + +func deleteTransformprofileFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteTransformprofileFunc") + client := meta.(*NetScalerNitroClient).client + transformprofileName := d.Id() + err := client.DeleteResource(netscaler.Transformprofile.Type(), transformprofileName) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_transformprofile_test.go b/citrixadc/resource_citrixadc_transformprofile_test.go new file mode 100644 index 000000000..1fec00b29 --- /dev/null +++ b/citrixadc/resource_citrixadc_transformprofile_test.go @@ -0,0 +1,140 @@ +/* +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" +) + +const testAccTransformprofile_basic_step1 = ` +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" + onlytransformabsurlinbody = "ON" +} +` + +const testAccTransformprofile_basic_step2 = ` +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some other comment" + onlytransformabsurlinbody = "OFF" +} +` + +const testAccTransformprofile_lean = ` +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" +} +` + +func TestAccTransformprofile_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckTransformprofileDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTransformprofile_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformprofileExist("citrixadc_transformprofile.tf_trans_profile", nil), + ), + }, + resource.TestStep{ + Config: testAccTransformprofile_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformprofileExist("citrixadc_transformprofile.tf_trans_profile", nil), + ), + }, + }, + }) +} + +func TestAccTransformprofile_lean(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckTransformprofileDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTransformprofile_lean, + Check: resource.ComposeTestCheckFunc( + testAccCheckTransformprofileExist("citrixadc_transformprofile.tf_trans_profile", nil), + ), + }, + }, + }) +} + +func testAccCheckTransformprofileExist(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 transformprofile 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.Transformprofile.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("transformprofile %s not found", n) + } + + return nil + } +} + +func testAccCheckTransformprofileDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_transformprofile" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(netscaler.Transformprofile.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("transformprofile %s still exists", rs.Primary.ID) + } + + } + + return nil +} From bfd15f99eb42a97a37f52833770bcf795ffcca42 Mon Sep 17 00:00:00 2001 From: George Nikolopoulos Date: Tue, 16 Feb 2021 15:41:07 +0200 Subject: [PATCH 3/3] Add transform examples Signed-off-by: George Nikolopoulos --- .../provider.tf | 10 ++++++++ .../resources.tf | 25 +++++++++++++++++++ .../provider.tf | 10 ++++++++ .../resources.tf | 25 +++++++++++++++++++ examples/transformpolicy/provider.tf | 10 ++++++++ examples/transformpolicy/resources.tf | 20 +++++++++++++++ 6 files changed, 100 insertions(+) create mode 100644 examples/csvserver_transformpolicy_binding/provider.tf create mode 100644 examples/csvserver_transformpolicy_binding/resources.tf create mode 100644 examples/lbvserver_transformpolicy_binding/provider.tf create mode 100644 examples/lbvserver_transformpolicy_binding/resources.tf create mode 100644 examples/transformpolicy/provider.tf create mode 100644 examples/transformpolicy/resources.tf diff --git a/examples/csvserver_transformpolicy_binding/provider.tf b/examples/csvserver_transformpolicy_binding/provider.tf new file mode 100644 index 000000000..e96880608 --- /dev/null +++ b/examples/csvserver_transformpolicy_binding/provider.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + citrixadc = { + source = "citrix/citrixadc" + } + } +} +provider "citrixadc" { + endpoint = "http://localhost:8000" +} diff --git a/examples/csvserver_transformpolicy_binding/resources.tf b/examples/csvserver_transformpolicy_binding/resources.tf new file mode 100644 index 000000000..f8bab477b --- /dev/null +++ b/examples/csvserver_transformpolicy_binding/resources.tf @@ -0,0 +1,25 @@ +resource "citrixadc_csvserver" "tf_csvserver" { + ipv46 = "10.10.10.33" + name = "tf_csvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_csvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_csvserver.tf_csvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 100 + bindpoint = "REQUEST" + gotopriorityexpression = "END" +} diff --git a/examples/lbvserver_transformpolicy_binding/provider.tf b/examples/lbvserver_transformpolicy_binding/provider.tf new file mode 100644 index 000000000..e96880608 --- /dev/null +++ b/examples/lbvserver_transformpolicy_binding/provider.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + citrixadc = { + source = "citrix/citrixadc" + } + } +} +provider "citrixadc" { + endpoint = "http://localhost:8000" +} diff --git a/examples/lbvserver_transformpolicy_binding/resources.tf b/examples/lbvserver_transformpolicy_binding/resources.tf new file mode 100644 index 000000000..cf647858d --- /dev/null +++ b/examples/lbvserver_transformpolicy_binding/resources.tf @@ -0,0 +1,25 @@ +resource "citrixadc_lbvserver" "tf_lbvserver" { + ipv46 = "10.10.10.33" + name = "tf_lbvserver" + port = 80 + servicetype = "HTTP" +} + +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +} + +resource "citrixadc_lbvserver_transformpolicy_binding" "tf_binding" { + name = citrixadc_lbvserver.tf_lbvserver.name + policyname = citrixadc_transformpolicy.tf_trans_policy.name + priority = 100 + bindpoint = "REQUEST" + gotopriorityexpression = "END" +} diff --git a/examples/transformpolicy/provider.tf b/examples/transformpolicy/provider.tf new file mode 100644 index 000000000..e96880608 --- /dev/null +++ b/examples/transformpolicy/provider.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + citrixadc = { + source = "citrix/citrixadc" + } + } +} +provider "citrixadc" { + endpoint = "http://localhost:8000" +} diff --git a/examples/transformpolicy/resources.tf b/examples/transformpolicy/resources.tf new file mode 100644 index 000000000..0446d516f --- /dev/null +++ b/examples/transformpolicy/resources.tf @@ -0,0 +1,20 @@ +resource "citrixadc_transformprofile" "tf_trans_profile" { + name = "tf_trans_profile" + comment = "Some comment" +} + +resource "citrixadc_transformaction" "tf_trans_action1" { + name = "tf_trans_action1" + profilename = citrixadc_transformprofile.tf_trans_profile.name + priority = 100 + requrlfrom = "http://m3.mydomain.com/(.*)" + requrlinto = "https://exp-proxy-v1.api.mydomain.com/$1" + resurlfrom = "https://exp-proxy-v1.api.mydomain.com/(.*)" + resurlinto = "https://m3.mydomain.com/$1" +} + +resource "citrixadc_transformpolicy" "tf_trans_policy" { + name = "tf_trans_policy" + profilename = citrixadc_transformprofile.tf_trans_profile.name + rule = "http.REQ.URL.CONTAINS(\"test_url\")" +}