-
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): add new resource to manage certificate (#6207)
- Loading branch information
1 parent
a702aa2
commit 2c86918
Showing
4 changed files
with
466 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,66 @@ | ||
--- | ||
subcategory: "Cloud Application Engine (CAE)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_cae_certificate" | ||
description: |- | ||
Manages a certificate resource within HuaweiCloud. | ||
--- | ||
|
||
# huaweicloud_cae_certificate | ||
|
||
Manages a certificate resource within HuaweiCloud. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "environment_id" {} | ||
variable "certificate_name" {} | ||
variable "certificate_content" {} | ||
variable "certificate_private_key" {} | ||
resource "huaweicloud_cae_certificate" "test" { | ||
environment_id = var.environment_id | ||
name = var.certificate_name | ||
crt = var.certificate_content | ||
key = var.certificate_private_key | ||
} | ||
``` | ||
|
||
## 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 name of the certificate. | ||
Changing this creates a new resource. | ||
The maximum length of the name is `64` characters, only lowercase letters, digits, hyphens (-) and dots (.) are | ||
allowed. | ||
The name must start and end with a lowercase letter or a digit. | ||
|
||
* `crt` - (Required, String) Specifies the content of the certificate. | ||
Base64 format corresponding to PEM encoding. | ||
|
||
* `key` - (Required, String) Specifies the private key of the certificate. | ||
Base64 format corresponding to PEM encoding. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The resource ID. | ||
|
||
* `created_at` - The creation time of the certificate, in RFC3339 format. | ||
|
||
## Import | ||
|
||
The certificate resource can be imported using `environment_id` and `name`, separated by a slash (/), e.g. | ||
|
||
```bash | ||
$ terraform import huaweicloud_cae_certificate.test <environment_id>/<name> | ||
``` |
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
108 changes: 108 additions & 0 deletions
108
huaweicloud/services/acceptance/cae/resource_huaweicloud_cae_certificate_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,108 @@ | ||
package cae | ||
|
||
import ( | ||
"fmt" | ||
"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 getCertificateFunc(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) | ||
} | ||
|
||
environmentId := state.Primary.Attributes["environment_id"] | ||
return cae.GetCertificateById(client, environmentId, state.Primary.ID) | ||
} | ||
|
||
func TestAccCertificate_basic(t *testing.T) { | ||
var ( | ||
obj interface{} | ||
|
||
name = acceptance.RandomAccResourceNameWithDash() | ||
|
||
rName = "huaweicloud_cae_certificate.test" | ||
rc = acceptance.InitResourceCheck( | ||
rName, | ||
&obj, | ||
getCertificateFunc, | ||
) | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckCaeEnvironment(t) | ||
acceptance.TestAccPreCheckCertificateWithoutRootCA(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: rc.CheckResourceDestroy(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCertificate_basic(name, acceptance.HW_CERTIFICATE_CONTENT, acceptance.HW_CERTIFICATE_PRIVATE_KEY), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(rName, "environment_id", acceptance.HW_CAE_ENVIRONMENT_ID), | ||
resource.TestCheckResourceAttr(rName, "name", name), | ||
resource.TestCheckResourceAttr(rName, "crt", acceptance.HW_CERTIFICATE_CONTENT), | ||
resource.TestCheckResourceAttr(rName, "key", acceptance.HW_CERTIFICATE_PRIVATE_KEY), | ||
), | ||
}, | ||
{ | ||
Config: testAccCertificate_basic(name, acceptance.HW_NEW_CERTIFICATE_CONTENT, acceptance.HW_NEW_CERTIFICATE_PRIVATE_KEY), | ||
Check: resource.ComposeTestCheckFunc( | ||
rc.CheckResourceExists(), | ||
resource.TestCheckResourceAttr(rName, "crt", acceptance.HW_NEW_CERTIFICATE_CONTENT), | ||
resource.TestCheckResourceAttr(rName, "key", acceptance.HW_NEW_CERTIFICATE_PRIVATE_KEY), | ||
), | ||
}, | ||
{ | ||
ResourceName: rName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateIdFunc: testAccCertificateImportStateFunc(rName), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCertificate_basic(name, content, privateKey string) string { | ||
return fmt.Sprintf(` | ||
resource "huaweicloud_cae_certificate" "test" { | ||
environment_id = "%[1]s" | ||
name = "%[2]s" | ||
# Base64 format corresponding to PEM encoding. | ||
crt = "%[3]s" | ||
key = "%[4]s" | ||
} | ||
`, acceptance.HW_CAE_ENVIRONMENT_ID, name, content, privateKey) | ||
} | ||
|
||
func testAccCertificateImportStateFunc(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"] | ||
certificateName = rs.Primary.Attributes["name"] | ||
) | ||
if environmentId == "" || certificateName == "" { | ||
return "", fmt.Errorf("some import IDs are missing, want '<environment_id>/<name>', but got '%s/%s'", | ||
environmentId, certificateName) | ||
} | ||
|
||
return fmt.Sprintf("%s/%s", environmentId, certificateName), nil | ||
} | ||
} |
Oops, something went wrong.