Skip to content

Commit

Permalink
feat: New apig group resource support (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 authored Jul 17, 2021
1 parent a4bda80 commit 6d22542
Show file tree
Hide file tree
Showing 9 changed files with 892 additions and 0 deletions.
93 changes: 93 additions & 0 deletions docs/resources/apig_api_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
subcategory: "API Gateway (APIG)"
---

# huaweicloud_apig_group

Manages an APIG (API) group resource within HuaweiCloud.

## Example Usage

```hcl
variable "instance_id" {}
variable "group_name" {}
variable "description" {}
variable "environment_id" {}
resource "huaweicloud_apig_group" "test" {
instance_id = var.instance_id
name = var.group_name
description = var.description
environment {
variable {
name = "TERRAFORM"
value = "/stage/terraform"
}
environment_id = var.environment_id
}
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the API group resource.
If omitted, the provider-level region will be used.
Changing this will create a new API group resource.

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

* `name` - (Required, String) Specifies the name of the API group.
The API group name consists of 3 to 64 characters, starting with a letter.
Only letters, digits and underscores (_) are allowed.
Chinese characters must be in UTF-8 or Unicode format.

* `description` - (Optional, String) Specifies the description about the API group.
The description contain a maximum of 255 characters and the angle brackets (< and >) are not allowed.
Chinese characters must be in UTF-8 or Unicode format.

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

The `environment` block supports:

* `variable` - (Required, List) Specifies an array of one or more APIG environment variables.
The object structure is documented below.
The environment variables of different groups are isolated in the same environment.

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

The `variable` block supports:

* `name` - (Required, String) Specifies the variable name, which can contains of 3 to 32 characters,
starting with a letter.
Only letters, digits, hyphens (-), and underscores (_) are allowed.
In the definition of an API, `name` (case-sensitive) indicates a variable, such as #Name#.
It is replaced by the actual value when the API is published in an environment.
The variable names are not allowed to be repeated for an API group.

* `value` - (Required, String) Specifies the environment ariable value, which can contains of 1 to 255 characters.
Only letters, digits and special characters (_-/.:) are allowed.

-> **NOTE:** The variable value will be displayed in plain text on the console.

## Attributes Reference

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

* `id` - ID of the API group.
* `registraion_time` - Registration time, in RFC-3339 format.
* `update_time` - Time when the API group was last modified, in RFC-3339 format.
* `environment/variable/variable_id` - ID of the environment variable.

## Import

API groups of the APIG can be imported using their `id` and the ID of the APIG instance to which the group belongs,
separated by a slash, e.g.
```
$ terraform import huaweicloud_apig_group.test <instance id>/<id>
```
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20210706092920-c12079d6a740 h1:RCAJI5jVLah5GJkr1lk9I0g6+PdTjUMCwzY5pUUd7fA=
github.com/huaweicloud/golangsdk v0.0.0-20210706092920-c12079d6a740/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210714031135-2f101c51a35d h1:GJFDZ3hxptdDs10RjT1+Tq4oDsWXu+e2yNoHzLRgJ38=
github.com/huaweicloud/golangsdk v0.0.0-20210714031135-2f101c51a35d/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210715061636-f0d85a483f0b h1:s3ZJgf61mC4EaZp3OEMrC7hbZixXMr9JbFL5cgATB/o=
Expand Down
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_apig_instance": apig.ResourceApigInstanceV2(),
"huaweicloud_apig_application": apig.ResourceApigApplicationV2(),
"huaweicloud_apig_environment": apig.ResourceApigEnvironmentV2(),
"huaweicloud_apig_group": apig.ResourceApigGroupV2(),
"huaweicloud_apig_vpc_channel": apig.ResourceApigVpcChannelV2(),
"huaweicloud_as_configuration": ResourceASConfiguration(),
"huaweicloud_as_group": ResourceASGroup(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
package apig

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/huaweicloud/golangsdk/openstack/apigw/v2/apigroups"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccApigGroupV2_basic(t *testing.T) {
var (
// Only letters, digits and underscores (_) are allowed in the name.
rName = fmt.Sprintf("tf_acc_test_%s", acctest.RandString(5))
resourceName = "huaweicloud_apig_group.test"
group apigroups.Group
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckEpsID(t) // Method testAccApigApplication_base needs HW_ENTERPRISE_PROJECT_ID.
},
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccCheckApigGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccApigGroup_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckApigGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "description", "Created by script"),
),
},
{
Config: testAccApigGroup_update(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckApigGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", rName+"_update"),
resource.TestCheckResourceAttr(resourceName, "description", "Updated by script"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccApigInstanceSubResourceImportStateIdFunc(resourceName),
},
},
})
}

func TestAccApigGroupV2_variables(t *testing.T) {
var (
// Only letters, digits and underscores (_) are allowed in the name.
rName = fmt.Sprintf("tf_acc_test_%s", acctest.RandString(5))
resourceName = "huaweicloud_apig_group.test"
group apigroups.Group
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckEpsID(t) // Method testAccApigApplication_base needs HW_ENTERPRISE_PROJECT_ID.
},
Providers: acceptance.TestAccProviders,
CheckDestroy: testAccCheckApigGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccApigGroup_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckApigGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", rName),
),
},
{
// Bind two environment to group, and create some variables.
Config: testAccApigGroup_variables(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckApigGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "environment.#", "2"),
),
},
{
// Update the variables for two environments.
Config: testAccApigGroup_variablesUpdate(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckApigGroupExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "environment.#", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccApigInstanceSubResourceImportStateIdFunc(resourceName),
},
},
})
}

func testAccCheckApigGroupDestroy(s *terraform.State) error {
config := acceptance.TestAccProvider.Meta().(*config.Config)
client, err := config.ApigV2Client(acceptance.HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud APIG v2 client: %s", err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "huaweicloud_apig_group" {
continue
}
_, err := apigroups.Get(client, rs.Primary.Attributes["instance_id"], rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("APIG v2 API group (%s) is still exists", rs.Primary.ID)
}
}
return nil
}

func testAccCheckApigGroupExists(groupName string, app *apigroups.Group) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[groupName]
if !ok {
return fmt.Errorf("Resource %s not found", groupName)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No APIG V2 API group Id")
}

config := acceptance.TestAccProvider.Meta().(*config.Config)
client, err := config.ApigV2Client(acceptance.HW_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud APIG v2 client: %s", err)
}
found, err := apigroups.Get(client, rs.Primary.Attributes["instance_id"], rs.Primary.ID).Extract()
if err != nil {
return fmt.Errorf("APIG v2 API group not exist: %s", err)
}
*app = *found
return nil
}
}

func testAccApigGroup_basic(rName string) string {
return fmt.Sprintf(`
%s
resource "huaweicloud_apig_group" "test" {
name = "%s"
instance_id = huaweicloud_apig_instance.test.id
description = "Created by script"
}
`, testAccApigApplication_base(rName), rName)
}

func testAccApigGroup_update(rName string) string {
return fmt.Sprintf(`
%s
resource "huaweicloud_apig_group" "test" {
name = "%s_update"
instance_id = huaweicloud_apig_instance.test.id
description = "Updated by script"
}
`, testAccApigApplication_base(rName), rName)
}

func testAccApigGroup_variablesBase(rName string) string {
return fmt.Sprintf(`
resource "huaweicloud_apig_environment" "test1" {
name = "%s_1"
instance_id = huaweicloud_apig_instance.test.id
description = "Created by script"
}
resource "huaweicloud_apig_environment" "test2" {
name = "%s_2"
instance_id = huaweicloud_apig_instance.test.id
description = "Created by script"
}
`, rName, rName)
}

// Create two environments for the group, and add a total of three variables to the two environments.
// Each of the two environments has a variable with the same name and different value.
func testAccApigGroup_variables(rName string) string {
return fmt.Sprintf(`
%s
%s
resource "huaweicloud_apig_group" "test" {
name = "%s"
instance_id = huaweicloud_apig_instance.test.id
description = "Created by script"
environment {
environment_id = huaweicloud_apig_environment.test1.id
variable {
name = "TERRAFORM"
value = "/stage/terraform"
}
}
environment {
environment_id = huaweicloud_apig_environment.test2.id
variable {
name = "TERRAFORM"
value = "/res/terraform"
}
variable {
name = "DEMO"
value = "/stage/demo"
}
}
}
`, testAccApigApplication_base(rName), testAccApigGroup_variablesBase(rName), rName)
}

func testAccApigGroup_variablesUpdate(rName string) string {
return fmt.Sprintf(`
%s
%s
resource "huaweicloud_apig_group" "test" {
name = "%s"
instance_id = huaweicloud_apig_instance.test.id
description = "Created by script"
environment {
environment_id = huaweicloud_apig_environment.test1.id
variable {
name = "TERRAFORM"
value = "/stage/terraform"
}
variable {
name = "TEST"
value = "/stage/test"
}
}
environment {
environment_id = huaweicloud_apig_environment.test2.id
variable {
name = "TERRAFORM"
value = "/stage/terraform"
}
}
}
`, testAccApigApplication_base(rName), testAccApigGroup_variablesBase(rName), rName)
}
Loading

0 comments on commit 6d22542

Please sign in to comment.