Skip to content

Commit

Permalink
feat(cae/app): add new resource to manage applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 committed Jan 7, 2025
1 parent 6d188aa commit 4ac2850
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 0 deletions.
56 changes: 56 additions & 0 deletions docs/resources/cae_application.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_application"
description: |-
Manages an application resource within HuaweiCloud.
---

# huaweicloud_cae_application

Manages an application resource within HuaweiCloud.

## Example Usage

```hcl
variable "environment_id" {}
variable "application_name" {}
resource "huaweicloud_cae_application" "test" {
environment_id = var.environment_id
name = var.application_name
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region where the application is located.
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 environment to which the application belongs.

Check failure on line 32 in docs/resources/cae_application.md

View workflow job for this annotation

GitHub Actions / markdownlint

Line length [Expected: 120; Actual: 121]
Changing this creates a new resource.

* `name` - (Required, String, ForceNew) Specifies the name of the application.
The valid length is limited from `2` to `63`, 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.

## Attribute Reference

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

* `id` - The resource ID.

* `created_at` - The creation time of the application, in RFC3339 format.

* `updated_at` - The latest update time of the application, in RFC3339 format.

## Import

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

```bash
$ terraform import huaweicloud_cae_application.test <environment_id>/<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_bms_instance": bms.ResourceBmsInstance(),
"huaweicloud_bcs_instance": bcs.ResourceInstance(),

"huaweicloud_cae_application": cae.ResourceApplication(),
"huaweicloud_cae_component": cae.ResourceComponent(),
"huaweicloud_cae_component_configurations": cae.ResourceComponentConfigurations(),
"huaweicloud_cae_component_deployment": cae.ResourceComponentDeployment(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
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"

Check failure on line 13 in huaweicloud/services/acceptance/cae/resource_huaweicloud_cae_application_test.go

View workflow job for this annotation

GitHub Actions / golangci

could not import github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cae (-: # github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cae
)

func getApplicationFunc(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)
}

envId := state.Primary.Attributes["environment_id"]
return cae.GetApplicationById(client, envId, state.Primary.ID)
}

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

resourceName = "huaweicloud_cae_application.test"
rc = acceptance.InitResourceCheck(resourceName, &obj, getApplicationFunc)

invalidName = "-tf-test-invalid-name"
name = acceptance.RandomAccResourceNameWithDash()
baseConfig = testAccApplication_base(name)
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccApplication_basic(baseConfig, invalidName),
ExpectError: regexp.MustCompile(`CAE.01500214`),
},
{
Config: testAccApplication_basic(baseConfig, name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceName, "name", name),
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,
ImportStateIdFunc: testAccApplicationImportStateFunc(resourceName),
},
},
})
}

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

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

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

func testAccApplication_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"
}
resource "huaweicloud_cae_environment" "test" {
name = "%[1]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
}
`, name)
}

func testAccApplication_basic(baseConfig, name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_cae_application" "test" {
environment_id = huaweicloud_cae_environment.test.id
name = "%[2]s"
}
`, baseConfig, name)
}
Loading

0 comments on commit 4ac2850

Please sign in to comment.