Skip to content

Commit

Permalink
Merge pull request #98 from citrix/nsrpcnode
Browse files Browse the repository at this point in the history
Add resource nsrpcnode
  • Loading branch information
George Nikolopoulos authored May 15, 2020
2 parents d0311ea + a499985 commit a597460
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 0 deletions.
1 change: 1 addition & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_rebooter": resourceCitrixAdcRebooter(),
"citrixadc_installer": resourceCitrixAdcInstaller(),
"citrixadc_pinger": resourceCitrixAdcPinger(),
"citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(),
}
}

Expand Down
147 changes: 147 additions & 0 deletions citrixadc/resource_citrixadc_nsrpcnode.go
Original file line number Diff line number Diff line change
@@ -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
}
102 changes: 102 additions & 0 deletions citrixadc/resource_citrixadc_nsrpcnode_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
`
3 changes: 3 additions & 0 deletions examples/nsrpcnode/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "citrixadc" {
endpoint = "http://localhost:8080"
}
6 changes: 6 additions & 0 deletions examples/nsrpcnode/resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "citrixadc_nsrpcnode" "tf_nsrpcnode" {
ipaddress = "10.78.60.201"
password = "secret"
secure = "ON"
srcip = "10.78.60.201"
}

0 comments on commit a597460

Please sign in to comment.