Skip to content

Commit

Permalink
feat(live): support a new resource to manage geo-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang committed Dec 13, 2024
1 parent ed99599 commit eae0b88
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/resources/live_geo_blocking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
subcategory: "Live"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_live_geo_blocking"
description: |-
Manages a Live geo-blocking resource within HuaweiCloud.
---

# huaweicloud_live_geo_blocking

Manages a Live geo-blocking resource within HuaweiCloud.

-> Destroying this resource means that there is no geo-blocking on the streaming domain name.

## Example Usage

```hcl
variable "domain_name" {}
resource "huaweicloud_live_geo_blocking" "test" {
domain_name = var.domain_name
app_name = "live"
area_whitelist = ["CN-IN", "CN-HK"]
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used. Changing this will create a new resource.

* `domain_name` - (Required, String, ForceNew) Specifies the streaming domain name.

Changing this will create a new resource.

* `app_name` - (Required, String) Specifies the application name.

* `area_whitelist` - (Required, List) Specifies the list of supported areas.
The values of all region codes, except that of China, contain two uppercase letters.
For the code format, see [ISO 3166-1alpha-2](https://www.iso.org/obp/ui/#search/code/).
Some options are as follows:
+ **CN-IN**: Chinese mainland.
+ **CN-HK**: Hong Kong (China).
+ **CN-MO**: Macao (China).
+ **CN-TW**: Taiwan (China).
+ **BR**: Brazil.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID.

## Import

The Live geo-blocking resource can be imported using `domain_name` and `app_name`, separated by a slash (/), e.g.

```bash
$ terraform import huaweicloud_live_geo_blocking.test <domain_name>/<app_name>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,7 @@ func Provider() *schema.Provider {
"huaweicloud_live_url_validation": live.ResourceUrlValidation(),
"huaweicloud_live_channel": live.ResourceChannel(),
"huaweicloud_live_https_certificate": live.ResourceHTTPSCertificate(),
"huaweicloud_live_geo_blocking": live.ResourceGeoBlocking(),

"huaweicloud_lts_aom_access": lts.ResourceAOMAccess(),
"huaweicloud_lts_group": lts.ResourceLTSGroup(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package live

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
geoBlocking "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/live"

Check warning on line 14 in huaweicloud/services/acceptance/live/resource_huaweicloud_live_geo_blocking_test.go

View workflow job for this annotation

GitHub Actions / golangci

import-alias-naming: import name (geoBlocking) must match the regular expression: ^[a-z][a-z0-9]{0,}$ (revive)
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func getResourceGeoBlockingFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
var (
region = acceptance.HW_REGION_NAME
product = "live"
domainName = state.Primary.Attributes["domain_name"]
app = state.Primary.Attributes["app_name"]
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return nil, fmt.Errorf("error creating Live client: %s", err)
}

respBody, err := geoBlocking.ReadGeoBlocking(client, domainName)
if err != nil {
return nil, fmt.Errorf("error retrieving Live geo blocking: %s", err)
}

expression := fmt.Sprintf("apps[?app == '%s']|[0].area_whitelist", app)
areaWhitelist := utils.PathSearch(expression, respBody, nil)
if areaWhitelist == nil {
return nil, golangsdk.ErrDefault404{}
}

return respBody, nil
}

func TestAccResourceGeoBlocking_basic(t *testing.T) {
var (
obj interface{}
rName = "huaweicloud_live_geo_blocking.test"
)

rc := acceptance.InitResourceCheck(
rName,
&obj,
getResourceGeoBlockingFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckLiveStreamingDomainName(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testResourceGeoBlocking_basic(),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "domain_name", acceptance.HW_LIVE_STREAMING_DOMAIN_NAME),
resource.TestCheckResourceAttr(rName, "app_name", "live"),
resource.TestCheckResourceAttr(rName, "area_whitelist.#", "5"),
),
},
{
Config: testResourceGeoBlocking_basic_update(),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "domain_name", acceptance.HW_LIVE_STREAMING_DOMAIN_NAME),
resource.TestCheckResourceAttr(rName, "app_name", "live"),
resource.TestCheckResourceAttr(rName, "area_whitelist.#", "3"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateIdFunc: testAccGeoBlockingImportState(rName),
},
},
})
}

func testResourceGeoBlocking_basic() string {
return fmt.Sprintf(`
resource "huaweicloud_live_geo_blocking" "test" {
domain_name = "%s"
app_name = "live"
area_whitelist = ["AE", "AF", "CN-IN", "CN-HK", "CN-MO"]
}
`, acceptance.HW_LIVE_STREAMING_DOMAIN_NAME)
}

func testResourceGeoBlocking_basic_update() string {
return fmt.Sprintf(`
resource "huaweicloud_live_geo_blocking" "test" {
domain_name = "%s"
app_name = "live"
area_whitelist = ["AE", "AF", "CN-IN"]
}
`, acceptance.HW_LIVE_STREAMING_DOMAIN_NAME)
}

func testAccGeoBlockingImportState(rName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[rName]
if !ok {
return "", fmt.Errorf("resource (%s) not found", rName)
}

domainName := rs.Primary.Attributes["domain_name"]
appName := rs.Primary.Attributes["app_name"]
if domainName == "" || appName == "" {
return "", fmt.Errorf("the imported ID format is invalid, want '<domain_name>/<app_name>',"+
" but got '%s/%s'", domainName, appName)
}
return fmt.Sprintf("%s/%s", domainName, appName), nil
}
}
Loading

0 comments on commit eae0b88

Please sign in to comment.