Skip to content

Commit

Permalink
chore(live): optimize live resource code and refactor some code (#6121)
Browse files Browse the repository at this point in the history
  • Loading branch information
deer-hang authored Dec 31, 2024
1 parent b3e1c1f commit b52b574
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 170 deletions.
4 changes: 2 additions & 2 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ func Provider() *schema.Provider {
"huaweicloud_lb_pool": lb.ResourcePoolV2(),
"huaweicloud_lb_whitelist": lb.ResourceWhitelistV2(),

"huaweicloud_live_bucket_authorization": live.ResourceLiveBucketAuthorization(),
"huaweicloud_live_bucket_authorization": live.ResourceBucketAuthorization(),
"huaweicloud_live_channel": live.ResourceChannel(),
"huaweicloud_live_disable_push_stream": live.ResourceDisablePushStream(),
"huaweicloud_live_domain": live.ResourceDomain(),
Expand All @@ -1895,7 +1895,7 @@ func Provider() *schema.Provider {
"huaweicloud_live_record_callback": live.ResourceRecordCallback(),
"huaweicloud_live_recording": live.ResourceRecording(),
"huaweicloud_live_referer_validation": live.ResourceRefererValidation(),
"huaweicloud_live_snapshot": live.ResourceLiveSnapshot(),
"huaweicloud_live_snapshot": live.ResourceSnapshot(),
"huaweicloud_live_transcoding": live.ResourceTranscoding(),
"huaweicloud_live_url_authentication": live.ResourceUrlAuthentication(),
"huaweicloud_live_url_validation": live.ResourceUrlValidation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,40 @@ import (
)

func getLiveSnapshotResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := acceptance.HW_REGION_NAME
// getLiveSnapshot: Query Live snapshot
var (
getLiveSnapshotHttpUrl = "v1/{project_id}/stream/snapshot"
getLiveSnapshotProduct = "live"
region = acceptance.HW_REGION_NAME
httpUrl = "v1/{project_id}/stream/snapshot"
product = "live"
)
getLiveSnapshotClient, err := cfg.NewServiceClient(getLiveSnapshotProduct, region)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return nil, fmt.Errorf("error creating Live Client: %s", err)
return nil, fmt.Errorf("error creating Live client: %s", err)
}

parts := strings.SplitN(state.Primary.ID, "/", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid id format, must be <domain_name>/<app_name>")
return nil, fmt.Errorf("invalid ID format, want '<domain_name>/<app_name>', but got '%s'", state.Primary.ID)
}
domainName := parts[0]
appName := parts[1]

getLiveSnapshotPath := getLiveSnapshotClient.Endpoint + getLiveSnapshotHttpUrl
getLiveSnapshotPath = strings.ReplaceAll(getLiveSnapshotPath, "{project_id}", getLiveSnapshotClient.ProjectID)

getLiveSnapshotQueryParams := buildGetLiveSnapshotQueryParams(domainName, appName)
getLiveSnapshotPath += getLiveSnapshotQueryParams

getLiveSnapshotOpt := golangsdk.RequestOpts{
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestPath += buildGetLiveSnapshotQueryParams(domainName, appName)
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
getLiveSnapshotResp, err := getLiveSnapshotClient.Request("GET", getLiveSnapshotPath, &getLiveSnapshotOpt)
resp, err := client.Request("GET", requestPath, &requestOpt)
if err != nil {
return nil, fmt.Errorf("error retrieving Live snapshot: %s", err)
}

getLiveSnapshotRespBody, err := utils.FlattenResponse(getLiveSnapshotResp)
respBody, err := utils.FlattenResponse(resp)
if err != nil {
return nil, err
}

snapshot := utils.PathSearch("snapshot_config_list|[0]", getLiveSnapshotRespBody, nil)
snapshot := utils.PathSearch("snapshot_config_list|[0]", respBody, nil)
if snapshot == nil {
return nil, fmt.Errorf("error get live snapshot")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import (
"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API Live PUT /v1/{project_id}/obs/authority
func ResourceLiveBucketAuthorization() *schema.Resource {
func ResourceBucketAuthorization() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLiveBucketAuthorizationCreate,
ReadContext: resourceLiveBucketAuthorizationRead,
DeleteContext: resourceLiveBucketAuthorizationDelete,
CreateContext: resourceBucketAuthorizationCreate,
ReadContext: resourceBucketAuthorizationRead,
DeleteContext: resourceBucketAuthorizationDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -46,51 +45,43 @@ func ResourceLiveBucketAuthorization() *schema.Resource {
}
}

func resourceLiveBucketAuthorizationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

// createLiveBucketAuthorization: create Live bucket authorization
func resourceBucketAuthorizationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
createLiveBucketAuthorizationHttpUrl = "v1/{project_id}/obs/authority"
createLiveBucketAuthorizationProduct = "live"
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/obs/authority"
product = "live"
)
createLiveBucketAuthorizationClient, err := cfg.NewServiceClient(createLiveBucketAuthorizationProduct, region)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating Live Client: %s", err)
return diag.Errorf("error creating Live client: %s", err)
}

createLiveBucketAuthorizationPath := createLiveBucketAuthorizationClient.Endpoint + createLiveBucketAuthorizationHttpUrl
createLiveBucketAuthorizationPath = strings.ReplaceAll(createLiveBucketAuthorizationPath, "{project_id}",
createLiveBucketAuthorizationClient.ProjectID)

createLiveBucketAuthorizationOpt := golangsdk.RequestOpts{
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
JSONBody: buildBucketAuthorizationBodyParams(d, 1),
}
createLiveBucketAuthorizationOpt.JSONBody = utils.RemoveNil(buildLiveBucketAuthorizationBodyParams(d, 1))
_, err = createLiveBucketAuthorizationClient.Request("PUT", createLiveBucketAuthorizationPath,
&createLiveBucketAuthorizationOpt)
_, err = client.Request("PUT", requestPath, &requestOpt)
if err != nil {
return diag.Errorf("error creating Live bucket Authorization: %s", err)
return diag.Errorf("error creating Live bucket authorization: %s", err)
}

d.SetId(d.Get("bucket").(string))

return resourceLiveBucketAuthorizationRead(ctx, d, meta)
return resourceBucketAuthorizationRead(ctx, d, meta)
}

func buildLiveBucketAuthorizationBodyParams(d *schema.ResourceData, operation int) map[string]interface{} {
func buildBucketAuthorizationBodyParams(d *schema.ResourceData, operation int) map[string]interface{} {
bodyParams := map[string]interface{}{
"bucket": utils.ValueIgnoreEmpty(d.Get("bucket")),
"bucket": d.Get("bucket"),
"operation": operation,
}
return bodyParams
}

func resourceLiveBucketAuthorizationRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func resourceBucketAuthorizationRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

Expand All @@ -103,35 +94,28 @@ func resourceLiveBucketAuthorizationRead(_ context.Context, d *schema.ResourceDa
return diag.FromErr(mErr.ErrorOrNil())
}

func resourceLiveBucketAuthorizationDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

// deleteLiveBucketAuthorization: Delete Live bucket Authorization
func resourceBucketAuthorizationDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
deleteLiveBucketAuthorizationHttpUrl = "v1/{project_id}/obs/authority"
deleteLiveBucketAuthorizationProduct = "live"
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
httpUrl = "v1/{project_id}/obs/authority"
product = "live"
)
deleteLiveBucketAuthorizationClient, err := cfg.NewServiceClient(deleteLiveBucketAuthorizationProduct, region)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating Live Client: %s", err)
return diag.Errorf("error creating Live client: %s", err)
}

deleteLiveBucketAuthorizationPath := deleteLiveBucketAuthorizationClient.Endpoint + deleteLiveBucketAuthorizationHttpUrl
deleteLiveBucketAuthorizationPath = strings.ReplaceAll(deleteLiveBucketAuthorizationPath, "{project_id}",
deleteLiveBucketAuthorizationClient.ProjectID)

deleteLiveBucketAuthorizationOpt := golangsdk.RequestOpts{
requestPath := client.Endpoint + httpUrl
requestPath = strings.ReplaceAll(requestPath, "{project_id}", client.ProjectID)
requestOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
JSONBody: buildBucketAuthorizationBodyParams(d, 0),
}
deleteLiveBucketAuthorizationOpt.JSONBody = utils.RemoveNil(buildLiveBucketAuthorizationBodyParams(d, 0))
_, err = deleteLiveBucketAuthorizationClient.Request("PUT", deleteLiveBucketAuthorizationPath,
&deleteLiveBucketAuthorizationOpt)
_, err = client.Request("PUT", requestPath,
&requestOpt)
if err != nil {
return diag.Errorf("error deleting Live bucket Authorization: %s", err)
return diag.Errorf("error deleting Live bucket authorization: %s", err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// ---------------------------------------------------------------
// *** AUTO GENERATED CODE ***
// @Product Live
// ---------------------------------------------------------------

package live

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
// @API Live POST /v1/{project_id}/domain
// @API Live DELETE /v1/{project_id}/domains_mapping
// @API Live PUT /v1/{project_id}/domains_mapping
// @API Live PUT /v1/{project_id}/domain/ipv6-switch
func ResourceDomain() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDomainCreate,
Expand Down
Loading

0 comments on commit b52b574

Please sign in to comment.