Skip to content

Commit

Permalink
apigw throttling policy associate
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-sidelnikov committed Apr 25, 2024
1 parent 767b9a6 commit 72e2f15
Show file tree
Hide file tree
Showing 4 changed files with 426 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/resources/apigw_throttling_policy_associate_v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
subcategory: "APIG"
---

# opentelekomcloud_apigw_throttling_policy_associate_v2

This API is used to bind a request throttling policy to an API that has been published in an environment within OpenTelekomCloud.
You can bind different request throttling policies to an API in different environments,
but can bind only one request throttling policy to the API in each environment.

## Example Usage

```hcl
variable "gateway_id" {}
variable "policy_id" {}
variable "publish_ids" {
type = list(string)
}
resource "opentelekomcloud_apigw_throttling_policy_associate_v2" "tpa" {
gateway_id = var.gateway_id
policy_id = var.policy_id
publish_ids = var.publish_ids
}
```

## Argument Reference

The following arguments are supported:
* `gateway_id` - (Required, String, ForceNew) Specifies the ID of the dedicated gateway to which the APIs and the
throttling policy belongs.
Changing this will create a new resource.

* `policy_id` - (Required, String, ForceNew) Specifies the ID of the throttling policy.
Changing this will create a new resource.

* `publish_ids` - (Required, List) Specifies the publishing IDs corresponding to the APIs bound by the throttling policy.

## Attribute Reference

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

* `id` - Resource ID. The format is `<instance_id>/<policy_id>`.

* `region` - Specifies the region where the dedicated instance and the throttling policy are located.

## Import

Resources can be imported using their `policy_id` and the APIGW dedicated gateway ID to which the policy
belongs, separated by a slash, e.g.

```shell
$ terraform import opentelekomcloud_apigw_throttling_policy_associate_v2.tpa <instance_id>/<policy_id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
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"
throttlingpolicy "github.com/opentelekomcloud/gophertelekomcloud/openstack/apigw/v2/tr_policy"
"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 resourceApigwAssociateName = "opentelekomcloud_apigw_throttling_policy_associate_v2.associate"

func getAssociateFunc(conf *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
c, err := conf.APIGWV2Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating OpenTelekomCloud APIG v2 client: %s", err)
}
opt := throttlingpolicy.ListBoundOpts{
GatewayID: state.Primary.Attributes["gateway_id"],
ThrottleID: state.Primary.Attributes["policy_id"],
}
return throttlingpolicy.ListAPIBoundPolicy(c, opt)
}

func TestAccThrottlingPolicyAssociate_basic(t *testing.T) {
var apiDetails []throttlingpolicy.ApiThrottle
name := fmt.Sprintf("apigw_acc_thpa%s", acctest.RandString(10))
rc := common.InitResourceCheck(
resourceApigwAssociateName,
&apiDetails,
getAssociateFunc,
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
common.TestAccPreCheck(t)
},
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccThrottlingPolicyAssociateV2_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(resourceApigwAssociateName, "gateway_id"),
resource.TestCheckResourceAttrSet(resourceApigwAssociateName, "policy_id"),
resource.TestCheckResourceAttr(resourceApigwAssociateName, "publish_ids.#", "1"),
),
}, {
Config: testAccThrottlingPolicyAssociateV2_update(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(resourceApigwAssociateName, "gateway_id"),
resource.TestCheckResourceAttrSet(resourceApigwAssociateName, "policy_id"),
resource.TestCheckResourceAttr(resourceApigwAssociateName, "publish_ids.#", "1"),
),
},
{
ResourceName: resourceApigwAssociateName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccThrottlingPolicyAssociateV2_basic(name string) string {
relatedConfig := testAccApigwApi_basic(testAccApigwApi_base(name), name)
return fmt.Sprintf(`
%s
resource "opentelekomcloud_apigw_environment_v2" "env_two" {
name = "second_env_%[2]s"
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
description = "test description"
}
resource "opentelekomcloud_apigw_api_publishment_v2" "pub_one" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
environment_id = opentelekomcloud_apigw_environment_v2.env.id
api_id = opentelekomcloud_apigw_api_v2.api.id
}
resource "opentelekomcloud_apigw_api_publishment_v2" "pub_two" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
environment_id = opentelekomcloud_apigw_environment_v2.env_two.id
api_id = opentelekomcloud_apigw_api_v2.api.id
}
resource "opentelekomcloud_apigw_throttling_policy_v2" "policy" {
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
name = "policy_%[2]s"
type = "API-based"
period = 15000
period_unit = "SECOND"
max_api_requests = 100
}
resource "opentelekomcloud_apigw_throttling_policy_associate_v2" "associate" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
policy_id = opentelekomcloud_apigw_throttling_policy_v2.policy.id
publish_ids = [
opentelekomcloud_apigw_api_publishment_v2.pub_one.publish_id
]
}
`, relatedConfig, name)
}

func testAccThrottlingPolicyAssociateV2_update(name string) string {
relatedConfig := testAccApigwApi_basic(testAccApigwApi_base(name), name)
return fmt.Sprintf(`
%s
resource "opentelekomcloud_apigw_environment_v2" "env_two" {
name = "second_env_%[2]s"
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
description = "test description"
}
resource "opentelekomcloud_apigw_api_publishment_v2" "pub_one" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
environment_id = opentelekomcloud_apigw_environment_v2.env.id
api_id = opentelekomcloud_apigw_api_v2.api.id
}
resource "opentelekomcloud_apigw_api_publishment_v2" "pub_two" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
environment_id = opentelekomcloud_apigw_environment_v2.env_two.id
api_id = opentelekomcloud_apigw_api_v2.api.id
}
resource "opentelekomcloud_apigw_throttling_policy_v2" "policy" {
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
name = "policy_%[2]s"
type = "API-based"
period = 15000
period_unit = "SECOND"
max_api_requests = 100
}
resource "opentelekomcloud_apigw_throttling_policy_associate_v2" "associate" {
gateway_id = opentelekomcloud_apigw_gateway_v2.gateway.id
policy_id = opentelekomcloud_apigw_throttling_policy_v2.policy.id
publish_ids = [
opentelekomcloud_apigw_api_publishment_v2.pub_two.publish_id
]
}
`, relatedConfig, name)
}
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_apigw_gateway_v2": apigw.ResourceAPIGWv2(),
"opentelekomcloud_apigw_group_v2": apigw.ResourceAPIGroupV2(),
"opentelekomcloud_apigw_throttling_policy_v2": apigw.ResourceAPIThrottlingPolicyV2(),
"opentelekomcloud_apigw_throttling_policy_associate_v2": apigw.ResourceAPIThrottlingPolicyAssociateV2(),
"opentelekomcloud_as_configuration_v1": as.ResourceASConfiguration(),
"opentelekomcloud_as_group_v1": as.ResourceASGroup(),
"opentelekomcloud_as_policy_v1": as.ResourceASPolicy(),
Expand Down
Loading

0 comments on commit 72e2f15

Please sign in to comment.