Skip to content

Commit 2d6678e

Browse files
authored
add argument to instance about eip (#85)
1 parent 6df8630 commit 2d6678e

File tree

82 files changed

+4823
-3273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+4823
-3273
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
## 1.24.0 (Unreleased)
2+
3+
ENHANCEMENTS:
4+
5+
* resource/ucloud_instance: add `network_interface` to the argument in order to create and associate `eip` with the instance.[GH-85]
6+
27
## 1.23.0 (2020-09-07)
38

49
ENHANCEMENTS:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ go 1.14
44

55
require (
66
github.com/hashicorp/terraform-plugin-sdk v1.4.0
7-
github.com/ucloud/ucloud-sdk-go v0.16.2
7+
github.com/ucloud/ucloud-sdk-go v0.17.5
88
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
99
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
184184
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
185185
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
186186
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
187-
github.com/ucloud/ucloud-sdk-go v0.16.2 h1:gNzMa1UYERoQe1FX4sdP7jgkqkouUaXuGTJZxzZVsjM=
188-
github.com/ucloud/ucloud-sdk-go v0.16.2/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
187+
github.com/ucloud/ucloud-sdk-go v0.17.5 h1:8GOmDmg/FnWoZRBV2WNVwF/DyJO6l1dQGghLUZ4dnGQ=
188+
github.com/ucloud/ucloud-sdk-go v0.17.5/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
189189
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
190190
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
191191
github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvcciqcxEHac4CYj89xI=

ucloud/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (c *Config) Client() (*UCloudClient, error) {
6262
// enable auto retry with http/connection error
6363
cfg.MaxRetries = c.MaxRetries
6464
cfg.LogLevel = log.PanicLevel
65-
cfg.UserAgent = "Terraform-UCloud/1.23.0"
65+
cfg.UserAgent = "Terraform-UCloud/1.24.0"
6666
cfg.BaseUrl = c.BaseURL
6767

6868
cred := auth.NewCredential()

ucloud/data_source_ucloud_instances.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,12 @@ func dataSourceUCloudInstancesSave(d *schema.ResourceData, instances []uhost.UHo
287287
}
288288
memory := instance.Memory
289289
cpu := instance.CPU
290-
data = append(data, map[string]interface{}{
290+
dataMap := map[string]interface{}{
291291
"availability_zone": instance.Zone,
292292
"id": instance.UHostId,
293293
"name": instance.Name,
294294
"cpu": cpu,
295295
"memory": memory / 1024,
296-
"instance_type": instanceTypeSetFunc(upperCvt.convert(instance.MachineType), cpu, memory/1024),
297296
"create_time": timestampToString(instance.CreateTime),
298297
"expire_time": timestampToString(instance.ExpireTime),
299298
"auto_renew": boolCamelCvt.unconvert(instance.AutoRenew),
@@ -306,7 +305,13 @@ func dataSourceUCloudInstancesSave(d *schema.ResourceData, instances []uhost.UHo
306305
"private_ip": privateIp,
307306
"vpc_id": vpcId,
308307
"subnet_id": subnetId,
309-
})
308+
}
309+
310+
if cpu != 0 {
311+
dataMap["instance_type"] = instanceTypeSetFunc(upperCvt.convert(instance.MachineType), cpu, memory/1024)
312+
}
313+
314+
data = append(data, dataMap)
310315
}
311316

312317
d.SetId(hashStringArray(ids))

ucloud/resource_ucloud_instance.go

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func resourceUCloudInstance() *schema.Resource {
4242
diffValidateChargeTypeWithDuration,
4343
diffValidateIsolationGroup,
4444
diffValidateDataDisks,
45+
diffValidateNetworkInterface,
4546
diffValidateCPUPlatform,
47+
diffValidateBootDiskTypeWithDataDisks,
4648
),
4749

4850
Schema: map[string]*schema.Schema{
@@ -178,6 +180,49 @@ func resourceUCloudInstance() *schema.Resource {
178180
},
179181
},
180182

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+
181226
"delete_disks_with_instance": {
182227
Type: schema.TypeBool,
183228
Optional: true,
@@ -439,15 +484,34 @@ func resourceUCloudInstanceCreate(d *schema.ResourceData, meta interface{}) erro
439484

440485
if v, ok := d.GetOk("data_disks"); ok {
441486
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+
}
442491
dataDisk := uhost.UHostDisk{}
443492
dataDisk.IsBoot = ucloud.String("false")
444-
disk := item.(map[string]interface{})
445493
dataDisk.Size = ucloud.Int(disk["size"].(int))
446494
dataDisk.Type = ucloud.String(upperCvt.unconvert(disk["type"].(string)))
447495
req.Disks = append(req.Disks, dataDisk)
448496
}
449497
}
450498

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+
451515
// if tag is empty string, use default tag
452516
if v, ok := d.GetOk("tag"); ok {
453517
req.Tag = ucloud.String(v.(string))
@@ -475,7 +539,7 @@ func resourceUCloudInstanceCreate(d *schema.ResourceData, meta interface{}) erro
475539
if isStringIn("CloudInit", imageResp.Features) {
476540
req.UserData = ucloud.String(base64.StdEncoding.EncodeToString([]byte(v.(string))))
477541
} 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)
479543
}
480544
}
481545
req.MachineType = ucloud.String(strings.ToUpper(t.HostType))
@@ -864,6 +928,9 @@ func resourceUCloudInstanceRead(d *schema.ResourceData, meta interface{}) error
864928
memory := instance.Memory
865929
cpu := instance.CPU
866930

