Skip to content

Commit

Permalink
feat(cae/env): add new resource to manage environments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 committed Jan 3, 2025
1 parent 952caff commit f0d056a
Show file tree
Hide file tree
Showing 4 changed files with 686 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/resources/cae_environment.md
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>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,7 @@ func Provider() *schema.Provider {
"huaweicloud_cae_component": cae.ResourceComponent(),
"huaweicloud_cae_component_configurations": cae.ResourceComponentConfigurations(),
"huaweicloud_cae_component_deployment": cae.ResourceComponentDeployment(),
"huaweicloud_cae_environment": cae.ResourceEnvironment(),

"huaweicloud_cbr_backup_share_accepter": cbr.ResourceBackupShareAccepter(),
"huaweicloud_cbr_backup_share": cbr.ResourceBackupShare(),
Expand Down
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)
}
Loading

0 comments on commit f0d056a

Please sign in to comment.