Skip to content

Commit c05b6fa

Browse files
committed
feat(RDS): add rds ssl cert download links data source
1 parent 05c9853 commit c05b6fa

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Relational Database Service (RDS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_rds_ssl_cert_download_links"
5+
description: |-
6+
Use this data source to get the list of RDS instance SSL certificates.
7+
---
8+
9+
# huaweicloud_rds_ssl_cert_download_links
10+
11+
Use this data source to get the list of RDS instance SSL certificates.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_rds_ssl_cert_download_links" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource.
24+
If omitted, the provider-level region will be used.
25+
26+
* `instance_id` - (Required, String) Specifies the ID of RDS instance.
27+
28+
## Attribute Reference
29+
30+
In addition to all arguments above, the following attributes are exported:
31+
32+
* `id` - The data source ID.
33+
34+
* `cert_info_list` - Indicates the list of certificates.
35+
36+
The [cert_info_list](#cert_info_list_struct) structure is documented below.
37+
38+
<a name="cert_info_list_struct"></a>
39+
The `cert_info_list` block supports:
40+
41+
* `download_link` - Indicates the download link of certificate.
42+
43+
* `category` - Indicates the category of certificate.
44+
The value can be: **international**, **national**.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ func Provider() *schema.Provider {
11731173
"huaweicloud_rds_extend_log_files": rds.DataSourceRdsExtendLogFiles(),
11741174
"huaweicloud_rds_extend_log_links": rds.DataSourceRdsExtendLogLinks(),
11751175
"huaweicloud_rds_slow_log_files": rds.DataSourceRdsSlowLogFiles(),
1176+
"huaweicloud_rds_ssl_cert_download_links": rds.DataSourceRdsSslCertDownloadLinks(),
11761177
"huaweicloud_rds_quotas": rds.DataSourceRdsQuotas(),
11771178
"huaweicloud_rds_tags": rds.DataSourceRdsTags(),
11781179
"huaweicloud_rds_predefined_tags": rds.DataSourceRdsPredefinedTags(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package rds
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common"
11+
)
12+
13+
func TestAccDataSourceRdsSslCertDownloadLinks_basic(t *testing.T) {
14+
dataSource := "data.huaweicloud_rds_ssl_cert_download_links.test"
15+
rName := acceptance.RandomAccResourceName()
16+
dc := acceptance.InitDataSourceCheck(dataSource)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
acceptance.TestAccPreCheck(t)
21+
},
22+
ProviderFactories: acceptance.TestAccProviderFactories,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: testDataSourceRdsSslCertDownloadLinks_basic(rName),
26+
Check: resource.ComposeTestCheckFunc(
27+
dc.CheckResourceExists(),
28+
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.#"),
29+
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.0.download_link"),
30+
resource.TestCheckResourceAttrSet(dataSource, "cert_info_list.0.category"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testDataSourceRdsSslCertDownloadLinks_base(name string) string {
38+
return fmt.Sprintf(`
39+
%[1]s
40+
41+
data "huaweicloud_rds_flavors" "test" {
42+
db_type = "MySQL"
43+
db_version = "8.0"
44+
instance_mode = "single"
45+
group_type = "dedicated"
46+
}
47+
48+
resource "huaweicloud_rds_instance" "test" {
49+
name = "%[2]s"
50+
flavor = data.huaweicloud_rds_flavors.test.flavors[0].name
51+
security_group_id = huaweicloud_networking_secgroup.test.id
52+
subnet_id = huaweicloud_vpc_subnet.test.id
53+
vpc_id = huaweicloud_vpc.test.id
54+
availability_zone = slice(sort(data.huaweicloud_rds_flavors.test.flavors[0].availability_zones), 0, 1)
55+
56+
db {
57+
password = "Huangwei!120521"
58+
type = "MySQL"
59+
version = "8.0"
60+
port = 3306
61+
}
62+
63+
volume {
64+
type = "CLOUDSSD"
65+
size = 40
66+
}
67+
}
68+
`, common.TestBaseNetwork(name), name)
69+
}
70+
71+
func testDataSourceRdsSslCertDownloadLinks_basic(name string) string {
72+
return fmt.Sprintf(`
73+
%[1]s
74+
75+
data "huaweicloud_rds_ssl_cert_download_links" "test" {
76+
instance_id = huaweicloud_rds_instance.test.id
77+
}
78+
`, testDataSourceRdsSslCertDownloadLinks_base(name))
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Generated by PMS #614
2+
package rds
3+
4+
import (
5+
"context"
6+
"strings"
7+
8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/go-uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/tidwall/gjson"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
17+
)
18+
19+
func DataSourceRdsSslCertDownloadLinks() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceRdsSslCertDownloadLinksRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
29+
},
30+
"instance_id": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: `Specifies the ID of RDS instance.`,
34+
},
35+
"cert_info_list": {
36+
Type: schema.TypeList,
37+
Computed: true,
38+
Description: `Indicates the list of certificates.`,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"download_link": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: `Indicates the download link of certificate.`,
45+
},
46+
"category": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: `Indicates the category of certificate.`,
50+
},
51+
},
52+
},
53+
},
54+
},
55+
}
56+
}
57+
58+
type SslCertDownloadLinksDSWrapper struct {
59+
*schemas.ResourceDataWrapper
60+
Config *config.Config
61+
}
62+
63+
func newSslCertDownloadLinksDSWrapper(d *schema.ResourceData, meta interface{}) *SslCertDownloadLinksDSWrapper {
64+
return &SslCertDownloadLinksDSWrapper{
65+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
66+
Config: meta.(*config.Config),
67+
}
68+
}
69+
70+
func dataSourceRdsSslCertDownloadLinksRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
71+
wrapper := newSslCertDownloadLinksDSWrapper(d, meta)
72+
lisSslCerDowLinRst, err := wrapper.ListSslCertDownloadLink()
73+
if err != nil {
74+
return diag.FromErr(err)
75+
}
76+
77+
id, err := uuid.GenerateUUID()
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
81+
d.SetId(id)
82+
83+
err = wrapper.listSslCertDownloadLinkToSchema(lisSslCerDowLinRst)
84+
if err != nil {
85+
return diag.FromErr(err)
86+
}
87+
88+
return nil
89+
}
90+
91+
// @API RDS GET /v3/{project_id}/instances/{instance_id}/ssl-cert/download-link
92+
func (w *SslCertDownloadLinksDSWrapper) ListSslCertDownloadLink() (*gjson.Result, error) {
93+
client, err := w.NewClient(w.Config, "rds")
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
uri := "/v3/{project_id}/instances/{instance_id}/ssl-cert/download-link"
99+
uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string))
100+
return httphelper.New(client).
101+
Method("GET").
102+
URI(uri).
103+
Request().
104+
Result()
105+
}
106+
107+
func (w *SslCertDownloadLinksDSWrapper) listSslCertDownloadLinkToSchema(body *gjson.Result) error {
108+
d := w.ResourceData
109+
mErr := multierror.Append(nil,
110+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
111+
d.Set("cert_info_list", schemas.SliceToList(body.Get("cert_info_list"),
112+
func(certInfoList gjson.Result) any {
113+
return map[string]any{
114+
"download_link": certInfoList.Get("download_link").Value(),
115+
"category": certInfoList.Get("category").Value(),
116+
}
117+
},
118+
)),
119+
)
120+
return mErr.ErrorOrNil()
121+
}

0 commit comments

Comments
 (0)