Skip to content

Commit 289c0ff

Browse files
authored
feat: support stream monitor (#2291)
* feat: support stream monitor * feat: add changelog * fix: modify test * fix: delete Delete obsolete doc
1 parent 9edaa1b commit 289c0ff

10 files changed

+513
-0
lines changed

.changelog/2291.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_css_start_stream_monitor
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_css_pull_stream_task_restart
7+
```

tencentcloud/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,9 @@ Cloud Streaming Services(CSS)
15351535
tencentcloud_css_timeshift_template
15361536
tencentcloud_css_timeshift_rule_attachment
15371537
tencentcloud_css_stream_monitor
1538+
tencentcloud_css_start_stream_monitor
1539+
tencentcloud_css_pull_stream_task_restart
1540+
15381541
Data Source
15391542
tencentcloud_css_domains
15401543
tencentcloud_css_backup_stream
@@ -3482,6 +3485,8 @@ func Provider() *schema.Provider {
34823485
"tencentcloud_css_timeshift_template": resourceTencentCloudCssTimeshiftTemplate(),
34833486
"tencentcloud_css_timeshift_rule_attachment": resourceTencentCloudCssTimeshiftRuleAttachment(),
34843487
"tencentcloud_css_stream_monitor": resourceTencentCloudCssStreamMonitor(),
3488+
"tencentcloud_css_start_stream_monitor": resourceTencentCloudCssStartStreamMonitor(),
3489+
"tencentcloud_css_pull_stream_task_restart": resourceTencentCloudCssPullStreamTaskRestart(),
34853490
"tencentcloud_pts_project": resourceTencentCloudPtsProject(),
34863491
"tencentcloud_pts_alert_channel": resourceTencentCloudPtsAlertChannel(),
34873492
"tencentcloud_pts_scenario": resourceTencentCloudPtsScenario(),
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Provides a resource to create a css restart_push_task
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_css_pull_stream_task_restart" "restart_push_task" {
8+
task_id = "3573"
9+
operator = "tf-test"
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"log"
17+
"time"
18+
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
21+
css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801"
22+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
23+
)
24+
25+
func resourceTencentCloudCssPullStreamTaskRestart() *schema.Resource {
26+
return &schema.Resource{
27+
Create: resourceTencentCloudCssPullStreamTaskRestartCreate,
28+
Read: resourceTencentCloudCssPullStreamTaskRestartRead,
29+
Delete: resourceTencentCloudCssPullStreamTaskRestartDelete,
30+
Importer: &schema.ResourceImporter{
31+
State: schema.ImportStatePassthrough,
32+
},
33+
Schema: map[string]*schema.Schema{
34+
"task_id": {
35+
Required: true,
36+
ForceNew: true,
37+
Type: schema.TypeString,
38+
Description: "Task Id.",
39+
},
40+
41+
"operator": {
42+
Required: true,
43+
ForceNew: true,
44+
Type: schema.TypeString,
45+
Description: "Task operator.",
46+
},
47+
},
48+
}
49+
}
50+
51+
func resourceTencentCloudCssPullStreamTaskRestartCreate(d *schema.ResourceData, meta interface{}) error {
52+
defer logElapsed("resource.tencentcloud_css_pull_stream_task_restart.create")()
53+
defer inconsistentCheck(d, meta)()
54+
55+
logId := getLogId(contextNil)
56+
57+
var (
58+
request = css.NewRestartLivePullStreamTaskRequest()
59+
taskId string
60+
)
61+
if v, ok := d.GetOk("task_id"); ok {
62+
taskId = v.(string)
63+
request.TaskId = helper.String(v.(string))
64+
}
65+
66+
if v, ok := d.GetOk("operator"); ok {
67+
request.Operator = helper.String(v.(string))
68+
}
69+
70+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
71+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCssClient().RestartLivePullStreamTask(request)
72+
if e != nil {
73+
return retryError(e)
74+
} else {
75+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
76+
}
77+
return nil
78+
})
79+
if err != nil {
80+
log.Printf("[CRITAL]%s operate css restartPushTask failed, reason:%+v", logId, err)
81+
return err
82+
}
83+
84+
d.SetId(taskId)
85+
86+
service := CssService{client: meta.(*TencentCloudClient).apiV3Conn}
87+
88+
conf := BuildStateChangeConf([]string{}, []string{"active"}, 6*readRetryTimeout, time.Second, service.CssRestartPushTaskStateRefreshFunc(d.Id(), []string{}))
89+
90+
if _, e := conf.WaitForState(); e != nil {
91+
return e
92+
}
93+
94+
return resourceTencentCloudCssPullStreamTaskRestartRead(d, meta)
95+
}
96+
97+
func resourceTencentCloudCssPullStreamTaskRestartRead(d *schema.ResourceData, meta interface{}) error {
98+
defer logElapsed("resource.tencentcloud_css_pull_stream_task_restart.read")()
99+
defer inconsistentCheck(d, meta)()
100+
101+
return nil
102+
}
103+
104+
func resourceTencentCloudCssPullStreamTaskRestartDelete(d *schema.ResourceData, meta interface{}) error {
105+
defer logElapsed("resource.tencentcloud_css_pull_stream_task_restart.delete")()
106+
defer inconsistentCheck(d, meta)()
107+
108+
return nil
109+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
// go test -test.run TestAccTencentCloudCssPullStreamTaskRestartResource_basic -v
12+
func TestAccTencentCloudNeedFixCssPullStreamTaskRestartResource_basic(t *testing.T) {
13+
t.Parallel()
14+
baseTime := time.Now().UTC().Add(10 * time.Hour)
15+
startTime := baseTime.Format(time.RFC3339)
16+
endTime := baseTime.Add(1 * time.Hour).Format(time.RFC3339)
17+
liveUrl := "rtmp://5000.liveplay.myqcloud.com/live/stream1"
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() {
20+
testAccPreCheck(t)
21+
},
22+
Providers: testAccProviders,
23+
Steps: []resource.TestStep{
24+
{
25+
Config: fmt.Sprintf(testAccCssRestartPushTask, defaultCSSLiveType, liveUrl, defaultCSSDomainName, defaultCSSAppName, defaultCSSStreamName, startTime, endTime, defaultCSSOperator),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttrSet("tencentcloud_css_pull_stream_task_restart.restart_push_task", "id"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccCssRestartPushTask = testAccCssPullStreamTask + `
35+
36+
resource "tencentcloud_css_pull_stream_task_restart" "restart_push_task" {
37+
task_id = tencentcloud_css_pull_stream_task.pull_stream_task.id
38+
operator = "tf-test"
39+
}
40+
41+
`
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
Provides a resource to create a css start_stream_monitor
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_css_start_stream_monitor" "start_stream_monitor" {
8+
monitor_id = "3d5738dd-1ca2-4601-a6e9-004c5ec75c0b"
9+
audible_input_index_list = [1]
10+
}
11+
```
12+
13+
Import
14+
15+
css start_stream_monitor can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_css_start_stream_monitor.start_stream_monitor start_stream_monitor_id
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"log"
26+
"time"
27+
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
29+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
30+
css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801"
31+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
32+
)
33+
34+
func resourceTencentCloudCssStartStreamMonitor() *schema.Resource {
35+
return &schema.Resource{
36+
Create: resourceTencentCloudCssStartStreamMonitorCreate,
37+
Read: resourceTencentCloudCssStartStreamMonitorRead,
38+
Delete: resourceTencentCloudCssStartStreamMonitorDelete,
39+
Importer: &schema.ResourceImporter{
40+
State: schema.ImportStatePassthrough,
41+
},
42+
Schema: map[string]*schema.Schema{
43+
"monitor_id": {
44+
Required: true,
45+
ForceNew: true,
46+
Type: schema.TypeString,
47+
Description: "Monitor id.",
48+
},
49+
50+
"audible_input_index_list": {
51+
Optional: true,
52+
ForceNew: true,
53+
Type: schema.TypeSet,
54+
Elem: &schema.Schema{
55+
Type: schema.TypeInt,
56+
},
57+
Description: "The input index for monitoring the screen audio, supports multiple input audio sources.The valid range for InputIndex is that it must already exist.If left blank, there will be no audio output by default.",
58+
},
59+
},
60+
}
61+
}
62+
63+
func resourceTencentCloudCssStartStreamMonitorCreate(d *schema.ResourceData, meta interface{}) error {
64+
defer logElapsed("resource.tencentcloud_css_start_stream_monitor.create")()
65+
defer inconsistentCheck(d, meta)()
66+
67+
logId := getLogId(contextNil)
68+
69+
var (
70+
request = css.NewStartLiveStreamMonitorRequest()
71+
monitorId string
72+
)
73+
if v, ok := d.GetOk("monitor_id"); ok {
74+
monitorId = v.(string)
75+
request.MonitorId = helper.String(v.(string))
76+
}
77+
78+
if v, ok := d.GetOk("audible_input_index_list"); ok {
79+
audibleInputIndexListSet := v.(*schema.Set).List()
80+
for i := range audibleInputIndexListSet {
81+
audibleInputIndexList := audibleInputIndexListSet[i].(int)
82+
request.AudibleInputIndexList = append(request.AudibleInputIndexList, helper.IntUint64(audibleInputIndexList))
83+
}
84+
}
85+
86+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
87+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCssClient().StartLiveStreamMonitor(request)
88+
if e != nil {
89+
return retryError(e)
90+
} else {
91+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
92+
}
93+
return nil
94+
})
95+
if err != nil {
96+
log.Printf("[CRITAL]%s create css StartStreamMonitor failed, reason:%+v", logId, err)
97+
return err
98+
}
99+
100+
d.SetId(monitorId)
101+
102+
service := CssService{client: meta.(*TencentCloudClient).apiV3Conn}
103+
104+
conf := BuildStateChangeConf([]string{}, []string{"1"}, 6*readRetryTimeout, time.Second, service.CssStartStreamMonitorStateRefreshFunc(d.Id(), []string{}))
105+
106+
if _, e := conf.WaitForState(); e != nil {
107+
return e
108+
}
109+
110+
return resourceTencentCloudCssStartStreamMonitorRead(d, meta)
111+
}
112+
113+
func resourceTencentCloudCssStartStreamMonitorRead(d *schema.ResourceData, meta interface{}) error {
114+
defer logElapsed("resource.tencentcloud_css_start_stream_monitor.read")()
115+
defer inconsistentCheck(d, meta)()
116+
117+
logId := getLogId(contextNil)
118+
119+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
120+
121+
service := CssService{client: meta.(*TencentCloudClient).apiV3Conn}
122+
123+
monitorId := d.Id()
124+
125+
StartStreamMonitor, err := service.DescribeCssStreamMonitorById(ctx, monitorId)
126+
if err != nil {
127+
return err
128+
}
129+
130+
if StartStreamMonitor == nil {
131+
d.SetId("")
132+
log.Printf("[WARN]%s resource `CssStartStreamMonitor` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
133+
return nil
134+
}
135+
136+
if StartStreamMonitor.MonitorId != nil {
137+
_ = d.Set("monitor_id", StartStreamMonitor.MonitorId)
138+
}
139+
140+
if StartStreamMonitor.AudibleInputIndexList != nil {
141+
_ = d.Set("audible_input_index_list", StartStreamMonitor.AudibleInputIndexList)
142+
}
143+
144+
return nil
145+
}
146+
147+
func resourceTencentCloudCssStartStreamMonitorDelete(d *schema.ResourceData, meta interface{}) error {
148+
defer logElapsed("resource.tencentcloud_css_start_stream_monitor.delete")()
149+
defer inconsistentCheck(d, meta)()
150+
151+
logId := getLogId(contextNil)
152+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
153+
154+
service := CssService{client: meta.(*TencentCloudClient).apiV3Conn}
155+
monitorId := d.Id()
156+
157+
if err := service.DeleteCssStartStreamMonitorById(ctx, monitorId); err != nil {
158+
return err
159+
}
160+
161+
conf := BuildStateChangeConf([]string{}, []string{"0"}, 6*readRetryTimeout, time.Second, service.CssStartStreamMonitorStateRefreshFunc(d.Id(), []string{}))
162+
163+
if _, e := conf.WaitForState(); e != nil {
164+
return e
165+
}
166+
167+
return nil
168+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCssStartStreamMonitorResource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCssStartStreamMonitor,
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttrSet("tencentcloud_css_start_stream_monitor.start_stream_monitor", "id"),
21+
),
22+
},
23+
{
24+
ResourceName: "tencentcloud_css_start_stream_monitor.start_stream_monitor",
25+
ImportState: true,
26+
ImportStateVerify: true,
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccCssStartStreamMonitor = testAccCssStreamMonitor + `
33+
34+
resource "tencentcloud_css_start_stream_monitor" "start_stream_monitor" {
35+
monitor_id = tencentcloud_css_stream_monitor.stream_monitor.id
36+
audible_input_index_list = [1]
37+
}
38+
39+
`

0 commit comments

Comments
 (0)