Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(live/geo_blockings): support live geo blockings dataSource #6036

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading