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

[APIGW]: groups resource #2466

Merged
merged 5 commits into from Mar 14, 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
2 changes: 1 addition & 1 deletion docs/resources/apigw_environment_v2.md
Expand Up @@ -2,7 +2,7 @@
subcategory: "APIG"
---

Up-to-date reference of API arguments for Anti-DDoS service you can get at
Up-to-date reference of API arguments for API Gateway service you can get at
`https://docs.otc.t-systems.com/api-gateway/api-ref/dedicated_gateway_apis_v2/index.html`.

# opentelekomcloud_apigw_environment_v2
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/apigw_gateway_v2.md
Expand Up @@ -2,7 +2,7 @@
subcategory: "APIG"
---

Up-to-date reference of API arguments for Anti-DDoS service you can get at
Up-to-date reference of API arguments for API Gateway service you can get at
`https://docs.otc.t-systems.com/api-gateway/api-ref/dedicated_gateway_apis_v2/index.html`.

# opentelekomcloud_apigw_gateway_v2
Expand Down
98 changes: 98 additions & 0 deletions docs/resources/apigw_group_v2.md
@@ -0,0 +1,98 @@
---
subcategory: "APIG"
---

Up-to-date reference of API arguments for API Gateway service you can get at
`https://docs.otc.t-systems.com/api-gateway/api-ref/dedicated_gateway_apis_v2/index.html`.

# opentelekomcloud_apigw_group_v2

API Gateway (APIG) is a high-performance, high-availability, and high-security API hosting service that helps you build,
manage, and deploy APIs at any scale.
With just a few clicks, you can integrate internal systems, and selectively expose capabilities with minimal costs and risks.

## Example Usage

