-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_cos_bucket_domain_certificate_attachment.go
196 lines (169 loc) · 6 KB
/
resource_tc_cos_bucket_domain_certificate_attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cos
import (
"context"
"encoding/xml"
"fmt"
"log"
"strings"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pkg/errors"
cos "github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudCosBucketDomainCertificateAttachment() *schema.Resource {
return &schema.Resource{
Read: resourceTencentCloudCosBucketDomainCertificateAttachmentRead,
Create: resourceTencentCloudCosBucketDomainCertificateAttachmentCreate,
// Update: resourceTencentCloudCosBucketDomainCertificateAttachmentUpdate,
Delete: resourceTencentCloudCosBucketDomainCertificateAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"bucket": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: tccommon.ValidateCosBucketName,
Description: "Bucket name.",
},
"domain_certificate": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
ForceNew: true,
Description: "The certificate of specified doamin.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"certificate": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Description: "Certificate info.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cert_type": {
Type: schema.TypeString,
Required: true,
Description: "Certificate type.",
},
"custom_cert": {
Type: schema.TypeList,
MaxItems: 1,
Required: true,
Description: "Custom certificate.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cert": {
Type: schema.TypeString,
Required: true,
Description: "Public key of certificate.",
},
"private_key": {
Type: schema.TypeString,
Required: true,
Description: "Private key of certificate.",
},
},
},
},
},
},
},
"domain": {
Type: schema.TypeString,
Required: true,
Description: "The name of domain.",
},
},
},
},
},
}
}
func resourceTencentCloudCosBucketDomainCertificateAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
var bucket string
if v, ok := d.GetOk("bucket"); ok {
bucket = v.(string)
} else {
return errors.New("get bucket failed!")
}
option := cos.BucketPutDomainCertificateOptions{}
if dcMap, ok := helper.InterfacesHeadMap(d, "domain_certificate"); ok {
if certMap, ok := helper.InterfaceToMap(dcMap, "certificate"); ok {
certificateInfo := cos.BucketDomainCertificateInfo{}
if v, ok := certMap["cert_type"]; ok {
certificateInfo.CertType = v.(string)
}
if CustomCertMap, ok := helper.InterfaceToMap(certMap, "custom_cert"); ok {
customCert := cos.BucketDomainCustomCert{}
if v, ok := CustomCertMap["cert"]; ok {
customCert.Cert = v.(string)
}
if v, ok := CustomCertMap["private_key"]; ok {
customCert.PrivateKey = v.(string)
}
certificateInfo.CustomCert = &customCert
}
option.CertificateInfo = &certificateInfo
}
if v, ok := dcMap["domain"]; ok {
option.DomainList = append(option.DomainList, v.(string))
}
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Bucket.PutDomainCertificate(ctx, &option)
if e != nil {
return tccommon.RetryError(e)
} else {
request, _ := xml.Marshal(option)
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, "PutDomainCertificate", request, result.Response.Body)
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create cos bucketDomainCertificate failed, reason:%+v", logId, err)
return err
}
ids := strings.Join([]string{bucket, option.DomainList[0]}, tccommon.FILED_SP)
d.SetId(ids)
return nil
}
func resourceTencentCloudCosBucketDomainCertificateAttachmentRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
id := d.Id()
certResult, bucket, err := service.DescribeCosBucketDomainCertificate(ctx, id)
log.Printf("[DEBUG] resource `bucketDomainCertificate certResult:%s`\n", certResult)
if err != nil {
return err
}
if certResult == nil {
d.SetId("")
return fmt.Errorf("resource `bucketDomainCertificate` %s does not exist", id)
}
_ = d.Set("bucket", bucket)
return nil
}
func resourceTencentCloudCosBucketDomainCertificateAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
id := d.Id()
defer tccommon.LogElapsed("resource.tencentcloud_cos_bucket_domain_certificate_attachment.delete id:", id)()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CosService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
if err := service.DeleteCosBucketDomainCertificate(ctx, id); err != nil {
return err
}
return nil
}