Skip to content

Commit

Permalink
feat(live): add a new resource to manage referer validation (#5999)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 authored Dec 13, 2024
1 parent 2a01a6e commit c88adf3
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 0 deletions.
67 changes: 67 additions & 0 deletions docs/resources/live_referer_validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
subcategory: "Live"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_live_referer_validation"
description: |-
Manages a referer validation resource within HuaweiCloud.
---

# huaweicloud_live_referer_validation

Manages a referer validation resource within HuaweiCloud.

## Example Usage

```hcl
variable "domain_name" {}
variable "referer_config_empty" {}
variable "referer_white_list" {}
variable "referer_auth_list" {
type = list(string)
}
resource "huaweicloud_live_referer_validation" "test" {
domain_name = var.domain_name
referer_config_empty = var.referer_config_empty
referer_white_list = var.referer_white_list
referer_auth_list = var.referer_auth_list
}
```

## 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 parameter will create a new resource.

* `domain_name` - (Required, String, ForceNew) Specifies the streaming domain name to which the referer validation
belongs.
Changing this parameter will create a new resource.

* `referer_config_empty` - (Required, String) Specifies whether the referer header is included.
The value can be **true** or **false**.

* `referer_white_list` - (Required, String) Specifies whether the referer is in the trustlist.
The valid values are as follows:
+ **true**: Indicates referer whitelist.
+ **false**: Indicates referer blacklist.

* `referer_auth_list` - (Required, List) Specifies the domain name list.
The maximum length is `100`.
The domain name can be a specific domain name or a regular expression. e.g. `www.example.com`, `www.*com`.

## Attribute Reference

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

* `id` - The resource ID, UUID format.

## Import

The resource can be imported using `domain_name`, e.g.

```bash
$ terraform import huaweicloud_live_referer_validation.test <domain_name>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,7 @@ func Provider() *schema.Provider {
"huaweicloud_live_domain": live.ResourceDomain(),
"huaweicloud_live_record_callback": live.ResourceRecordCallback(),
"huaweicloud_live_recording": live.ResourceRecording(),
"huaweicloud_live_referer_validation": live.ResourceRefererValidation(),
"huaweicloud_live_snapshot": live.ResourceLiveSnapshot(),
"huaweicloud_live_transcoding": live.ResourceTranscoding(),
"huaweicloud_live_url_validation": live.ResourceUrlValidation(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package live

import (
"fmt"
"strings"
"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"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func getRefererValidationFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := acceptance.HW_REGION_NAME
client, err := cfg.NewServiceClient("live", region)
if err != nil {
return nil, fmt.Errorf("error creating Live client: %s", err)
}

getHttpUrl := "v1/{project_id}/guard/referer-chain"
getPath := client.Endpoint + getHttpUrl
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
getPath = fmt.Sprintf("%s?domain=%v", getPath, state.Primary.Attributes["domain_name"])

getOpts := golangsdk.RequestOpts{
KeepResponseBody: true,
}
getResp, err := client.Request("GET", getPath, &getOpts)
if err != nil {
return nil, fmt.Errorf("error retrieving referer validation: %s", err)
}

getRespBody, err := utils.FlattenResponse(getResp)
if err != nil {
return nil, err
}

refererAuthList := utils.PathSearch("referer_auth_list", getRespBody, make([]interface{}, 0)).([]interface{})
if len(refererAuthList) == 0 {
return nil, golangsdk.ErrDefault404{}
}

return getRespBody, nil
}

func TestAccRefererValidation_basic(t *testing.T) {
var (
refererValidationObj interface{}
rName = "huaweicloud_live_referer_validation.test"
domainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
)

rc := acceptance.InitResourceCheck(
rName,
&refererValidationObj,
getRefererValidationFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccRefererValidation_basic(domainName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "domain_name", "huaweicloud_live_domain.test", "name"),
resource.TestCheckResourceAttr(rName, "referer_config_empty", "true"),
resource.TestCheckResourceAttr(rName, "referer_white_list", "true"),
resource.TestCheckResourceAttr(rName, "referer_auth_list.#", "1"),
),
},
{
Config: testAccRefererValidation_update(domainName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "referer_config_empty", "false"),
resource.TestCheckResourceAttr(rName, "referer_white_list", "false"),
resource.TestCheckResourceAttr(rName, "referer_auth_list.#", "2"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: false,
ImportStateIdFunc: testAccRefererValidationImportState(rName),
},
},
})
}

func testAccRefererValidation_basic(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "test" {
name = "%s"
type = "pull"
}
resource "huaweicloud_live_referer_validation" "test" {
domain_name = huaweicloud_live_domain.test.name
referer_config_empty = "true"
referer_white_list = "true"
referer_auth_list = ["www.test.com"]
}
`, name)
}

func testAccRefererValidation_update(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "test" {
name = "%s"
type = "pull"
}
resource "huaweicloud_live_referer_validation" "test" {
domain_name = huaweicloud_live_domain.test.name
referer_config_empty = "false"
referer_white_list = "false"
referer_auth_list = ["www.test.com","www.*com"]
}
`, name)
}

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

domainName = rs.Primary.Attributes["domain_name"]
if domainName == "" {
return "", fmt.Errorf("invalid format specified for import ID, 'domain_name' is empty")
}
return domainName, nil
}
}
Loading

0 comments on commit c88adf3

Please sign in to comment.