Skip to content

Commit c19fc19

Browse files
tongyimingmikatong
andauthored
ssl_certificate support tags (#1143)
Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 95ee83c commit c19fc19

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

tencentcloud/resource_tc_ssl_certificate.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ func resourceTencentCloudSslCertificate() *schema.Resource {
104104
return
105105
},
106106
},
107-
107+
"tags": {
108+
Type: schema.TypeMap,
109+
Optional: true,
110+
Computed: true,
111+
Description: "Tags of the SSL certificate.",
112+
},
108113
// computed
109114
"product_zh_name": {
110115
Type: schema.TypeString,
@@ -208,6 +213,15 @@ func resourceTencentCloudSslCertificateCreate(d *schema.ResourceData, m interfac
208213
log.Printf("[CRITAL]%s create certificate failed, reason: %v", logId, outErr)
209214
return outErr
210215
}
216+
217+
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
218+
tagClient := m.(*TencentCloudClient).apiV3Conn
219+
tagService := &TagService{client: tagClient}
220+
resourceName := BuildTagResourceName("ssl", "certificate", tagClient.Region, id)
221+
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
222+
return err
223+
}
224+
}
211225
d.SetId(id)
212226

213227
return resourceTencentCloudSslCertificateRead(d, m)
@@ -287,6 +301,14 @@ func resourceTencentCloudSslCertificateRead(d *schema.ResourceData, m interface{
287301
}
288302
_ = d.Set("subject_names", subjectAltNames)
289303

304+
tagClient := m.(*TencentCloudClient).apiV3Conn
305+
tagService := TagService{client: tagClient}
306+
307+
tags, err := tagService.DescribeResourceTags(ctx, "ssl", "certificate", tagClient.Region, d.Id())
308+
if err != nil {
309+
return err
310+
}
311+
_ = d.Set("tags", tags)
290312
return nil
291313
}
292314

@@ -347,6 +369,18 @@ func resourceTencentCloudSslCertificateUpdate(d *schema.ResourceData, m interfac
347369
}
348370
d.SetPartial("project_id")
349371
}
372+
373+
if d.HasChange("tags") {
374+
oldInterface, newInterface := d.GetChange("tags")
375+
replaceTags, deleteTags := diffTags(oldInterface.(map[string]interface{}), newInterface.(map[string]interface{}))
376+
tagClient := m.(*TencentCloudClient).apiV3Conn
377+
tagService := TagService{client: tagClient}
378+
resourceName := BuildTagResourceName("ssl", "certificate", tagClient.Region, id)
379+
err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags)
380+
if err != nil {
381+
return err
382+
}
383+
}
350384
d.Partial(false)
351385
return resourceTencentCloudSslCertificateRead(d, m)
352386
}

tencentcloud/resource_tc_ssl_certificate_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,37 @@ func TestAccTencentCloudSslCertificate_basic(t *testing.T) {
104104
})
105105
}
106106

107+
func TestAccTencentCloudSslCertificate_tags(t *testing.T) {
108+
t.Parallel()
109+
resource.Test(t, resource.TestCase{
110+
PreCheck: func() { testAccPreCheck(t) },
111+
Providers: testAccProviders,
112+
CheckDestroy: testAccCheckSslCertificateDestroy,
113+
Steps: []resource.TestStep{
114+
{
115+
Config: testAccSslCertificateWithTags(`{
116+
tagKey1="tagValue1"
117+
}`),
118+
Check: resource.ComposeTestCheckFunc(
119+
testAccCheckSslCertificateExists("tencentcloud_ssl_certificate.test-ssl-certificate-tag"),
120+
resource.TestCheckResourceAttr("tencentcloud_ssl_certificate.test-ssl-certificate-tag", "tags.tagKey1", "tagValue1"),
121+
),
122+
},
123+
{
124+
Config: testAccSslCertificateWithTags(`{
125+
tagKey1="tagValue1"
126+
tagKey2="tagValue2"
127+
}`),
128+
Check: resource.ComposeTestCheckFunc(
129+
testAccCheckSslCertificateExists("tencentcloud_ssl_certificate.test-ssl-certificate-tag"),
130+
resource.TestCheckResourceAttr("tencentcloud_ssl_certificate.test-ssl-certificate-tag", "tags.tagKey1", "tagValue1"),
131+
resource.TestCheckResourceAttr("tencentcloud_ssl_certificate.test-ssl-certificate-tag", "tags.tagKey2", "tagValue2"),
132+
),
133+
},
134+
},
135+
})
136+
}
137+
107138
func TestAccTencentCloudSslCertificate_svr(t *testing.T) {
108139
t.Parallel()
109140
resource.Test(t, resource.TestCase{
@@ -242,6 +273,17 @@ resource "tencentcloud_ssl_certificate" "foo" {
242273
return fmt.Sprintf(str, certificateType, cert, name, key)
243274
}
244275

276+
func testAccSslCertificateWithTags(tags string) string {
277+
const str = `
278+
resource "tencentcloud_ssl_certificate" "test-ssl-certificate-tag" {
279+
type = "CA"
280+
cert = "%s"
281+
name = "test-ssl-certificate-tag"
282+
tags =%s
283+
}`
284+
return fmt.Sprintf(str, testAccSslCertificateCA, tags)
285+
}
286+
245287
const testAccSslCertificateCA = "-----BEGIN CERTIFICATE-----\\nMIIERzCCAq+gAwIBAgIBAjANBgkqhkiG9w0BAQsF" +
246288
"ADAoMQ0wCwYDVQQDEwR0ZXN0\\nMRcwFQYDVQQKEw50ZXJyYWZvcm0gdGVzdDAeFw0xOTA4MTM" +
247289
"wMzE5MzlaFw0yOTA4\\nMTAwMzE5MzlaMC4xEzARBgNVBAMTCnNlcnZlciBzc2wxFzAVBgNVBA" +

website/docs/r/ssl_certificate.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The following arguments are supported:
3131
* `key` - (Optional, ForceNew) Key of the SSL certificate and required when certificate type is `SVR`. Not allowed newline at the start and end.
3232
* `name` - (Optional) Name of the SSL certificate.
3333
* `project_id` - (Optional) Project ID of the SSL certificate. Default is `0`.
34+
* `tags` - (Optional) Tags of the SSL certificate.
3435

3536
## Attributes Reference
3637

0 commit comments

Comments
 (0)