@@ -42,7 +42,9 @@ func resourceUCloudInstance() *schema.Resource {
42
42
diffValidateChargeTypeWithDuration ,
43
43
diffValidateIsolationGroup ,
44
44
diffValidateDataDisks ,
45
+ diffValidateNetworkInterface ,
45
46
diffValidateCPUPlatform ,
47
+ diffValidateBootDiskTypeWithDataDisks ,
46
48
),
47
49
48
50
Schema : map [string ]* schema.Schema {
@@ -178,6 +180,49 @@ func resourceUCloudInstance() *schema.Resource {
178
180
},
179
181
},
180
182
183
+ "network_interface" : {
184
+ Type : schema .TypeList ,
185
+ Optional : true ,
186
+ MinItems : 1 ,
187
+ MaxItems : 1 ,
188
+ Elem : & schema.Resource {
189
+ Schema : map [string ]* schema.Schema {
190
+ "eip_bandwidth" : {
191
+ Type : schema .TypeInt ,
192
+ Required : true ,
193
+ ForceNew : true ,
194
+ ValidateFunc : validation .IntBetween (1 , 800 ),
195
+ },
196
+
197
+ "eip_internet_type" : {
198
+ Type : schema .TypeString ,
199
+ Required : true ,
200
+ ForceNew : true ,
201
+ ValidateFunc : validation .StringInSlice ([]string {
202
+ "bgp" ,
203
+ "international" ,
204
+ }, false ),
205
+ },
206
+
207
+ "eip_charge_mode" : {
208
+ Type : schema .TypeString ,
209
+ Required : true ,
210
+ ForceNew : true ,
211
+ ValidateFunc : validation .StringInSlice ([]string {
212
+ "traffic" ,
213
+ "bandwidth" ,
214
+ }, false ),
215
+ },
216
+ },
217
+ },
218
+ },
219
+
220
+ "delete_eips_with_instance" : {
221
+ Type : schema .TypeBool ,
222
+ Optional : true ,
223
+ ForceNew : true ,
224
+ },
225
+
181
226
"delete_disks_with_instance" : {
182
227
Type : schema .TypeBool ,
183
228
Optional : true ,
@@ -439,15 +484,34 @@ func resourceUCloudInstanceCreate(d *schema.ResourceData, meta interface{}) erro
439
484
440
485
if v , ok := d .GetOk ("data_disks" ); ok {
441
486
for _ , item := range v .([]interface {}) {
487
+ disk , ok := item .(map [string ]interface {})
488
+ if ! ok {
489
+ return fmt .Errorf ("error on parse data_disks when creating instance" )
490
+ }
442
491
dataDisk := uhost.UHostDisk {}
443
492
dataDisk .IsBoot = ucloud .String ("false" )
444
- disk := item .(map [string ]interface {})
445
493
dataDisk .Size = ucloud .Int (disk ["size" ].(int ))
446
494
dataDisk .Type = ucloud .String (upperCvt .unconvert (disk ["type" ].(string )))
447
495
req .Disks = append (req .Disks , dataDisk )
448
496
}
449
497
}
450
498
499
+ if v , ok := d .GetOk ("network_interface" ); ok {
500
+ for _ , item := range v .([]interface {}) {
501
+ netIp , ok := item .(map [string ]interface {})
502
+ if ! ok {
503
+ return fmt .Errorf ("error on parse network_interface when creating instance" )
504
+ }
505
+ eip := uhost.CreateUHostInstanceParamNetworkInterface {}
506
+ eip .EIP = & uhost.CreateUHostInstanceParamNetworkInterfaceEIP {
507
+ Bandwidth : ucloud .Int (netIp ["eip_bandwidth" ].(int )),
508
+ OperatorName : ucloud .String (upperCamelCvt .unconvert (netIp ["eip_internet_type" ].(string ))),
509
+ PayMode : ucloud .String (upperCamelCvt .unconvert (netIp ["eip_charge_mode" ].(string ))),
510
+ }
511
+ req .NetworkInterface = append (req .NetworkInterface , eip )
512
+ }
513
+ }
514
+
451
515
// if tag is empty string, use default tag
452
516
if v , ok := d .GetOk ("tag" ); ok {
453
517
req .Tag = ucloud .String (v .(string ))
@@ -475,7 +539,7 @@ func resourceUCloudInstanceCreate(d *schema.ResourceData, meta interface{}) erro
475
539
if isStringIn ("CloudInit" , imageResp .Features ) {
476
540
req .UserData = ucloud .String (base64 .StdEncoding .EncodeToString ([]byte (v .(string ))))
477
541
} else {
478
- return fmt .Errorf ("error on creating instance, the image %s must have %q feature, got %#v" , "CloudInit" , imageId , imageResp .Features )
542
+ return fmt .Errorf ("error on creating instance, the image %s must have %q feature, got %#v" , imageId , "CloudInit" , imageResp .Features )
479
543
}
480
544
}
481
545
req .MachineType = ucloud .String (strings .ToUpper (t .HostType ))
@@ -864,6 +928,9 @@ func resourceUCloudInstanceRead(d *schema.ResourceData, meta interface{}) error
864
928
memory := instance .Memory
865
929
cpu := instance .CPU
866
930
931
+ if cpu != 0 {
932
+ d .Set ("instance_type" , instanceTypeSetFunc (upperCvt .convert (instance .MachineType ), cpu , memory / 1024 ))
933
+ }
867
934
d .Set ("root_password" , d .Get ("root_password" ))
868
935
d .Set ("isolation_group" , instance .IsolationGroup )
869
936
d .Set ("name" , instance .Name )
@@ -875,7 +942,6 @@ func resourceUCloudInstanceRead(d *schema.ResourceData, meta interface{}) error
875
942
d .Set ("expire_time" , timestampToString (instance .ExpireTime ))
876
943
d .Set ("auto_renew" , boolCamelCvt .unconvert (instance .AutoRenew ))
877
944
d .Set ("remark" , instance .Remark )
878
- d .Set ("instance_type" , instanceTypeSetFunc (upperCvt .convert (instance .MachineType ), cpu , memory / 1024 ))
879
945
d .Set ("cpu_platform" , instance .CpuPlatform )
880
946
881
947
//in order to be compatible with returns null
@@ -958,6 +1024,9 @@ func resourceUCloudInstanceDelete(d *schema.ResourceData, meta interface{}) erro
958
1024
if v , ok := d .GetOkExists ("delete_disks_with_instance" ); ok {
959
1025
deleReq .ReleaseUDisk = ucloud .Bool (v .(bool ))
960
1026
}
1027
+ if v , ok := d .GetOkExists ("delete_eips_with_instance" ); ok {
1028
+ deleReq .ReleaseEIP = ucloud .Bool (v .(bool ))
1029
+ }
961
1030
962
1031
return resource .Retry (15 * time .Minute , func () * resource.RetryError {
963
1032
instance , err := client .describeInstanceById (d .Id ())
@@ -1133,6 +1202,24 @@ func diffValidateBootDiskTypeWithDataDiskType(diff *schema.ResourceDiff, meta in
1133
1202
return nil
1134
1203
}
1135
1204
1205
+ func diffValidateBootDiskTypeWithDataDisks (diff * schema.ResourceDiff , meta interface {}) error {
1206
+ var bootDiskType string
1207
+
1208
+ if v , ok := diff .GetOk ("boot_disk_type" ); ok {
1209
+ bootDiskType = v .(string )
1210
+ } else {
1211
+ bootDiskType = "local_normal"
1212
+ }
1213
+
1214
+ if _ , ok := diff .GetOk ("data_disks" ); ok {
1215
+ if isStringIn (bootDiskType , []string {"local_normal" , "local_ssd" }) {
1216
+ return fmt .Errorf ("the %q cannot be local data disk, when set %q, got %q" , "boot_disk_type" , "data_disks" , bootDiskType )
1217
+ }
1218
+ }
1219
+
1220
+ return nil
1221
+ }
1222
+
1136
1223
func diffValidateChargeTypeWithDuration (diff * schema.ResourceDiff , meta interface {}) error {
1137
1224
if v , ok := diff .GetOkExists ("duration" ); ok && v .(int ) == 0 {
1138
1225
chargeType := diff .Get ("charge_type" ).(string )
@@ -1175,8 +1262,21 @@ func diffValidateDataDisks(diff *schema.ResourceDiff, meta interface{}) error {
1175
1262
return nil
1176
1263
}
1177
1264
1265
+ func diffValidateNetworkInterface (diff * schema.ResourceDiff , meta interface {}) error {
1266
+ if _ , ok := diff .GetOk ("network_interface" ); ok {
1267
+ if _ , ok1 := diff .GetOkExists ("delete_eips_with_instance" ); ! ok1 {
1268
+ return fmt .Errorf ("the argument %q is required when set %q" , "delete_eips_with_instance" , "network_interface" )
1269
+ }
1270
+ }
1271
+
1272
+ return nil
1273
+ }
1274
+
1178
1275
func diffValidateCPUPlatform (diff * schema.ResourceDiff , meta interface {}) error {
1179
- t , _ := parseInstanceType (diff .Get ("instance_type" ).(string ))
1276
+ t , err := parseInstanceType (diff .Get ("instance_type" ).(string ))
1277
+ if err != nil {
1278
+ return err
1279
+ }
1180
1280
if v , ok := diff .GetOk ("min_cpu_platform" ); ok {
1181
1281
supportedCPUForOS := []string {"Intel/Auto" , "Intel/CascadelakeR" }
1182
1282
if t .HostType == "os" && ! isStringIn (v .(string ), supportedCPUForOS ) {
0 commit comments