-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_css_live_transcode_rule_attachment.go
210 lines (170 loc) · 6.41 KB
/
resource_tc_css_live_transcode_rule_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package css
import (
"context"
"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"
css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudCssLiveTranscodeRuleAttachment() *schema.Resource {
return &schema.Resource{
Read: resourceTencentCloudCssLiveTranscodeRuleAttachmentRead,
Create: resourceTencentCloudCssLiveTranscodeRuleAttachmentCreate,
Delete: resourceTencentCloudCssLiveTranscodeRuleAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "domain name hich you want to bind the transcode template.",
},
"app_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "app name which you want to bind, can be empty string if not binding specific app name.",
},
"stream_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "stream name which you want to bind, can be empty string if not binding specific stream.",
},
"template_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "template created by css_live_transcode_template.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "create time.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "update time.",
},
},
}
}
func resourceTencentCloudCssLiveTranscodeRuleAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_css_live_transcode_rule_attachment.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var (
request = css.NewCreateLiveTranscodeRuleRequest()
domainName string
appName string
streamName string
templateId int64
)
if v, ok := d.GetOk("domain_name"); ok {
domainName = v.(string)
request.DomainName = helper.String(v.(string))
}
if v, ok := d.GetOk("app_name"); ok {
appName = v.(string)
request.AppName = helper.String(v.(string))
}
if v, ok := d.GetOk("stream_name"); ok {
streamName = v.(string)
request.StreamName = helper.String(v.(string))
}
if v, ok := d.GetOk("template_id"); ok {
templateId = (int64)(v.(int))
request.TemplateId = helper.IntInt64(v.(int))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCssClient().CreateLiveTranscodeRule(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create css liveTranscodeRuleAttachment failed, reason:%+v", logId, err)
return err
}
d.SetId(domainName + tccommon.FILED_SP + appName + tccommon.FILED_SP + streamName + tccommon.FILED_SP + helper.Int64ToStr(templateId))
return resourceTencentCloudCssLiveTranscodeRuleAttachmentRead(d, meta)
}
func resourceTencentCloudCssLiveTranscodeRuleAttachmentRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_css_live_transcode_rule_attachment.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CssService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 4 {
return fmt.Errorf("id is broken,%s", d.Id())
}
domainName := idSplit[0]
appName := idSplit[1]
streamName := idSplit[2]
templateId := idSplit[3]
rules, err := service.DescribeCssLiveTranscodeRuleAttachment(ctx, helper.String(domainName), helper.String(templateId))
if err != nil {
return err
}
if rules == nil {
d.SetId("")
return fmt.Errorf("resource `liveTranscodeRuleAttachment` %s does not exist", d.Id())
}
for _, v := range rules {
if *v.DomainName != domainName || *v.AppName != appName || *v.StreamName != streamName || *v.TemplateId != helper.StrToInt64(templateId) {
log.Printf("[DEBUG]%s api[%s] this rule does not match with:[%s]\n", logId, "query attachment", d.Id())
continue
}
liveTranscodeRuleAttachment := v
if liveTranscodeRuleAttachment.DomainName != nil {
_ = d.Set("domain_name", liveTranscodeRuleAttachment.DomainName)
}
if liveTranscodeRuleAttachment.AppName != nil {
_ = d.Set("app_name", liveTranscodeRuleAttachment.AppName)
}
if liveTranscodeRuleAttachment.StreamName != nil {
_ = d.Set("stream_name", liveTranscodeRuleAttachment.StreamName)
}
if liveTranscodeRuleAttachment.TemplateId != nil {
_ = d.Set("template_id", liveTranscodeRuleAttachment.TemplateId)
}
if liveTranscodeRuleAttachment.CreateTime != nil {
_ = d.Set("create_time", liveTranscodeRuleAttachment.CreateTime)
}
if liveTranscodeRuleAttachment.UpdateTime != nil {
_ = d.Set("update_time", liveTranscodeRuleAttachment.UpdateTime)
}
}
return nil
}
func resourceTencentCloudCssLiveTranscodeRuleAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_css_live_transcode_rule_attachment.delete")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := CssService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
if len(idSplit) != 4 {
return fmt.Errorf("id is broken,%s", d.Id())
}
domainName := idSplit[0]
appName := idSplit[1]
streamName := idSplit[2]
templateId := idSplit[3]
if err := service.DeleteCssLiveTranscodeRuleAttachmentById(ctx, helper.String(domainName), helper.String(appName), helper.String(streamName), helper.String(templateId)); err != nil {
return err
}
return nil
}