Skip to content

Commit

Permalink
Merge pull request #453 from citrix/filteraction
Browse files Browse the repository at this point in the history
Added filteraction Resource
  • Loading branch information
George Nikolopoulos authored Apr 20, 2022
2 parents 2ed219a + 03544d7 commit 6567c12
Show file tree
Hide file tree
Showing 6 changed files with 350 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 @@ -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(),
}
}

Expand Down
165 changes: 165 additions & 0 deletions citrixadc/resource_citrixadc_filteraction.go
Original file line number Diff line number Diff line change
@@ -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
}
123 changes: 123 additions & 0 deletions citrixadc/resource_citrixadc_filteraction_test.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions docs/resources/filteraction.md
Original file line number Diff line number Diff line change
@@ -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 <header_name>:<header_value>. 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
```
12 changes: 12 additions & 0 deletions examples/filteraction/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
citrixadc = {
source = "citrix/citrixadc"
}
}
}
provider "citrixadc" {
endpoint = "http://localhost:8080"
username = "<username>"
password = "<password>"
}
5 changes: 5 additions & 0 deletions examples/filteraction/resources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "citrixadc_filteraction" "tf_filteraction" {
name = "tf_filteraction"
qual = "corrupt"
value = "X-Forwarded-For"
}

0 comments on commit 6567c12

Please sign in to comment.