Skip to content

Commit

Permalink
feat(live): add a new resource to manage notification cofiguration (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 authored Dec 24, 2024
1 parent 1172c45 commit b993762
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 14 deletions.
60 changes: 60 additions & 0 deletions docs/resources/live_notification_cofiguration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
subcategory: "Live"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_live_notification_cofiguration"
description: |-
Manages a notification cofiguration resource within HuaweiCloud.
---

# huaweicloud_live_notification_cofiguration

Manages a notification cofiguration resource within HuaweiCloud.

## Example Usage

```hcl
variable "domain_name" {}
variable "url" {}
resource "huaweicloud_live_notification_cofiguration" "test" {
domain_name = var.domain_name
url = var.url
}
```

## 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 ingest domain name to which the notification cofiguration
belongs.
Changing this parameter will create a new resource.

* `url` - (Required, String) Specifies the callback URL, which must start with `http://` or `https://`.

* `auth_sign_key` - (Optional, String) Specifies the authentication key.
The valid length is `32` to `128` characters.

* `call_back_area` - (Optional, String) Specifies the region where the server that receives callback notifications
is located.
The valid vaules are as follows:
+ **mainland_china**: Indicates Chinese mainland.
+ **outside_mainland_china**: Indicates outside the Chinese mainland.

## 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_notification_cofiguration.test <domain_name>
```
29 changes: 15 additions & 14 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,20 +1867,21 @@ func Provider() *schema.Provider {
"huaweicloud_lb_pool": lb.ResourcePoolV2(),
"huaweicloud_lb_whitelist": lb.ResourceWhitelistV2(),

"huaweicloud_live_bucket_authorization": live.ResourceLiveBucketAuthorization(),
"huaweicloud_live_disable_push_stream": live.ResourceDisablePushStream(),
"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(),
"huaweicloud_live_channel": live.ResourceChannel(),
"huaweicloud_live_https_certificate": live.ResourceHTTPSCertificate(),
"huaweicloud_live_geo_blocking": live.ResourceGeoBlocking(),
"huaweicloud_live_ip_acl": live.ResourceIpAcl(),
"huaweicloud_live_hls_configuration": live.ResourceHlsConfiguration(),
"huaweicloud_live_bucket_authorization": live.ResourceLiveBucketAuthorization(),
"huaweicloud_live_channel": live.ResourceChannel(),
"huaweicloud_live_disable_push_stream": live.ResourceDisablePushStream(),
"huaweicloud_live_domain": live.ResourceDomain(),
"huaweicloud_live_geo_blocking": live.ResourceGeoBlocking(),
"huaweicloud_live_hls_configuration": live.ResourceHlsConfiguration(),
"huaweicloud_live_https_certificate": live.ResourceHTTPSCertificate(),
"huaweicloud_live_ip_acl": live.ResourceIpAcl(),
"huaweicloud_live_notification_cofiguration": live.ResourceNotificationCofiguration(),
"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(),

"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,148 @@
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 getNotificationConfigFunc(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}/notifications/publish"
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 notification cofiguration: %s", err)
}

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

key := utils.PathSearch("url", getRespBody, "").(string)
if key == "" {
return nil, golangsdk.ErrDefault404{}
}

return getRespBody, nil
}

func TestAccNotificationConfiguration_basic(t *testing.T) {
var (
notifyConfigObj interface{}
rName = "huaweicloud_live_notification_cofiguration.test"
domainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
authKey1 = "8DDEtfv0ZE3Z3EAUokupq2Zf2NAgyL5vSZAmlgmX5jzBd2fHohrA9u727I9U0RZR8mHsxTnwDBKXNUHw52NmA1iZHHitlXZ"
authkey2 = "RHzXPbi4Ll0Sr4IvwEvBKzn6tEP1pIt3vtGUAdlJU0kxOzYbKXkJpTVWd2Z2ZdPDTu2koXalAXRc8o3HVp8K8S5rjAAtFE"
)

rc := acceptance.InitResourceCheck(
rName,
&notifyConfigObj,
getNotificationConfigFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccNotificationConfig_basic(domainName, authKey1),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttrPair(rName, "domain_name", "huaweicloud_live_domain.test", "name"),
resource.TestCheckResourceAttr(rName, "url", "http://mycallback.com/notify_cofig"),
resource.TestCheckResourceAttr(rName, "auth_sign_key", authKey1),
resource.TestCheckResourceAttr(rName, "call_back_area", "mainland_china"),
),
},
{
Config: testAccNotificationConfig_update(domainName, authkey2),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "url", "https://mycallback.com.cn/notify_cofig"),
resource.TestCheckResourceAttr(rName, "auth_sign_key", authkey2),
resource.TestCheckResourceAttr(rName, "call_back_area", "outside_mainland_china"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: false,
ImportStateIdFunc: testAccNotificationConfigImportState(rName),
},
},
})
}

func testAccNotificationConfig_basic(name, signKey string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "test" {
name = "%[1]s"
type = "push"
}
resource "huaweicloud_live_notification_cofiguration" "test" {
domain_name = huaweicloud_live_domain.test.name
url = "http://mycallback.com/notify_cofig"
auth_sign_key = "%[2]s"
call_back_area = "mainland_china"
}
`, name, signKey)
}

func testAccNotificationConfig_update(name, signKey string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "test" {
name = "%[1]s"
type = "push"
}
resource "huaweicloud_live_notification_cofiguration" "test" {
domain_name = huaweicloud_live_domain.test.name
url = "https://mycallback.com.cn/notify_cofig"
auth_sign_key = "%[2]s"
call_back_area = "outside_mainland_china"
}
`, name, signKey)
}

func testAccNotificationConfigImportState(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 b993762

Please sign in to comment.