-
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/geo_blockings): support live geo blockings dataSource
- Loading branch information
1 parent
bbb9702
commit f18e3ca
Showing
4 changed files
with
211 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_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 values are as follows: | ||
+ **CN-IN**: Chinese Mainland. | ||
+ **CN-HK**: Hong Kong, China. | ||
+ **CN-MO**: Macao, China. | ||
+ **CN-TW**: Taiwan, China. | ||
+ **BR**: Brazil. |
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
48 changes: 48 additions & 0 deletions
48
huaweicloud/services/acceptance/live/data_source_huaweicloud_live_geo_blockings_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,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) | ||
} |
105 changes: 105 additions & 0 deletions
105
huaweicloud/services/live/data_source_huaweicloud_live_geo_blockings.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,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 | ||
} |