-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(live): add new datasource to get the list of disable push streams
- Loading branch information
1 parent
bbb9702
commit 8735649
Showing
4 changed files
with
319 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
subcategory: "Live" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_live_disable_push_streams" | ||
description: |- | ||
Use this data source to get the list of disable push streams. | ||
--- | ||
|
||
# huaweicloud_live_disable_push_streams | ||
|
||
Use this datasource to get the list of disable push streams. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "domain_name" {} | ||
data "huaweicloud_live_disable_push_streams" "test" { | ||
domain_name = var.domain_name | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String) Specifies the region in which to query the resource. | ||
If omitted, the provider-level region will be used. | ||
|
||
* `domain_name` - (Required, String) Specifies the ingest domain name. | ||
|
||
* `app_name` - (Optional, String) Specifies the application name of the disable push stream. | ||
|
||
* `stream_name` - (Optional, String) Specifies the stream name of the disable push stream. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `blocks` - The list of the recording rules. | ||
|
||
The [blocks](#rules_struct) structure is documented below. | ||
|
||
<a name="blocks_struct"></a> | ||
The `blocks` block supports: | ||
|
||
* `app_name` - The application name of the disable push stream. | ||
|
||
* `stream_name` - The stream name of the disable push stream. | ||
|
||
* `resume_time` - The time of the resuming push stream. | ||
The format is **yyyy-mm-ddThh:mm:ssZ**. e.g. **2024-09-01T15:30:20Z**. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...icloud/services/acceptance/live/data_source_huaweicloud_live_disable_push_streams_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package live | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceDisablePushStreams_basic(t *testing.T) { | ||
var ( | ||
dataSourceName = "data.huaweicloud_live_disable_push_streams.test" | ||
domainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash()) | ||
createTime = time.Now().UTC().Add(24 * time.Hour).Format("2006-01-02T15:04:05Z") | ||
dc = acceptance.InitDataSourceCheck(dataSourceName) | ||
|
||
byAppName = "data.huaweicloud_live_disable_push_streams.filter_by_app_name" | ||
dcByAppName = acceptance.InitDataSourceCheck(byAppName) | ||
|
||
byStreamName = "data.huaweicloud_live_disable_push_streams.filter_by_stream_name" | ||
dcByStreamName = acceptance.InitDataSourceCheck(byStreamName) | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceDisablePushStreams_basic(domainName, createTime), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "blocks.#"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "blocks.0.app_name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "blocks.0.stream_name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "blocks.0.type"), | ||
|
||
dcByAppName.CheckResourceExists(), | ||
resource.TestCheckOutput("app_name_filter_useful", "true"), | ||
|
||
dcByStreamName.CheckResourceExists(), | ||
resource.TestCheckOutput("stream_name_filter_useful", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testDataSourceDisablePushStreams_basic(name, time string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "huaweicloud_live_disable_push_streams" "test" { | ||
depends_on = [ | ||
huaweicloud_live_disable_push_stream.test | ||
] | ||
domain_name = huaweicloud_live_domain.test.name | ||
} | ||
locals { | ||
app_name = data.huaweicloud_live_disable_push_streams.test.blocks[0].app_name | ||
} | ||
data "huaweicloud_live_disable_push_streams" "filter_by_app_name" { | ||
domain_name = huaweicloud_live_domain.test.name | ||
app_name = local.app_name | ||
} | ||
output "app_name_filter_useful" { | ||
value = length(data.huaweicloud_live_disable_push_streams.filter_by_app_name.blocks) > 0 && alltrue( | ||
[for v in data.huaweicloud_live_disable_push_streams.filter_by_app_name.blocks[*].app_name : v == local.app_name] | ||
) | ||
} | ||
locals { | ||
stream_name = data.huaweicloud_live_disable_push_streams.test.blocks[0].stream_name | ||
} | ||
data "huaweicloud_live_disable_push_streams" "filter_by_stream_name" { | ||
domain_name = huaweicloud_live_domain.test.name | ||
stream_name = local.stream_name | ||
} | ||
output "stream_name_filter_useful" { | ||
value = length(data.huaweicloud_live_disable_push_streams.filter_by_stream_name.blocks) > 0 && alltrue( | ||
[for v in data.huaweicloud_live_disable_push_streams.filter_by_stream_name.blocks[*].stream_name : v == local.stream_name] | ||
) | ||
} | ||
`, testAccDisablePushStream_basic(name, time)) | ||
} |
166 changes: 166 additions & 0 deletions
166
huaweicloud/services/live/data_source_huaweicloud_live_disable_push_streams.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package live | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API LIVE GET /v1/{project_id}/stream/blocks | ||
func DataSourceDisablePushStreams() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceDisablePushStreamsRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, | ||
}, | ||
"domain_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Specifies the ingest domain name of the disable push stream.`, | ||
}, | ||
"app_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Specifies the application name of the disable push stream.`, | ||
}, | ||
"stream_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: `Specifies the stream name of the disable push stream.`, | ||
}, | ||
"blocks": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `The list of the disable push streams.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"app_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The application name of the disable push stream.`, | ||
}, | ||
"stream_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The stream name of the disable push stream.`, | ||
}, | ||
"resume_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `The time of the resuming push stream.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDisablePushStreamsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
client, err := cfg.NewServiceClient("live", region) | ||
if err != nil { | ||
return diag.Errorf("error creating Live client: %s", err) | ||
} | ||
|
||
recordings, err := queryDisablePushStreams(d, client) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
dataSourceId, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
d.SetId(dataSourceId) | ||
|
||
mErr := multierror.Append( | ||
d.Set("region", region), | ||
d.Set("blocks", flattenDisablePushStreams(recordings)), | ||
) | ||
|
||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func queryDisablePushStreams(d *schema.ResourceData, client *golangsdk.ServiceClient) ([]interface{}, error) { | ||
var ( | ||
httpUrl = "v1/{project_id}/stream/blocks?size=100" | ||
page = 0 | ||
result = make([]interface{}, 0) | ||
) | ||
|
||
listPath := client.Endpoint + httpUrl | ||
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID) | ||
listPath = fmt.Sprintf("%s&domain=%v", listPath, d.Get("domain_name")) | ||
listPath += buildDisablePushStreamsQueryParams(d) | ||
|
||
opt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
} | ||
|
||
for { | ||
// The page indicates the page number. | ||
// The default value is 0, which represents the first page. | ||
listPathWithPage := fmt.Sprintf("%s&page=%d", listPath, page) | ||
requestResp, err := client.Request("GET", listPathWithPage, &opt) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving disable push stream information: %s", err) | ||
} | ||
|
||
respBody, err := utils.FlattenResponse(requestResp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
block := utils.PathSearch("blocks", respBody, make([]interface{}, 0)).([]interface{}) | ||
result = append(result, block...) | ||
if len(block) == 0 { | ||
break | ||
} | ||
page++ | ||
} | ||
return result, nil | ||
} | ||
|
||
func buildDisablePushStreamsQueryParams(d *schema.ResourceData) string { | ||
res := "" | ||
if appName, ok := d.GetOk("app_name"); ok { | ||
res = fmt.Sprintf("%s&app_name=%v", res, appName) | ||
} | ||
if streamName, ok := d.GetOk("stream_name"); ok { | ||
res = fmt.Sprintf("%s&stream_name=%v", res, streamName) | ||
} | ||
return res | ||
} | ||
|
||
func flattenDisablePushStreams(blocks []interface{}) []map[string]interface{} { | ||
if len(blocks) < 1 { | ||
return nil | ||
} | ||
|
||
result := make([]map[string]interface{}, len(blocks)) | ||
for i, v := range blocks { | ||
result[i] = map[string]interface{}{ | ||
"app_name": utils.PathSearch("app_name", v, nil), | ||
"stream_name": utils.PathSearch("stream_name", v, nil), | ||
"resume_time": utils.PathSearch("resume_time", v, nil), | ||
} | ||
} | ||
return result | ||
} |