|
| 1 | +/* |
| 2 | +Provide a resource to create a VOD sub application. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_vod_sub_application" "foo" { |
| 8 | + name = "foo" |
| 9 | + status = "On" |
| 10 | + description = "this is sub application" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +VOD super player config can be imported using the name+, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +$ terraform import tencentcloud_vod_sub_application.foo name+"#"+id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 31 | + vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudVodSubApplication() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudVodSubApplicationCreate, |
| 39 | + Read: resourceTencentCloudVodSubApplicationRead, |
| 40 | + Update: resourceTencentCloudVodSubApplicationUpdate, |
| 41 | + Delete: resourceTencentCloudVodSubApplicationDelete, |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: schema.ImportStatePassthrough, |
| 44 | + }, |
| 45 | + |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + ValidateFunc: validateStringLengthInRange(1, 40), |
| 51 | + Description: "Sub application name, which can contain up to 64 letters, digits, underscores, and hyphens (such as test_ABC-123) and must be unique under a user.", |
| 52 | + }, |
| 53 | + "status": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + Description: "Sub appliaction status.", |
| 57 | + ValidateFunc: validateAllowedStringValue(VOD_SUB_APPLICATION_STATUS), |
| 58 | + }, |
| 59 | + "description": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Description: "Sub application description.", |
| 63 | + }, |
| 64 | + //computed |
| 65 | + "create_time": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "The time when the sub application was created.", |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func resourceTencentCloudVodSubApplicationCreate(d *schema.ResourceData, meta interface{}) error { |
| 75 | + defer logElapsed("resource.tencentcloud_vod_sub_application.create")() |
| 76 | + |
| 77 | + var ( |
| 78 | + logId = getLogId(contextNil) |
| 79 | + request = vod.NewCreateSubAppIdRequest() |
| 80 | + subAppId *uint64 |
| 81 | + subAppName *string |
| 82 | + ) |
| 83 | + |
| 84 | + if v, ok := d.GetOk("name"); ok { |
| 85 | + subAppName = helper.String(v.(string)) |
| 86 | + request.Name = subAppName |
| 87 | + } |
| 88 | + |
| 89 | + if v, ok := d.GetOk("description"); ok { |
| 90 | + request.Description = helper.String(v.(string)) |
| 91 | + } |
| 92 | + |
| 93 | + if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 94 | + ratelimit.Check(request.GetAction()) |
| 95 | + response, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().CreateSubAppId(request) |
| 96 | + if err != nil { |
| 97 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error()) |
| 98 | + return retryError(err) |
| 99 | + } |
| 100 | + subAppId = response.Response.SubAppId |
| 101 | + return nil |
| 102 | + }); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + d.SetId(*subAppName + FILED_SP + helper.UInt64ToStr(*subAppId)) |
| 107 | + |
| 108 | + if v, ok := d.GetOk("status"); ok { |
| 109 | + statusResquest := vod.NewModifySubAppIdStatusRequest() |
| 110 | + statusResquest.SubAppId = subAppId |
| 111 | + statusResquest.Status = helper.String(v.(string)) |
| 112 | + |
| 113 | + if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 114 | + ratelimit.Check(statusResquest.GetAction()) |
| 115 | + _, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusResquest) |
| 116 | + if err != nil { |
| 117 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error()) |
| 118 | + return retryError(err) |
| 119 | + } |
| 120 | + return nil |
| 121 | + }); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return resourceTencentCloudVodSubApplicationRead(d, meta) |
| 127 | +} |
| 128 | + |
| 129 | +func resourceTencentCloudVodSubApplicationRead(d *schema.ResourceData, meta interface{}) error { |
| 130 | + defer logElapsed("resource.tencentcloud_vod_sub_application.read")() |
| 131 | + defer inconsistentCheck(d, meta)() |
| 132 | + |
| 133 | + var ( |
| 134 | + //logId = getLogId(contextNil) |
| 135 | + //ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 136 | + client = meta.(*TencentCloudClient).apiV3Conn |
| 137 | + request = vod.NewDescribeSubAppIdsRequest() |
| 138 | + appInfo = vod.SubAppIdInfo{} |
| 139 | + ) |
| 140 | + |
| 141 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 142 | + if len(idSplit) != 2 { |
| 143 | + return fmt.Errorf("sub application id is borken, id is %s", d.Id()) |
| 144 | + } |
| 145 | + subAppName := idSplit[0] |
| 146 | + subAppId := idSplit[1] |
| 147 | + |
| 148 | + request.Name = &subAppName |
| 149 | + |
| 150 | + response, err := client.UseVodClient().DescribeSubAppIds(request) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + infoSet := response.Response.SubAppIdInfoSet |
| 155 | + if len(infoSet) == 0 { |
| 156 | + d.SetId("") |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + for _, info := range infoSet { |
| 161 | + if helper.UInt64ToStr(helper.PUint64(info.SubAppId)) == subAppId { |
| 162 | + appInfo = *info |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + _ = d.Set("name", appInfo.Name) |
| 167 | + _ = d.Set("description", appInfo.Description) |
| 168 | + // there may hide a bug, appInfo do not return status. So use the user input |
| 169 | + //_ = d.Set("status", appInfo.Status) |
| 170 | + _ = d.Set("status", helper.String(d.Get("status").(string))) |
| 171 | + _ = d.Set("create_time", appInfo.CreateTime) |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func resourceTencentCloudVodSubApplicationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 176 | + defer logElapsed("resource.tencentcloud_vod_sub_application.update")() |
| 177 | + |
| 178 | + var ( |
| 179 | + logId = getLogId(contextNil) |
| 180 | + request = vod.NewModifySubAppIdInfoRequest() |
| 181 | + changeFlag = false |
| 182 | + ) |
| 183 | + |
| 184 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 185 | + if len(idSplit) != 2 { |
| 186 | + return fmt.Errorf("sub application id is borken, id is %s", d.Id()) |
| 187 | + } |
| 188 | + subAppId := idSplit[1] |
| 189 | + |
| 190 | + if d.HasChange("name") { |
| 191 | + changeFlag = true |
| 192 | + if v, ok := d.GetOk("name"); ok { |
| 193 | + request.Name = helper.String(v.(string)) |
| 194 | + } |
| 195 | + } |
| 196 | + if d.HasChange("description") { |
| 197 | + changeFlag = true |
| 198 | + if v, ok := d.GetOk("description"); ok { |
| 199 | + request.Description = helper.String(v.(string)) |
| 200 | + } |
| 201 | + } |
| 202 | + request.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId)) |
| 203 | + |
| 204 | + if changeFlag { |
| 205 | + var err error |
| 206 | + err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 207 | + ratelimit.Check(request.GetAction()) |
| 208 | + _, err = meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdInfo(request) |
| 209 | + if err != nil { |
| 210 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error()) |
| 211 | + return retryError(err) |
| 212 | + } |
| 213 | + return nil |
| 214 | + }) |
| 215 | + if err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + } |
| 219 | + if d.HasChange("status") { |
| 220 | + var statusRequest = vod.NewModifySubAppIdStatusRequest() |
| 221 | + if v, ok := d.GetOk("status"); ok { |
| 222 | + statusRequest.Status = helper.String(v.(string)) |
| 223 | + } |
| 224 | + statusRequest.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId)) |
| 225 | + var err error |
| 226 | + err = resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 227 | + ratelimit.Check(statusRequest.GetAction()) |
| 228 | + _, err = meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest) |
| 229 | + if err != nil { |
| 230 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error()) |
| 231 | + return retryError(err) |
| 232 | + } |
| 233 | + return nil |
| 234 | + }) |
| 235 | + } |
| 236 | + return resourceTencentCloudVodSubApplicationRead(d, meta) |
| 237 | +} |
| 238 | + |
| 239 | +func resourceTencentCloudVodSubApplicationDelete(d *schema.ResourceData, meta interface{}) error { |
| 240 | + defer logElapsed("resource.tencentcloud_vod_sub_application.delete")() |
| 241 | + logId := getLogId(contextNil) |
| 242 | + |
| 243 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 244 | + if len(idSplit) != 2 { |
| 245 | + return fmt.Errorf("sub application id is borken, id is %s", d.Id()) |
| 246 | + } |
| 247 | + subAppId := idSplit[1] |
| 248 | + var statusRequest = vod.NewModifySubAppIdStatusRequest() |
| 249 | + // first turn off |
| 250 | + statusRequest.Status = helper.String("Off") |
| 251 | + statusRequest.SubAppId = helper.Uint64(helper.StrToUInt64(subAppId)) |
| 252 | + if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 253 | + ratelimit.Check(statusRequest.GetAction()) |
| 254 | + if _, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest); err != nil { |
| 255 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error()) |
| 256 | + return retryError(err, InternalError) |
| 257 | + } |
| 258 | + return nil |
| 259 | + }); err != nil { |
| 260 | + return err |
| 261 | + } |
| 262 | + // then destroy |
| 263 | + statusRequest.Status = helper.String("Destroyed") |
| 264 | + if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 265 | + ratelimit.Check(statusRequest.GetAction()) |
| 266 | + if _, err := meta.(*TencentCloudClient).apiV3Conn.UseVodClient().ModifySubAppIdStatus(statusRequest); err != nil { |
| 267 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, statusRequest.GetAction(), err.Error()) |
| 268 | + return retryError(err, InternalError) |
| 269 | + } |
| 270 | + return nil |
| 271 | + }); err != nil { |
| 272 | + return err |
| 273 | + } |
| 274 | + |
| 275 | + return nil |
| 276 | +} |
0 commit comments