Skip to content

Commit

Permalink
feat(cph): support cph server switch
Browse files Browse the repository at this point in the history
  • Loading branch information
luoping-12345 committed Nov 29, 2024
1 parent 3fd5148 commit 7bb2f1b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 30 deletions.
27 changes: 6 additions & 21 deletions docs/resources/cph_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,20 @@ The following arguments are supported:

Changing this parameter will create a new resource.

* `phone_flavor` - (Required, String, ForceNew) The cloud phone flavor.

Changing this parameter will create a new resource.
* `phone_flavor` - (Required, String) The cloud phone flavor.

* `image_id` - (Required, String, ForceNew) The cloud phone image ID.

Changing this parameter will create a new resource.
* `image_id` - (Required, String) The cloud phone image ID.

* `vpc_id` - (Required, String, ForceNew) The ID of VPC which the cloud server belongs to.
* `vpc_id` - (Required, String) The ID of VPC which the cloud server belongs to.

Changing this parameter will create a new resource.

* `subnet_id` - (Required, String, ForceNew) The ID of subnet which the cloud server belongs to.

Changing this parameter will create a new resource.
* `subnet_id` - (Required, String) The ID of subnet which the cloud server belongs to.

* `availability_zone` - (Optional, String, ForceNew) The name of the AZ where the cloud server is located.

Changing this parameter will create a new resource.

* `eip_id` - (Optional, String, ForceNew) The ID of an **existing** EIP assigned to the cloud server.
This parameter and `eip_type`, `bandwidth` are alternative.
Changing this parameter will create a new resource.

* `eip_type` - (Optional, String, ForceNew) The type of an EIP that will be automatically assigned to the cloud server.
The options are as follows:
Expand All @@ -94,8 +85,6 @@ The following arguments are supported:

* `bandwidth` - (Optional, List, ForceNew) The bandwidth of an EIP that will be automatically assigned to
the cloud server.

Changing this parameter will create a new resource.
The [BandWidth](#cphServer_BandWidth) structure is documented below.

* `period_unit` - (Required, String, ForceNew) The charging period unit.
Expand All @@ -117,13 +106,9 @@ The following arguments are supported:
* `keypair_name` - (Optional, String) Specifies the key pair name, which is used for logging in to
the cloud phone through ADB.

* `enterprise_project_id` - (Optional, String, ForceNew) The enterprise project ID.
* `enterprise_project_id` - (Optional, String) The enterprise project ID.

Changing this parameter will create a new resource.

* `ports` - (Optional, List, ForceNew) The application port enabled by the cloud phone.

Changing this parameter will create a new resource.
* `ports` - (Optional, List) The application port enabled by the cloud phone.
The [ApplicationPort](#cphServer_ApplicationPort) structure is documented below.

* `phone_data_volume` - (Optional, List) The phone data volume.
Expand Down
90 changes: 81 additions & 9 deletions huaweicloud/services/cph/resource_huaweicloud_cph_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// @API CPH GET /v1/{project_id}/cloud-phone/servers/{server_id}
// @API CPH PUT /v1/{project_id}/cloud-phone/servers/open-access
// @API CPH POST /v2/{project_id}/cloud-phone/servers
// @API CPH POST /v2/{project_id}/cloud-phone/servers/{server_id}/change
// @API BSS POST /v2/orders/subscriptions/resources/unsubscribe
func ResourceCphServer() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -69,38 +70,32 @@ func ResourceCphServer() *schema.Resource {
"phone_flavor": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The cloud phone flavor.`,
},
"image_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The cloud phone image ID.`,
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of VPC which the cloud server belongs to`,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The ID of subnet which the cloud server belongs to`,
},
"eip_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"eip_type", "bandwidth"},
Description: `The ID of an **existing** EIP assigned to the server.`,
},
"eip_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"eip_id"},
RequiredWith: []string{"bandwidth"},
Description: `The type of an EIP that will be automatically assigned to the cloud server.`,
Expand All @@ -112,7 +107,6 @@ func ResourceCphServer() *schema.Resource {
RequiredWith: []string{"eip_type"},
Elem: cphServerBandWidthSchema(),
Optional: true,
ForceNew: true,
Description: `The bandwidth used by the cloud phone.`,
},
"period_unit": {
Expand Down Expand Up @@ -153,15 +147,13 @@ func ResourceCphServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `the enterprise project ID.`,
},
"ports": {
Type: schema.TypeList,
Elem: cphServerApplicationPortSchema(),
Optional: true,
Computed: true,
ForceNew: true,
Description: `The application port enabled by the cloud phone.`,
},
"phone_data_volume": {
Expand Down Expand Up @@ -772,6 +764,25 @@ func resourceCphServerUpdate(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}
}

updateServerSwitchChanges := []string{
"phone_flavor",
"phone_model_name",
"keypair_name",
"ports",
"tenant_vpc_id",
"nics",
"public_ip",
"band_width",
"image_id",
"extend_param",
}
if d.HasChanges(updateServerSwitchChanges...) {
err := updateServerChange(ctx, client, d, cfg)
if err != nil {
return diag.FromErr(err)
}
}
return resourceCphServerRead(ctx, d, meta)
}

