diff --git a/citrixadc/provider.go b/citrixadc/provider.go index 47f6a21b9..8fcca5b8b 100644 --- a/citrixadc/provider.go +++ b/citrixadc/provider.go @@ -131,6 +131,7 @@ func providerResources() map[string]*schema.Resource { "citrixadc_rebooter": resourceCitrixAdcRebooter(), "citrixadc_installer": resourceCitrixAdcInstaller(), "citrixadc_pinger": resourceCitrixAdcPinger(), + "citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(), } } diff --git a/citrixadc/resource_citrixadc_nsrpcnode.go b/citrixadc/resource_citrixadc_nsrpcnode.go new file mode 100644 index 000000000..271c8ac20 --- /dev/null +++ b/citrixadc/resource_citrixadc_nsrpcnode.go @@ -0,0 +1,147 @@ +package citrixadc + +import ( + "github.com/chiradeep/go-nitro/config/ns" + + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcNsrpcnode() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createNsrpcnodeFunc, + Read: readNsrpcnodeFunc, + Update: updateNsrpcnodeFunc, + Delete: deleteNsrpcnodeFunc, + Schema: map[string]*schema.Schema{ + "ipaddress": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "password": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + Sensitive: true, + }, + "secure": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "srcip": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func createNsrpcnodeFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createNsrpcnodeFunc") + client := meta.(*NetScalerNitroClient).client + nsrpcnodeIpaddress := d.Get("ipaddress").(string) + + nsrpcnode := ns.Nsrpcnode{ + Ipaddress: d.Get("ipaddress").(string), + Password: d.Get("password").(string), + Secure: d.Get("secure").(string), + Srcip: d.Get("srcip").(string), + } + + err := client.UpdateUnnamedResource(netscaler.Nsrpcnode.Type(), &nsrpcnode) + if err != nil { + return err + } + + d.SetId(nsrpcnodeIpaddress) + + err = readNsrpcnodeFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this nsrpcnode but we can't read it ?? %s", nsrpcnodeIpaddress) + return nil + } + return nil +} + +func readNsrpcnodeFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readNsrpcnodeFunc") + client := meta.(*NetScalerNitroClient).client + nsrpcnodeIpaddress := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading nsrpcnode state %s", nsrpcnodeIpaddress) + findParams := netscaler.FindParams{ + ResourceType: "nsrpcnode", + ResourceName: nsrpcnodeIpaddress, + } + dataArray, err := client.FindResourceArrayWithParams(findParams) + if err != nil { + log.Printf("[DEBUG] citrixadc-provider: Read error %s", err.Error()) + log.Printf("[WARN] citrixadc-provider: Clearing nsrpcnode state %s", nsrpcnodeIpaddress) + d.SetId("") + return nil + } + + if len(dataArray) != 1 { + return fmt.Errorf("[ERROR] Read multiple nsprcnode instances %v", dataArray) + } + data := dataArray[0] + + d.Set("ipaddress", data["ipaddress"]) + // Password read is a random string that changes contantly + //d.Set("password", data["password"]) + d.Set("secure", data["secure"]) + d.Set("srcip", data["srcip"]) + + return nil + +} + +func updateNsrpcnodeFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updateNsrpcnodeFunc") + client := meta.(*NetScalerNitroClient).client + nsrpcnodeIpaddress := d.Get("ipaddress").(string) + + nsrpcnode := ns.Nsrpcnode{ + Ipaddress: nsrpcnodeIpaddress, + } + hasChange := false + if d.HasChange("password") { + log.Printf("[DEBUG] citrixadc-provider: Password has changed for nsrpcnode %s, starting update", nsrpcnodeIpaddress) + nsrpcnode.Password = d.Get("password").(string) + hasChange = true + } + if d.HasChange("secure") { + log.Printf("[DEBUG] citrixadc-provider: Secure has changed for nsrpcnode %s, starting update", nsrpcnodeIpaddress) + nsrpcnode.Secure = d.Get("secure").(string) + hasChange = true + } + if d.HasChange("srcip") { + log.Printf("[DEBUG] citrixadc-provider: Srcip has changed for nsrpcnode %s, starting update", nsrpcnodeIpaddress) + nsrpcnode.Srcip = d.Get("srcip").(string) + hasChange = true + } + + if hasChange { + err := client.UpdateUnnamedResource(netscaler.Nsrpcnode.Type(), &nsrpcnode) + if err != nil { + return fmt.Errorf("Error updating nsrpcnode %s. %s", nsrpcnodeIpaddress, err.Error()) + } + } + return readNsrpcnodeFunc(d, meta) +} + +func deleteNsrpcnodeFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteNsrpcnodeFunc") + // Rpc node always exists in ADC + // Just remove the reference from local state + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_nsrpcnode_test.go b/citrixadc/resource_citrixadc_nsrpcnode_test.go new file mode 100644 index 000000000..9865bc4a5 --- /dev/null +++ b/citrixadc/resource_citrixadc_nsrpcnode_test.go @@ -0,0 +1,102 @@ +/* +Copyright 2016 Citrix Systems, Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package citrixadc + +import ( + "fmt" + "github.com/chiradeep/go-nitro/netscaler" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "testing" +) + +func TestAccNsrpcnode_basic(t *testing.T) { + if isCpxRun { + t.Skip("Operation not permitted under CPX") + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccNsrpcnode_basic_step1, + Check: resource.ComposeTestCheckFunc( + testAccCheckNsrpcnodeExist("citrixadc_nsrpcnode.tf_nsrpcnode", nil), + ), + }, + resource.TestStep{ + Config: testAccNsrpcnode_basic_step2, + Check: resource.ComposeTestCheckFunc( + testAccCheckNsrpcnodeExist("citrixadc_nsrpcnode.tf_nsrpcnode", nil), + ), + }, + }, + }) +} + +func testAccCheckNsrpcnodeExist(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 id 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.Nsrpcnode.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("RPC node %s not found", n) + } + + return nil + } +} + +const testAccNsrpcnode_basic_step1 = ` + +resource "citrixadc_nsrpcnode" "tf_nsrpcnode" { + ipaddress = "10.78.60.201" + password = "notnsroot" + secure = "ON" + srcip = "10.78.60.201" +} +` + +const testAccNsrpcnode_basic_step2 = ` + +resource "citrixadc_nsrpcnode" "tf_nsrpcnode" { + ipaddress = "10.78.60.201" + password = "notnsroot" + secure = "OFF" + srcip = "10.78.60.201" +} +` diff --git a/examples/nsrpcnode/provider.tf b/examples/nsrpcnode/provider.tf new file mode 100644 index 000000000..3d4508593 --- /dev/null +++ b/examples/nsrpcnode/provider.tf @@ -0,0 +1,3 @@ +provider "citrixadc" { + endpoint = "http://localhost:8080" +} diff --git a/examples/nsrpcnode/resources.tf b/examples/nsrpcnode/resources.tf new file mode 100644 index 000000000..ebd495cf4 --- /dev/null +++ b/examples/nsrpcnode/resources.tf @@ -0,0 +1,6 @@ +resource "citrixadc_nsrpcnode" "tf_nsrpcnode" { + ipaddress = "10.78.60.201" + password = "secret" + secure = "ON" + srcip = "10.78.60.201" +}