Skip to content

Commit

Permalink
feat(live/geo_blockings): support live geo blockings dataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyangyang222 committed Dec 17, 2024
1 parent f3e2a1d commit 883eeb4
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 4 deletions.
54 changes: 54 additions & 0 deletions docs/data-sources/live_geo_blockings.md
Original file line number Diff line number Diff line change
@@ -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.

<a name="apps_struct"></a>
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 valid values are as follows:
+ **CN-IN**: Chinese Mainland.
+ **CN-HK**: Hong Kong, China.
+ **CN-MO**: Macao, China.
+ **CN-TW**: Taiwan, China.
+ **BR**: Brazil.
9 changes: 5 additions & 4 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,11 @@ 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_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_lts_aom_accesses": lts.DataSourceAOMAccesses(),
"huaweicloud_lts_cce_accesses": lts.DataSourceCceAccesses(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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: %s", 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
}

0 comments on commit 883eeb4

Please sign in to comment.