Skip to content

Commit

Permalink
feat(cae): add new resource to manage domain name (#6179)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong authored Jan 20, 2025
1 parent 3beed89 commit 3daf909
Show file tree
Hide file tree
Showing 4 changed files with 401 additions and 0 deletions.
56 changes: 56 additions & 0 deletions docs/resources/cae_domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
subcategory: "Cloud Application Engine (CAE)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cae_domain"
description: |-
Manage a CAE domain name resource within HuaweiCloud.
---

# huaweicloud_cae_domain

Manage a CAE domain name resource within HuaweiCloud.

## Example Usage

```hcl
variable "environment_id" {}
variable "domain_name" {}
resource "huaweicloud_cae_domain" "test" {
environment_id = var.environment_id
name = var.domain_name
}
```

## Argument Reference

The following arguments are supported:

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

* `environment_id` - (Required, String, ForceNew) Specifies the ID of the CAE environment.
Changing this creates a new resource.

* `name` - (Required, String, ForceNew) Specifies the domain name to be associated with the CAE environment.
Changing this creates a new resource.
The maximum length of the domain name is `254` characters.
The domain name consists of multiple strings separated by dots (.), and the maximum length of a single string is `63` characters.
Only letters, digits, and hyphens (-) allowed, and must start with a letter or a digit.

## Attribute Reference

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

* `id` - The resource ID, in UUID format.

* `created_at` - The time when the domain name is associated, in RFC3339 format.

## Import

The resource can be imported using `environment_id` and `name`, separated by a slash (/), e.g.

```bash
$ terraform import huaweicloud_cae_domain.test <environment_id>/<name>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,7 @@ func Provider() *schema.Provider {
"huaweicloud_cae_component": cae.ResourceComponent(),
"huaweicloud_cae_component_configurations": cae.ResourceComponentConfigurations(),
"huaweicloud_cae_component_deployment": cae.ResourceComponentDeployment(),
"huaweicloud_cae_domain": cae.ResourceDomain(),
"huaweicloud_cae_environment": cae.ResourceEnvironment(),
"huaweicloud_cae_notification_rule": cae.ResourceNotificationRule(),
"huaweicloud_cae_vpc_egress": cae.ResourceVpcEgress(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 getResourceDomainFunc(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.GetDomainById(client, state.Primary.Attributes["environment_id"], state.Primary.ID)
}

func TestAccResourceDomain_basic(t *testing.T) {
var (
obj interface{}

domainName = fmt.Sprintf("%s.com", acceptance.RandomAccResourceNameWithDash())

rName = "huaweicloud_cae_domain.test"
rc = acceptance.InitResourceCheck(rName, &obj, getResourceDomainFunc)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckCaeEnvironment(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),

Steps: []resource.TestStep{
{
Config: testAccResourceDomain_basic(domainName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "environment_id", acceptance.HW_CAE_ENVIRONMENT_ID),
resource.TestCheckResourceAttr(rName, "name", domainName),
resource.TestMatchResourceAttr(rName, "created_at",
regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}?(Z|([+-]\d{2}:\d{2}))$`)),
),
},
{
Config: testAccResourceDomain_existed(domainName),
ExpectError: regexp.MustCompile(`the domain has already existed`),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccDomainImportFunc(rName),
},
},
})
}

func testAccResourceDomain_basic(domainName string) string {
return fmt.Sprintf(`
resource "huaweicloud_cae_domain" "test" {
environment_id = "%[1]s"
name = "%[2]s"
}
`, acceptance.HW_CAE_ENVIRONMENT_ID, domainName)
}

func testAccResourceDomain_existed(domainName string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_cae_domain" "test2" {
environment_id = "%[2]s"
name = "%[3]s"
}
`, testAccResourceDomain_basic(domainName), acceptance.HW_CAE_ENVIRONMENT_ID, domainName)
}

func testAccDomainImportFunc(name string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[name]
if !ok {
return "", fmt.Errorf("resource (%s) not found: %s", name, rs)
}

var (
environmentId = rs.Primary.Attributes["environment_id"]
domainName = rs.Primary.Attributes["name"]
)
if environmentId == "" || domainName == "" {
return "", fmt.Errorf("some import IDs are missing, want '<environment_id>/<name>', but got '%s/%s'",
environmentId, domainName)
}

return fmt.Sprintf("%s/%s", environmentId, domainName), nil
}
}
Loading

0 comments on commit 3daf909

Please sign in to comment.