Skip to content

Commit 9285114

Browse files
committed
feat(cae/env): add new resource to manage environments
1 parent 6d188aa commit 9285114

File tree

4 files changed

+677
-0
lines changed

4 files changed

+677
-0
lines changed

docs/resources/cae_environment.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
subcategory: "Cloud Application Engine (CAE)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cae_environment"
5+
description: |-
6+
Manages an environment resource within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cae_environment
10+
11+
Manages an environment resource within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
variable "environment_name" {}
17+
variable "vpc_id" {}
18+
variable "subnet_id" {}
19+
variable "security_group_id" {}
20+
variable "swr_organization_name" {}
21+
22+
resource "huaweicloud_cae_environment" "test" {
23+
name = var.environment_name
24+
25+
annotations = {
26+
type = "exclusive"
27+
vpc_id = var.vpc_id
28+
subnet_id = var.subnet_id
29+
security_group_id = var.security_group_id
30+
group_name = var.swr_organization_name
31+
}
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `region` - (Optional, String, ForceNew) Specifies the region where the environment is located.
40+
If omitted, the provider-level region will be used. Changing this creates a new resource.
41+
42+
* `name` - (Required, String, ForceNew) Specifies the name of the environment.
43+
The valid length is limited from `3` to `30`, only lowercase letters, digits and hyphens (-) are allowed.
44+
The name must start with a lowercase letter and end with a lowercase letter or a digit.
45+
Changing this creates a new resource.
46+
47+
* `annotations` - (Required, Map, ForceNew) Specifies the additional attributes of the environment.
48+
Changing this creates a new resource.
49+
The required keys are as follows:
50+
+ **vpc_id**: The VPC ID bound to the environment.
51+
+ **subnet_id**: The ID of the VPC subnet bound to the environment.
52+
+ **group_name**: The SWR organization name bound to the environment.
53+
54+
The optional keys are as follows:
55+
+ **type**: The environment type. Currently, only **exclusive** is supported.
56+
+ **security_group_id**: The ID of the security group bound to the environment.
57+
If omitted, the CAE service will automatically create it.
58+
59+
-> Deleting the resource does not delete the security group that the service automatically created.
60+
61+
* `enterprise_project_id` - (Optional, String, ForceNew) Specifies the ID of the enterprise project to which the
62+
environment belongs.
63+
Changing this creates a new resource.
64+
This parameter is only valid for enterprise users, if omitted, default enterprise project will be used.
65+
66+
* `enterprise_project_id` - (Optional, Int) Specifies the maximum retry number of the environment operation.
67+
Defaults to `0`.
68+
69+
## Attribute Reference
70+
71+
In addition to all arguments above, the following attributes are exported:
72+
73+
* `id` - The resource ID.
74+
75+
* `status` - The status of the environment.
76+
77+
* `created_at` - The creation time of the environment, in RFC3339 format.
78+
79+
* `updated_at` - The latest update time of the environment, in RFC3339 format.
80+
81+
## Import
82+
83+
The environment can be imported using `id`, e.g.
84+
85+
```bash
86+
$ terraform import huaweicloud_cae_environment.test <id>
87+
```

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ func Provider() *schema.Provider {
14021402
"huaweicloud_cae_component": cae.ResourceComponent(),
14031403
"huaweicloud_cae_component_configurations": cae.ResourceComponentConfigurations(),
14041404
"huaweicloud_cae_component_deployment": cae.ResourceComponentDeployment(),
1405+
"huaweicloud_cae_environment": cae.ResourceEnvironment(),
14051406
"huaweicloud_cae_notification_rule": cae.ResourceNotificationRule(),
14061407

14071408
"huaweicloud_cbr_backup_share_accepter": cbr.ResourceBackupShareAccepter(),
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package cae
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
11+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
12+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cae"
14+
)
15+
16+
func getEnvironmentFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
17+
client, err := cfg.NewServiceClient("cae", acceptance.HW_REGION_NAME)
18+
if err != nil {
19+
return nil, fmt.Errorf("error creating CAE client: %s", err)
20+
}
21+
22+
return cae.GetEnvironmentById(client, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST, state.Primary.ID)
23+
}
24+
25+
func TestAccEnvironment_basic(t *testing.T) {
26+
var (
27+
obj interface{}
28+
29+
resourceName = "huaweicloud_cae_environment.test"
30+
rc = acceptance.InitResourceCheck(resourceName, &obj, getEnvironmentFunc)
31+
32+
invalidName = "-tf-test-invalid-name"
33+
name = acceptance.RandomAccResourceNameWithDash()
34+
baseConfig = testAccEnvironment_base(name)
35+
)
36+
37+
resource.Test(t, resource.TestCase{
38+
PreCheck: func() {
39+
acceptance.TestAccPreCheck(t)
40+
acceptance.TestAccPreCheckEpsID(t)
41+
},
42+
ProviderFactories: acceptance.TestAccProviderFactories,
43+
CheckDestroy: rc.CheckResourceDestroy(),
44+
Steps: []resource.TestStep{
45+
{
46+
Config: testAccEnvironment_basic(baseConfig, invalidName),
47+
ExpectError: regexp.MustCompile(`Invalid param`),
48+
},
49+
{
50+
Config: testAccEnvironment_basic(baseConfig, name),
51+
Check: resource.ComposeTestCheckFunc(
52+
rc.CheckResourceExists(),
53+
resource.TestCheckResourceAttr(resourceName, "name", name),
54+
resource.TestCheckResourceAttr(resourceName, "enterprise_project_id",
55+
acceptance.HW_ENTERPRISE_PROJECT_ID_TEST),
56+
resource.TestCheckResourceAttr(resourceName, "annotations.type", "exclusive"),
57+
resource.TestCheckResourceAttrPair(resourceName, "annotations.vpc_id",
58+
"huaweicloud_vpc.test", "id"),
59+
resource.TestCheckResourceAttrPair(resourceName, "annotations.subnet_id",
60+
"huaweicloud_vpc_subnet.test", "id"),
61+
resource.TestCheckResourceAttrPair(resourceName, "annotations.security_group_id",
62+
"huaweicloud_networking_secgroup.test", "id"),
63+
resource.TestCheckResourceAttrPair(resourceName, "annotations.group_name",
64+
"huaweicloud_swr_organization.test", "name"),
65+
resource.TestCheckResourceAttrSet(resourceName, "status"),
66+
resource.TestMatchResourceAttr(resourceName, "created_at",
67+
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
68+
resource.TestMatchResourceAttr(resourceName, "updated_at",
69+
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
70+
),
71+
},
72+
{
73+
ResourceName: resourceName,
74+
ImportState: true,
75+
ImportStateVerify: true,
76+
ImportStateVerifyIgnore: []string{
77+
"max_retries",
78+
},
79+
},
80+
},
81+
})
82+
}
83+
84+
func testAccEnvironment_base(name string) string {
85+
return fmt.Sprintf(`
86+
resource "huaweicloud_vpc" "test" {
87+
name = "%[1]s"
88+
cidr = "192.168.0.0/16"
89+
}
90+
91+
resource "huaweicloud_vpc_subnet" "test" {
92+
vpc_id = huaweicloud_vpc.test.id
93+
name = "%[1]s"
94+
cidr = cidrsubnet(huaweicloud_vpc.test.cidr, 4, 1)
95+
gateway_ip = cidrhost(cidrsubnet(huaweicloud_vpc.test.cidr, 4, 1), 1)
96+
}
97+
98+
resource "huaweicloud_networking_secgroup" "test" {
99+
name = "%[1]s"
100+
delete_default_rules = true
101+
}
102+
103+
resource "huaweicloud_swr_organization" "test" {
104+
name = "%[1]s"
105+
}
106+
`, name)
107+
}
108+
109+
func testAccEnvironment_basic(baseConfig, name string) string {
110+
return fmt.Sprintf(`
111+
%[1]s
112+
113+
resource "huaweicloud_cae_environment" "test" {
114+
name = "%[2]s"
115+
# enterprise_project_id = "%[3]s"
116+
117+
annotations = {
118+
type = "exclusive"
119+
vpc_id = huaweicloud_vpc.test.id
120+
subnet_id = huaweicloud_vpc_subnet.test.id
121+
security_group_id = huaweicloud_networking_secgroup.test.id
122+
group_name = huaweicloud_swr_organization.test.name
123+
}
124+
125+
// To avoid k8s container deploy failed.
126+
max_retries = 1
127+
}
128+
`, baseConfig, name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
129+
}

0 commit comments

Comments
 (0)