Skip to content

Commit

Permalink
refactor(live): refactor the Live domain resource code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 committed Jan 10, 2025
1 parent fc2b876 commit c89b98b
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 240 deletions.
2 changes: 0 additions & 2 deletions docs/resources/live_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ The following arguments are supported:
Defaults to **mainland_china**. Changing this parameter will create a new resource.

* `enterprise_project_id` - (Optional, String, ForceNew) Specifies the enterprise project ID.
For enterprise users, if omitted, default enterprise project will be used.

Changing this parameter will create a new resource.

* `ingest_domain_name` - (Optional, String) Specifies the ingest domain name, which associates with the streaming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,113 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"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 getDomainResourceFunc(conf *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.HcLiveV1Client(acceptance.HW_REGION_NAME)
func getResourceDomainFunc(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)
}

return client.ShowDomain(&model.ShowDomainRequest{Domain: &state.Primary.ID})
return live.GetDomain(client, state.Primary.ID)
}

func TestAccDomain_basic(t *testing.T) {
var obj model.CreateDomainMappingResponse

ingestDomainName := fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
streamingDomainName := fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
ingestResourceName := "huaweicloud_live_domain.ingestDomain"
streamingResourceName := "huaweicloud_live_domain.streamingDomain"
func TestAccResourceDomain_basic(t *testing.T) {
var (
domainInfo interface{}
ingestDomainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
ingestResourceName = "huaweicloud_live_domain.ingestDomain"
)

rc := acceptance.InitResourceCheck(
ingestResourceName,
&obj,
getDomainResourceFunc,
&domainInfo,
getResourceDomainFunc,
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckEpsID(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testDomain_basic(ingestDomainName, streamingDomainName),
Config: testAccIngestDomain_basic(ingestDomainName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(ingestResourceName, "name", ingestDomainName),
resource.TestCheckResourceAttr(ingestResourceName, "type", "push"),
resource.TestCheckResourceAttr(ingestResourceName, "status", "on"),
resource.TestCheckResourceAttr(ingestResourceName, "service_area", "mainland_china"),
resource.TestCheckResourceAttr(ingestResourceName, "is_ipv6", "true"),
resource.TestCheckResourceAttr(ingestResourceName, "enterprise_project_id", acceptance.HW_ENTERPRISE_PROJECT_ID_TEST),
resource.TestCheckResourceAttr(ingestResourceName, "enterprise_project_id", "0"),
),
},
{
Config: testAccIngestDomain_update(ingestDomainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ingestResourceName, "is_ipv6", "false"),
),
},
{
ResourceName: ingestResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccResourceDomain_streamDomain(t *testing.T) {
var (
domainInfo interface{}
streamingDomainName = fmt.Sprintf("%s.huaweicloud.com", acceptance.RandomAccResourceNameWithDash())
streamingResourceName = "huaweicloud_live_domain.streamingDomain"
)

rc := acceptance.InitResourceCheck(
streamingResourceName,
&domainInfo,
getResourceDomainFunc,
)

resource.Test(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckLiveIngestDomainName(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccStreamDomain_basic(streamingDomainName),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(streamingResourceName, "name", streamingDomainName),
resource.TestCheckResourceAttr(streamingResourceName, "type", "pull"),
resource.TestCheckResourceAttr(streamingResourceName, "status", "on"),
resource.TestCheckResourceAttr(streamingResourceName, "ingest_domain_name", ingestDomainName),
resource.TestCheckResourceAttr(streamingResourceName, "is_ipv6", "true"),
resource.TestCheckResourceAttr(streamingResourceName, "service_area", "outside_mainland_china"),
resource.TestCheckResourceAttr(streamingResourceName, "enterprise_project_id", acceptance.HW_ENTERPRISE_PROJECT_ID_TEST),
resource.TestCheckResourceAttr(streamingResourceName, "enterprise_project_id", "0"),
resource.TestCheckResourceAttr(streamingResourceName, "ingest_domain_name", acceptance.HW_LIVE_INGEST_DOMAIN_NAME),
),
},
{
Config: testDomain_basic_update(ingestDomainName, streamingDomainName),
Config: testAccStreamDomain_update_1(streamingDomainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(ingestResourceName, "name", ingestDomainName),
resource.TestCheckResourceAttr(ingestResourceName, "type", "push"),
resource.TestCheckResourceAttr(ingestResourceName, "status", "on"),
resource.TestCheckResourceAttr(ingestResourceName, "service_area", "mainland_china"),
resource.TestCheckResourceAttr(ingestResourceName, "is_ipv6", "false"),
resource.TestCheckResourceAttr(ingestResourceName, "enterprise_project_id", acceptance.HW_ENTERPRISE_PROJECT_ID_TEST),

resource.TestCheckResourceAttr(streamingResourceName, "name", streamingDomainName),
resource.TestCheckResourceAttr(streamingResourceName, "type", "pull"),
resource.TestCheckResourceAttr(streamingResourceName, "status", "off"),
resource.TestCheckResourceAttr(streamingResourceName, "is_ipv6", "false"),
resource.TestCheckResourceAttr(streamingResourceName, "ingest_domain_name", ""),
resource.TestCheckResourceAttr(streamingResourceName, "service_area", "outside_mainland_china"),
resource.TestCheckResourceAttr(streamingResourceName, "enterprise_project_id", acceptance.HW_ENTERPRISE_PROJECT_ID_TEST),
),
},
{
Config: testAccStreamDomain_update_2(streamingDomainName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(streamingResourceName, "status", "off"),
),
},
{
Expand All @@ -90,42 +125,64 @@ func TestAccDomain_basic(t *testing.T) {
})
}

func testDomain_basic(pushDomain, pullDomain string) string {
func testAccIngestDomain_basic(pushDomain string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "ingestDomain" {
name = "%[1]s"
name = "%s"
type = "push"
service_area = "mainland_china"
enterprise_project_id = "0"
is_ipv6 = true
enterprise_project_id = "%[3]s"
}
`, pushDomain)
}

func testAccIngestDomain_update(pushDomain string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "ingestDomain" {
name = "%s"
type = "push"
service_area = "mainland_china"
enterprise_project_id = "0"
is_ipv6 = false
}
`, pushDomain)
}

func testAccStreamDomain_basic(streamDomain string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "streamingDomain" {
name = "%[2]s"
name = "%[1]s"
type = "pull"
ingest_domain_name = huaweicloud_live_domain.ingestDomain.name
ingest_domain_name = "%[2]s"
service_area = "outside_mainland_china"
enterprise_project_id = "%[3]s"
enterprise_project_id = "0"
is_ipv6 = true
}
`, pushDomain, pullDomain, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
`, streamDomain, acceptance.HW_LIVE_INGEST_DOMAIN_NAME)
}

func testDomain_basic_update(pushDomain, pullDomain string) string {
func testAccStreamDomain_update_1(streamDomain string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "ingestDomain" {
name = "%[1]s"
type = "push"
service_area = "mainland_china"
resource "huaweicloud_live_domain" "streamingDomain" {
name = "%s"
type = "pull"
service_area = "outside_mainland_china"
enterprise_project_id = "0"
is_ipv6 = false
enterprise_project_id = "%[3]s"
}
`, streamDomain)
}

func testAccStreamDomain_update_2(streamDomain string) string {
return fmt.Sprintf(`
resource "huaweicloud_live_domain" "streamingDomain" {
name = "%[2]s"
name = "%s"
type = "pull"
status = "off"
service_area = "outside_mainland_china"
enterprise_project_id = "%[3]s"
enterprise_project_id = "0"
is_ipv6 = false
}
`, pushDomain, pullDomain, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
`, streamDomain)
}
Loading

0 comments on commit c89b98b

Please sign in to comment.