Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat(cph): support cph server switch #5857

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
123 changes: 114 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,26 @@ func resourceCphServerUpdate(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}
}

updateServerSwitchChanges := []string{
"phone_flavor",
"keypair_name",
"ports",
"tenant_vpc_id",
"nics",
"public_ip",
"band_width",
"image_id",
"extend_param",
"phone_data_volume",
"server_share_data_volume",
}
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 +940,96 @@ 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": buildUpdateServerRequestBodyBandWidth(d.Get("bandwidth")),
"image_id": utils.ValueIgnoreEmpty(d.Get("image_id")),
"extend_param": map[string]interface{}{
"enterprise_project_id": utils.ValueIgnoreEmpty(cfg.GetEnterpriseProjectID(d)),
},
"phone_data_volume": buildCreateCphServerRequestBodyPhoneDataVolume(d.Get("phone_data_volume")),
"server_share_data_volume": buildCreateCphServerRequestBodyShareDataVolume(d.Get("server_share_data_volume")),
"image_type": "gold",
}

return bodyParams
}

func buildUpdateServerRequestBodyBandWidth(rawParams interface{}) map[string]interface{} {
if rawArray, ok := rawParams.([]interface{}); ok {
if len(rawArray) == 0 {
return nil
}
raw := rawArray[0].(map[string]interface{})

shareType, _ := strconv.Atoi(utils.ValueIgnoreEmpty(raw["share_type"]).(string))
chargeModeInteger, _ := strconv.Atoi(utils.ValueIgnoreEmpty(raw["charge_mode"]).(string))

params := map[string]interface{}{
"band_width_charge_mode": chargeModeInteger,
"band_width_share_type": shareType,
}

if chargeModeInteger == 0 {
params["band_width_id"] = utils.ValueIgnoreEmpty(raw["id"])
}

if chargeModeInteger == 1 {
params["band_width_size"] = utils.ValueIgnoreEmpty(raw["size"])
}

return params
}

return nil
}

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
}
Loading