From f18e3ca0258e4d75f5a8632c0bb612b555325c2e Mon Sep 17 00:00:00 2001 From: jinyangyang222 Date: Mon, 16 Dec 2024 20:46:57 +0800 Subject: [PATCH] feat(live/geo_blockings): support live geo blockings dataSource --- docs/data-sources/live_geo_blockings.md | 54 +++++++++ huaweicloud/provider.go | 7 +- ...rce_huaweicloud_live_geo_blockings_test.go | 48 ++++++++ ...a_source_huaweicloud_live_geo_blockings.go | 105 ++++++++++++++++++ 4 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 docs/data-sources/live_geo_blockings.md create mode 100644 huaweicloud/services/acceptance/live/data_source_huaweicloud_live_geo_blockings_test.go create mode 100644 huaweicloud/services/live/data_source_huaweicloud_live_geo_blockings.go diff --git a/docs/data-sources/live_geo_blockings.md b/docs/data-sources/live_geo_blockings.md new file mode 100644 index 00000000000..4893f5c6250 --- /dev/null +++ b/docs/data-sources/live_geo_blockings.md @@ -0,0 +1,54 @@ +--- +subcategory: "Live" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_live_geo_blockings" +description: |- + Use this data source to get the list of Live geo blockings within HuaweiCloud. +--- + +# huaweicloud_live_geo_blockings + +Use this data source to get the list of Live geo blockings within HuaweiCloud. + +## Example Usage + +```hcl +variable "domain_name" {} + +data "huaweicloud_live_geo_blockings" "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 streaming domain name. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID in UUID format. + +* `apps` - The list of the application. + + The [apps](#apps_struct) structure is documented below. + + +The `apps` block supports: + +* `app_name` - The application name. + +* `area_whitelist` - The restricted area list, an empty list indicates no restrictions. + Except for China, codes for other regions are capitalized with `2` letters. + Some values are as follows: + + **CN-IN**: Chinese Mainland. + + **CN-HK**: Hong Kong, China. + + **CN-MO**: Macao, China. + + **CN-TW**: Taiwan, China. + + **BR**: Brazil. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index d1fb29c78f6..18f94d958da 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -903,9 +903,10 @@ 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_domains": live.DataSourceLiveDomains(), + "huaweicloud_live_recordings": live.DataSourceLiveRecordings(), + "huaweicloud_live_transcodings": live.DataSourceLiveTranscodings(), + "huaweicloud_live_geo_blockings": live.DataSourceGeoBlockings(), "huaweicloud_lts_aom_accesses": lts.DataSourceAOMAccesses(), "huaweicloud_lts_cce_accesses": lts.DataSourceCceAccesses(), diff --git a/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_geo_blockings_test.go b/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_geo_blockings_test.go new file mode 100644 index 00000000000..52accb59e3d --- /dev/null +++ b/huaweicloud/services/acceptance/live/data_source_huaweicloud_live_geo_blockings_test.go @@ -0,0 +1,48 @@ +package live + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccDataSourceGeoBlockings_basic(t *testing.T) { + var ( + dataSourceName = "data.huaweicloud_live_geo_blockings.test" + dc = acceptance.InitDataSourceCheck(dataSourceName) + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckLiveStreamingDomainName(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGeoBlockings_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttr(dataSourceName, "apps.#", "1"), + resource.TestCheckResourceAttr(dataSourceName, "apps.0.app_name", "live"), + resource.TestCheckResourceAttr(dataSourceName, "apps.0.area_whitelist.#", "5"), + ), + }, + }, + }) +} + +func testDataSourceGeoBlockings_basic() string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_live_geo_blockings" "test" { + domain_name = "%[2]s" + + depends_on = [huaweicloud_live_geo_blocking.test] +} +`, testResourceGeoBlocking_basic(), acceptance.HW_LIVE_STREAMING_DOMAIN_NAME) +} diff --git a/huaweicloud/services/live/data_source_huaweicloud_live_geo_blockings.go b/huaweicloud/services/live/data_source_huaweicloud_live_geo_blockings.go new file mode 100644 index 00000000000..7b0ecbf6d0f --- /dev/null +++ b/huaweicloud/services/live/data_source_huaweicloud_live_geo_blockings.go @@ -0,0 +1,105 @@ +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/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +// @API LIVE GET /v1/{project_id}/domain/geo-blocking +func DataSourceGeoBlockings() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceGeoBlockingsRead, + + 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, + Required: true, + Description: `Specifies the ingest domain name to which the recording rules belong.`, + }, + "apps": { + Type: schema.TypeList, + Computed: true, + Description: `The list of the recording rules.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "app_name": { + Type: schema.TypeString, + Computed: true, + Description: `The recording rule ID.`, + }, + "area_whitelist": { + Type: schema.TypeList, + Computed: true, + Description: `The default recording configuration rule.`, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + }, + }, + }, + } +} + +func dataSourceGeoBlockingsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var ( + cfg = meta.(*config.Config) + region = cfg.GetRegion(d) + mErr *multierror.Error + product = "live" + ) + + client, err := cfg.NewServiceClient(product, region) + if err != nil { + return diag.Errorf("error creating Live client: %s", err) + } + + respBody, err := ReadGeoBlocking(client, d.Get("domain_name").(string)) + if err != nil { + return diag.Errorf("error retrieving Live geo blockings", err) + } + + dataSourceId, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + + d.SetId(dataSourceId) + + apps := utils.PathSearch("apps", respBody, make([]interface{}, 0)).([]interface{}) + mErr = multierror.Append( + mErr, + d.Set("region", region), + d.Set("apps", flattenApps(apps)), + ) + + return diag.FromErr(mErr.ErrorOrNil()) +} + +func flattenApps(apps []interface{}) []map[string]interface{} { + if len(apps) == 0 { + return nil + } + + result := make([]map[string]interface{}, len(apps)) + for i, v := range apps { + result[i] = map[string]interface{}{ + "app_name": utils.PathSearch("app", v, nil), + "area_whitelist": utils.PathSearch("area_whitelist", v, nil), + } + } + + return result +}