From a3b019f0a9305e762120b00ec527a0f8a4c580f1 Mon Sep 17 00:00:00 2001 From: houpeng80 Date: Mon, 6 Jan 2025 16:13:09 +0800 Subject: [PATCH] feat(GaussDB): add gaussdb opengauss ssl cert download link data source --- ...aussdb_opengauss_ssl_cert_download_link.md | 38 +++++++ huaweicloud/provider.go | 27 ++--- ...b_opengauss_ssl_cert_download_link_test.go | 103 +++++++++++++++++ ..._opengauss_ssl_cert_download_links_test.go | 105 ++++++++++++++++++ ...aussdb_opengauss_ssl_cert_download_link.go | 99 +++++++++++++++++ 5 files changed, 359 insertions(+), 13 deletions(-) create mode 100644 docs/data-sources/gaussdb_opengauss_ssl_cert_download_link.md create mode 100644 huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link_test.go create mode 100644 huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_links_test.go create mode 100644 huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link.go diff --git a/docs/data-sources/gaussdb_opengauss_ssl_cert_download_link.md b/docs/data-sources/gaussdb_opengauss_ssl_cert_download_link.md new file mode 100644 index 00000000000..9d903ebbf89 --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_ssl_cert_download_link.md @@ -0,0 +1,38 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_ssl_cert_download_link" +description: |- + Use this data source to get the address for downloading the SSL certificate of a GaussDB OpenGauss instance. +--- + +# huaweicloud_gaussdb_opengauss_ssl_cert_download_link + +Use this data source to get the address for downloading the SSL certificate of a GaussDB OpenGauss instance. + +## Example Usage + +```hcl +variable "instance_id" {} + +data "huaweicloud_gaussdb_opengauss_ssl_cert_download_link" "test"{ + instance_id = var.instance_id +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) Specifies the region in which to query the resource. + If omitted, the provider-level region will be used. + +* `instance_id` - (Required, String) Specifies the ID of the GaussDB OpenGauss instance. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `download_link` - Indicates the download address of the SSL certificate. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 4e7dfbe6e56..30083344e1d 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -814,19 +814,20 @@ func Provider() *schema.Provider { "huaweicloud_gaussdb_redis_flavors": geminidb.DataSourceGaussDBRedisFlavors(), "huaweicloud_gaussdb_influx_instances": geminidb.DataSourceGaussDBInfluxInstances(), - "huaweicloud_gaussdb_opengauss_storage_types": gaussdb.DataSourceGaussdbOpengaussStorageTypes(), - "huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(), - "huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(), - "huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(), - "huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(), - "huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(), - "huaweicloud_gaussdb_opengauss_instance_coordinators": gaussdb.DataSourceGaussdbOpengaussInstanceCoordinators(), - "huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(), - "huaweicloud_gaussdb_opengauss_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(), - "huaweicloud_gaussdb_opengauss_pt_modify_records": gaussdb.DataSourceGaussdbOpengaussPtModifyRecords(), - "huaweicloud_gaussdb_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(), - "huaweicloud_gaussdb_opengauss_restorable_instances": gaussdb.DataSourceGaussdbOpengaussRestorableInstances(), - "huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(), + "huaweicloud_gaussdb_opengauss_storage_types": gaussdb.DataSourceGaussdbOpengaussStorageTypes(), + "huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(), + "huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(), + "huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(), + "huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(), + "huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(), + "huaweicloud_gaussdb_opengauss_instance_coordinators": gaussdb.DataSourceGaussdbOpengaussInstanceCoordinators(), + "huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(), + "huaweicloud_gaussdb_opengauss_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(), + "huaweicloud_gaussdb_opengauss_pt_modify_records": gaussdb.DataSourceGaussdbOpengaussPtModifyRecords(), + "huaweicloud_gaussdb_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(), + "huaweicloud_gaussdb_opengauss_restorable_instances": gaussdb.DataSourceGaussdbOpengaussRestorableInstances(), + "huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(), + "huaweicloud_gaussdb_opengauss_ssl_cert_download_link": gaussdb.DataSourceGaussdbOpengaussSslCertDownloadLink(), "huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(), "huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link_test.go new file mode 100644 index 00000000000..0d8ecfc5a96 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link_test.go @@ -0,0 +1,103 @@ +package gaussdb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" +) + +func TestAccDataSourceGaussdbOpengaussSslCertDownloadLink_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_ssl_cert_download_link.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGaussdbOpengaussSslCertDownloadLink_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "download_link"), + ), + }, + }, + }) +} + +func testDataSourceGaussdbOpengaussSslCertDownloadLink_base(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_gaussdb_opengauss_flavors" "test" { + version = "8.201" + ha_mode = "centralization_standard" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "ingress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "egress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_gaussdb_opengauss_instance" "test" { + depends_on = [ + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss_egress + ] + + vpc_id = huaweicloud_vpc.test.id + subnet_id = huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.test.id + flavor = data.huaweicloud_gaussdb_opengauss_flavors.test.flavors[0].spec_code + name = "%[2]s" + password = "Huangwei!120521" + enterprise_project_id = "%[3]s" + + availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], + data.huaweicloud_availability_zones.test.names[1], + data.huaweicloud_availability_zones.test.names[2]]) + + ha { + mode = "centralization_standard" + replication_mode = "sync" + consistency = "eventual" + instance_mode = "basic" + } + + volume { + type = "ULTRAHIGH" + size = 40 + } +} +`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceGaussdbOpengaussSslCertDownloadLink_basic(name string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_gaussdb_opengauss_ssl_cert_download_link" "test" { + instance_id = huaweicloud_gaussdb_opengauss_instance.test.id +} +`, testDataSourceGaussdbOpengaussSslCertDownloadLink_base(name)) +} diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_links_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_links_test.go new file mode 100644 index 00000000000..6525466ee00 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_links_test.go @@ -0,0 +1,105 @@ +package gaussdb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" +) + +func TestAccDataSourceGaussdbOpengaussSslCertDownloadLinks_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_ssl_cert_download_links.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGaussdbOpengaussSslCertDownloadLinks_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "download_link"), + ), + }, + }, + }) +} + +func testDataSourceGaussdbOpengaussSslCertDownloadLinks_base(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_gaussdb_opengauss_flavors" "test" { + version = "8.201" + ha_mode = "centralization_standard" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "ingress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "egress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_gaussdb_opengauss_instance" "test" { + depends_on = [ + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss_egress + ] + + vpc_id = huaweicloud_vpc.test.id + subnet_id = huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.test.id + flavor = data.huaweicloud_gaussdb_opengauss_flavors.test.flavors[0].spec_code + name = "%[2]s" + password = "Huangwei!120521" + enterprise_project_id = "%[3]s" + + availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], + data.huaweicloud_availability_zones.test.names[1], + data.huaweicloud_availability_zones.test.names[2]]) + + ha { + mode = "centralization_standard" + replication_mode = "sync" + consistency = "eventual" + instance_mode = "basic" + } + + volume { + type = "ULTRAHIGH" + size = 40 + } +} +`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceGaussdbOpengaussSslCertDownloadLinks_basic(name string) string { + return fmt.Sprintf(` +%s + +data "huaweicloud_gaussdb_opengauss_ssl_cert_download_links" "test" { + instance_id = huaweicloud_gaussdb_opengauss_instance.test.id +} +`, testDataSourceGaussdbOpengaussSslCertDownloadLinks_base(name)) +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link.go new file mode 100644 index 00000000000..6d8c7734c77 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_ssl_cert_download_link.go @@ -0,0 +1,99 @@ +// Generated by PMS #511 +package gaussdb + +import ( + "context" + "strings" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/tidwall/gjson" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" +) + +func DataSourceGaussdbOpengaussSslCertDownloadLink() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceGaussdbOpengaussSslCertDownloadLinkRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, + }, + "instance_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the ID of the GaussDB OpenGauss instance.`, + }, + "download_link": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the download address of the SSL certificate.`, + }, + }, + } +} + +type OpengaussSslCertDownloadLinkDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newOpengaussSslCertDownloadLinkDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussSslCertDownloadLinkDSWrapper { + return &OpengaussSslCertDownloadLinkDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceGaussdbOpengaussSslCertDownloadLinkRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newOpengaussSslCertDownloadLinkDSWrapper(d, meta) + shoSslCerDowLinRst, err := wrapper.ShowSslCertDownloadLink() + if err != nil { + return diag.FromErr(err) + } + + err = wrapper.showSslCertDownloadLinkToSchema(shoSslCerDowLinRst) + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + return nil +} + +// @API GaussDB GET /v3/{project_id}/instances/{instance_id}/ssl-cert/download-link +func (w *OpengaussSslCertDownloadLinkDSWrapper) ShowSslCertDownloadLink() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "opengauss") + if err != nil { + return nil, err + } + + uri := "/v3/{project_id}/instances/{instance_id}/ssl-cert/download-link" + uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string)) + return httphelper.New(client). + Method("GET"). + URI(uri). + Request(). + Result() +} + +func (w *OpengaussSslCertDownloadLinkDSWrapper) showSslCertDownloadLinkToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("download_link", body.Get("download_link").Value()), + ) + return mErr.ErrorOrNil() +}