|
| 1 | +/* |
| 2 | +Provides a resource to create a css watermark_rule |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +Binding watermark rule with a css stream |
| 7 | +
|
| 8 | +```hcl |
| 9 | +resource "tencentcloud_css_pull_stream_task" "example" { |
| 10 | + stream_name = "tf_example_stream_name" |
| 11 | + source_type = "PullLivePushLive" |
| 12 | + source_urls = ["rtmp://xxx.com/live/stream"] |
| 13 | + domain_name = "test.domain.com" |
| 14 | + app_name = "live" |
| 15 | + start_time = "2023-09-27T10:28:21Z" |
| 16 | + end_time = "2023-09-27T17:28:21Z" |
| 17 | + operator = "tf_admin" |
| 18 | + comment = "This is a e2e test case." |
| 19 | +} |
| 20 | +
|
| 21 | +resource "tencentcloud_css_watermark" "example" { |
| 22 | + picture_url = "picture_url" |
| 23 | + watermark_name = "watermark_name" |
| 24 | + x_position = 0 |
| 25 | + y_position = 0 |
| 26 | + width = 0 |
| 27 | + height = 0 |
| 28 | +} |
| 29 | +
|
| 30 | +resource "tencentcloud_css_watermark_rule_attachment" "watermark_rule_attachment" { |
| 31 | + domain_name = tencentcloud_css_pull_stream_task.example.domain_name |
| 32 | + app_name = tencentcloud_css_pull_stream_task.example.app_name |
| 33 | + stream_name = tencentcloud_css_pull_stream_task.example.stream_name |
| 34 | + template_id = tencentcloud_css_watermark.example.id |
| 35 | +} |
| 36 | +``` |
| 37 | +
|
| 38 | +Import |
| 39 | +
|
| 40 | +css watermark_rule_attachment can be imported using the id, e.g. |
| 41 | +
|
| 42 | +``` |
| 43 | +terraform import tencentcloud_css_watermark_rule_attachment.watermark_rule domain_name#app_name#stream_name#template_id |
| 44 | +``` |
| 45 | +*/ |
| 46 | +package tencentcloud |
| 47 | + |
| 48 | +import ( |
| 49 | + "context" |
| 50 | + "fmt" |
| 51 | + "log" |
| 52 | + "strings" |
| 53 | + |
| 54 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 55 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 56 | + css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801" |
| 57 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 58 | +) |
| 59 | + |
| 60 | +func resourceTencentCloudCssWatermarkRuleAttachment() *schema.Resource { |
| 61 | + return &schema.Resource{ |
| 62 | + Create: resourceTencentCloudCssWatermarkRuleAttachmentCreate, |
| 63 | + Read: resourceTencentCloudCssWatermarkRuleAttachmentRead, |
| 64 | + Delete: resourceTencentCloudCssWatermarkRuleAttachmentDelete, |
| 65 | + Importer: &schema.ResourceImporter{ |
| 66 | + State: schema.ImportStatePassthrough, |
| 67 | + }, |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + "domain_name": { |
| 70 | + Required: true, |
| 71 | + ForceNew: true, |
| 72 | + Type: schema.TypeString, |
| 73 | + Description: "rule domain name.", |
| 74 | + }, |
| 75 | + |
| 76 | + "app_name": { |
| 77 | + Required: true, |
| 78 | + ForceNew: true, |
| 79 | + Type: schema.TypeString, |
| 80 | + Description: "rule app name.", |
| 81 | + }, |
| 82 | + |
| 83 | + "stream_name": { |
| 84 | + Required: true, |
| 85 | + ForceNew: true, |
| 86 | + Type: schema.TypeString, |
| 87 | + Description: "rule stream name.", |
| 88 | + }, |
| 89 | + |
| 90 | + "template_id": { |
| 91 | + Required: true, |
| 92 | + ForceNew: true, |
| 93 | + Type: schema.TypeInt, |
| 94 | + Description: "The template Id can be acquired by the Id of `tencentcloud_css_watermark`.", |
| 95 | + }, |
| 96 | + |
| 97 | + "create_time": { |
| 98 | + Computed: true, |
| 99 | + Type: schema.TypeString, |
| 100 | + Description: "create time.", |
| 101 | + }, |
| 102 | + |
| 103 | + "update_time": { |
| 104 | + Computed: true, |
| 105 | + Type: schema.TypeString, |
| 106 | + Description: "update time.", |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func resourceTencentCloudCssWatermarkRuleAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 113 | + defer logElapsed("resource.tencentcloud_css_watermark_rule_attachment.create")() |
| 114 | + defer inconsistentCheck(d, meta)() |
| 115 | + |
| 116 | + logId := getLogId(contextNil) |
| 117 | + |
| 118 | + var ( |
| 119 | + request = css.NewCreateLiveWatermarkRuleRequest() |
| 120 | + domainName string |
| 121 | + appName string |
| 122 | + streamName string |
| 123 | + templateId int |
| 124 | + ) |
| 125 | + if v, ok := d.GetOk("domain_name"); ok { |
| 126 | + domainName = v.(string) |
| 127 | + request.DomainName = helper.String(domainName) |
| 128 | + } |
| 129 | + |
| 130 | + if v, ok := d.GetOk("app_name"); ok { |
| 131 | + appName = v.(string) |
| 132 | + request.AppName = helper.String(appName) |
| 133 | + } |
| 134 | + |
| 135 | + if v, ok := d.GetOk("stream_name"); ok { |
| 136 | + streamName = v.(string) |
| 137 | + request.StreamName = helper.String(streamName) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOkExists("template_id"); ok { |
| 141 | + templateId = v.(int) |
| 142 | + request.TemplateId = helper.IntInt64(templateId) |
| 143 | + } |
| 144 | + |
| 145 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 146 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCssClient().CreateLiveWatermarkRule(request) |
| 147 | + if e != nil { |
| 148 | + return retryError(e) |
| 149 | + } else { |
| 150 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 151 | + } |
| 152 | + return nil |
| 153 | + }) |
| 154 | + if err != nil { |
| 155 | + log.Printf("[CRITAL]%s create css watermarkRule failed, reason:%+v", logId, err) |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + d.SetId(strings.Join([]string{domainName, appName, streamName, helper.IntToStr(templateId)}, FILED_SP)) |
| 160 | + |
| 161 | + return resourceTencentCloudCssWatermarkRuleAttachmentRead(d, meta) |
| 162 | +} |
| 163 | + |
| 164 | +func resourceTencentCloudCssWatermarkRuleAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 165 | + defer logElapsed("resource.tencentcloud_css_watermark_rule_attachment.read")() |
| 166 | + defer inconsistentCheck(d, meta)() |
| 167 | + |
| 168 | + logId := getLogId(contextNil) |
| 169 | + |
| 170 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 171 | + |
| 172 | + service := CssService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 173 | + |
| 174 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 175 | + if len(idSplit) != 4 { |
| 176 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 177 | + } |
| 178 | + domainName := idSplit[0] |
| 179 | + appName := idSplit[1] |
| 180 | + streamName := idSplit[2] |
| 181 | + templateId := idSplit[3] |
| 182 | + |
| 183 | + watermarkRule, err := service.DescribeCssWatermarkRuleAttachment(ctx, domainName, appName, streamName, templateId) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + if watermarkRule == nil { |
| 189 | + d.SetId("") |
| 190 | + log.Printf("[WARN]%s resource `CssWatermarkRule` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 191 | + return nil |
| 192 | + } |
| 193 | + |
| 194 | + if watermarkRule.DomainName != nil { |
| 195 | + _ = d.Set("domain_name", watermarkRule.DomainName) |
| 196 | + } |
| 197 | + |
| 198 | + if watermarkRule.AppName != nil { |
| 199 | + _ = d.Set("app_name", watermarkRule.AppName) |
| 200 | + } |
| 201 | + |
| 202 | + if watermarkRule.StreamName != nil { |
| 203 | + _ = d.Set("stream_name", watermarkRule.StreamName) |
| 204 | + } |
| 205 | + |
| 206 | + if watermarkRule.TemplateId != nil { |
| 207 | + _ = d.Set("template_id", watermarkRule.TemplateId) |
| 208 | + } |
| 209 | + |
| 210 | + if watermarkRule.CreateTime != nil { |
| 211 | + _ = d.Set("create_time", watermarkRule.CreateTime) |
| 212 | + } |
| 213 | + |
| 214 | + if watermarkRule.UpdateTime != nil { |
| 215 | + _ = d.Set("update_time", watermarkRule.UpdateTime) |
| 216 | + } |
| 217 | + |
| 218 | + return nil |
| 219 | +} |
| 220 | + |
| 221 | +func resourceTencentCloudCssWatermarkRuleAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 222 | + defer logElapsed("resource.tencentcloud_css_watermark_rule_attachment.delete")() |
| 223 | + defer inconsistentCheck(d, meta)() |
| 224 | + |
| 225 | + logId := getLogId(contextNil) |
| 226 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 227 | + |
| 228 | + service := CssService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 229 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 230 | + if len(idSplit) != 4 { |
| 231 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 232 | + } |
| 233 | + domainName := idSplit[0] |
| 234 | + appName := idSplit[1] |
| 235 | + streamName := idSplit[2] |
| 236 | + |
| 237 | + if err := service.DetachCssWatermarkRuleAttachment(ctx, domainName, appName, streamName); err != nil { |
| 238 | + return err |
| 239 | + } |
| 240 | + |
| 241 | + return nil |
| 242 | +} |
0 commit comments