Skip to content

Commit 6109766

Browse files
committed
feat(cph): support cph server switch
1 parent 3035d16 commit 6109766

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

docs/resources/cph_server.md

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,20 @@ The following arguments are supported:
5959

6060
Changing this parameter will create a new resource.
6161

62-
* `phone_flavor` - (Required, String, ForceNew) The cloud phone flavor.
63-
64-
Changing this parameter will create a new resource.
62+
* `phone_flavor` - (Required, String) The cloud phone flavor.
6563

66-
* `image_id` - (Required, String, ForceNew) The cloud phone image ID.
67-
68-
Changing this parameter will create a new resource.
64+
* `image_id` - (Required, String) The cloud phone image ID.
6965

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

72-
Changing this parameter will create a new resource.
73-
74-
* `subnet_id` - (Required, String, ForceNew) The ID of subnet which the cloud server belongs to.
75-
76-
Changing this parameter will create a new resource.
68+
* `subnet_id` - (Required, String) The ID of subnet which the cloud server belongs to.
7769

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

8072
Changing this parameter will create a new resource.
8173

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

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

9586
* `bandwidth` - (Optional, List, ForceNew) The bandwidth of an EIP that will be automatically assigned to
9687
the cloud server.
97-
98-
Changing this parameter will create a new resource.
9988
The [BandWidth](#cphServer_BandWidth) structure is documented below.
10089

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

120-
* `enterprise_project_id` - (Optional, String, ForceNew) The enterprise project ID.
109+
* `enterprise_project_id` - (Optional, String) The enterprise project ID.
121110

122-
Changing this parameter will create a new resource.
123-
124-
* `ports` - (Optional, List, ForceNew) The application port enabled by the cloud phone.
125-
126-
Changing this parameter will create a new resource.
111+
* `ports` - (Optional, List) The application port enabled by the cloud phone.
127112
The [ApplicationPort](#cphServer_ApplicationPort) structure is documented below.
128113

129114
<a name="cphServer_BandWidth"></a>

huaweicloud/services/cph/resource_huaweicloud_cph_server.go

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
// @API CPH GET /v1/{project_id}/cloud-phone/servers/{server_id}
3030
// @API CPH PUT /v1/{project_id}/cloud-phone/servers/open-access
3131
// @API CPH POST /v2/{project_id}/cloud-phone/servers
32+
// @API CPH POST /v2/{project_id}/cloud-phone/servers/{server_id}/change
3233
// @API BSS POST /v2/orders/subscriptions/resources/unsubscribe
3334
func ResourceCphServer() *schema.Resource {
3435
return &schema.Resource{
@@ -69,38 +70,32 @@ func ResourceCphServer() *schema.Resource {
6970
"phone_flavor": {
7071
Type: schema.TypeString,
7172
Required: true,
72-
ForceNew: true,
7373
Description: `The cloud phone flavor.`,
7474
},
7575
"image_id": {
7676
Type: schema.TypeString,
7777
Required: true,
78-
ForceNew: true,
7978
Description: `The cloud phone image ID.`,
8079
},
8180
"vpc_id": {
8281
Type: schema.TypeString,
8382
Required: true,
84-
ForceNew: true,
8583
Description: `The ID of VPC which the cloud server belongs to`,
8684
},
8785
"subnet_id": {
8886
Type: schema.TypeString,
8987
Required: true,
90-
ForceNew: true,
9188
Description: `The ID of subnet which the cloud server belongs to`,
9289
},
9390
"eip_id": {
9491
Type: schema.TypeString,
9592
Optional: true,
96-
ForceNew: true,
9793
ConflictsWith: []string{"eip_type", "bandwidth"},
9894
Description: `The ID of an **existing** EIP assigned to the server.`,
9995
},
10096
"eip_type": {
10197
Type: schema.TypeString,
10298
Optional: true,
103-
ForceNew: true,
10499
ConflictsWith: []string{"eip_id"},
105100
RequiredWith: []string{"bandwidth"},
106101
Description: `The type of an EIP that will be automatically assigned to the cloud server.`,
@@ -112,7 +107,6 @@ func ResourceCphServer() *schema.Resource {
112107
RequiredWith: []string{"eip_type"},
113108
Elem: cphServerBandWidthSchema(),
114109
Optional: true,
115-
ForceNew: true,
116110
Description: `The bandwidth used by the cloud phone.`,
117111
},
118112
"period_unit": {
@@ -153,15 +147,13 @@ func ResourceCphServer() *schema.Resource {
153147
Type: schema.TypeString,
154148
Optional: true,
155149
Computed: true,
156-
ForceNew: true,
157150
Description: `the enterprise project ID.`,
158151
},
159152
"ports": {
160153
Type: schema.TypeList,
161154
Elem: cphServerApplicationPortSchema(),
162155
Optional: true,
163156
Computed: true,
164-
ForceNew: true,
165157
Description: `The application port enabled by the cloud phone.`,
166158
},
167159
"order_id": {
@@ -619,6 +611,25 @@ func resourceCphServerUpdate(ctx context.Context, d *schema.ResourceData, meta i
619611
return diag.FromErr(err)
620612
}
621613
}
614+
615+
updateServerSwitchChanges := []string{
616+
"phone_flavor",
617+
"phone_model_name",
618+
"keypair_name",
619+
"ports",
620+
"tenant_vpc_id",
621+
"nics",
622+
"public_ip",
623+
"band_width",
624+
"image_id",
625+
"extend_param",
626+
}
627+
if d.HasChanges(updateServerSwitchChanges...) {
628+
err := updateServerChange(ctx, client, d, cfg)
629+
if err != nil {
630+
return diag.FromErr(err)
631+
}
632+
}
622633
return resourceCphServerRead(ctx, d, meta)
623634
}
624635

@@ -775,3 +786,64 @@ func checkCphJobStatus(ctx context.Context, client *golangsdk.ServiceClient, id
775786
}
776787
return nil
777788
}
789+
790+
func updateServerChange(ctx context.Context, client *golangsdk.ServiceClient, d *schema.ResourceData, cfg *config.Config) error {
791+
// updateCphServerName: update CPH server change
792+
updateServerChangeHttpUrl := "v2/{project_id}/cloud-phone/servers/{server_id}/change"
793+
updateServerChangePath := client.Endpoint + updateServerChangeHttpUrl
794+
updateServerChangePath = strings.ReplaceAll(updateServerChangePath, "{project_id}", client.ProjectID)
795+
updateServerChangePath = strings.ReplaceAll(updateServerChangePath, "{server_id}", d.Id())
796+
797+
updateCphServerChangeOpt := golangsdk.RequestOpts{
798+
KeepResponseBody: true,
799+
}
800+
updateCphServerChangeOpt.JSONBody = utils.RemoveNil(buildUpdateCphServerChangeBodyParams(d, cfg))
801+
_, err := client.Request("POST", updateServerChangePath, &updateCphServerChangeOpt)
802+
if err != nil {
803+
return fmt.Errorf("error updating CPH server change: %s", err)
804+
}
805+
806+
err = checkServerSwitchStatus(ctx, client, d.Id(), d.Timeout(schema.TimeoutCreate))
807+
if err != nil {
808+
return err
809+
}
810+
return nil
811+
}
812+
813+
func buildUpdateCphServerChangeBodyParams(d *schema.ResourceData, cfg *config.Config) map[string]interface{} {
814+
bodyParams := map[string]interface{}{
815+
"phone_model_name": utils.ValueIgnoreEmpty(d.Get("phone_flavor")),
816+
"keypair_name": utils.ValueIgnoreEmpty(d.Get("keypair_name")),
817+
"ports": buildCreateCphServerRequestBodyApplicationPort(d.Get("ports")),
818+
"tenant_vpc_id": utils.ValueIgnoreEmpty(d.Get("vpc_id")),
819+
"nics": []map[string]interface{}{
820+
{
821+
"subnet_id": utils.ValueIgnoreEmpty(d.Get("subnet_id")),
822+
},
823+
},
824+
"public_ip": buildCreateCphServerRequestBodyPublicIp(d),
825+
"band_width": buildCreateCphServerRequestBodyBandWidth(d.Get("bandwidth")),
826+
"image_id": utils.ValueIgnoreEmpty(d.Get("image_id")),
827+
"extend_param": map[string]interface{}{
828+
"enterprise_project_id": utils.ValueIgnoreEmpty(cfg.GetEnterpriseProjectID(d)),
829+
},
830+
}
831+
832+
return bodyParams
833+
}
834+
835+
func checkServerSwitchStatus(ctx context.Context, client *golangsdk.ServiceClient, id string, timeout time.Duration) error {
836+
stateConf := &resource.StateChangeConf{
837+
Pending: []string{"PENDING"},
838+
Target: []string{"COMPLETED"},
839+
Refresh: serverStateRefreshFunc(client, id),
840+
Timeout: timeout,
841+
PollInterval: 10 * timeout,
842+
Delay: 10 * time.Second,
843+
}
844+
_, err := stateConf.WaitForStateContext(ctx)
845+
if err != nil {
846+
return fmt.Errorf("error waiting for CPH server switch to be completed: %s", err)
847+
}
848+
return nil
849+
}

0 commit comments

Comments
 (0)