Skip to content

Commit

Permalink
refactor(live): refactor the transcoding resource code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 committed Jan 16, 2025
1 parent cbbd028 commit 738a59e
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 301 deletions.
45 changes: 22 additions & 23 deletions docs/resources/live_transcoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@
subcategory: "Live"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_live_transcoding"
description: ""
description: |-
Manages a Live transcoding resource within HuaweiCloud.
---

# huaweicloud_live_transcoding

Manages a Live transcoding within HuaweiCloud.
Manages a Live transcoding resource within HuaweiCloud.

## Example Usage

### Create a transcoding

```hcl
variable "ingest_domain_name" {}
resource "huaweicloud_live_domain" "ingestDomain" {
name = var.ingest_domain_name
type = "push"
}
variable "app_name" {}
variable "video_encoding" {}
variable "template_name" {}
resource "huaweicloud_live_transcoding" "test" {
domain_name = huaweicloud_live_domain.ingestDomain.name
app_name = "live"
video_encoding = "H264"
domain_name = var.ingest_domain_name
app_name = var.app_name
video_encoding = var.video_encoding
templates {
name = "L"
name = var.template_name
width = 300
height = 400
bitrate = 300
Expand All @@ -39,14 +38,15 @@ resource "huaweicloud_live_transcoding" "test" {

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create this resource. If omitted,
the provider-level region will be used. Changing this parameter will create a new resource.
* `region` - (Optional, String, ForceNew) Specifies the region in which to create this 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.
Changing this parameter will create a new resource.
Changing this parameter will create a new resource.

* `app_name` - (Required, String, ForceNew) Specifies the application name.
Changing this parameter will create a new resource.
Changing this parameter will create a new resource.

* `video_encoding` - (Required, String) Specifies the video codec. The valid values are **H264** and **H265**.

Expand Down Expand Up @@ -76,13 +76,13 @@ contains letters, digits and hyphens (-).
+ **When the video encoding is H265**, value range: `320` ~ `3,840` and must be a multiple of `4`.

* `height` - (Required, Int) Specifies video height (unit: pixel).
+ **When the video encoding is H264**, value range: 32 ~ 2160 and must be a multiple of `2`.
+ **When the video encoding is H265**, value range: 240 ~ 2160 and must be a multiple of `4`.
+ **When the video encoding is H264**, value range: `32` ~ `2,160` and must be a multiple of `2`.
+ **When the video encoding is H265**, value range: `240` ~ `2,160` and must be a multiple of `4`.

* `bitrate` - (Required, Int) Specifies the bitrate of a transcoded video, in kbit/s. Value range: `40` ~ `30,000`.

* `frame_rate` - (Optional, Int) Specifies the frame rate of the transcoded video, in fps. Value range: `0` ~ `30`.
Value 0 indicates that the frame rate remains unchanged.
Value `0` indicates that the frame rate remains unchanged.

* `protocol` - (Optional, String) Specifies the protocol type supported for transcoding output.
The valid value is **RTMP**. Defaults to **RTMP**.
Expand Down Expand Up @@ -122,15 +122,14 @@ Value 0 indicates that the frame rate remains unchanged.

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

* `id` - The resource ID in format of **domain_name/app_name**. It is composed of domain name and the application name,
separated by a slash.
* `id` - The resource ID in format of **domain_name/app_name**.

## Import

Transcoding can be imported using the `domain_name` and `app_name`, separated by a slash. e.g.
The resource can be imported using the `domain_name` and `app_name`, separated by a slash, e.g.

```bash
$ terraform import huaweicloud_live_transcoding.test play.example.demo.com/live
$ terraform import huaweicloud_live_transcoding.test <domian_name>/<app_name>
```

Note that the imported state may not be identical to your resource definition, due to some attributes missing from the
Expand All @@ -141,7 +140,7 @@ with the resource. Also, you can ignore changes as below.

```hcl
resource "huaweicloud_live_transcoding" "test" {
...
...
lifecycle {
ignore_changes = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,51 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/live/v1/model"

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

func getTranscodingResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.HcLiveV1Client(acceptance.HW_REGION_NAME)
func getTranscodingFunc(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 v1 client: %s", err)
return nil, fmt.Errorf("error creating Live client: %s", err)
}

var (
domain = state.Primary.Attributes["domain_name"]
appName = state.Primary.Attributes["app_name"]
getOpts = &model.ShowTranscodingsTemplateRequest{
Domain: domain,
AppName: &appName,
}
)
resp, err := client.ShowTranscodingsTemplate(getOpts)
getRespBody, err := live.GetTranscodingTemplates(client, state.Primary.Attributes["domain_name"],
state.Primary.Attributes["app_name"])
if err != nil {
return nil, fmt.Errorf("error retrieving Live transcoding: %s", err)
}

if resp.Templates == nil || len(*resp.Templates) == 0 {
return nil, golangsdk.ErrDefault404{}
}

return resp, nil
return getRespBody, nil
}

func TestAccTranscoding_basic(t *testing.T) {
var (
obj model.ShowTranscodingsTemplateResponse
pushDomainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
transcodingObj interface{}
rName = "huaweicloud_live_transcoding.test"
)
rc := acceptance.InitResourceCheck(
rName,
&obj,
getTranscodingResourceFunc,
&transcodingObj,
getTranscodingFunc,
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckLiveIngestDomainName(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testTranscoding_basic(pushDomainName),
Config: testAccTranscoding_basic(),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "domain_name", pushDomainName),
resource.TestCheckResourceAttr(rName, "domain_name", acceptance.HW_LIVE_INGEST_DOMAIN_NAME),
resource.TestCheckResourceAttr(rName, "app_name", "live"),
resource.TestCheckResourceAttr(rName, "video_encoding", "H264"),
resource.TestCheckResourceAttr(rName, "trans_type", "publish"),
Expand All @@ -72,6 +61,7 @@ func TestAccTranscoding_basic(t *testing.T) {
resource.TestCheckResourceAttr(rName, "templates.0.width", "300"),
resource.TestCheckResourceAttr(rName, "templates.0.height", "400"),
resource.TestCheckResourceAttr(rName, "templates.0.bitrate", "300"),
resource.TestCheckResourceAttr(rName, "templates.0.frame_rate", "60"),
resource.TestCheckResourceAttr(rName, "templates.0.protocol", "RTMP"),
resource.TestCheckResourceAttr(rName, "templates.0.i_frame_interval", "500"),
resource.TestCheckResourceAttr(rName, "templates.0.gop", "0"),
Expand All @@ -80,15 +70,16 @@ func TestAccTranscoding_basic(t *testing.T) {
),
},
{
Config: testTranscoding_update(pushDomainName),
Config: testAccTranscoding_update(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rName, "domain_name", pushDomainName),
resource.TestCheckResourceAttr(rName, "app_name", "live"),
resource.TestCheckResourceAttr(rName, "video_encoding", "H264"),
resource.TestCheckResourceAttr(rName, "video_encoding", "H265"),
resource.TestCheckResourceAttr(rName, "trans_type", "play"),
resource.TestCheckResourceAttr(rName, "low_bitrate_hd", "true"),
resource.TestCheckResourceAttr(rName, "templates.#", "2"),
resource.TestCheckResourceAttr(rName, "templates.0.protocol", "RTMP"),
resource.TestCheckResourceAttr(rName, "templates.0.width", "480"),
resource.TestCheckResourceAttr(rName, "templates.0.height", "520"),
resource.TestCheckResourceAttr(rName, "templates.0.bitrate", "600"),
resource.TestCheckResourceAttr(rName, "templates.0.frame_rate", "0"),
resource.TestCheckResourceAttr(rName, "templates.0.i_frame_interval", "0"),
resource.TestCheckResourceAttr(rName, "templates.0.gop", "10"),
resource.TestCheckResourceAttr(rName, "templates.0.bitrate_adaptive", "minimum"),
Expand All @@ -97,8 +88,10 @@ func TestAccTranscoding_basic(t *testing.T) {
resource.TestCheckResourceAttr(rName, "templates.1.width", "600"),
resource.TestCheckResourceAttr(rName, "templates.1.height", "800"),
resource.TestCheckResourceAttr(rName, "templates.1.bitrate", "300"),
resource.TestCheckResourceAttr(rName, "templates.1.frame_rate", "30"),
resource.TestCheckResourceAttr(rName, "templates.1.protocol", "RTMP"),
resource.TestCheckResourceAttr(rName, "templates.1.gop", "10"),
resource.TestCheckResourceAttr(rName, "templates.1.i_frame_interval", "50"),
resource.TestCheckResourceAttr(rName, "templates.1.gop", "2"),
resource.TestCheckResourceAttr(rName, "templates.1.bitrate_adaptive", "off"),
resource.TestCheckResourceAttr(rName, "templates.1.i_frame_policy", "auto"),
),
Expand All @@ -107,7 +100,6 @@ func TestAccTranscoding_basic(t *testing.T) {
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateId: fmt.Sprintf("%s/live", pushDomainName),
ImportStateVerifyIgnore: []string{
"trans_type",
},
Expand All @@ -116,15 +108,10 @@ func TestAccTranscoding_basic(t *testing.T) {
})
}

func testTranscoding_basic(pushDomainName string) string {
func testAccTranscoding_basic() string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "ingestDomain" {
name = "%s"
type = "push"
}
resource "huaweicloud_live_transcoding" "test" {
domain_name = huaweicloud_live_domain.ingestDomain.name
domain_name = "%s"
app_name = "live"
video_encoding = "H264"
trans_type = "publish"
Expand All @@ -134,38 +121,35 @@ resource "huaweicloud_live_transcoding" "test" {
width = 300
height = 400
bitrate = 300
frame_rate = 60
protocol = "RTMP"
i_frame_interval = 500
gop = 0
i_frame_interval = "500"
gop = "0"
bitrate_adaptive = "adaptive"
i_frame_policy = "strictSync"
}
}
`, pushDomainName)
`, acceptance.HW_LIVE_INGEST_DOMAIN_NAME)
}

func testTranscoding_update(pushDomainName string) string {
func testAccTranscoding_update() string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "ingestDomain" {
name = "%s"
type = "push"
}
resource "huaweicloud_live_transcoding" "test" {
domain_name = huaweicloud_live_domain.ingestDomain.name
domain_name = "%s"
app_name = "live"
video_encoding = "H264"
video_encoding = "H265"
trans_type = "play"
low_bitrate_hd = true
templates {
name = "t1"
width = 300
height = 400
bitrate = 300
width = 480
height = 520
bitrate = 600
frame_rate = 0
protocol = "RTMP"
i_frame_interval = 0
gop = 10
i_frame_interval = "0"
gop = "10"
bitrate_adaptive = "minimum"
i_frame_policy = "auto"
}
Expand All @@ -175,11 +159,11 @@ resource "huaweicloud_live_transcoding" "test" {
width = 600
height = 800
bitrate = 300
frame_rate = 30
protocol = "RTMP"
gop = 10
bitrate_adaptive = "off"
i_frame_policy = "auto"
}
}
`, pushDomainName)
`, acceptance.HW_LIVE_INGEST_DOMAIN_NAME)
}
Loading

0 comments on commit 738a59e

Please sign in to comment.