931+
if cpu != 0 {
932+
d.Set("instance_type", instanceTypeSetFunc(upperCvt.convert(instance.MachineType), cpu, memory/1024))
933+
}
867934
d.Set("root_password", d.Get("root_password"))
868935
d.Set("isolation_group", instance.IsolationGroup)
869936
d.Set("name", instance.Name)
@@ -875,7 +942,6 @@ func resourceUCloudInstanceRead(d *schema.ResourceData, meta interface{}) error
875942
d.Set("expire_time", timestampToString(instance.ExpireTime))
876943
d.Set("auto_renew", boolCamelCvt.unconvert(instance.AutoRenew))
877944
d.Set("remark", instance.Remark)
878-
d.Set("instance_type", instanceTypeSetFunc(upperCvt.convert(instance.MachineType), cpu, memory/1024))
879945
d.Set("cpu_platform", instance.CpuPlatform)
880946

881947
//in order to be compatible with returns null
@@ -958,6 +1024,9 @@ func resourceUCloudInstanceDelete(d *schema.ResourceData, meta interface{}) erro
9581024
if v, ok := d.GetOkExists("delete_disks_with_instance"); ok {
9591025
deleReq.ReleaseUDisk = ucloud.Bool(v.(bool))
9601026
}
1027+
if v, ok := d.GetOkExists("delete_eips_with_instance"); ok {
1028+
deleReq.ReleaseEIP = ucloud.Bool(v.(bool))
1029+
}
9611030

9621031
return resource.Retry(15*time.Minute, func() *resource.RetryError {
9631032
instance, err := client.describeInstanceById(d.Id())
@@ -1133,6 +1202,24 @@ func diffValidateBootDiskTypeWithDataDiskType(diff *schema.ResourceDiff, meta in
11331202
return nil
11341203
}
11351204

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+
11361223
func diffValidateChargeTypeWithDuration(diff *schema.ResourceDiff, meta interface{}) error {
11371224
if v, ok := diff.GetOkExists("duration"); ok && v.(int) == 0 {
11381225
chargeType := diff.Get("charge_type").(string)
@@ -1175,8 +1262,21 @@ func diffValidateDataDisks(diff *schema.ResourceDiff, meta interface{}) error {
11751262
return nil
11761263
}
11771264

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+
11781275
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+
}
11801280
if v, ok := diff.GetOk("min_cpu_platform"); ok {
11811281
supportedCPUForOS := []string{"Intel/Auto", "Intel/CascadelakeR"}
11821282
if t.HostType == "os" && !isStringIn(v.(string), supportedCPUForOS) {

ucloud/resource_ucloud_instance_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ func TestAccUCloudInstance_basic(t *testing.T) {
5454
})
5555
}
5656

57+
func TestAccUCloudInstance_EIP(t *testing.T) {
58+
var instance uhost.UHostInstanceSet
59+
60+
resource.ParallelTest(t, resource.TestCase{
61+
PreCheck: func() {
62+
testAccPreCheck(t)
63+
},
64+
65+
IDRefreshName: "ucloud_instance.foo",
66+
Providers: testAccProviders,
67+
CheckDestroy: testAccCheckInstanceDestroy,
68+
69+
Steps: []resource.TestStep{
70+
{
71+
Config: testAccInstanceConfigEIP,
72+
73+
Check: resource.ComposeTestCheckFunc(
74+
testAccCheckInstanceExists("ucloud_instance.foo", &instance),
75+
resource.TestCheckResourceAttr("ucloud_instance.foo", "name", "tf-acc-instance-eip"),
76+
resource.TestCheckResourceAttr("ucloud_instance.foo", "tag", "tf-acc"),
77+
),
78+
},
79+
},
80+
})
81+
}
82+
5783
func TestAccUCloudInstance_outstanding(t *testing.T) {
5884
var instance uhost.UHostInstanceSet
5985

@@ -670,3 +696,34 @@ resource "ucloud_instance" "foo" {
670696
security_group = "${data.ucloud_security_groups.default.security_groups.0.id}"
671697
}
672698
`
699+
700+
const testAccInstanceConfigEIP = `
701+
data "ucloud_zones" "default" {
702+
}
703+
704+
data "ucloud_security_groups" "default" {
705+
type = "recommend_web"
706+
}
707+
708+
data "ucloud_images" "default" {
709+
availability_zone = "${data.ucloud_zones.default.zones.0.id}"
710+
name_regex = "^CentOS 7.6 64"
711+
image_type = "base"
712+
}
713+
714+
resource "ucloud_instance" "foo" {
715+
name = "tf-acc-instance-eip"
716+
tag = "tf-acc"
717+
availability_zone = "${data.ucloud_zones.default.zones.0.id}"
718+
image_id = "${data.ucloud_images.default.images.0.id}"
719+
instance_type = "n-basic-1"
720+
root_password = "wA1234567"
721+
security_group = "${data.ucloud_security_groups.default.security_groups.0.id}"
722+
network_interface {
723+
eip_bandwidth = 2
724+
eip_charge_mode = "bandwidth"
725+
eip_internet_type = "bgp"
726+
}
727+
delete_eips_with_instance = true
728+
}
729+
`

vendor/github.com/ucloud/ucloud-sdk-go/external/sts_creds.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)