Skip to content

Commit

Permalink
Merge pull request #103 from citrix/new_resources
Browse files Browse the repository at this point in the history
New resources
  • Loading branch information
George Nikolopoulos authored Jun 22, 2020
2 parents ee0e0a7 + 2aff42e commit b8cbbcc
Show file tree
Hide file tree
Showing 25 changed files with 783 additions and 0 deletions.
2 changes: 2 additions & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_pinger": resourceCitrixAdcPinger(),
"citrixadc_nsrpcnode": resourceCitrixAdcNsrpcnode(),
"citrixadc_routerdynamicrouting": resourceCitrixAdcRouterdynamicrouting(),
"citrixadc_policyexpression": resourceCitrixAdcPolicyexpression(),
"citrixadc_systemextramgmtcpu": resourceCitrixAdcSystemextramgmtcpu(),
}
}

Expand Down
155 changes: 155 additions & 0 deletions citrixadc/resource_citrixadc_policyexpression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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 resourceCitrixAdcPolicyexpression() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createPolicyexpressionFunc,
Read: readPolicyexpressionFunc,
Update: updatePolicyexpressionFunc,
Delete: deletePolicyexpressionFunc,
Schema: map[string]*schema.Schema{
"clientsecuritymessage": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"comment": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}

func createPolicyexpressionFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In createPolicyexpressionFunc")
client := meta.(*NetScalerNitroClient).client
policyexpressionName := d.Get("name").(string)
policyexpression := policy.Policyexpression{
Clientsecuritymessage: d.Get("clientsecuritymessage").(string),
Comment: d.Get("comment").(string),
Description: d.Get("description").(string),
Name: d.Get("name").(string),
Value: d.Get("value").(string),
}

_, err := client.AddResource(netscaler.Policyexpression.Type(), policyexpressionName, &policyexpression)
if err != nil {
return err
}

d.SetId(policyexpressionName)

err = readPolicyexpressionFunc(d, meta)
if err != nil {
log.Printf("[ERROR] netscaler-provider: ?? we just created this policyexpression but we can't read it ?? %s", policyexpressionName)
return nil
}
return nil
}

func readPolicyexpressionFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readPolicyexpressionFunc")
client := meta.(*NetScalerNitroClient).client
policyexpressionName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading policyexpression state %s", policyexpressionName)
data, err := client.FindResource(netscaler.Policyexpression.Type(), policyexpressionName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing policyexpression state %s", policyexpressionName)
d.SetId("")
return nil
}
d.Set("name", data["name"])
d.Set("clientsecuritymessage", data["clientsecuritymessage"])
d.Set("comment", data["comment"])
d.Set("description", data["description"])
d.Set("name", data["name"])
d.Set("value", data["value"])

return nil

}

func updatePolicyexpressionFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In updatePolicyexpressionFunc")
client := meta.(*NetScalerNitroClient).client
policyexpressionName := d.Get("name").(string)

policyexpression := policy.Policyexpression{
Name: d.Get("name").(string),
}
hasChange := false
if d.HasChange("clientsecuritymessage") {
log.Printf("[DEBUG] citrixadc-provider: Clientsecuritymessage has changed for policyexpression %s, starting update", policyexpressionName)
policyexpression.Clientsecuritymessage = d.Get("clientsecuritymessage").(string)
hasChange = true
}
if d.HasChange("comment") {
log.Printf("[DEBUG] citrixadc-provider: Comment has changed for policyexpression %s, starting update", policyexpressionName)
policyexpression.Comment = d.Get("comment").(string)
hasChange = true
}
if d.HasChange("description") {
log.Printf("[DEBUG] citrixadc-provider: Description has changed for policyexpression %s, starting update", policyexpressionName)
policyexpression.Description = d.Get("description").(string)
hasChange = true
}
if d.HasChange("name") {
log.Printf("[DEBUG] citrixadc-provider: Name has changed for policyexpression %s, starting update", policyexpressionName)
policyexpression.Name = d.Get("name").(string)
hasChange = true
}
if d.HasChange("value") {
log.Printf("[DEBUG] citrixadc-provider: Value has changed for policyexpression %s, starting update", policyexpressionName)
policyexpression.Value = d.Get("value").(string)
hasChange = true
}

