-
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): support a new resource to manage geo-blocking
- Loading branch information
Showing
4 changed files
with
413 additions
and
0 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,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> | ||
``` |
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
126 changes: 126 additions & 0 deletions
126
huaweicloud/services/acceptance/live/resource_huaweicloud_live_geo_blocking_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,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" | ||
"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 | ||
} | ||
} |
Oops, something went wrong.