Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WAF] reference table resource and data-source for WAFD #2453

Merged
merged 3 commits into from Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/data-sources/waf_dedicated_reference_tables_v1.md
@@ -0,0 +1,49 @@
---
subcategory: "Dedicated Web Application Firewall (WAFD)"
---

Up-to-date reference of API arguments for WAF dedicated Precise Protection rule you can get at
[Official Docs Portal](https://docs.otc.t-systems.com/web-application-firewall-dedicated/api-ref/apis/rule_management/querying_the_reference_table_list.html).


# opentelekomcloud_waf_dedicated_reference_tables_v1

Use this data source to get a list of OpenTelekomCloud WAF reference tables.

## Example Usage

```hcl
data "opentelekomcloud_waf_dedicated_reference_tables_v1" "table" {
name = "reference_table_1"
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) The region in which to create the WAF reference table resource.
If omitted, the provider-level region will be used.

* `name` - (Optional, String) The name of the reference table. The value is case-sensitive and matches exactly.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `tables` - A list of WAF reference tables.

The `tables` block supports:

* `id` - The id of the reference table.

* `name` - The name of the reference table. The maximum length is 64 characters.

* `type` - The type of the reference table, The options are: `url`, `user-agent`, `ip`, `params`, `cookie`, `referer`
and `header`.

* `conditions` - The conditions of the reference table.

* `description` - The description of the reference table.

* `created_at` - The time when reference table was created.
60 changes: 60 additions & 0 deletions docs/resources/waf_dedicated_reference_table_v1.md
@@ -0,0 +1,60 @@
---
subcategory: "Dedicated Web Application Firewall (WAFD)"
---

Up-to-date reference of API arguments for WAF dedicated Precise Protection rule you can get at
[Official Docs Portal](https://docs.otc.t-systems.com/web-application-firewall-dedicated/api-ref/apis/rule_management/creating_a_reference_table.html).


# opentelekomcloud_waf_dedicated_reference_table_v1

Manages a WAF Dedicated Reference Table resource within OpenTelekomCloud.

## Example Usage

```hcl
resource "opentelekomcloud_waf_dedicated_reference_table_v1" "table" {
name = "%s"
type = "url"

conditions = [
"/admin",
"/manage"
]
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) The region in which to create the WAF reference table resource. If omitted,
the provider-level region will be used. Changing this setting will push a new reference table.

* `name` - (Required, String) The name of the reference table. Only letters, digits, and underscores(_) are allowed. The
maximum length is 64 characters.

* `type` - (Required, String, ForceNew) The type of the reference table, The options are `url`, `user-agent`, `ip`,
`params`, `cookie`, `referer` and `header`. Changing this setting will push a new reference table.

* `conditions` - (Required, List) The conditions of the reference table. The maximum length is 30. The maximum length of
condition is 2048 characters.

* `description` - (Optional, String) The description of the reference table. The maximum length is 128 characters.
Currently, could be set only on update.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The id of the reference table.

* `created_at` - The time when reference table was created.

## Import

Dedicated WAF Reference Table can be imported using `id`, e.g.

```bash
$ terraform import opentelekomcloud_waf_dedicated_reference_table_v1.table <id>
```
@@ -0,0 +1,57 @@
package acceptance

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
)

func TestAccDataSourceWafDedicatedRefTablesV1_basic(t *testing.T) {
var name = fmt.Sprintf("wafd_rt_%s", acctest.RandString(5))
dataSourceName := "data.opentelekomcloud_waf_dedicated_reference_tables_v1.table"

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccWafDedicatedRefTablesV1_ds(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckReferenceTablesId(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "tables.0.name"),
resource.TestCheckResourceAttrSet(dataSourceName, "tables.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "tables.0.type"),
resource.TestCheckResourceAttrSet(dataSourceName, "tables.0.conditions.0"),
resource.TestCheckResourceAttrSet(dataSourceName, "tables.0.created_at"),
),
},
},
})
}

func testAccCheckReferenceTablesId(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[r]
if !ok {
return fmt.Errorf("can't find OpenTelekomCloud WAF reference tables data source: %s", r)
}
if rs.Primary.ID == "" {
return fmt.Errorf("the OpenTelekomCloud WAF reference tables data source ID not set")
}
return nil
}
}

func testAccWafDedicatedRefTablesV1_ds(name string) string {
return fmt.Sprintf(`
%s

data "opentelekomcloud_waf_dedicated_reference_tables_v1" "table" {
depends_on = [opentelekomcloud_waf_dedicated_reference_table_v1.table]
}
`, testAccWafDedicatedRefTablesV1_basic(name))
}
@@ -0,0 +1,139 @@
package acceptance

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/rules"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)

const wafdRefTableResourceName = "opentelekomcloud_waf_dedicated_reference_table_v1.table"

func TestAccWafDedicatedReferenceTableV1_basic(t *testing.T) {
var refTable rules.ReferenceTable
var name = fmt.Sprintf("wafd_rt_%s", acctest.RandString(5))
updateName := name + "_update"

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckWafDedicatedRefTableV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccWafDedicatedRefTablesV1_basic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckWafDedicatedRefTableV1Exists(wafdRefTableResourceName, &refTable),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "name", name),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "description", ""),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "type", "url"),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "conditions.#", "2"),
),
},
{
Config: testAccWafReferenceTableV1_update(updateName),
Check: resource.ComposeTestCheckFunc(
testAccCheckWafDedicatedRefTableV1Exists(wafdRefTableResourceName, &refTable),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "name", updateName),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "type", "url"),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "conditions.#", "2"),
resource.TestCheckResourceAttr(wafdRefTableResourceName, "description", "new description"),
resource.TestCheckResourceAttrSet(wafdRefTableResourceName, "created_at"),
),
},
{
ResourceName: wafdRefTableResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckWafDedicatedRefTableV1Destroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.WafDedicatedV1Client(env.OS_REGION_NAME)
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_waf_dedicated_reference_table_v1" {
continue
}
_, err = rules.GetReferenceTable(client, rs.Primary.ID)
if err == nil {
return fmt.Errorf("waf dedicated reference table (%s) still exists", rs.Primary.ID)
}
if _, ok := err.(golangsdk.ErrDefault404); !ok {
return err
}
}
return nil
}

func testAccCheckWafDedicatedRefTableV1Exists(n string, instance *rules.ReferenceTable) 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")
}

config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.WafDedicatedV1Client(env.OS_REGION_NAME)
if err != nil {
return err
}

var found *rules.ReferenceTable
found, err = rules.GetReferenceTable(client, rs.Primary.ID)
if err != nil {
return err
}
*instance = *found

return nil
}
}

func testAccWafDedicatedRefTablesV1_basic(name string) string {
return fmt.Sprintf(`


resource "opentelekomcloud_waf_dedicated_reference_table_v1" "table" {
name = "%s"
type = "url"

conditions = [
"/admin",
"/manage"
]
}
`, name)
}

func testAccWafReferenceTableV1_update(name string) string {
return fmt.Sprintf(`

resource "opentelekomcloud_waf_dedicated_reference_table_v1" "table" {
name = "%s"
type = "url"
description = "new description"

conditions = [
"/bill",
"/sql"
]
}
`, name)
}