-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New apig group resource support (#1284)
- Loading branch information
1 parent
a4bda80
commit 6d22542
Showing
9 changed files
with
892 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
260 changes: 260 additions & 0 deletions
260
huaweicloud/services/acceptance/apig/resource_huaweicloud_apig_group_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.