From 30301309a9b27d18b4db67fc60a6015d6dfe4e34 Mon Sep 17 00:00:00 2001 From: ChengXiangdong Date: Thu, 22 Jul 2021 17:18:33 +0800 Subject: [PATCH] Add data source for Dedicated ELB certificates(#1264). --- docs/data-sources/elb_certificate.md | 46 +++++++ huaweicloud/provider.go | 2 + ...source_huaweicloud_elb_certificate_test.go | 120 ++++++++++++++++++ ...data_source_huaweicloud_elb_certificate.go | 109 ++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 docs/data-sources/elb_certificate.md create mode 100644 huaweicloud/services/acceptance/elb/data_source_huaweicloud_elb_certificate_test.go create mode 100644 huaweicloud/services/elb/data_source_huaweicloud_elb_certificate.go diff --git a/docs/data-sources/elb_certificate.md b/docs/data-sources/elb_certificate.md new file mode 100644 index 0000000000..6db036fb37 --- /dev/null +++ b/docs/data-sources/elb_certificate.md @@ -0,0 +1,46 @@ +--- +subcategory: "Dedicated Load Balance (Dedicated ELB)" +--- + +# huaweicloud_elb_certificate + +Use this data source to get the certificate in HuaweiCloud Dedicated Load Balance (Dedicated ELB). + +## Example Usage + +```hcl +variable "certificate_name" {} + +data "huaweicloud_elb_certificate" "test" { + name = var.certificate_name +} +``` +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) The region in which to obtain the Dedicated ELB certificate. + If omitted, the provider-level region will be used. + +* `name` - (Required, String) The name of certificate. + The value is case sensitive and does not supports fuzzy matching. + + -> **NOTE:** The certificate name is not unique. + Only returns the last created one when matched multiple certificates. + + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The certificate ID in UUID format. + +* `domain` - The domain of the Certificate. This parameter is valid only when `type` is "server". + +* `type` - Specifies the certificate type. The value can be one of the following: + * `server`: indicates the server certificate. + * `client`: indicates the CA certificate. + +* `description` - Human-readable description for the Certificate. + +* `expiration` - Indicates the time when the certificate expires. \ No newline at end of file diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index d316d3b005..8140ae2b6c 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -12,6 +12,7 @@ import ( "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/apig" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/deprecated" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/elb" "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/waf" ) @@ -302,6 +303,7 @@ func Provider() terraform.ResourceProvider { "huaweicloud_kms_key": DataSourceKmsKeyV1(), "huaweicloud_kms_data_key": DataSourceKmsDataKeyV1(), "huaweicloud_lb_loadbalancer": DataSourceELBV2Loadbalancer(), + "huaweicloud_elb_certificate": elb.DataSourceELBCertificateV3(), "huaweicloud_nat_gateway": DataSourceNatGatewayV2(), "huaweicloud_networking_port": DataSourceNetworkingPortV2(), "huaweicloud_networking_secgroup": DataSourceNetworkingSecGroupV2(), diff --git a/huaweicloud/services/acceptance/elb/data_source_huaweicloud_elb_certificate_test.go b/huaweicloud/services/acceptance/elb/data_source_huaweicloud_elb_certificate_test.go new file mode 100644 index 0000000000..38226613e7 --- /dev/null +++ b/huaweicloud/services/acceptance/elb/data_source_huaweicloud_elb_certificate_test.go @@ -0,0 +1,120 @@ +/* + Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. +*/ + +package elb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/fmtp" +) + +func TestAccDataSourceELbCertificateV3_basic(t *testing.T) { + name := fmt.Sprintf("cert-%s", acctest.RandString(6)) + dataSourceName := "data.huaweicloud_elb_certificate.cert_1" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + Providers: acceptance.TestAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccELbCertDataSourceV3_conf(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckELBCertDataSourceID(dataSourceName), + resource.TestCheckResourceAttr(dataSourceName, "name", name), + resource.TestCheckResourceAttrSet(dataSourceName, "expiration"), + resource.TestCheckResourceAttrSet(dataSourceName, "domain"), + resource.TestCheckResourceAttrSet(dataSourceName, "type"), + resource.TestCheckResourceAttrSet(dataSourceName, "description"), + ), + }, + }, + }) +} + +func testAccCheckELBCertDataSourceID(r string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[r] + if !ok { + return fmtp.Errorf("Can't find Dedicated ELB data source: %s ", r) + } + if rs.Primary.ID == "" { + return fmtp.Errorf("The Dedicated ELB Certificate data source ID not set.") + } + return nil + } +} + +func testAccELbCertDataSourceV3_conf(name string) string { + return fmt.Sprintf(` +data "huaweicloud_elb_certificate" "cert_1" { + name = huaweicloud_elb_certificate.certificate_1.name +} + +resource "huaweicloud_elb_certificate" "certificate_1" { + name = "%s" + description = "terraform test certificate" + domain = "www.elb.com" + private_key = < 0 { + setCertificateAttributes(d, certs[0]) + if err != nil { + return err + } + } else { + return fmtp.Errorf("Your query returned no results. " + + "Please change your search criteria and try again.") + } + + return nil +} + +func setCertificateAttributes(d *schema.ResourceData, c certificates.Certificate) error { + d.SetId(c.ID) + + var expiration string + tm, err := time.Parse(time.RFC3339, c.ExpireTime) + if err != nil { + // If the format of ExpireTime is not expected, set the original value directly. + expiration = c.ExpireTime + logp.Printf("[WAIN] The format of the ExpireTime field of the Dedicated ELB certificate "+ + "is not expected:%s", c.ExpireTime) + } else { + expiration = tm.Format("2006-01-02 15:04:05 MST") + } + + mErr := multierror.Append(nil, + d.Set("name", c.Name), + d.Set("domain", c.Domain), + d.Set("description", c.Description), + d.Set("type", c.Type), + d.Set("expiration", expiration), + ) + if err := mErr.ErrorOrNil(); err != nil { + return fmtp.Errorf("error setting Dedicated ELB Certificate fields: %s", err) + } + return nil +}