Expand Down Expand Up @@ -928,3 +939,64 @@ func checkCphJobStatus(ctx context.Context, client *golangsdk.ServiceClient, id
}
return nil
}

func updateServerChange(ctx context.Context, client *golangsdk.ServiceClient, d *schema.ResourceData, cfg *config.Config) error {
// updateCphServerName: update CPH server change
updateServerChangeHttpUrl := "v2/{project_id}/cloud-phone/servers/{server_id}/change"
updateServerChangePath := client.Endpoint + updateServerChangeHttpUrl
updateServerChangePath = strings.ReplaceAll(updateServerChangePath, "{project_id}", client.ProjectID)
updateServerChangePath = strings.ReplaceAll(updateServerChangePath, "{server_id}", d.Id())

updateCphServerChangeOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
}
updateCphServerChangeOpt.JSONBody = utils.RemoveNil(buildUpdateCphServerChangeBodyParams(d, cfg))
_, err := client.Request("POST", updateServerChangePath, &updateCphServerChangeOpt)
if err != nil {
return fmt.Errorf("error updating CPH server change: %s", err)
}

err = checkServerSwitchStatus(ctx, client, d.Id(), d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
return nil
}

func buildUpdateCphServerChangeBodyParams(d *schema.ResourceData, cfg *config.Config) map[string]interface{} {
bodyParams := map[string]interface{}{
"phone_model_name": utils.ValueIgnoreEmpty(d.Get("phone_flavor")),
"keypair_name": utils.ValueIgnoreEmpty(d.Get("keypair_name")),
"ports": buildCreateCphServerRequestBodyApplicationPort(d.Get("ports")),
"tenant_vpc_id": utils.ValueIgnoreEmpty(d.Get("vpc_id")),
"nics": []map[string]interface{}{
{
"subnet_id": utils.ValueIgnoreEmpty(d.Get("subnet_id")),
},
},
"public_ip": buildCreateCphServerRequestBodyPublicIp(d),
"band_width": buildCreateCphServerRequestBodyBandWidth(d.Get("bandwidth")),
"image_id": utils.ValueIgnoreEmpty(d.Get("image_id")),
"extend_param": map[string]interface{}{
"enterprise_project_id": utils.ValueIgnoreEmpty(cfg.GetEnterpriseProjectID(d)),
},
}

return bodyParams
}

func checkServerSwitchStatus(ctx context.Context, client *golangsdk.ServiceClient, id string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING"},
Target: []string{"COMPLETED"},
Refresh: serverStateRefreshFunc(client, id),
Timeout: timeout,
PollInterval: 10 * timeout,
Delay: 10 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
return fmt.Errorf("error waiting for CPH server switch to be completed: %s", err)
}
return nil
}

0 comments on commit 7bb2f1b

Please sign in to comment.