diff --git a/citrixadc/provider.go b/citrixadc/provider.go index 1bac7b8c5..7eb0c7dad 100644 --- a/citrixadc/provider.go +++ b/citrixadc/provider.go @@ -438,6 +438,7 @@ func providerResources() map[string]*schema.Resource { "citrixadc_botprofile_trapinsertionurl_binding": resourceCitrixAdcBotprofile_trapinsertionurl_binding(), "citrixadc_botprofile_logexpression_binding": resourceCitrixAdcBotprofile_logexpression_binding(), "citrixadc_botprofile_whitelist_binding": resourceCitrixAdcBotprofile_whitelist_binding(), + "citrixadc_filteraction": resourceCitrixAdcFilteraction(), } } diff --git a/citrixadc/resource_citrixadc_filteraction.go b/citrixadc/resource_citrixadc_filteraction.go new file mode 100644 index 000000000..a9b0eec94 --- /dev/null +++ b/citrixadc/resource_citrixadc_filteraction.go @@ -0,0 +1,165 @@ +package citrixadc + +import ( + "github.com/citrix/adc-nitro-go/resource/config/filter" + + "github.com/citrix/adc-nitro-go/service" + "github.com/hashicorp/terraform/helper/schema" + + "fmt" + "log" +) + +func resourceCitrixAdcFilteraction() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + Create: createFilteractionFunc, + Read: readFilteractionFunc, + Update: updateFilteractionFunc, + Delete: deleteFilteractionFunc, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Computed: false, + ForceNew: true, + }, + "qual": &schema.Schema{ + Type: schema.TypeString, + Required: true, + Computed: false, + }, + "page": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "respcode": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "servicename": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "value": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + } +} + +func createFilteractionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In createFilteractionFunc") + client := meta.(*NetScalerNitroClient).client + filteractionName := d.Get("name").(string) + filteraction := filter.Filteraction{ + Name: d.Get("name").(string), + Page: d.Get("page").(string), + Qual: d.Get("qual").(string), + Respcode: d.Get("respcode").(int), + Servicename: d.Get("servicename").(string), + Value: d.Get("value").(string), + } + + _, err := client.AddResource(service.Filteraction.Type(), filteractionName, &filteraction) + if err != nil { + return err + } + + d.SetId(filteractionName) + + err = readFilteractionFunc(d, meta) + if err != nil { + log.Printf("[ERROR] netscaler-provider: ?? we just created this filteraction but we can't read it ?? %s", filteractionName) + return nil + } + return nil +} + +func readFilteractionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In readFilteractionFunc") + client := meta.(*NetScalerNitroClient).client + filteractionName := d.Id() + log.Printf("[DEBUG] citrixadc-provider: Reading filteraction state %s", filteractionName) + data, err := client.FindResource(service.Filteraction.Type(), filteractionName) + if err != nil { + log.Printf("[WARN] citrixadc-provider: Clearing filteraction state %s", filteractionName) + d.SetId("") + return nil + } + d.Set("name", data["name"]) + d.Set("page", data["page"]) + d.Set("qual", data["qual"]) + d.Set("respcode", data["respcode"]) + d.Set("servicename", data["servicename"]) + d.Set("value", data["value"]) + + return nil + +} + +func updateFilteractionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In updateFilteractionFunc") + client := meta.(*NetScalerNitroClient).client + filteractionName := d.Get("name").(string) + + filteraction := filter.Filteraction{ + Name: d.Get("name").(string), + } + hasChange := false + if d.HasChange("page") { + log.Printf("[DEBUG] citrixadc-provider: Page has changed for filteraction %s, starting update", filteractionName) + filteraction.Page = d.Get("page").(string) + hasChange = true + } + if d.HasChange("qual") { + log.Printf("[DEBUG] citrixadc-provider: Qual has changed for filteraction %s, starting update", filteractionName) + filteraction.Qual = d.Get("qual").(string) + hasChange = true + } + if d.HasChange("respcode") { + log.Printf("[DEBUG] citrixadc-provider: Respcode has changed for filteraction %s, starting update", filteractionName) + filteraction.Respcode = d.Get("respcode").(int) + hasChange = true + } + if d.HasChange("servicename") { + log.Printf("[DEBUG] citrixadc-provider: Servicename has changed for filteraction %s, starting update", filteractionName) + filteraction.Servicename = d.Get("servicename").(string) + hasChange = true + } + if d.HasChange("value") { + log.Printf("[DEBUG] citrixadc-provider: Value has changed for filteraction %s, starting update", filteractionName) + filteraction.Value = d.Get("value").(string) + hasChange = true + } + + if hasChange { + _, err := client.UpdateResource(service.Filteraction.Type(), filteractionName, &filteraction) + if err != nil { + return fmt.Errorf("Error updating filteraction %s", filteractionName) + } + } + return readFilteractionFunc(d, meta) +} + +func deleteFilteractionFunc(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] citrixadc-provider: In deleteFilteractionFunc") + client := meta.(*NetScalerNitroClient).client + filteractionName := d.Id() + err := client.DeleteResource(service.Filteraction.Type(), filteractionName) + if err != nil { + return err + } + + d.SetId("") + + return nil +} diff --git a/citrixadc/resource_citrixadc_filteraction_test.go b/citrixadc/resource_citrixadc_filteraction_test.go new file mode 100644 index 000000000..378fc7f51 --- /dev/null +++ b/citrixadc/resource_citrixadc_filteraction_test.go @@ -0,0 +1,123 @@ +/* +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/citrix/adc-nitro-go/service" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "testing" +) + +const testAccFilteraction_add = ` + + resource "citrixadc_filteraction" "tf_filteraction" { + name = "tf_filteraction" + qual = "corrupt" + value = "X-Forwarded-For" + } +` +const testAccFilteraction_update = ` + + resource "citrixadc_filteraction" "tf_filteraction" { + name = "tf_filteraction" + qual = "corrupt" + value = "X-Forwarded" + } +` + +func TestAccFilteraction_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckFilteractionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccFilteraction_add, + Check: resource.ComposeTestCheckFunc( + testAccCheckFilteractionExist("citrixadc_filteraction.tf_filteraction", nil), + resource.TestCheckResourceAttr("citrixadc_filteraction.tf_filteraction", "name", "tf_filteraction"), + resource.TestCheckResourceAttr("citrixadc_filteraction.tf_filteraction", "value", "X-Forwarded-For"), + ), + }, + resource.TestStep{ + Config: testAccFilteraction_update, + Check: resource.ComposeTestCheckFunc( + testAccCheckFilteractionExist("citrixadc_filteraction.tf_filteraction", nil), + resource.TestCheckResourceAttr("citrixadc_filteraction.tf_filteraction", "name", "tf_filteraction"), + resource.TestCheckResourceAttr("citrixadc_filteraction.tf_filteraction", "value", "X-Forwarded"), + ), + }, + }, + }) +} + +func testAccCheckFilteractionExist(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 filteraction 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(service.Filteraction.Type(), rs.Primary.ID) + + if err != nil { + return err + } + + if data == nil { + return fmt.Errorf("filteraction %s not found", n) + } + + return nil + } +} + +func testAccCheckFilteractionDestroy(s *terraform.State) error { + nsClient := testAccProvider.Meta().(*NetScalerNitroClient).client + + for _, rs := range s.RootModule().Resources { + if rs.Type != "citrixadc_filteraction" { + continue + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No name is set") + } + + _, err := nsClient.FindResource(service.Filteraction.Type(), rs.Primary.ID) + if err == nil { + return fmt.Errorf("filteraction %s still exists", rs.Primary.ID) + } + + } + + return nil +} diff --git a/docs/resources/filteraction.md b/docs/resources/filteraction.md new file mode 100644 index 000000000..af11c3193 --- /dev/null +++ b/docs/resources/filteraction.md @@ -0,0 +1,44 @@ +--- +subcategory: "Filter" +--- + +# Resource: filteraction + +The filteraction resource is used to create Filter Action Resource. + + +## Example usage + +```hcl +resource "citrixadc_filteraction" "tf_filteraction" { + name = "tf_filteraction" + qual = "corrupt" + value = "X-Forwarded-For" +} +``` + + +## Argument Reference + +* `name` - (Required) Name for the filtering action. Must begin with a letter, number, or the underscore character (_). Other characters allowed, after the first character, are the hyphen (-), period (.) hash (#), space ( ), at sign (@), equals (=), and colon (:) characters. Choose a name that helps identify the type of action. The name of a filter action cannot be changed after it is created. CLI Users: If the name includes one or more spaces, enclose the name in double or single quotation marks (for example, "my action" or 'my action'). +* `qual` - (Required) Qualifier, which is the action to be performed. The qualifier cannot be changed after it is set. The available options function as follows: ADD - Adds the specified HTTP header. RESET - Terminates the connection, sending the appropriate termination notice to the user's browser. FORWARD - Redirects the request to the designated service. You must specify either a service name or a page, but not both. DROP - Silently deletes the request, without sending a response to the user's browser. CORRUPT - Modifies the designated HTTP header to prevent it from performing the function it was intended to perform, then sends the request/response to the server/browser. ERRORCODE. Returns the designated HTTP error code to the user's browser (for example, 404, the standard HTTP code for a non-existent Web page). +* `page` - (Optional) HTML page to return for HTTP requests (For use with the ERRORCODE qualifier). +* `respcode` - (Optional) Response code to be returned for HTTP requests (for use with the ERRORCODE qualifier). +* `servicename` - (Optional) Service to which to forward HTTP requests. Required if the qualifier is FORWARD. +* `value` - (Optional) String containing the header_name and header_value. If the qualifier is ADD, specify :. If the qualifier is CORRUPT, specify only the header_name + + +## Attribute Reference + +In addition to the arguments, the following attributes are available: + +* `id` - The id of the filteraction. It has the same value as the `name` attribute. + + +## Import + +A filteraction can be imported using its name, e.g. + +```shell +terraform import citrixadc_filteraction.tf_filteraction tf_filteraction +``` diff --git a/examples/filteraction/provider.tf b/examples/filteraction/provider.tf new file mode 100644 index 000000000..21e391d26 --- /dev/null +++ b/examples/filteraction/provider.tf @@ -0,0 +1,12 @@ +terraform { + required_providers { + citrixadc = { + source = "citrix/citrixadc" + } + } +} +provider "citrixadc" { + endpoint = "http://localhost:8080" + username = "" + password = "" +} diff --git a/examples/filteraction/resources.tf b/examples/filteraction/resources.tf new file mode 100644 index 000000000..1e4c407aa --- /dev/null +++ b/examples/filteraction/resources.tf @@ -0,0 +1,5 @@ +resource "citrixadc_filteraction" "tf_filteraction" { + name = "tf_filteraction" + qual = "corrupt" + value = "X-Forwarded-For" +} \ No newline at end of file