-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathresource_tc_mdl_stream_live_input.go
380 lines (321 loc) · 12.7 KB
/
resource_tc_mdl_stream_live_input.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package mdl
import (
"context"
"log"
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"
mdl "github.com/tencentcloud/tencentcloud-sdk-go-intl-en/tencentcloud/mdl/v20200326"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudMdlStreamLiveInput() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudMdlStreamLiveInputCreate,
Read: resourceTencentCloudMdlStreamLiveInputRead,
Update: resourceTencentCloudMdlStreamLiveInputUpdate,
Delete: resourceTencentCloudMdlStreamLiveInputDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Required: true,
Type: schema.TypeString,
Description: "Input name, which can contain 1-32 case-sensitive letters, digits, and underscores and must be unique at the region level.",
},
"type": {
Required: true,
ForceNew: true,
Type: schema.TypeString,
Description: "Input typeValid values: `RTMP_PUSH`, `RTP_PUSH`, `UDP_PUSH`, `RTMP_PULL`, `HLS_PULL`, `MP4_PULL`.",
},
"security_group_ids": {
Optional: true,
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "ID of the input security group to attachYou can attach only one security group to an input.",
},
"input_settings": {
Optional: true,
Computed: true,
Type: schema.TypeList,
Description: "Input settings. For the type `RTMP_PUSH`, `RTMP_PULL`, `HLS_PULL`, or `MP4_PULL`, 1 or 2 inputs of the corresponding type can be configured.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"app_name": {
Type: schema.TypeString,
Optional: true,
Description: "Application name, which is valid if `Type` is `RTMP_PUSH` and can contain 1-32 letters and digitsNote: This field may return `null`, indicating that no valid value was found.",
},
"stream_name": {
Type: schema.TypeString,
Optional: true,
Description: "Stream name, which is valid if `Type` is `RTMP_PUSH` and can contain 1-32 letters and digitsNote: This field may return `null`, indicating that no valid value was found.",
},
"source_url": {
Type: schema.TypeString,
Optional: true,
Description: "Source URL, which is valid if `Type` is `RTMP_PULL`, `HLS_PULL`, or `MP4_PULL` and can contain 1-512 charactersNote: This field may return `null`, indicating that no valid value was found.",
},
"input_address": {
Type: schema.TypeString,
Optional: true,
Description: "RTP/UDP input address, which does not need to be entered for the input parameter.Note: this field may return null, indicating that no valid values can be obtained.",
},
"source_type": {
Type: schema.TypeString,
Optional: true,
Description: "Source type for stream pulling and relaying. To pull content from private-read COS buckets under the current account, set this parameter to `TencentCOS`; otherwise, leave it empty.Note: this field may return `null`, indicating that no valid value was found.",
},
"delay_time": {
Type: schema.TypeInt,
Optional: true,
Description: "Delayed time (ms) for playback, which is valid if `Type` is `RTMP_PUSH`Value range: 0 (default) or 10000-600000The value must be a multiple of 1,000.Note: This field may return `null`, indicating that no valid value was found.",
},
"input_domain": {
Type: schema.TypeString,
Optional: true,
Description: "The domain of an SRT_PUSH address. If this is a request parameter, you do not need to specify it.Note: This field may return `null`, indicating that no valid value was found.",
},
"user_name": {
Type: schema.TypeString,
Optional: true,
Description: "The username, which is used for authentication.Note: This field may return `null`, indicating that no valid value was found.",
},
"password": {
Type: schema.TypeString,
Optional: true,
Description: "The password, which is used for authentication.Note: This field may return `null`, indicating that no valid value was found.",
},
},
},
},
},
}
}
func resourceTencentCloudMdlStreamLiveInputCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_mdl_stream_live_input.create")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
var (
request = mdl.NewCreateStreamLiveInputRequest()
response = mdl.NewCreateStreamLiveInputResponse()
id string
)
if v, ok := d.GetOk("name"); ok {
request.Name = helper.String(v.(string))
}
if v, ok := d.GetOk("type"); ok {
request.Type = helper.String(v.(string))
}
if v, ok := d.GetOk("security_group_ids"); ok {
securityGroupIdsSet := v.(*schema.Set).List()
for i := range securityGroupIdsSet {
securityGroupIds := securityGroupIdsSet[i].(string)
request.SecurityGroupIds = append(request.SecurityGroupIds, &securityGroupIds)
}
}
if v, ok := d.GetOk("input_settings"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
inputSettingInfo := mdl.InputSettingInfo{}
if v, ok := dMap["app_name"]; ok {
inputSettingInfo.AppName = helper.String(v.(string))
}
if v, ok := dMap["stream_name"]; ok {
inputSettingInfo.StreamName = helper.String(v.(string))
}
if v, ok := dMap["source_url"]; ok {
inputSettingInfo.SourceUrl = helper.String(v.(string))
}
if v, ok := dMap["input_address"]; ok {
inputSettingInfo.InputAddress = helper.String(v.(string))
}
if v, ok := dMap["source_type"]; ok {
inputSettingInfo.SourceType = helper.String(v.(string))
}
if v, ok := dMap["delay_time"]; ok {
inputSettingInfo.DelayTime = helper.IntInt64(v.(int))
}
if v, ok := dMap["input_domain"]; ok {
inputSettingInfo.InputDomain = helper.String(v.(string))
}
if v, ok := dMap["user_name"]; ok {
inputSettingInfo.UserName = helper.String(v.(string))
}
if v, ok := dMap["password"]; ok {
inputSettingInfo.Password = helper.String(v.(string))
}
request.InputSettings = append(request.InputSettings, &inputSettingInfo)
}
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMdlClient().CreateStreamLiveInput(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())
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s create mdl streamliveInput failed, reason:%+v", logId, err)
return err
}
id = *response.Response.Id
d.SetId(id)
return resourceTencentCloudMdlStreamLiveInputRead(d, meta)
}
func resourceTencentCloudMdlStreamLiveInputRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_mdl_stream_live_input.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := MdlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
id := d.Id()
streamliveInput, err := service.DescribeMdlStreamLiveInputById(ctx, id)
if err != nil {
return err
}
if streamliveInput == nil {
d.SetId("")
log.Printf("[WARN]%s resource `MdlStreamliveInput` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
if streamliveInput.Name != nil {
_ = d.Set("name", streamliveInput.Name)
}
if streamliveInput.Type != nil {
_ = d.Set("type", streamliveInput.Type)
}
if streamliveInput.SecurityGroupIds != nil {
_ = d.Set("security_group_ids", streamliveInput.SecurityGroupIds)
}
if streamliveInput.InputSettings != nil {
inputSettingsList := []interface{}{}
for _, inputSettings := range streamliveInput.InputSettings {
inputSettingsMap := map[string]interface{}{}
if inputSettings.AppName != nil {
inputSettingsMap["app_name"] = inputSettings.AppName
}
if inputSettings.StreamName != nil {
inputSettingsMap["stream_name"] = inputSettings.StreamName
}
if inputSettings.SourceUrl != nil {
inputSettingsMap["source_url"] = inputSettings.SourceUrl
}
if inputSettings.InputAddress != nil {
inputSettingsMap["input_address"] = inputSettings.InputAddress
}
if inputSettings.SourceType != nil {
inputSettingsMap["source_type"] = inputSettings.SourceType
}
if inputSettings.DelayTime != nil {
inputSettingsMap["delay_time"] = inputSettings.DelayTime
}
if inputSettings.InputDomain != nil {
inputSettingsMap["input_domain"] = inputSettings.InputDomain
}
if inputSettings.UserName != nil {
inputSettingsMap["user_name"] = inputSettings.UserName
}
if inputSettings.Password != nil {
inputSettingsMap["password"] = inputSettings.Password
}
inputSettingsList = append(inputSettingsList, inputSettingsMap)
}
_ = d.Set("input_settings", inputSettingsList)
}
return nil
}
func resourceTencentCloudMdlStreamLiveInputUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_mdl_streamlive_input.update")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
request := mdl.NewModifyStreamLiveInputRequest()
id := d.Id()
request.Id = &id
needChange := false
mutableArgs := []string{"name", "security_group_ids", "input_settings"}
for _, v := range mutableArgs {
if d.HasChange(v) {
needChange = true
break
}
}
if needChange {
if v, ok := d.GetOk("name"); ok {
request.Name = helper.String(v.(string))
}
if v, ok := d.GetOk("security_group_ids"); ok {
securityGroupIdsSet := v.(*schema.Set).List()
for i := range securityGroupIdsSet {
securityGroupIds := securityGroupIdsSet[i].(string)
request.SecurityGroupIds = append(request.SecurityGroupIds, &securityGroupIds)
}
}
if v, ok := d.GetOk("input_settings"); ok {
for _, item := range v.([]interface{}) {
dMap := item.(map[string]interface{})
inputSettingInfo := mdl.InputSettingInfo{}
if v, ok := dMap["app_name"]; ok {
inputSettingInfo.AppName = helper.String(v.(string))
}
if v, ok := dMap["stream_name"]; ok {
inputSettingInfo.StreamName = helper.String(v.(string))
}
if v, ok := dMap["source_url"]; ok {
inputSettingInfo.SourceUrl = helper.String(v.(string))
}
if v, ok := dMap["input_address"]; ok {
inputSettingInfo.InputAddress = helper.String(v.(string))
}
if v, ok := dMap["source_type"]; ok {
inputSettingInfo.SourceType = helper.String(v.(string))
}
if v, ok := dMap["delay_time"]; ok {
inputSettingInfo.DelayTime = helper.IntInt64(v.(int))
}
if v, ok := dMap["input_domain"]; ok {
inputSettingInfo.InputDomain = helper.String(v.(string))
}
if v, ok := dMap["user_name"]; ok {
inputSettingInfo.UserName = helper.String(v.(string))
}
if v, ok := dMap["password"]; ok {
inputSettingInfo.Password = helper.String(v.(string))
}
request.InputSettings = append(request.InputSettings, &inputSettingInfo)
}
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMdlClient().ModifyStreamLiveInput(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 update mdl streamliveInput failed, reason:%+v", logId, err)
return err
}
}
return resourceTencentCloudMdlStreamLiveInputRead(d, meta)
}
func resourceTencentCloudMdlStreamLiveInputDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_mdl_stream_live_input.delete")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service := MdlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
id := d.Id()
if err := service.DeleteMdlStreamLiveInputById(ctx, id); err != nil {
return err
}
return nil
}