-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from citrix/nsrpcnode
Add resource nsrpcnode
- Loading branch information
Showing
5 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "citrixadc" { | ||
endpoint = "http://localhost:8080" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |