forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(live): add a new resource to manage notification cofiguration (h…
- Loading branch information
1 parent
1172c45
commit b993762
Showing
4 changed files
with
456 additions
and
14 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,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> | ||
``` |
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
148 changes: 148 additions & 0 deletions
148
...loud/services/acceptance/live/resource_huaweicloud_live_notification_cofiguration_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,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, | ||
¬ifyConfigObj, | ||
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 | ||
} | ||
} |
Oops, something went wrong.