@@ -29,6 +29,7 @@ import (
29
29
// @API CPH GET /v1/{project_id}/cloud-phone/servers/{server_id}
30
30
// @API CPH PUT /v1/{project_id}/cloud-phone/servers/open-access
31
31
// @API CPH POST /v2/{project_id}/cloud-phone/servers
32
+ // @API CPH POST /v2/{project_id}/cloud-phone/servers/{server_id}/change
32
33
// @API BSS POST /v2/orders/subscriptions/resources/unsubscribe
33
34
func ResourceCphServer () * schema.Resource {
34
35
return & schema.Resource {
@@ -69,38 +70,32 @@ func ResourceCphServer() *schema.Resource {
69
70
"phone_flavor" : {
70
71
Type : schema .TypeString ,
71
72
Required : true ,
72
- ForceNew : true ,
73
73
Description : `The cloud phone flavor.` ,
74
74
},
75
75
"image_id" : {
76
76
Type : schema .TypeString ,
77
77
Required : true ,
78
- ForceNew : true ,
79
78
Description : `The cloud phone image ID.` ,
80
79
},
81
80
"vpc_id" : {
82
81
Type : schema .TypeString ,
83
82
Required : true ,
84
- ForceNew : true ,
85
83
Description : `The ID of VPC which the cloud server belongs to` ,
86
84
},
87
85
"subnet_id" : {
88
86
Type : schema .TypeString ,
89
87
Required : true ,
90
- ForceNew : true ,
91
88
Description : `The ID of subnet which the cloud server belongs to` ,
92
89
},
93
90
"eip_id" : {
94
91
Type : schema .TypeString ,
95
92
Optional : true ,
96
- ForceNew : true ,
97
93
ConflictsWith : []string {"eip_type" , "bandwidth" },
98
94
Description : `The ID of an **existing** EIP assigned to the server.` ,
99
95
},
100
96
"eip_type" : {
101
97
Type : schema .TypeString ,
102
98
Optional : true ,
103
- ForceNew : true ,
104
99
ConflictsWith : []string {"eip_id" },
105
100
RequiredWith : []string {"bandwidth" },
106
101
Description : `The type of an EIP that will be automatically assigned to the cloud server.` ,
@@ -112,7 +107,6 @@ func ResourceCphServer() *schema.Resource {
112
107
RequiredWith : []string {"eip_type" },
113
108
Elem : cphServerBandWidthSchema (),
114
109
Optional : true ,
115
- ForceNew : true ,
116
110
Description : `The bandwidth used by the cloud phone.` ,
117
111
},
118
112
"period_unit" : {
@@ -153,15 +147,13 @@ func ResourceCphServer() *schema.Resource {
153
147
Type : schema .TypeString ,
154
148
Optional : true ,
155
149
Computed : true ,
156
- ForceNew : true ,
157
150
Description : `the enterprise project ID.` ,
158
151
},
159
152
"ports" : {
160
153
Type : schema .TypeList ,
161
154
Elem : cphServerApplicationPortSchema (),
162
155
Optional : true ,
163
156
Computed : true ,
164
- ForceNew : true ,
165
157
Description : `The application port enabled by the cloud phone.` ,
166
158
},
167
159
"order_id" : {
@@ -619,6 +611,25 @@ func resourceCphServerUpdate(ctx context.Context, d *schema.ResourceData, meta i
619
611
return diag .FromErr (err )
620
612
}
621
613
}
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
+ }
622
633
return resourceCphServerRead (ctx , d , meta )
623
634
}
624
635
@@ -775,3 +786,64 @@ func checkCphJobStatus(ctx context.Context, client *golangsdk.ServiceClient, id
775
786
}
776
787
return nil
777
788
}
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