|
| 1 | +/* |
| 2 | +Provides a resource to create a sms sign |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_sms_sign" "sign" { |
| 8 | + sign_name = "SignName" |
| 9 | + sign_type = 0 |
| 10 | + document_type = 0 |
| 11 | + international = 0 |
| 12 | + sign_purpose = 0 |
| 13 | + proof_image = "" |
| 14 | + commission_image = "" |
| 15 | + remark = "" |
| 16 | +} |
| 17 | +
|
| 18 | +``` |
| 19 | +Import |
| 20 | +
|
| 21 | +sms sign can be imported using the id, e.g. |
| 22 | +``` |
| 23 | +$ terraform import tencentcloud_sms_sign.sign sign_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 35 | + sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudSmsSign() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Read: resourceTencentCloudSmsSignRead, |
| 42 | + Create: resourceTencentCloudSmsSignCreate, |
| 43 | + Update: resourceTencentCloudSmsSignUpdate, |
| 44 | + Delete: resourceTencentCloudSmsSignDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "sign_name": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Required: true, |
| 52 | + Description: "Sms sign name, unique.", |
| 53 | + }, |
| 54 | + |
| 55 | + "sign_type": { |
| 56 | + Type: schema.TypeInt, |
| 57 | + Required: true, |
| 58 | + Description: "Sms sign type: 0, 1, 2, 3, 4, 5, 6.", |
| 59 | + }, |
| 60 | + |
| 61 | + "document_type": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Required: true, |
| 64 | + Description: "DocumentType is used for enterprise authentication, or website, app authentication, etc. DocumentType: 0, 1, 2, 3, 4, 5, 6, 7, 8.", |
| 65 | + }, |
| 66 | + |
| 67 | + "international": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Required: true, |
| 70 | + Description: "Whether it is Global SMS: 0: Mainland China SMS; 1: Global SMS.", |
| 71 | + }, |
| 72 | + |
| 73 | + "sign_purpose": { |
| 74 | + Type: schema.TypeInt, |
| 75 | + Required: true, |
| 76 | + Description: "Signature purpose: 0: for personal use; 1: for others.", |
| 77 | + }, |
| 78 | + |
| 79 | + "proof_image": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Required: true, |
| 82 | + Description: "You should Base64-encode the image of the identity certificate corresponding to the signature first, remove the prefix data:image/jpeg;base64, from the resulted string, and then use it as the value of this parameter.", |
| 83 | + }, |
| 84 | + |
| 85 | + "commission_image": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Optional: true, |
| 88 | + Description: "Power of attorney, which should be submitted if SignPurpose is for use by others. You should Base64-encode the image first, remove the prefix data:image/jpeg;base64, from the resulted string, and then use it as the value of this parameter. Note: this field will take effect only when SignPurpose is 1 (for user by others).", |
| 89 | + }, |
| 90 | + |
| 91 | + "remark": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Optional: true, |
| 94 | + Description: "Signature application remarks.", |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudSmsSignCreate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer logElapsed("resource.tencentcloud_sms_sign.create")() |
| 102 | + defer inconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + logId := getLogId(contextNil) |
| 105 | + |
| 106 | + var ( |
| 107 | + request = sms.NewAddSmsSignRequest() |
| 108 | + response *sms.AddSmsSignResponse |
| 109 | + signId uint64 |
| 110 | + ) |
| 111 | + |
| 112 | + if v, ok := d.GetOk("sign_name"); ok { |
| 113 | + request.SignName = helper.String(v.(string)) |
| 114 | + } |
| 115 | + |
| 116 | + if v, _ := d.GetOk("sign_type"); v != nil { |
| 117 | + request.SignType = helper.IntUint64(v.(int)) |
| 118 | + } |
| 119 | + |
| 120 | + if v, _ := d.GetOk("document_type"); v != nil { |
| 121 | + request.DocumentType = helper.IntUint64(v.(int)) |
| 122 | + } |
| 123 | + |
| 124 | + if v, _ := d.GetOk("international"); v != nil { |
| 125 | + request.International = helper.IntUint64(v.(int)) |
| 126 | + } |
| 127 | + |
| 128 | + if v, _ := d.GetOk("sign_purpose"); v != nil { |
| 129 | + request.SignPurpose = helper.IntUint64(v.(int)) |
| 130 | + } |
| 131 | + |
| 132 | + if v, ok := d.GetOk("proof_image"); ok { |
| 133 | + request.ProofImage = helper.String(v.(string)) |
| 134 | + } |
| 135 | + |
| 136 | + if v, ok := d.GetOk("commission_image"); ok { |
| 137 | + request.CommissionImage = helper.String(v.(string)) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOk("remark"); ok { |
| 141 | + request.Remark = helper.String(v.(string)) |
| 142 | + } |
| 143 | + |
| 144 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 145 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseSmsClient().AddSmsSign(request) |
| 146 | + if e != nil { |
| 147 | + return retryError(e) |
| 148 | + } else { |
| 149 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 150 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 151 | + } |
| 152 | + response = result |
| 153 | + return nil |
| 154 | + }) |
| 155 | + |
| 156 | + if err != nil { |
| 157 | + log.Printf("[CRITAL]%s create sms sign failed, reason:%+v", logId, err) |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + signId = *response.Response.AddSignStatus.SignId |
| 162 | + d.SetId(helper.UInt64ToStr(signId)) |
| 163 | + return resourceTencentCloudSmsSignRead(d, meta) |
| 164 | +} |
| 165 | + |
| 166 | +func resourceTencentCloudSmsSignRead(d *schema.ResourceData, meta interface{}) error { |
| 167 | + defer logElapsed("resource.tencentcloud_sms_sign.read")() |
| 168 | + defer inconsistentCheck(d, meta)() |
| 169 | + |
| 170 | + logId := getLogId(contextNil) |
| 171 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 172 | + |
| 173 | + service := SmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 174 | + |
| 175 | + signId := d.Id() |
| 176 | + |
| 177 | + sign, err := service.DescribeSmsSign(ctx, signId) |
| 178 | + |
| 179 | + if err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + |
| 183 | + if sign == nil { |
| 184 | + d.SetId("") |
| 185 | + return fmt.Errorf("resource `sign` %s does not exist", signId) |
| 186 | + } |
| 187 | + |
| 188 | + if sign.SignName != nil { |
| 189 | + _ = d.Set("sign_name", sign.SignName) |
| 190 | + } |
| 191 | + |
| 192 | + if sign.International != nil { |
| 193 | + _ = d.Set("international", sign.International) |
| 194 | + } |
| 195 | + |
| 196 | + return nil |
| 197 | +} |
| 198 | + |
| 199 | +func resourceTencentCloudSmsSignUpdate(d *schema.ResourceData, meta interface{}) error { |
| 200 | + defer logElapsed("resource.tencentcloud_sms_sign.update")() |
| 201 | + defer inconsistentCheck(d, meta)() |
| 202 | + |
| 203 | + logId := getLogId(contextNil) |
| 204 | + |
| 205 | + request := sms.NewModifySmsSignRequest() |
| 206 | + |
| 207 | + signId := d.Id() |
| 208 | + |
| 209 | + request.SignId = helper.Uint64(helper.StrToUInt64(signId)) |
| 210 | + |
| 211 | + if d.HasChange("sign_name") { |
| 212 | + if v, ok := d.GetOk("sign_name"); ok { |
| 213 | + request.SignName = helper.String(v.(string)) |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if d.HasChange("sign_type") { |
| 218 | + if v, _ := d.GetOk("sign_type"); v != nil { |
| 219 | + request.SignType = helper.IntUint64(v.(int)) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + if d.HasChange("document_type") { |
| 224 | + if v, _ := d.GetOk("document_type"); v != nil { |
| 225 | + request.DocumentType = helper.IntUint64(v.(int)) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if d.HasChange("international") { |
| 230 | + if v, _ := d.GetOk("international"); v != nil { |
| 231 | + request.International = helper.IntUint64(v.(int)) |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + if d.HasChange("sign_purpose") { |
| 236 | + if v, _ := d.GetOk("sign_purpose"); v != nil { |
| 237 | + request.SignPurpose = helper.IntUint64(v.(int)) |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + if d.HasChange("proof_image") { |
| 242 | + if v, ok := d.GetOk("proof_image"); ok { |
| 243 | + request.ProofImage = helper.String(v.(string)) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + if d.HasChange("commission_image") { |
| 248 | + if v, ok := d.GetOk("commission_image"); ok { |
| 249 | + request.CommissionImage = helper.String(v.(string)) |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + if d.HasChange("remark") { |
| 254 | + if v, ok := d.GetOk("remark"); ok { |
| 255 | + request.Remark = helper.String(v.(string)) |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 260 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseSmsClient().ModifySmsSign(request) |
| 261 | + if e != nil { |
| 262 | + return retryError(e) |
| 263 | + } else { |
| 264 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 265 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 266 | + } |
| 267 | + return nil |
| 268 | + }) |
| 269 | + |
| 270 | + if err != nil { |
| 271 | + log.Printf("[CRITAL]%s create sms sign failed, reason:%+v", logId, err) |
| 272 | + return err |
| 273 | + } |
| 274 | + |
| 275 | + return resourceTencentCloudSmsSignRead(d, meta) |
| 276 | +} |
| 277 | + |
| 278 | +func resourceTencentCloudSmsSignDelete(d *schema.ResourceData, meta interface{}) error { |
| 279 | + defer logElapsed("resource.tencentcloud_sms_sign.delete")() |
| 280 | + defer inconsistentCheck(d, meta)() |
| 281 | + |
| 282 | + logId := getLogId(contextNil) |
| 283 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 284 | + |
| 285 | + service := SmsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 286 | + |
| 287 | + signId := d.Id() |
| 288 | + |
| 289 | + if err := service.DeleteSmsSignById(ctx, signId); err != nil { |
| 290 | + return err |
| 291 | + } |
| 292 | + |
| 293 | + return nil |
| 294 | +} |
0 commit comments