From d914ed00f35396ebbe5d85729841e0101fc803f7 Mon Sep 17 00:00:00 2001 From: luxiaohang Date: Mon, 4 Nov 2024 17:28:55 +0800 Subject: [PATCH] chore(waf): maintain waf certificate datasource and fix some problems --- docs/data-sources/waf_certificate.md | 30 ++-- huaweicloud/provider.go | 2 +- ...source_huaweicloud_waf_certificate_test.go | 141 +++++++++------- ...data_source_huaweicloud_waf_certificate.go | 156 ++++++++++++------ 4 files changed, 201 insertions(+), 128 deletions(-) diff --git a/docs/data-sources/waf_certificate.md b/docs/data-sources/waf_certificate.md index 57d50c4dc89..7eb969da993 100644 --- a/docs/data-sources/waf_certificate.md +++ b/docs/data-sources/waf_certificate.md @@ -2,12 +2,15 @@ subcategory: "Web Application Firewall (WAF)" layout: "huaweicloud" page_title: "HuaweiCloud: huaweicloud_waf_certificate" -description: "" +description: | + Use this data source to get the certificate of WAF within HuaweiCloud. --- # huaweicloud_waf_certificate -Get the certificate in the WAF, including the one pushed from SCM. +Use this data source to get the certificate of WAF within HuaweiCloud. + +-> When multiple pieces of data are queried, the datasource will process the first piece of data and put it back. ## Example Usage @@ -24,19 +27,20 @@ data "huaweicloud_waf_certificate" "certificate_1" { The following arguments are supported: -* `region` - (Optional, String) The region in which to obtain the WAF. If omitted, the provider-level region will be - used. +* `region` - (Optional, String) Specifies the region in which to obtain the WAF. If omitted, the provider-level region + will be used. -* `name` - (Required, String) The name of certificate. The value is case sensitive and supports fuzzy matching. +* `name` - (Optional, String) Specifies the name of certificate. The value is case-sensitive and supports fuzzy matching. - -> **NOTE:** The certificate name is not unique. Only returns the last created one when matched multiple certificates. +* `enterprise_project_id` - (Optional, String) Specifies the enterprise project ID of WAF certificate. + For enterprise users, if omitted, default enterprise project will be used. -* `expire_status` - (Optional, Int) The expire status of certificate. Defaults is `0`. The value can be: - + `0`: not expire - + `1`: has expired - + `2`: wil expired soon +* `expiration_status` - (Optional, String) Specifies the certificate expiration status. The options are as follows: + + `0`: Not expired; + + `1`: Expired; + + `2`: Expired soon (The certificate will expire in one month.) -* `enterprise_project_id` - (Optional, String) The enterprise project ID of WAF certificate. + -> If this field is not configured, all certificates that meet the expired status will be found. ## Attribute Reference @@ -44,4 +48,6 @@ In addition to all arguments above, the following attributes are exported: * `id` - The certificate ID in UUID format. -* `expiration` - Indicates the time when the certificate expires. +* `created_at` - Indicates the time when the certificate uploaded, in RFC3339 format. + +* `expired_at` - Indicates the time when the certificate expires, in RFC3339 format. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 3557c7de6ea..5ebe9ad5a4e 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -1075,7 +1075,7 @@ func Provider() *schema.Provider { "huaweicloud_vpn_user_groups": vpn.DataSourceVpnUserGroups(), "huaweicloud_waf_address_groups": waf.DataSourceWafAddressGroups(), - "huaweicloud_waf_certificate": waf.DataSourceWafCertificateV1(), + "huaweicloud_waf_certificate": waf.DataSourceWafCertificate(), "huaweicloud_waf_dedicated_domains": waf.DataSourceWafDedicatedDomains(), "huaweicloud_waf_dedicated_instances": waf.DataSourceWafDedicatedInstancesV1(), "huaweicloud_waf_domains": waf.DataSourceWafDomains(), diff --git a/huaweicloud/services/acceptance/waf/data_source_huaweicloud_waf_certificate_test.go b/huaweicloud/services/acceptance/waf/data_source_huaweicloud_waf_certificate_test.go index b0910aa8f29..af4d696cf7c 100644 --- a/huaweicloud/services/acceptance/waf/data_source_huaweicloud_waf_certificate_test.go +++ b/huaweicloud/services/acceptance/waf/data_source_huaweicloud_waf_certificate_test.go @@ -5,39 +5,18 @@ import ( "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/services/acceptance" - "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils/fmtp" ) -func TestAccDataSourceWafCertificateV1_basic(t *testing.T) { - name := acceptance.RandomAccResourceName() - dataSourceName := "data.huaweicloud_waf_certificate.cert_1" +// Before running the test case, please ensure that there is at least one WAF instance in the current region. +func TestAccDataSourceWafCertificate_basic(t *testing.T) { + var ( + name = acceptance.RandomAccResourceName() - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { - acceptance.TestAccPreCheck(t) - acceptance.TestAccPrecheckWafInstance(t) - }, - ProviderFactories: acceptance.TestAccProviderFactories, - Steps: []resource.TestStep{ - { - Config: testAccWafCertificateListV1_conf(name), - Check: resource.ComposeTestCheckFunc( - testAccCheckWafCertDataSourceID(dataSourceName), - resource.TestCheckResourceAttr(dataSourceName, "name", name), - resource.TestCheckResourceAttr(dataSourceName, "expire_status", "1"), - resource.TestCheckResourceAttrSet(dataSourceName, "expiration"), - ), - }, - }, - }) -} - -func TestAccDataSourceWafCertificateV1_withEpsID(t *testing.T) { - name := acceptance.RandomAccResourceName() - dataSourceName := "data.huaweicloud_waf_certificate.cert_1" + datasourceName = "data.huaweicloud_waf_certificate.test" + dc = acceptance.InitDataSourceCheck(datasourceName) + ) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { @@ -48,59 +27,93 @@ func TestAccDataSourceWafCertificateV1_withEpsID(t *testing.T) { ProviderFactories: acceptance.TestAccProviderFactories, Steps: []resource.TestStep{ { - Config: testAccWafCertificateListV1_conf_withEpsID(name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST), + Config: testAccWafCertificateList_basic(name), Check: resource.ComposeTestCheckFunc( - testAccCheckWafCertDataSourceID(dataSourceName), - resource.TestCheckResourceAttr(dataSourceName, "enterprise_project_id", acceptance.HW_ENTERPRISE_PROJECT_ID_TEST), - resource.TestCheckResourceAttr(dataSourceName, "name", name), - resource.TestCheckResourceAttr(dataSourceName, "expire_status", "1"), - resource.TestCheckResourceAttrSet(dataSourceName, "expiration"), + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(datasourceName, "name"), + resource.TestCheckResourceAttrSet(datasourceName, "enterprise_project_id"), + resource.TestCheckResourceAttrSet(datasourceName, "created_at"), ), }, }, }) } -func testAccCheckWafCertDataSourceID(r string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[r] - if !ok { - return fmtp.Errorf("Can't find waf data source: %s ", r) - } - if rs.Primary.ID == "" { - return fmtp.Errorf("The Waf Certificate data source ID not set ") - } - return nil - } -} - -func testAccWafCertificateListV1_conf(name string) string { +func testAccWafCertificateList_base(name string) string { return fmt.Sprintf(` -%s +resource "huaweicloud_waf_certificate" "test" { + name = "%[1]s" + enterprise_project_id = "%[2]s" -data "huaweicloud_waf_certificate" "cert_1" { - name = huaweicloud_waf_certificate.certificate_1.name - expire_status = 1 + certificate = <