-
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(cae/env): add new resource to manage environments
- Loading branch information
1 parent
952caff
commit f0d056a
Showing
4 changed files
with
686 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,84 @@ | ||
--- | ||
subcategory: "Cloud Application Engine (CAE)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cae_environment" | ||
description: |- | ||
Manages an environment resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_cae_environment | ||
|
||
Manages an environment resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "environment_name" {} | ||
variable "vpc_id" {} | ||
variable "subnet_id" {} | ||
variable "security_group_id" {} | ||
variable "swr_organization_name" {} | ||
resource "huaweicloud_cae_environment" "test" { | ||
name = var.environment_name | ||
annotations = { | ||
type = "exclusive" | ||
vpc_id = var.vpc_id | ||
subnet_id = var.subnet_id | ||
security_group_id = var.security_group_id | ||
group_name = var.swr_organization_name | ||
} | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String, ForceNew) Specifies the region where the environment is located. | ||
If omitted, the provider-level region will be used. Changing this creates a new resource. | ||
|
||
* `name` - (Required, String, ForceNew) Specifies the name of the environment. | ||
The valid length is limited from `3` to `30`, only lowercase letters, digits and hyphens (-) are allowed. | ||
The name must start with a lowercase letter and end with a lowercase letter or a digit. | ||
Changing this creates a new resource. | ||
|
||
* `annotations` - (Required, Map, ForceNew) Specifies the additional attributes of the environment. | ||
Changing this creates a new resource. | ||
The required keys are as follows: | ||
+ **vpc_id**: The VPC ID bound to the environment. | ||
+ **subnet_id**: The ID of the VPC subnet bound to the environment. | ||
+ **group_name**: The SWR organization name bound to the environment. | ||
|
||
The optional keys are as follows: | ||
+ **type**: The environment type. Currently, only **exclusive** is supported. | ||
+ **security_group_id**: The ID of the security group bound to the environment. | ||
If omitted, the CAE service will automatically create it. | ||
|
||
-> Deleting the resource does not delete the security group that the service automatically created. | ||
|
||
* `enterprise_project_id` - (Optional, String, ForceNew) Specifies the ID of the enterprise project to which the | ||
environment belongs. | ||
Changing this creates a new resource. | ||
This parameter is only valid for enterprise users, if omitted, default enterprise project will be used. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. | ||
|
||
* `status` - The status of the environment. | ||
|
||
* `created_at` - The creation time of the environment, in RFC3339 format. | ||
|
||
* `updated_at` - The latest update time of the environment, in RFC3339 format. | ||
|
||
## Import | ||
|
||
The environment can be imported using `id`, e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_cae_environment.test <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
129 changes: 129 additions & 0 deletions
129
huaweicloud/services/acceptance/cae/resource_huaweicloud_cae_environment_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,129 @@ | ||
package cae | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cae" | ||
) | ||
|
||
func getEnvironmentFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) { | ||
client, err := cfg.NewServiceClient("cae", acceptance.HW_REGION_NAME) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating CAE client: %s", err) | ||
} | ||
|
||
return cae.GetEnvironmentById(client, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST, state.Primary.ID) | ||
} | ||
|
||
func TestAccEnvironment_basic(t *testing.T) { | ||
var ( | ||
obj interface{} | ||
|
||
resourceName = "huaweicloud_cae_environment.test" | ||
rc = acceptance.InitResourceCheck(resourceName, &obj, getEnvironmentFunc) | ||
|
||
invalidName = "-tf-test-invalid-name" | ||
name = acceptance.RandomAccResourceNameWithDash() | ||
baseConfig = testAccEnvironment_base(name) | ||
) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckEpsID(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccEnvironment_basic(baseConfig, invalidName), | ||
ExpectError: regexp.MustCompile(`Invalid param`), | ||
}, | ||
{ | ||
Config: testAccEnvironment_basic(baseConfig, name), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(resourceName, "name", name), | ||
resource.TestCheckResourceAttr(resourceName, "enterprise_project_id", | ||
acceptance.HW_ENTERPRISE_PROJECT_ID_TEST), | ||
resource.TestCheckResourceAttr(resourceName, "annotations.type", "exclusive"), | ||
resource.TestCheckResourceAttrPair(resourceName, "annotations.vpc_id", | ||
"huaweicloud_vpc.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "annotations.subnet_id", | ||
"huaweicloud_vpc_subnet.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "annotations.security_group_id", | ||
"huaweicloud_networking_secgroup.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "annotations.group_name", | ||
"huaweicloud_swr_organization.test", "name"), | ||
resource.TestCheckResourceAttrSet(resourceName, "status"), | ||
resource.TestMatchResourceAttr(resourceName, "created_at", | ||
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)), | ||
resource.TestMatchResourceAttr(resourceName, "updated_at", | ||
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{ | ||
"max_retries", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccEnvironment_base(name string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_vpc" "test" { | ||
name = "%[1]s" | ||
cidr = "192.168.0.0/16" | ||
} | ||
resource "huaweicloud_vpc_subnet" "test" { | ||
vpc_id = huaweicloud_vpc.test.id | ||
name = "%[1]s" | ||
cidr = cidrsubnet(huaweicloud_vpc.test.cidr, 4, 1) | ||
gateway_ip = cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 4, 1), 1) | ||
} | ||
resource "huaweicloud_networking_secgroup" "test" { | ||
name = "%[1]s" | ||
delete_default_rules = true | ||
} | ||
resource "huaweicloud_swr_organization" "test" { | ||
name = "%[1]s" | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccEnvironment_basic(baseConfig, name string) string { | ||
return fmt.Sprintf(` | ||
%[1]s | ||
resource "huaweicloud_cae_environment" "test" { | ||
name = "%[2]s" | ||
# enterprise_project_id = "%[3]s" | ||
annotations = { | ||
type = "exclusive" | ||
vpc_id = huaweicloud_vpc.test.id | ||
subnet_id = huaweicloud_vpc_subnet.test.id | ||
security_group_id = huaweicloud_networking_secgroup.test.id | ||
group_name = huaweicloud_swr_organization.test.name | ||
} | ||
// To avoid k8s container deploy failed. | ||
max_retries = 1 | ||
} | ||
`, baseConfig, name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) | ||
} |
Oops, something went wrong.