Skip to content

Commit

Permalink
Merge pull request #77 from citrix/sumanth-csaction
Browse files Browse the repository at this point in the history
Sumanth csaction
  • Loading branch information
George Nikolopoulos authored Mar 9, 2020
2 parents e789978 + 372e1ac commit e2252d0
Show file tree
Hide file tree
Showing 11 changed files with 660 additions and 1 deletion.
1 change: 1 addition & 0 deletions citrixadc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func providerResources() map[string]*schema.Resource {
"citrixadc_service": resourceCitrixAdcService(),
"citrixadc_csvserver": resourceCitrixAdcCsvserver(),
"citrixadc_cspolicy": resourceCitrixAdcCspolicy(),
"citrixadc_csaction": resourceCitrixAdcCsaction(),
"citrixadc_sslcertkey": resourceCitrixAdcSslcertkey(),
"citrixadc_lbmonitor": resourceCitrixAdcLbmonitor(),
"citrixadc_servicegroup": resourceCitrixAdcServicegroup(),
Expand Down
156 changes: 156 additions & 0 deletions citrixadc/resource_citrixadc_csaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package citrixadc

import (
"github.com/chiradeep/go-nitro/config/cs"

"github.com/chiradeep/go-nitro/netscaler"
"github.com/hashicorp/terraform/helper/schema"

"fmt"
"log"
)

func resourceCitrixAdcCsaction() *schema.Resource {
return &schema.Resource{
SchemaVersion: 1,
Create: createCsactionFunc,
Read: readCsactionFunc,
Update: updateCsactionFunc,
Delete: deleteCsactionFunc,
Schema: map[string]*schema.Schema{
"comment": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"targetlbvserver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"targetvserver": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"targetvserverexpr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}

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

csaction := cs.Csaction{
Comment: d.Get("comment").(string),
Name: d.Get("name").(string),
Targetlbvserver: d.Get("targetlbvserver").(string),
Targetvserver: d.Get("targetvserver").(string),
Targetvserverexpr: d.Get("targetvserverexpr").(string),
}

_, err := client.AddResource(netscaler.Csaction.Type(), csactionName, &csaction)
if err != nil {
return err
}

d.SetId(csactionName)

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

func readCsactionFunc(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] citrixadc-provider: In readCsactionFunc")
client := meta.(*NetScalerNitroClient).client
csactionName := d.Id()
log.Printf("[DEBUG] citrixadc-provider: Reading csaction state %s", csactionName)
data, err := client.FindResource(netscaler.Csaction.Type(), csactionName)
if err != nil {
log.Printf("[WARN] citrixadc-provider: Clearing csaction state %s", csactionName)
d.SetId("")
return nil
}
d.Set("name", data["name"])
d.Set("comment", data["comment"])
d.Set("targetlbvserver", data["targetlbvserver"])
d.Set("targetvserver", data["targetvserver"])
d.Set("targetvserverexpr", data["targetvserverexpr"])

return nil

}

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

csaction := cs.Csaction{
Name: d.Get("name").(string),
}
hasChange := false
if d.HasChange("comment") {
log.Printf("[DEBUG] citrixadc-provider: Comment has changed for csaction %s, starting update", csactionName)
csaction.Comment = d.Get("comment").(string)
hasChange = true
}
if d.HasChange("name") {
log.Printf("[DEBUG] citrixadc-provider: Name has changed for csaction %s, starting update", csactionName)
csaction.Name = d.Get("name").(string)
hasChange = true
}
if d.HasChange("targetlbvserver") {
log.Printf("[DEBUG] citrixadc-provider: Targetlbvserver has changed for csaction %s, starting update", csactionName)
csaction.Targetlbvserver = d.Get("targetlbvserver").(string)
hasChange = true
}
if d.HasChange("targetvserver") {
log.Printf("[DEBUG] citrixadc-provider: Targetvserver has changed for csaction %s, starting update", csactionName)
csaction.Targetvserver = d.Get("targetvserver").(string)
hasChange = true
}
if d.HasChange("targetvserverexpr") {
log.Printf("[DEBUG] citrixadc-provider: Targetvserverexpr has changed for csaction %s, starting update", csactionName)
csaction.Targetvserverexpr = d.Get("targetvserverexpr").(string)
hasChange = true
}

if hasChange {
_, err := client.UpdateResource(netscaler.Csaction.Type(), csactionName, &csaction)
if err != nil {
return fmt.Errorf("Error updating csaction %s", csactionName)
}
}
return readCsactionFunc(d, meta)
}

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

d.SetId("")

return nil
}
197 changes: 197 additions & 0 deletions citrixadc/resource_citrixadc_csaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
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"
"testing"