if hasChange {
_, err := client.UpdateResource(netscaler.Policyexpression.Type(), policyexpressionName, &policyexpression)
if err != nil {
return fmt.Errorf("Error updating policyexpression %s: %s", policyexpressionName, err.Error())
}
}
return readPolicyexpressionFunc(d, meta)
}

func deletePolicyexpressionFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In deletePolicyexpressionFunc")
client := meta.(*NetScalerNitroClient).client
policyexpressionName := d.Id()
err := client.DeleteResource(netscaler.Policyexpression.Type(), policyexpressionName)
if err != nil {
return err
}

d.SetId("")

return nil
}
190 changes: 190 additions & 0 deletions citrixadc/resource_citrixadc_policyexpression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
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 TestAccPolicyexpression_advanced(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPolicyexpressionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPolicyexpression_advanced_step1,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_advanced_policyexpression", nil),
),
},
resource.TestStep{
Config: testAccPolicyexpression_advanced_step2,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_advanced_policyexpression", nil),
),
},
resource.TestStep{
Config: testAccPolicyexpression_advanced_step3,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_advanced_policyexpression", nil),
),
},
},
})
}

func TestAccPolicyexpression_classic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPolicyexpressionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccPolicyexpression_classic_step1,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_classic_policyexpression", nil),
),
},
resource.TestStep{
Config: testAccPolicyexpression_classic_step2,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_classic_policyexpression", nil),
),
},
resource.TestStep{
Config: testAccPolicyexpression_classic_step3,
Check: resource.ComposeTestCheckFunc(
testAccCheckPolicyexpressionExist("citrixadc_policyexpression.tf_classic_policyexpression", nil),
),
},
},
})
}

func testAccCheckPolicyexpressionExist(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 lb vserver name is set")
}

if id != nil {
if *id != "" && *id != rs.Primary.ID {
return fmt.Errorf("Resource ID has changed!")
}

*id = rs.Primary.ID
}

nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client
data, err := nsClient.FindResource(netscaler.Policyexpression.Type(), rs.Primary.ID)

if err != nil {
return err
}

if data == nil {
return fmt.Errorf("LB vserver %s not found", n)
}

return nil
}
}

func testAccCheckPolicyexpressionDestroy(s *terraform.State) error {
nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client

for _, rs := range s.RootModule().Resources {
if rs.Type != "citrixadc_policyexpression" {
continue
}

if rs.Primary.ID == "" {
return fmt.Errorf("No name is set")
}

_, err := nsClient.FindResource(netscaler.Policyexpression.Type(), rs.Primary.ID)
if err == nil {
return fmt.Errorf("LB vserver %s still exists", rs.Primary.ID)
}

}

return nil
}

const testAccPolicyexpression_advanced_step1 = `
resource "citrixadc_policyexpression" "tf_advanced_policyexpression" {
name = "tf_advanced_policyexrpession"
value = "HTTP.REQ.URL.SUFFIX.EQ(\"cgi\")"
comment = "comment"
}
`

const testAccPolicyexpression_advanced_step2 = `
resource "citrixadc_policyexpression" "tf_advanced_policyexpression" {
name = "tf_advanced_policyexrpession"
value = "HTTP.REQ.URL.SUFFIX.EQ(\"cginew\")"
comment = "comment"
}
`

const testAccPolicyexpression_advanced_step3 = `
resource "citrixadc_policyexpression" "tf_advanced_policyexpression" {
name = "tf_advanced_policyexrpession_new"
value = "HTTP.REQ.URL.SUFFIX.EQ(\"cginew\")"
comment = "comment"
}
`

const testAccPolicyexpression_classic_step1 = `
resource "citrixadc_policyexpression" "tf_classic_policyexpression" {
name = "tf_classic_policyexrpession"
value = "HEADER Cookie EXISTS"
clientsecuritymessage = "security message"
}
`

const testAccPolicyexpression_classic_step2 = `
resource "citrixadc_policyexpression" "tf_classic_policyexpression" {
name = "tf_classic_policyexrpession"
value = "METHOD != GET"
clientsecuritymessage = "security message"
}
`

const testAccPolicyexpression_classic_step3 = `
resource "citrixadc_policyexpression" "tf_classic_policyexpression" {
name = "tf_classic_policyexrpession_new"
value = "METHOD != GET"
clientsecuritymessage = "new security message"
}
`
Loading

0 comments on commit b8cbbcc

Please sign in to comment.