Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 19c8767

Browse files
committed
gcore_baremetal interfaces - is_trunk -> is_parent, is_parent optional now
1 parent 74ad651 commit 19c8767

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

docs/resources/gcore_baremetal.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ resource "gcore_baremetal" "bm" {
3636
3737
// interface {
3838
// type = "external"
39+
// is_parent = "true" // if is_parent = true interface cant be detached, and always connected first
3940
// }
4041
4142
keypair_name = "test" // your keypair name
@@ -89,13 +90,14 @@ Optional:
8990

9091
- **existing_fip_id** (String)
9192
- **fip_source** (String)
93+
- **is_parent** (Boolean) If not set will be calculated after creation. Trunk interface always attached first. Can't detach interface if is_parent true. Fields affect only on creation
9294
- **network_id** (String) required if type is 'subnet' or 'any_subnet'
95+
- **order** (Number) Order of attaching interface. Trunk interface always attached first, fields affect only on creation
9396
- **subnet_id** (String) required if type is 'subnet'
9497

9598
Read-Only:
9699

97100
- **ip_address** (String)
98-
- **is_trunk** (Boolean) Calculated after creation. Can't detach interface if is_trunk true
99101
- **port_id** (String)
100102

101103

examples/resources/gcore_baremetal/resource.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ resource "gcore_baremetal" "bm" {
2121

2222
// interface {
2323
// type = "external"
24+
// is_parent = "true" // if is_parent = true interface cant be detached, and always connected first
2425
// }
2526

2627
keypair_name = "test" // your keypair name

gcore/resource_gcore_baremetal.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,16 @@ func resourceBmInstance() *schema.Resource {
122122
Required: true,
123123
Description: fmt.Sprintf("Avalilable value is '%s', '%s'", types.SubnetInterfaceType, types.ExternalInterfaceType),
124124
},
125-
"is_trunk": {
125+
"is_parent": {
126126
Type: schema.TypeBool,
127127
Computed: true,
128-
Description: "Calculated after creation. Can't detach interface if is_trunk true",
128+
Optional: true,
129+
Description: "If not set will be calculated after creation. Trunk interface always attached first. Can't detach interface if is_parent true. Fields affect only on creation",
130+
},
131+
"order": {
132+
Type: schema.TypeInt,
133+
Optional: true,
134+
Description: "Order of attaching interface. Trunk interface always attached first, fields affect only on creation",
129135
},
130136
"network_id": {
131137
Type: schema.TypeString,
@@ -261,9 +267,11 @@ func resourceBmInstanceCreate(ctx context.Context, d *schema.ResourceData, m int
261267
return diag.FromErr(err)
262268
}
263269

264-
ints := d.Get("interface").(*schema.Set).List()
265-
newInterface := make([]bminstances.InterfaceOpts, len(ints))
266-
for i, iface := range ints {
270+
ifs := d.Get("interface").(*schema.Set).List()
271+
//sort interfaces by 'is_parent' at first and by 'order' key to attach it in right order
272+
sort.Sort(instanceInterfaces(ifs))
273+
newInterface := make([]bminstances.InterfaceOpts, len(ifs))
274+
for i, iface := range ifs {
267275
raw := iface.(map[string]interface{})
268276
newIface := bminstances.InterfaceOpts{
269277
Type: types.InterfaceType(raw["type"].(string)),
@@ -282,6 +290,7 @@ func resourceBmInstanceCreate(ctx context.Context, d *schema.ResourceData, m int
282290
newInterface[i] = newIface
283291
}
284292

293+
log.Printf("[DEBUG] Baremetal interfaces: %+v", newInterface)
285294
opts := bminstances.CreateOpts{
286295
Flavor: d.Get("flavor_id").(string),
287296
Names: []string{d.Get("name").(string)},
@@ -397,14 +406,14 @@ func resourceBmInstanceRead(ctx context.Context, d *schema.ResourceData, m inter
397406
subnetID := assignment.SubnetID
398407

399408
//bad idea, but what to do
400-
var iOpts instances.InterfaceOpts
409+
var iOpts OrderedInterfaceOpts
401410
var orderedIOpts OrderedInterfaceOpts
402411
var ok bool
403412
// we need to match our interfaces with api's interfaces
404413
// but with don't have any unique value, that's why we use exactly that list of keys
405414
for _, k := range []string{subnetID, iface.PortID, iface.NetworkID, types.ExternalInterfaceType.String()} {
406415
if orderedIOpts, ok = interfaces[k]; ok {
407-
iOpts = orderedIOpts.InterfaceOpts
416+
iOpts = orderedIOpts
408417
break
409418
}
410419
}
@@ -419,7 +428,8 @@ func resourceBmInstanceRead(ctx context.Context, d *schema.ResourceData, m inter
419428
i["network_id"] = iface.NetworkID
420429
i["subnet_id"] = subnetID
421430
i["port_id"] = iface.PortID
422-
i["is_trunk"] = true
431+
i["is_parent"] = true
432+
i["order"] = iOpts.Order
423433
if iOpts.FloatingIP != nil {
424434
i["fip_source"] = iOpts.FloatingIP.Source.String()
425435
i["existing_fip_id"] = iOpts.FloatingIP.ExistingFloatingID
@@ -434,14 +444,14 @@ func resourceBmInstanceRead(ctx context.Context, d *schema.ResourceData, m inter
434444
subnetID := assignment.SubnetID
435445

436446
//bad idea, but what to do
437-
var iOpts instances.InterfaceOpts
447+
var iOpts OrderedInterfaceOpts
438448
var orderedIOpts OrderedInterfaceOpts
439449
var ok bool
440450
// we need to match our interfaces with api's interfaces
441451
// but with don't have any unique value, that's why we use exactly that list of keys
442452
for _, k := range []string{subnetID, iface1.PortID, iface1.NetworkID, types.ExternalInterfaceType.String()} {
443453
if orderedIOpts, ok = interfaces[k]; ok {
444-
iOpts = orderedIOpts.InterfaceOpts
454+
iOpts = orderedIOpts
445455
break
446456
}
447457
}
@@ -456,7 +466,8 @@ func resourceBmInstanceRead(ctx context.Context, d *schema.ResourceData, m inter
456466
i["network_id"] = iface1.NetworkID
457467
i["subnet_id"] = subnetID
458468
i["port_id"] = iface1.PortID
459-
i["is_trunk"] = false
469+
i["is_parent"] = false
470+
i["order"] = iOpts.Order
460471
if iOpts.FloatingIP != nil {
461472
i["fip_source"] = iOpts.FloatingIP.Source.String()
462473
i["existing_fip_id"] = iOpts.FloatingIP.ExistingFloatingID
@@ -607,7 +618,7 @@ func resourceBmInstanceUpdate(ctx context.Context, d *schema.ResourceData, m int
607618
}
608619

609620
iface := i.(map[string]interface{})
610-
if iface["is_trunk"].(bool) {
621+
if iface["is_parent"].(bool) {
611622
return diag.Errorf("could not detach trunk interface")
612623
}
613624

gcore/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ func (s instanceInterfaces) Len() int {
8484
func (s instanceInterfaces) Less(i, j int) bool {
8585
ifLeft := s[i].(map[string]interface{})
8686
ifRight := s[j].(map[string]interface{})
87+
88+
//only bm instance has a parent interface, and it should be attached first
89+
isTrunkLeft, okLeft := ifLeft["is_parent"]
90+
isTrunkRight, okRight := ifRight["is_parent"]
91+
if okLeft && okRight {
92+
left, _ := isTrunkLeft.(bool)
93+
right, _ := isTrunkRight.(bool)
94+
switch {
95+
case left && !right:
96+
return true
97+
case right && !left:
98+
return false
99+
}
100+
}
101+
87102
lOrder, _ := ifLeft["order"].(int)
88103
rOrder, _ := ifRight["order"].(int)
89104
return lOrder < rOrder

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/G-Core/gcorelabscloud-go v0.4.37
1212
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
1313
github.com/hashicorp/terraform-exec v0.15.0 // indirect
14+
github.com/hashicorp/terraform-plugin-docs v0.5.1 // indirect
1415
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
1516
github.com/mattn/go-colorable v0.1.11 // indirect
1617
github.com/mitchellh/mapstructure v1.4.1

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ github.com/G-Core/gcorelabscdn-go v0.1.2 h1:AiwZY3oIHL4wMJNmIlLgxw4eQmSqtQmkTzrk
4949
github.com/G-Core/gcorelabscdn-go v0.1.2/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
5050
github.com/G-Core/gcorelabscloud-go v0.4.37 h1:Ae2EhrtMbTTd6MFyyrDAPnX9AIbQG696Jwn97hp6gek=
5151
github.com/G-Core/gcorelabscloud-go v0.4.37/go.mod h1:Z1MF80mWagEUrxygtYkvW/MJEYNmIUPsIEYBB3cKjOM=
52+
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
5253
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
54+
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
5355
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
56+
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
5457
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
5558
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
5659
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
@@ -83,6 +86,7 @@ github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/
8386
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
8487
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
8588
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
89+
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 h1:BUAU3CGlLvorLI26FmByPp2eC2qla6E1Tw+scpcg/to=
8690
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
8791
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
8892
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
@@ -98,6 +102,7 @@ github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPI
98102
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
99103
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
100104
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ00z/TKoufEY6K/a0k6AhaJrQKdFe6OfVXsa4=
105+
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
101106
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
102107
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
103108
github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s=
@@ -386,6 +391,8 @@ github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0u
386391
github.com/hashicorp/terraform-json v0.12.0/go.mod h1:pmbq9o4EuL43db5+0ogX10Yofv1nozM+wskr/bGFJpI=
387392
github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=
388393
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
394+
github.com/hashicorp/terraform-plugin-docs v0.5.1 h1:WwrUcamix9x0TqfTw/WGHMRqoTe1QPZKaeWJPuFb4lQ=
395+
github.com/hashicorp/terraform-plugin-docs v0.5.1/go.mod h1:SQwEgy0/B0UPQ07rNEG1Wpt6E3jvRcCwkVHPNybGgc0=
389396
github.com/hashicorp/terraform-plugin-go v0.3.0 h1:AJqYzP52JFYl9NABRI7smXI1pNjgR5Q/y2WyVJ/BOZA=
390397
github.com/hashicorp/terraform-plugin-go v0.3.0/go.mod h1:dFHsQMaTLpON2gWhVWT96fvtlc/MF1vSy3OdMhWBzdM=
391398
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0 h1:SuI59MqNjYDrL7EfqHX9V6P/24isgqYx/FdglwVs9bg=
@@ -394,6 +401,7 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKe
394401
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
395402
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
396403
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
404+
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
397405
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
398406
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
399407
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -476,6 +484,7 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
476484
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
477485
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
478486
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
487+
github.com/mitchellh/cli v1.1.2 h1:PvH+lL2B7IQ101xQL63Of8yFS2y+aDlsFcsqNc+u/Kw=
479488
github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4=
480489
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
481490
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
@@ -532,11 +541,14 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
532541
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
533542
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
534543
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
544+
github.com/posener/complete v1.1.1 h1:ccV59UEOTzVDnDUEFdT95ZzHVZ+5+158q8+SJb2QV5w=
535545
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
536546
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
537547
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
538548
github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
539549
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
550+
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
551+
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
540552
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
541553
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
542554
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=

0 commit comments

Comments
 (0)