"github.com/chiradeep/go-nitro/netscaler"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

const testAccCsaction_create = `
resource "citrixadc_csaction" "foo" {
name = "tf_test_csaction"
targetlbvserver = citrixadc_lbvserver.tf_image_lb.name
comment = "Forwards image requests to the image_lb"
}
resource "citrixadc_lbvserver" "tf_image_lb" {
name = "image_lb"
ipv46 = "10.0.2.5"
port = "80"
servicetype = "HTTP"
}
resource "citrixadc_lbvserver" "tf_video_lb" {
name = "video_lb"
ipv46 = "10.0.2.6"
port = "80"
servicetype = "HTTP"
}
`

const testAccCsaction_update = `
resource "citrixadc_csaction" "foo" {
name = "tf_test_csaction"
targetlbvserver = citrixadc_lbvserver.tf_video_lb.name
comment = "Forwards video requests to the video_lb"
}
resource "citrixadc_lbvserver" "tf_image_lb" {
name = "image_lb"
ipv46 = "10.0.2.5"
port = "80"
servicetype = "HTTP"
}
resource "citrixadc_lbvserver" "tf_video_lb" {
name = "video_lb"
ipv46 = "10.0.2.6"
port = "80"
servicetype = "HTTP"
}
`

const testAccCsaction_update_name = `
resource "citrixadc_csaction" "foo" {
name = "tf_test_csaction_newname"
targetlbvserver = citrixadc_lbvserver.tf_image_lb.name
comment = "Forwards video requests to the image_lb"
}
resource "citrixadc_lbvserver" "tf_image_lb" {
name = "image_lb"
ipv46 = "10.0.2.5"
port = "80"
servicetype = "HTTP"
}
`
func TestAccCsaction_create_update(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCsactionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCsaction_create,
Check: resource.ComposeTestCheckFunc(
testAccCheckCsactionExist("citrixadc_csaction.foo", nil),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "name", "tf_test_csaction"),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "targetlbvserver", "image_lb"),
),
},
resource.TestStep{
Config: testAccCsaction_update,
Check: resource.ComposeTestCheckFunc(
testAccCheckCsactionExist("citrixadc_csaction.foo", nil),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "name", "tf_test_csaction"),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "targetlbvserver", "video_lb"),
),
},
},
})
}

func TestAccCsaction_create_update_name(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCsactionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCsaction_create,
Check: resource.ComposeTestCheckFunc(
testAccCheckCsactionExist("citrixadc_csaction.foo", nil),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "name", "tf_test_csaction"),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "targetlbvserver", "image_lb"),
),
},
resource.TestStep{
Config: testAccCsaction_update_name,
Check: resource.ComposeTestCheckFunc(
testAccCheckCsactionExist("citrixadc_csaction.foo", nil),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "name", "tf_test_csaction_newname"),
resource.TestCheckResourceAttr("citrixadc_csaction.foo", "targetlbvserver", "image_lb"),
),
},
},
})
}

func testAccCheckCsactionExist(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.Csaction.Type(), rs.Primary.ID)

if err != nil {
return err
}

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

return nil
}
}

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

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

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

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

}

return nil
}
3 changes: 3 additions & 0 deletions examples/csaction/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "citrixadc" {
endpoint = "http://localhost:8080"
}
20 changes: 20 additions & 0 deletions examples/csaction/resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "citrixadc_csaction" "tf_csaction" {
name = "tf_test_csaction2"
targetlbvserver = citrixadc_lbvserver.tf_image_lb.name
comment = "Forwards image requests to the image_lb"
}

resource "citrixadc_lbvserver" "tf_image_lb" {
name = "image_lb"
ipv46 = "10.0.2.5"
port = "80"
servicetype = "HTTP"
}

# For update, change the `targetlbvserser` accordingly
resource "citrixadc_lbvserver" "tf_image_lb2" {
name = "image_lb2"
ipv46 = "10.0.2.6"
port = "80"
servicetype = "HTTP"
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
Loading

0 comments on commit e2252d0

Please sign in to comment.