|
| 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 | +} |
0 commit comments