From d68252744232b02842465a3bd7b9ff9cfa39f66b Mon Sep 17 00:00:00 2001 From: luxiaohang Date: Wed, 18 Dec 2024 12:03:56 +0800 Subject: [PATCH] chore(live): add a new datasource to query record callback configurations --- docs/data-sources/live_record_callbacks.md | 66 +++++++ huaweicloud/provider.go | 11 +- ..._huaweicloud_live_record_callbacks_test.go | 122 +++++++++++++ ...ource_huaweicloud_live_record_callbacks.go | 169 ++++++++++++++++++ 4 files changed, 363 insertions(+), 5 deletions(-) create mode 100644 docs/data-sources/live_record_callbacks.md create mode 100644 huaweicloud/services/acceptance/live/data_source_huaweicloud_live_record_callbacks_test.go create mode 100644 huaweicloud/services/live/data_source_huaweicloud_live_record_callbacks.go diff --git a/docs/data-sources/live_record_callbacks.md b/docs/data-sources/live_record_callbacks.md new file mode 100644 index 0000000000..d170cffe96 --- /dev/null +++ b/docs/data-sources/live_record_callbacks.md @@ -0,0 +1,66 @@ +--- +subcategory: "Live" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_live_record_callbacks" +description: |- + Use this data source to get a list of the Live record callback configurations. +--- + +# huaweicloud_live_record_callbacks + +Use this data source to get a list of the Live record callback configurations. + +## Example Usage + +```hcl +resource "huaweicloud_live_record_callbacks" "test" { +} +``` + +## 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` - (Optional, String) Specifies the ingest domain name. + +* `app_name` - (Optional, String) Specifies the application name. + To match all applications, set this parameter to a wildcard character *****. + Exact application matching is preferred. If no application is matched, all applications will be matched. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `callbacks` - The callback configurations. + + The [callbacks](#callbacks_struct) structure is documented below. + + +The `callbacks` block supports: + +* `sign_type` - The encryption type. Contains the following values: + + **HMACSHA256** + + **MD5** + +* `created_at` - The creation time in the format of **yyyy-mm-ddThh:mm:ssZ** (UTC time). + +* `updated_at` - The latest modification time in the format of **yyyy-mm-ddThh:mm:ssZ** (UTC time). + +* `id` - The recording callback ID. + +* `domain_name` - The ingest domain name. + +* `app_name` - The application name. + +* `url` - The callback URL for sending recording notifications. + +* `types` - The types of recording notifications. Contains the following values: + + **RECORD_NEW_FILE_START**: Recording started. + + **RECORD_FILE_COMPLETE**: Recording file generated. + + **RECORD_OVER**: Recording completed. + + **RECORD_FAILED**: Recording failed. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 761e5a5374..3962062c07 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -906,11 +906,12 @@ func Provider() *schema.Provider { "huaweicloud_lb_certificate": lb.DataSourceLBCertificateV2(), "huaweicloud_lb_pools": lb.DataSourcePools(), - "huaweicloud_live_domains": live.DataSourceLiveDomains(), - "huaweicloud_live_recordings": live.DataSourceLiveRecordings(), - "huaweicloud_live_transcodings": live.DataSourceLiveTranscodings(), - "huaweicloud_live_snapshots": live.DataSourceLiveSnapshots(), - "huaweicloud_live_geo_blockings": live.DataSourceGeoBlockings(), + "huaweicloud_live_domains": live.DataSourceLiveDomains(), + "huaweicloud_live_recordings": live.DataSourceLiveRecordings(), + "huaweicloud_live_transcodings": live.DataSourceLiveTranscodings(), + "huaweicloud_live_snapshots": live.DataSourceLiveSnapshots(), + "huaweicloud_live_geo_blockings": live.DataSourceGeoBlockings(), + "huaweicloud_live_record_callbacks": live.DataSourceLiveRecordCallbacks(), "huaweicloud_lts_aom_accesses": lts.DataSourceAOMAccesses(), "huaweicloud_lts_cce_accesses": lts.DataSourceCceAccesses(), diff --git a/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_record_callbacks_test.go b/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_record_callbacks_test.go new file mode 100644 index 0000000000..f59917db1e --- /dev/null +++ b/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_record_callbacks_test.go @@ -0,0 +1,122 @@ +package live + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceRecordCallbacks_basic(t *testing.T) { + var ( + dataSource = "data.huaweicloud_live_record_callbacks.test" + dc = acceptance.InitDataSourceCheck(dataSource) + + byDomainName = "data.huaweicloud_live_record_callbacks.filter_by_domain_name" + dcByDomainName = acceptance.InitDataSourceCheck(byDomainName) + + byAppName = "data.huaweicloud_live_record_callbacks.filter_by_app_name" + dcByAppName = acceptance.InitDataSourceCheck(byAppName) + + allByAppName = "data.huaweicloud_live_record_callbacks.filter_all_by_app_name" + dcAllByAppName = acceptance.InitDataSourceCheck(allByAppName) + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckLiveIngestDomainName(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceRecordCallbacks_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.app_name"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.created_at"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.domain_name"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.id"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.sign_type"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.types.#"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.updated_at"), + resource.TestCheckResourceAttrSet(dataSource, "callbacks.0.url"), + + dcByDomainName.CheckResourceExists(), + resource.TestCheckOutput("domain_name_filter_is_useful", "true"), + + dcByAppName.CheckResourceExists(), + resource.TestCheckOutput("app_name_filter_is_useful", "true"), + + dcAllByAppName.CheckResourceExists(), + resource.TestCheckOutput("all_app_name_filter_is_useful", "true"), + ), + }, + }, + }) +} + +func testDataSourceRecordCallbacks_basic() string { + return fmt.Sprintf(` +%s + +data "huaweicloud_live_record_callbacks" "test" { + depends_on = [huaweicloud_live_record_callback.test] +} + +# Filter by domain_name +locals { + domain_name = data.huaweicloud_live_record_callbacks.test.callbacks[0].domain_name +} + +data "huaweicloud_live_record_callbacks" "filter_by_domain_name" { + depends_on = [huaweicloud_live_record_callback.test] + + domain_name = local.domain_name +} + +locals { + domain_name_filter_result = [ + for v in data.huaweicloud_live_record_callbacks.filter_by_domain_name.callbacks[*].domain_name : v == local.domain_name + ] +} + +output "domain_name_filter_is_useful" { + value = alltrue(local.domain_name_filter_result) && length(local.domain_name_filter_result) > 0 +} + +# Filter by app_name +locals { + app_name = data.huaweicloud_live_record_callbacks.test.callbacks[0].app_name +} + +data "huaweicloud_live_record_callbacks" "filter_by_app_name" { + depends_on = [huaweicloud_live_record_callback.test] + + app_name = local.app_name +} + +locals { + app_name_filter_result = [ + for v in data.huaweicloud_live_record_callbacks.filter_by_app_name.callbacks[*].app_name : v == local.app_name || v == "*" + ] +} + +output "app_name_filter_is_useful" { + value = alltrue(local.app_name_filter_result) && length(local.app_name_filter_result) > 0 +} + +# Filter all by app_name (*) +data "huaweicloud_live_record_callbacks" "filter_all_by_app_name" { + depends_on = [huaweicloud_live_record_callback.test] + + app_name = "*" +} + +output "all_app_name_filter_is_useful" { + value = length(data.huaweicloud_live_record_callbacks.filter_all_by_app_name.callbacks) > 0 +} +`, testCallBack_basic()) +} diff --git a/huaweicloud/services/live/data_source_huaweicloud_live_record_callbacks.go b/huaweicloud/services/live/data_source_huaweicloud_live_record_callbacks.go new file mode 100644 index 0000000000..401fd17ad8 --- /dev/null +++ b/huaweicloud/services/live/data_source_huaweicloud_live_record_callbacks.go @@ -0,0 +1,169 @@ +// Generated by PMS #479 +package live + +import ( + "context" + + "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/tidwall/gjson" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +func DataSourceLiveRecordCallbacks() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceLiveRecordCallbacksRead, + + 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.`, + }, + "app_name": { + Type: schema.TypeString, + Optional: true, + Description: `Specifies the application name.`, + }, + "callbacks": { + Type: schema.TypeList, + Computed: true, + Description: `The callback configurations.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "sign_type": { + Type: schema.TypeString, + Computed: true, + Description: `The encryption type.`, + }, + "created_at": { + Type: schema.TypeString, + Computed: true, + Description: `The creation time in the format of yyyy-mm-ddThh:mm:ssZ (UTC time).`, + }, + "updated_at": { + Type: schema.TypeString, + Computed: true, + Description: `The latest modification time in the format of yyyy-mm-ddThh:mm:ssZ (UTC time).`, + }, + "id": { + Type: schema.TypeString, + Computed: true, + Description: `The recording callback ID.`, + }, + "domain_name": { + Type: schema.TypeString, + Computed: true, + Description: `The ingest domain name.`, + }, + "app_name": { + Type: schema.TypeString, + Computed: true, + Description: `The application name.`, + }, + "url": { + Type: schema.TypeString, + Computed: true, + Description: `The callback URL for sending recording notifications.`, + }, + "types": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Description: `The types of recording notifications.`, + }, + }, + }, + }, + }, + } +} + +type RecordCallbacksDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newRecordCallbacksDSWrapper(d *schema.ResourceData, meta interface{}) *RecordCallbacksDSWrapper { + return &RecordCallbacksDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceLiveRecordCallbacksRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newRecordCallbacksDSWrapper(d, meta) + lisRecCalConRst, err := wrapper.ListRecordCallbackConfigs() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listRecordCallbackConfigsToSchema(lisRecCalConRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API LIVE GET /v1/{project_id}/record/callbacks +func (w *RecordCallbacksDSWrapper) ListRecordCallbackConfigs() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "live") + if err != nil { + return nil, err + } + + uri := "/v1/{project_id}/record/callbacks" + params := map[string]any{ + "publish_domain": w.Get("domain_name"), + "app": w.Get("app_name"), + } + params = utils.RemoveNil(params) + return httphelper.New(client). + Method("GET"). + URI(uri). + Query(params). + OffsetPager("callback_config", "offset", "limit", 0). + Request(). + Result() +} + +func (w *RecordCallbacksDSWrapper) listRecordCallbackConfigsToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("callbacks", schemas.SliceToList(body.Get("callback_config"), + func(callbacks gjson.Result) any { + return map[string]any{ + "sign_type": callbacks.Get("sign_type").Value(), + "created_at": callbacks.Get("create_time").Value(), + "updated_at": callbacks.Get("update_time").Value(), + "id": callbacks.Get("id").Value(), + "domain_name": callbacks.Get("publish_domain").Value(), + "app_name": callbacks.Get("app").Value(), + "url": callbacks.Get("notify_callback_url").Value(), + "types": schemas.SliceToStrList(callbacks.Get("notify_event_subscription")), + } + }, + )), + ) + return mErr.ErrorOrNil() +}