-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cae/app): add new resource to manage applications
- Loading branch information
1 parent
6d188aa
commit 4ac2850
Showing
4 changed files
with
417 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,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. | ||
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> | ||
``` |
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
137 changes: 137 additions & 0 deletions
137
huaweicloud/services/acceptance/cae/resource_huaweicloud_cae_application_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,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 GitHub Actions / golangci
|
||
) | ||
|
||
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) | ||
} |
Oops, something went wrong.