```hcl
resource "opentelekomcloud_apigw_group_v2" "group" {
instance_id = var.gateway_id
name = "group-name"
description = "test description"

environment {
variable {
name = "test-name"
value = "test-value"
}
environment_id = var.env_id
}
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region where the APIGW group is located.
If omitted, the provider-level region will be used. Changing this will create a new resource.

* `instance_id` - (Required, String, ForceNew) Specifies the ID of the dedicated instance to which the group belongs.
Changing this will create a new resource.

* `name` - (Required, String) Specifies the group name.
The valid length is limited from `3` to `64`, only letters, digits and hyphens (-) are
allowed.

* `description` - (Optional, String) Specifies the group description.

* `environment` - (Optional, List) Specifies an array of one or more environments of the associated group.
The `environment` structure is documented below.

The `environment` block supports:

* `variable` - (Required, List) Specifies an array of one or more environment variables.
The `variable` structure is documented below.

-> The environment variables of different groups are isolated in the same environment.

* `environment_id` - (Required, String) Specifies the environment ID of the associated group.

The `variable` block supports:

* `name` - (Required, String) Specifies the variable name.
The valid length is limited from `3` to `32` characters.
Only letters, digits, hyphens (-), and underscores (_) are allowed, and must start with a letter.

* `value` - (Required, String) Specifies the variable value.
The valid length is limited from `1` to `255` characters.
Only letters, digits and special characters (_-/.:) are allowed.

## Attribute Reference

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

* `id` - The group ID.

* `registration_time` - The registration time, in RFC-3339 format.

* `updated_at` - The time when the API group was last modified, in RFC-3339 format.

* `environment` - The array of one or more environments of the associated group.
The `environment` structure is documented below.

The `environment` block supports:

* `variable` - The array of one or more environment variables.
The `variable` structure is documented below.

The `variable` block supports:

* `id` - The variable ID.

## Import

API groups can be imported using their `id` and the ID of the related dedicated instance, separated by a slash, e.g.

```shell
$ terraform import opentelekomcloud_apigw_group_v2.test <instance_id>/<id>
```
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240312142134-ca4aaa4c0eb1
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240313140551-3f39e43bf104
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.17.0
golang.org/x/sync v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -160,6 +160,8 @@ github.com/opentelekomcloud/gophertelekomcloud v0.9.0 h1:9H8ZY0+lC7obhRJXCwUfnl6
github.com/opentelekomcloud/gophertelekomcloud v0.9.0/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240312142134-ca4aaa4c0eb1 h1:agiMGYkxoQooTnY6cey54iiwQTipnaax97Tugj12KrY=
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240312142134-ca4aaa4c0eb1/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240313140551-3f39e43bf104 h1:HePUjJe/F1ksMx7UE74oNPTa8dp9zAs9+CUZE5DZnhk=
github.com/opentelekomcloud/gophertelekomcloud v0.9.1-0.20240313140551-3f39e43bf104/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Expand Up @@ -100,13 +100,13 @@ func TestAccAPIGWEnvironmentV2ImportBasic(t *testing.T) {
ResourceName: resourceNameEnvironment,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccCCENodePoolV3ImportStateIdFunc(),
ImportStateIdFunc: testAccAPIGWv2EnvironmentImportStateIdFunc(),
},
},
})
}

func testAccCCENodePoolV3ImportStateIdFunc() resource.ImportStateIdFunc {
func testAccAPIGWv2EnvironmentImportStateIdFunc() resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var gatewayID string
var envName string
Expand Down
@@ -0,0 +1,175 @@
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/gophertelekomcloud/openstack/apigw/v2/group"
"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 resourceGroupName = "opentelekomcloud_apigw_group_v2.group"

func TestAccAPIGWv2Group_basic(t *testing.T) {
var groupConfig group.GroupResp
name := acctest.RandString(10)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
common.TestAccPreCheck(t)
},
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccAPIGWv2GroupBasic(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAPIGWv2GroupExists(resourceGroupName, &groupConfig),
resource.TestCheckResourceAttr(resourceGroupName, "name", "group_"+name),
resource.TestCheckResourceAttr(resourceGroupName, "description", "test description"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.0.name", "test-name"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.0.value", "test-value"),
),
},
{
Config: testAccAPIGWv2GroupUpdated(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAPIGWv2GroupExists(resourceGroupName, &groupConfig),
resource.TestCheckResourceAttr(resourceGroupName, "name", "group_"+name+"_updated"),
resource.TestCheckResourceAttr(resourceGroupName, "description", "test description updated"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.1.name", "test-name-2"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.1.value", "test-value-2"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.0.name", "test-name"),
resource.TestCheckResourceAttr(resourceGroupName, "environment.0.variable.0.value", "test-value"),
),
},
},
})
}

func testAccCheckAPIGWv2GroupExists(n string, configuration *group.GroupResp) 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")
}

rsgw, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", n)
}

if rsgw.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}

config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.APIGWV2Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud APIGWv2 client: %w", err)
}

found, err := group.Get(client, rsgw.Primary.ID, rs.Primary.ID)
if err != nil {
return err
}

if found.ID != rs.Primary.ID {
return fmt.Errorf("APIGW group not found")
}
configuration = found

return nil
}
}

func TestAccAPIGWGroupV2ImportBasic(t *testing.T) {
name := acctest.RandString(10)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckAPIGWv2GatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAPIGWv2GroupBasic(name),
},
{
ResourceName: resourceGroupName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccAPIGWv2GroupImportStateIdFunc(),
},
},
})
}

func testAccAPIGWv2GroupImportStateIdFunc() resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
var gatewayID string
var groupID string
for _, rs := range s.RootModule().Resources {
if rs.Type == "opentelekomcloud_apigw_gateway_v2" {
gatewayID = rs.Primary.ID
} else if rs.Type == "opentelekomcloud_apigw_group_v2" && rs.Primary.ID != "" {
groupID = rs.Primary.ID
}
}
if gatewayID == "" || groupID == "" {
return "", fmt.Errorf("resource not found: %s/%s", gatewayID, groupID)
}
return fmt.Sprintf("%s/%s", gatewayID, groupID), nil
}
}

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

resource "opentelekomcloud_apigw_group_v2" "group"{
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
name = "%s"
description = "test description"

environment {
variable {
name = "test-name"
value = "test-value"
}
environment_id = opentelekomcloud_apigw_environment_v2.env.id
}
}
`, testAccAPIGWv2EnvironmentBasic("gateway-"+name, "env_"+name), "group_"+name)
}

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

resource "opentelekomcloud_apigw_group_v2" "group"{
instance_id = opentelekomcloud_apigw_gateway_v2.gateway.id
name = "%s"
description = "test description updated"

environment {
variable {
name = "test-name"
value = "test-value"
}
variable {
name = "test-name-2"
value = "test-value-2"
}
environment_id = opentelekomcloud_apigw_environment_v2.env.id
}
}
`, testAccAPIGWv2EnvironmentBasic("gateway-"+name, "env_"+name), "group_"+name+"_updated")
}
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Expand Up @@ -349,6 +349,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_antiddos_v1": antiddos.ResourceAntiDdosV1(),
"opentelekomcloud_apigw_environment_v2": apigw.ResourceAPIEnvironmentv2(),
"opentelekomcloud_apigw_gateway_v2": apigw.ResourceAPIGWv2(),
"opentelekomcloud_apigw_group_v2": apigw.ResourceAPIGroupV2(),
"opentelekomcloud_as_configuration_v1": as.ResourceASConfiguration(),
"opentelekomcloud_as_group_v1": as.ResourceASGroup(),
"opentelekomcloud_as_policy_v1": as.ResourceASPolicy(),
Expand Down