Skip to content

feat(RDS): add rds ssl cert download links data source #6709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/data-sources/rds_ssl_cert_download_links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
subcategory: "Relational Database Service (RDS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_rds_ssl_cert_download_links"
description: |-
Use this data source to get the list of RDS instance SSL certificates.
---

# huaweicloud_rds_ssl_cert_download_links

Use this data source to get the list of RDS instance SSL certificates.

## Example Usage

```hcl
data "huaweicloud_rds_ssl_cert_download_links" "test" {}
```

## 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 RDS instance.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `cert_info_list` - Indicates the list of certificates.

The [cert_info_list](#cert_info_list_struct) structure is documented below.

<a name="cert_info_list_struct"></a>
The `cert_info_list` block supports:

* `download_link` - Indicates the download link of certificate.

* `category` - Indicates the category of certificate.
The value can be: **international**, **national**.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ func Provider() *schema.Provider {
"huaweicloud_rds_extend_log_files": rds.DataSourceRdsExtendLogFiles(),
"huaweicloud_rds_extend_log_links": rds.DataSourceRdsExtendLogLinks(),
"huaweicloud_rds_slow_log_files": rds.DataSourceRdsSlowLogFiles(),
"huaweicloud_rds_ssl_cert_download_links": rds.DataSourceRdsSslCertDownloadLinks(),
"huaweicloud_rds_quotas": rds.DataSourceRdsQuotas(),
"huaweicloud_rds_tags": rds.DataSourceRdsTags(),
"huaweicloud_rds_predefined_tags": rds.DataSourceRdsPredefinedTags(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package rds

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 TestAccDataSourceRdsSslCertDownloadLinks_basic(t *testing.T) {
dataSource := "data.huaweicloud_rds_ssl_cert_download_links.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: testDataSourceRdsSslCertDownloadLinks_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.#"),
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.0.download_link"),
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.0.category"),
),
},
},
})
}

func testDataSourceRdsSslCertDownloadLinks_base(name string) string {
return fmt.Sprintf(`
%[1]s

data "huaweicloud_rds_flavors" "test" {
db_type = "MySQL"
db_version = "8.0"
instance_mode = "single"
group_type = "dedicated"
}

resource "huaweicloud_rds_instance" "test" {
name = "%[2]s"
flavor = data.huaweicloud_rds_flavors.test.flavors[0].name
security_group_id = huaweicloud_networking_secgroup.test.id
subnet_id = huaweicloud_vpc_subnet.test.id
vpc_id = huaweicloud_vpc.test.id
availability_zone = slice(sort(data.huaweicloud_rds_flavors.test.flavors[0].availability_zones), 0, 1)

db {
password = "Huangwei!120521"
type = "MySQL"
version = "8.0"
port = 3306
}

volume {
type = "CLOUDSSD"
size = 40
}
}
`, common.TestBaseNetwork(name), name)
}

func testDataSourceRdsSslCertDownloadLinks_basic(name string) string {
return fmt.Sprintf(`
%[1]s

data "huaweicloud_rds_ssl_cert_download_links" "test" {
instance_id = huaweicloud_rds_instance.test.id
}
`, testDataSourceRdsSslCertDownloadLinks_base(name))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Generated by PMS #614
package rds

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 DataSourceRdsSslCertDownloadLinks() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRdsSslCertDownloadLinksRead,

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 RDS instance.`,
},
"cert_info_list": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the list of certificates.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"download_link": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the download link of certificate.`,
},
"category": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the category of certificate.`,
},
},
},
},
},
}
}

type SslCertDownloadLinksDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newSslCertDownloadLinksDSWrapper(d *schema.ResourceData, meta interface{}) *SslCertDownloadLinksDSWrapper {
return &SslCertDownloadLinksDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceRdsSslCertDownloadLinksRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newSslCertDownloadLinksDSWrapper(d, meta)
lisSslCerDowLinRst, err := wrapper.ListSslCertDownloadLink()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listSslCertDownloadLinkToSchema(lisSslCerDowLinRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API RDS GET /v3/{project_id}/instances/{instance_id}/ssl-cert/download-link
func (w *SslCertDownloadLinksDSWrapper) ListSslCertDownloadLink() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rds")
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 *SslCertDownloadLinksDSWrapper) listSslCertDownloadLinkToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("cert_info_list", schemas.SliceToList(body.Get("cert_info_list"),
func(certInfoList gjson.Result) any {
return map[string]any{
"download_link": certInfoList.Get("download_link").Value(),
"category": certInfoList.Get("category").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading