|
| 1 | +package vod |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 12 | + vod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod/v20180717" |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudVodSampleSnapshotTemplate() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudVodSampleSnapshotTemplateCreate, |
| 20 | + Read: resourceTencentCloudVodSampleSnapshotTemplateRead, |
| 21 | + Update: resourceTencentCloudVodSampleSnapshotTemplateUpdate, |
| 22 | + Delete: resourceTencentCloudVodSampleSnapshotTemplateDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "sample_type": { |
| 28 | + Required: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "Sampled screencapturing type. Valid values: Percent: by percent. Time: by time interval.", |
| 31 | + }, |
| 32 | + |
| 33 | + "sample_interval": { |
| 34 | + Required: true, |
| 35 | + Type: schema.TypeInt, |
| 36 | + Description: "Sampling interval. If `SampleType` is `Percent`, sampling will be performed at an interval of the specified percentage. If `SampleType` is `Time`, sampling will be performed at the specified time interval in seconds.", |
| 37 | + }, |
| 38 | + |
| 39 | + "sub_app_id": { |
| 40 | + Required: true, |
| 41 | + Type: schema.TypeInt, |
| 42 | + Description: "The VOD [application](https://intl.cloud.tencent.com/document/product/266/14574) ID. For customers who activate VOD service from December 25, 2023, if they want to access resources in a VOD application (whether it's the default application or a newly created one), they must fill in this field with the application ID.", |
| 43 | + }, |
| 44 | + |
| 45 | + "name": { |
| 46 | + Optional: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "Name of a sampled screencapturing template. Length limit: 64 characters.", |
| 49 | + }, |
| 50 | + |
| 51 | + "width": { |
| 52 | + Optional: true, |
| 53 | + Type: schema.TypeInt, |
| 54 | + Description: "Maximum value of the width (or long side) of a screenshot in px. Value range: 0 and [128, 4,096]. If both `Width` and `Height` are 0, the resolution will be the same as that of the source video; If `Width` is 0, but `Height` is not 0, `Width` will be proportionally scaled; If `Width` is not 0, but `Height` is 0, `Height` will be proportionally scaled; If both `Width` and `Height` are not 0, the custom resolution will be used.Default value: 0.", |
| 55 | + }, |
| 56 | + |
| 57 | + "height": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeInt, |
| 60 | + Description: "Maximum value of the height (or short side) of a screenshot in px. Value range: 0 and [128, 4,096]. If both `Width` and `Height` are 0, the resolution will be the same as that of the source video; If `Width` is 0, but `Height` is not 0, `Width` will be proportionally scaled; If `Width` is not 0, but `Height` is 0, `Height` will be proportionally scaled; If both `Width` and `Height` are not 0, the custom resolution will be used.Default value: 0.", |
| 61 | + }, |
| 62 | + |
| 63 | + "resolution_adaptive": { |
| 64 | + Optional: true, |
| 65 | + Type: schema.TypeString, |
| 66 | + Description: "Resolution adaption. Valid values: open: enabled. In this case, `Width` represents the long side of a video, while `Height` the short side; close: disabled. In this case, `Width` represents the width of a video, while `Height` the height.Default value: open.", |
| 67 | + }, |
| 68 | + |
| 69 | + "format": { |
| 70 | + Optional: true, |
| 71 | + Type: schema.TypeString, |
| 72 | + Description: "Image format. Valid values: jpg, png. Default value: jpg.", |
| 73 | + }, |
| 74 | + |
| 75 | + "comment": { |
| 76 | + Optional: true, |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Template description. Length limit: 256 characters.", |
| 79 | + }, |
| 80 | + |
| 81 | + "fill_type": { |
| 82 | + Optional: true, |
| 83 | + Type: schema.TypeString, |
| 84 | + Description: "Fill type. Fill refers to the way of processing a screenshot when its aspect ratio is different from that of the source video. The following fill types are supported: stretch: stretch. The screenshot will be stretched frame by frame to match the aspect ratio of the source video, which may make the screenshot shorter or longer; black: fill with black. This option retains the aspect ratio of the source video for the screenshot and fills the unmatched area with black color blocks. white: fill with white. This option retains the aspect ratio of the source video for the screenshot and fills the unmatched area with white color blocks. gauss: fill with Gaussian blur. This option retains the aspect ratio of the source video for the screenshot and fills the unmatched area with Gaussian blur.Default value: black.", |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func resourceTencentCloudVodSampleSnapshotTemplateCreate(d *schema.ResourceData, meta interface{}) error { |
| 91 | + defer tccommon.LogElapsed("resource.tencentcloud_vod_sample_snapshot_template.create")() |
| 92 | + defer tccommon.InconsistentCheck(d, meta)() |
| 93 | + |
| 94 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 95 | + |
| 96 | + var ( |
| 97 | + request = vod.NewCreateSampleSnapshotTemplateRequest() |
| 98 | + response = vod.NewCreateSampleSnapshotTemplateResponse() |
| 99 | + subAppId string |
| 100 | + ) |
| 101 | + |
| 102 | + if v, ok := d.GetOk("sample_type"); ok { |
| 103 | + request.SampleType = helper.String(v.(string)) |
| 104 | + } |
| 105 | + |
| 106 | + if v, ok := d.GetOkExists("sample_interval"); ok { |
| 107 | + request.SampleInterval = helper.IntUint64(v.(int)) |
| 108 | + } |
| 109 | + |
| 110 | + if v, ok := d.GetOkExists("sub_app_id"); ok { |
| 111 | + subAppId = helper.IntToStr(v.(int)) |
| 112 | + request.SubAppId = helper.IntUint64(v.(int)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOk("name"); ok { |
| 116 | + request.Name = helper.String(v.(string)) |
| 117 | + } |
| 118 | + |
| 119 | + if v, ok := d.GetOkExists("width"); ok { |
| 120 | + request.Width = helper.IntUint64(v.(int)) |
| 121 | + } |
| 122 | + |
| 123 | + if v, ok := d.GetOkExists("height"); ok { |
| 124 | + request.Height = helper.IntUint64(v.(int)) |
| 125 | + } |
| 126 | + |
| 127 | + if v, ok := d.GetOk("resolution_adaptive"); ok { |
| 128 | + request.ResolutionAdaptive = helper.String(v.(string)) |
| 129 | + } |
| 130 | + |
| 131 | + if v, ok := d.GetOk("format"); ok { |
| 132 | + request.Format = helper.String(v.(string)) |
| 133 | + } |
| 134 | + |
| 135 | + if v, ok := d.GetOk("comment"); ok { |
| 136 | + request.Comment = helper.String(v.(string)) |
| 137 | + } |
| 138 | + |
| 139 | + if v, ok := d.GetOk("fill_type"); ok { |
| 140 | + request.FillType = helper.String(v.(string)) |
| 141 | + } |
| 142 | + |
| 143 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 144 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVodClient().CreateSampleSnapshotTemplate(request) |
| 145 | + if e != nil { |
| 146 | + if sdkError, ok := e.(*sdkErrors.TencentCloudSDKError); ok { |
| 147 | + if sdkError.Code == "FailedOperation" && sdkError.Message == "invalid vod user" { |
| 148 | + return resource.RetryableError(e) |
| 149 | + } |
| 150 | + } |
| 151 | + log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), e.Error()) |
| 152 | + return resource.NonRetryableError(e) |
| 153 | + } else { |
| 154 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 155 | + } |
| 156 | + response = result |
| 157 | + return nil |
| 158 | + }) |
| 159 | + if err != nil { |
| 160 | + log.Printf("[CRITAL]%s create vod sampleSnapshotTemplate failed, reason:%+v", logId, err) |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + definition := *response.Response.Definition |
| 165 | + |
| 166 | + d.SetId(subAppId + tccommon.FILED_SP + helper.UInt64ToStr(definition)) |
| 167 | + |
| 168 | + return resourceTencentCloudVodSampleSnapshotTemplateRead(d, meta) |
| 169 | +} |
| 170 | + |
| 171 | +func resourceTencentCloudVodSampleSnapshotTemplateRead(d *schema.ResourceData, meta interface{}) error { |
| 172 | + defer tccommon.LogElapsed("resource.tencentcloud_vod_sample_snapshot_template.read")() |
| 173 | + defer tccommon.InconsistentCheck(d, meta)() |
| 174 | + |
| 175 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 176 | + |
| 177 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 178 | + |
| 179 | + service := VodService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 180 | + |
| 181 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 182 | + if len(idSplit) != 2 { |
| 183 | + return fmt.Errorf("sample snapshot id is borken, id is %s", d.Id()) |
| 184 | + } |
| 185 | + subAppId := idSplit[0] |
| 186 | + definition := idSplit[1] |
| 187 | + sampleSnapshotTemplate, err := service.DescribeVodSampleSnapshotTemplateById(ctx, helper.StrToUInt64(subAppId), helper.StrToUInt64(definition)) |
| 188 | + if err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + |
| 192 | + if sampleSnapshotTemplate == nil { |
| 193 | + d.SetId("") |
| 194 | + log.Printf("[WARN]%s resource `VodSampleSnapshotTemplate` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 195 | + return nil |
| 196 | + } |
| 197 | + |
| 198 | + _ = d.Set("sub_app_id", helper.StrToInt(subAppId)) |
| 199 | + if sampleSnapshotTemplate.SampleType != nil { |
| 200 | + _ = d.Set("sample_type", sampleSnapshotTemplate.SampleType) |
| 201 | + } |
| 202 | + |
| 203 | + if sampleSnapshotTemplate.SampleInterval != nil { |
| 204 | + _ = d.Set("sample_interval", sampleSnapshotTemplate.SampleInterval) |
| 205 | + } |
| 206 | + |
| 207 | + if sampleSnapshotTemplate.Name != nil { |
| 208 | + _ = d.Set("name", sampleSnapshotTemplate.Name) |
| 209 | + } |
| 210 | + |
| 211 | + if sampleSnapshotTemplate.Width != nil { |
| 212 | + _ = d.Set("width", sampleSnapshotTemplate.Width) |
| 213 | + } |
| 214 | + |
| 215 | + if sampleSnapshotTemplate.Height != nil { |
| 216 | + _ = d.Set("height", sampleSnapshotTemplate.Height) |
| 217 | + } |
| 218 | + |
| 219 | + if sampleSnapshotTemplate.ResolutionAdaptive != nil { |
| 220 | + _ = d.Set("resolution_adaptive", sampleSnapshotTemplate.ResolutionAdaptive) |
| 221 | + } |
| 222 | + |
| 223 | + if sampleSnapshotTemplate.Format != nil { |
| 224 | + _ = d.Set("format", sampleSnapshotTemplate.Format) |
| 225 | + } |
| 226 | + |
| 227 | + if sampleSnapshotTemplate.Comment != nil { |
| 228 | + _ = d.Set("comment", sampleSnapshotTemplate.Comment) |
| 229 | + } |
| 230 | + |
| 231 | + if sampleSnapshotTemplate.FillType != nil { |
| 232 | + _ = d.Set("fill_type", sampleSnapshotTemplate.FillType) |
| 233 | + } |
| 234 | + |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +func resourceTencentCloudVodSampleSnapshotTemplateUpdate(d *schema.ResourceData, meta interface{}) error { |
| 239 | + defer tccommon.LogElapsed("resource.tencentcloud_vod_sample_snapshot_template.update")() |
| 240 | + defer tccommon.InconsistentCheck(d, meta)() |
| 241 | + |
| 242 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 243 | + |
| 244 | + request := vod.NewModifySampleSnapshotTemplateRequest() |
| 245 | + |
| 246 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 247 | + if len(idSplit) != 2 { |
| 248 | + return fmt.Errorf("sample snapshot id is borken, id is %s", d.Id()) |
| 249 | + } |
| 250 | + subAppId := idSplit[0] |
| 251 | + definition := idSplit[1] |
| 252 | + |
| 253 | + immutableArgs := []string{"sub_app_id"} |
| 254 | + |
| 255 | + for _, v := range immutableArgs { |
| 256 | + if d.HasChange(v) { |
| 257 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + request.Definition = helper.StrToUint64Point(definition) |
| 262 | + request.SubAppId = helper.StrToUint64Point(subAppId) |
| 263 | + |
| 264 | + if d.HasChange("sample_type") || d.HasChange("sample_interval") || d.HasChange("name") || d.HasChange("width") || d.HasChange("height") || d.HasChange("resolution_adaptive") || d.HasChange("format") || d.HasChange("comment") || d.HasChange("fill_type") { |
| 265 | + if v, ok := d.GetOk("sample_type"); ok { |
| 266 | + request.SampleType = helper.String(v.(string)) |
| 267 | + } |
| 268 | + if v, ok := d.GetOkExists("sample_interval"); ok { |
| 269 | + request.SampleInterval = helper.IntUint64(v.(int)) |
| 270 | + } |
| 271 | + if v, ok := d.GetOk("name"); ok { |
| 272 | + request.Name = helper.String(v.(string)) |
| 273 | + } |
| 274 | + if v, ok := d.GetOkExists("width"); ok { |
| 275 | + request.Width = helper.IntUint64(v.(int)) |
| 276 | + } |
| 277 | + if v, ok := d.GetOkExists("height"); ok { |
| 278 | + request.Height = helper.IntUint64(v.(int)) |
| 279 | + } |
| 280 | + if v, ok := d.GetOk("resolution_adaptive"); ok { |
| 281 | + request.ResolutionAdaptive = helper.String(v.(string)) |
| 282 | + } |
| 283 | + if v, ok := d.GetOk("format"); ok { |
| 284 | + request.Format = helper.String(v.(string)) |
| 285 | + } |
| 286 | + if v, ok := d.GetOk("comment"); ok { |
| 287 | + request.Comment = helper.String(v.(string)) |
| 288 | + } |
| 289 | + if v, ok := d.GetOk("fill_type"); ok { |
| 290 | + request.FillType = helper.String(v.(string)) |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 295 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseVodClient().ModifySampleSnapshotTemplate(request) |
| 296 | + if e != nil { |
| 297 | + return tccommon.RetryError(e) |
| 298 | + } else { |
| 299 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 300 | + } |
| 301 | + return nil |
| 302 | + }) |
| 303 | + if err != nil { |
| 304 | + log.Printf("[CRITAL]%s update vod sampleSnapshotTemplate failed, reason:%+v", logId, err) |
| 305 | + return err |
| 306 | + } |
| 307 | + |
| 308 | + return resourceTencentCloudVodSampleSnapshotTemplateRead(d, meta) |
| 309 | +} |
| 310 | + |
| 311 | +func resourceTencentCloudVodSampleSnapshotTemplateDelete(d *schema.ResourceData, meta interface{}) error { |
| 312 | + defer tccommon.LogElapsed("resource.tencentcloud_vod_sample_snapshot_template.delete")() |
| 313 | + defer tccommon.InconsistentCheck(d, meta)() |
| 314 | + |
| 315 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 316 | + ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 317 | + |
| 318 | + service := VodService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 319 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 320 | + if len(idSplit) != 2 { |
| 321 | + return fmt.Errorf("sample snapshot id is borken, id is %s", d.Id()) |
| 322 | + } |
| 323 | + subAppId := idSplit[0] |
| 324 | + definition := idSplit[1] |
| 325 | + |
| 326 | + if err := service.DeleteVodSampleSnapshotTemplateById(ctx, helper.StrToUInt64(subAppId), helper.StrToUInt64(definition)); err != nil { |
| 327 | + return err |
| 328 | + } |
| 329 | + |
| 330 | + return nil |
| 331 | +} |
0 commit comments