|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of css backup_stream |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_css_backup_stream" "backup_stream" { |
| 8 | + stream_name = "live" |
| 9 | +} |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 19 | + css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801" |
| 20 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 21 | +) |
| 22 | + |
| 23 | +func dataSourceTencentCloudCssBackupStream() *schema.Resource { |
| 24 | + return &schema.Resource{ |
| 25 | + Read: dataSourceTencentCloudCssBackupStreamRead, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "stream_name": { |
| 28 | + Optional: true, |
| 29 | + Type: schema.TypeString, |
| 30 | + Description: "Stream id.", |
| 31 | + }, |
| 32 | + |
| 33 | + "stream_info_list": { |
| 34 | + Computed: true, |
| 35 | + Type: schema.TypeList, |
| 36 | + Description: "Backup stream group info.", |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "stream_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "Stream name.", |
| 43 | + }, |
| 44 | + "backup_list": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Computed: true, |
| 47 | + Description: "Backup stream info.", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "domain_name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Push domain.", |
| 54 | + }, |
| 55 | + "app_name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Push path.", |
| 59 | + }, |
| 60 | + "publish_time": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "UTC time, eg, 2018-06-29T19:00:00Z.", |
| 64 | + }, |
| 65 | + "upstream_sequence": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Push stream sequence.", |
| 69 | + }, |
| 70 | + "source_from": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Source from.", |
| 74 | + }, |
| 75 | + "master_flag": { |
| 76 | + Type: schema.TypeInt, |
| 77 | + Computed: true, |
| 78 | + Description: "Master stream flag.", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + "optimal_enable": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: "Optimal switch, 1-enable, 0-disable.", |
| 87 | + }, |
| 88 | + "host_group_name": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + Description: "Group name.", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + |
| 97 | + "result_output_file": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Optional: true, |
| 100 | + Description: "Used to save results.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func dataSourceTencentCloudCssBackupStreamRead(d *schema.ResourceData, meta interface{}) error { |
| 107 | + defer logElapsed("data_source.tencentcloud_css_backup_stream.read")() |
| 108 | + defer inconsistentCheck(d, meta)() |
| 109 | + |
| 110 | + logId := getLogId(contextNil) |
| 111 | + |
| 112 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 113 | + |
| 114 | + paramMap := make(map[string]interface{}) |
| 115 | + if v, ok := d.GetOk("stream_name"); ok { |
| 116 | + paramMap["StreamName"] = helper.String(v.(string)) |
| 117 | + } |
| 118 | + |
| 119 | + service := CssService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 120 | + |
| 121 | + var streamInfoList []*css.BackupStreamGroupInfo |
| 122 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 123 | + result, e := service.DescribeCssBackupStreamByFilter(ctx, paramMap) |
| 124 | + if e != nil { |
| 125 | + return retryError(e) |
| 126 | + } |
| 127 | + streamInfoList = result |
| 128 | + return nil |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + ids := make([]string, 0, len(streamInfoList)) |
| 135 | + tmpList := make([]map[string]interface{}, 0, len(streamInfoList)) |
| 136 | + |
| 137 | + if streamInfoList != nil { |
| 138 | + for _, backupStreamGroupInfo := range streamInfoList { |
| 139 | + backupStreamGroupInfoMap := map[string]interface{}{} |
| 140 | + |
| 141 | + if backupStreamGroupInfo.StreamName != nil { |
| 142 | + backupStreamGroupInfoMap["stream_name"] = backupStreamGroupInfo.StreamName |
| 143 | + } |
| 144 | + |
| 145 | + if backupStreamGroupInfo.BackupList != nil { |
| 146 | + backupListList := []interface{}{} |
| 147 | + for _, backupList := range backupStreamGroupInfo.BackupList { |
| 148 | + backupListMap := map[string]interface{}{} |
| 149 | + |
| 150 | + if backupList.DomainName != nil { |
| 151 | + backupListMap["domain_name"] = backupList.DomainName |
| 152 | + } |
| 153 | + |
| 154 | + if backupList.AppName != nil { |
| 155 | + backupListMap["app_name"] = backupList.AppName |
| 156 | + } |
| 157 | + |
| 158 | + if backupList.PublishTime != nil { |
| 159 | + backupListMap["publish_time"] = backupList.PublishTime |
| 160 | + } |
| 161 | + |
| 162 | + if backupList.UpstreamSequence != nil { |
| 163 | + backupListMap["upstream_sequence"] = backupList.UpstreamSequence |
| 164 | + } |
| 165 | + |
| 166 | + if backupList.SourceFrom != nil { |
| 167 | + backupListMap["source_from"] = backupList.SourceFrom |
| 168 | + } |
| 169 | + |
| 170 | + if backupList.MasterFlag != nil { |
| 171 | + backupListMap["master_flag"] = backupList.MasterFlag |
| 172 | + } |
| 173 | + |
| 174 | + backupListList = append(backupListList, backupListMap) |
| 175 | + } |
| 176 | + |
| 177 | + backupStreamGroupInfoMap["backup_list"] = backupListList |
| 178 | + } |
| 179 | + |
| 180 | + if backupStreamGroupInfo.OptimalEnable != nil { |
| 181 | + backupStreamGroupInfoMap["optimal_enable"] = backupStreamGroupInfo.OptimalEnable |
| 182 | + } |
| 183 | + |
| 184 | + if backupStreamGroupInfo.HostGroupName != nil { |
| 185 | + backupStreamGroupInfoMap["host_group_name"] = backupStreamGroupInfo.HostGroupName |
| 186 | + } |
| 187 | + |
| 188 | + ids = append(ids, *backupStreamGroupInfo.StreamName) |
| 189 | + tmpList = append(tmpList, backupStreamGroupInfoMap) |
| 190 | + } |
| 191 | + |
| 192 | + _ = d.Set("stream_info_list", tmpList) |
| 193 | + } |
| 194 | + |
| 195 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 196 | + output, ok := d.GetOk("result_output_file") |
| 197 | + if ok && output.(string) != "" { |
| 198 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 199 | + return e |
| 200 | + } |
| 201 | + } |
| 202 | + return nil |
| 203 | +} |
0 commit comments