Skip to content

Commit a7ed71b

Browse files
treywelshfrousselet
authored andcommitted
F #477: add nic attributes
1 parent 1d1d50d commit a7ed71b

7 files changed

+137
-30
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ FEATURES:
88
* **New Data Source**: `opennebula_marketplace_appliance` (#476)
99
* resources/opennebula_virtual_router_nic: add floating IP allocation (#510)
1010

11+
ENHANCEMENTS:
12+
13+
* resources/opennebula_virtual_machine: add `nic` scheduling attributes: `network_mode_auto`, `sched_requirements`, `sched_rank`. (#477)
14+
* resources/opennebula_template: add `nic` scheduling attributes: `network_mode_auto`, `sched_requirements`, `sched_rank`. (#477)
15+
1116
BUG FIXES:
1217

1318
* resources/opennebula_virtual_machine: fix `cpumodel` update (#463)

opennebula/helpers_vm.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,19 @@ func vmDiskResize(ctx context.Context, vmc *goca.VMController, timeout time.Dura
217217
// vmNICAttach is an helper that synchronously attach a nic
218218
func vmNICAttach(ctx context.Context, vmc *goca.VMController, timeout time.Duration, nicTpl *shared.NIC) (int, error) {
219219

220-
networkID, err := nicTpl.GetI(shared.NetworkID)
221-
if err != nil {
222-
return -1, fmt.Errorf("NIC template doesn't have a network ID")
220+
isNetworkMode := false
221+
networkMode, netModeErr := nicTpl.Get(shared.NetworkMode)
222+
networkID, netIDErr := nicTpl.GetI(shared.NetworkID)
223+
if netIDErr == nil {
224+
log.Printf("[DEBUG] Attach NIC to network (ID:%d)", networkID)
225+
} else {
226+
if netModeErr != nil {
227+
return -1, fmt.Errorf("NIC template neither have a network ID or a network mode")
228+
}
229+
log.Printf("[DEBUG] Attach NIC with network mode %s", networkMode)
230+
isNetworkMode = true
223231
}
224232

225-
log.Printf("[DEBUG] Attach NIC to network (ID:%d)", networkID)
226-
227233
// Retrieve NIC list
228234
vm, err := vmc.Info(false)
229235
if err != nil {
@@ -237,7 +243,11 @@ func vmNICAttach(ctx context.Context, vmc *goca.VMController, timeout time.Durat
237243

238244
err = vmc.AttachNIC(nicTpl.String())
239245
if err != nil {
240-
return -1, fmt.Errorf("can't attach network with ID:%d: %s\n", networkID, err)
246+
if isNetworkMode {
247+
return -1, fmt.Errorf("can't attach NIC (mode:%s): %s\n", networkMode, err)
248+
} else {
249+
return -1, fmt.Errorf("can't attach network (ID:%d): %s\n", networkID, err)
250+
}
241251
}
242252

243253
// wait before checking NIC list
@@ -277,7 +287,11 @@ func vmNICAttach(ctx context.Context, vmc *goca.VMController, timeout time.Durat
277287

278288
vmerr, _ := vm.UserTemplate.Get(vmk.Error)
279289

280-
return -1, fmt.Errorf("network %d: %s", networkID, vmerr)
290+
if isNetworkMode {
291+
return -1, fmt.Errorf("network (mode:%s): %s\n", networkMode, vmerr)
292+
} else {
293+
return -1, fmt.Errorf("network (ID:%d): %s", networkID, vmerr)
294+
}
281295

282296
} else {
283297

@@ -314,7 +328,12 @@ func vmNICAttach(ctx context.Context, vmc *goca.VMController, timeout time.Durat
314328
break
315329
}
316330
if attachedNIC == nil {
317-
return -1, fmt.Errorf("network %d: can't find the nic", networkID)
331+
332+
if isNetworkMode {
333+
return -1, fmt.Errorf("network (mode %s): can't find the NIC\n", networkMode)
334+
} else {
335+
return -1, fmt.Errorf("network (ID:%d): can't find the NIC", networkID)
336+
}
318337
}
319338
}
320339

opennebula/resource_opennebula_virtual_machine.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,21 @@ func flattenVMNICComputed(NICConfig map[string]interface{}, NIC shared.NIC) map[
902902
NICMap["security_groups"] = NICMap["computed_security_groups"]
903903
}
904904

905+
networkMode, err := NIC.Get(shared.NetworkMode)
906+
if err == nil && networkMode == "auto" {
907+
NICMap["network_mode_auto"] = true
908+
}
909+
910+
schedReqs, err := NIC.Get(shared.SchedRequirements)
911+
if err == nil {
912+
NICMap["sched_requirements"] = schedReqs
913+
}
914+
915+
schedRank, err := NIC.Get(shared.SchedRank)
916+
if err == nil {
917+
NICMap["sched_rank"] = schedRank
918+
}
919+
905920
return NICMap
906921
}
907922

@@ -940,6 +955,10 @@ func matchNIC(NICConfig map[string]interface{}, NIC shared.NIC) bool {
940955

941956
model, _ := NIC.Get(shared.Model)
942957
virtioQueues, _ := NIC.GetStr("VIRTIO_QUEUES")
958+
schedRequirements, _ := NIC.Get(shared.SchedRequirements)
959+
schedRank, _ := NIC.Get(shared.SchedRank)
960+
networkMode, _ := NIC.Get(shared.NetworkMode)
961+
943962
securityGroupsArray, _ := NIC.Get(shared.SecurityGroups)
944963

945964
if NICConfig["security_groups"] != nil && len(NICConfig["security_groups"].([]interface{})) > 0 {
@@ -976,7 +995,10 @@ func matchNIC(NICConfig map[string]interface{}, NIC shared.NIC) bool {
976995
emptyOrEqual(NICConfig["mac"], mac) &&
977996
emptyOrEqual(NICConfig["physical_device"], physicalDevice) &&
978997
emptyOrEqual(NICConfig["model"], model) &&
979-
emptyOrEqual(NICConfig["virtio_queues"], virtioQueues)
998+
emptyOrEqual(NICConfig["virtio_queues"], virtioQueues) &&
999+
emptyOrEqual(NICConfig["sched_requirements"], schedRequirements) &&
1000+
emptyOrEqual(NICConfig["sched_rank"], schedRank) &&
1001+
(NICConfig["network_mode_auto"].(bool) == false || networkMode == "auto")
9801002
}
9811003

9821004
func matchNICComputed(NICConfig map[string]interface{}, NIC shared.NIC) bool {
@@ -1051,8 +1073,13 @@ NICLoop:
10511073
match = true
10521074
nicMap = flattenVMNICComputed(nicConfig, nic)
10531075

1054-
networkID, _ := nic.GetI(shared.NetworkID)
1055-
nicMap["network_id"] = networkID
1076+
networkIDCfg := nicConfig["network_id"].(int)
1077+
if networkIDCfg == -1 {
1078+
nicMap["network_id"] = -1
1079+
} else {
1080+
networkID, _ := nic.GetI(shared.NetworkID)
1081+
nicMap["network_id"] = networkID
1082+
}
10561083

10571084
nicList = append(nicList, nicMap)
10581085

@@ -1863,7 +1890,11 @@ func updateNIC(ctx context.Context, d *schema.ResourceData, meta interface{}) er
18631890
"security_groups",
18641891
"model",
18651892
"virtio_queues",
1866-
"physical_device")
1893+
"physical_device",
1894+
"network_mode_auto",
1895+
"sched_requirements",
1896+
"sched_rank",
1897+
)
18671898

18681899
// in case of NICs updated in the middle of the NIC list
18691900
// they would be reattached at the end of the list (we don't have in place XML-RPC update method).

opennebula/resource_opennebula_virtual_machine_nic_test.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ func TestAccVirtualMachineNICUpdate(t *testing.T) {
6060
Check: resource.ComposeTestCheckFunc(
6161
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
6262
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "keep_nic_order", "false"),
63-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.#", "4"),
63+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.#", "5"),
6464
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.0.computed_ip", "172.16.100.112"),
6565
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.1.computed_ip", "172.16.100.132"),
6666
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.2.computed_ip", "172.16.100.113"),
67-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.3.computed_ip", "172.16.100.133"),
67+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.4.computed_ip", "172.16.100.133"),
6868
),
6969
},
7070
{
7171
Config: testAccVirtualMachineTemplateConfigMultipleNICsOrderedUpdate,
7272
Check: resource.ComposeTestCheckFunc(
7373
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
7474
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "keep_nic_order", "true"),
75-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.#", "4"),
75+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.#", "5"),
7676
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.0.computed_ip", "172.16.100.112"),
7777
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.1.computed_ip", "172.16.100.134"),
7878
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.2.computed_ip", "172.16.100.113"),
79-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.3.computed_ip", "172.16.100.133"),
79+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "nic.4.computed_ip", "172.16.100.133"),
8080
),
8181
},
8282
{
@@ -105,7 +105,7 @@ func TestAccVirtualMachineTemplateNIC(t *testing.T) {
105105
Check: resource.ComposeTestCheckFunc(
106106
testAccSetDSdummy(),
107107
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
108-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "1"),
108+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "2"),
109109
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_ip", "172.16.100.131"),
110110
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_model", "virtio"),
111111
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_virtio_queues", "2"),
@@ -116,7 +116,7 @@ func TestAccVirtualMachineTemplateNIC(t *testing.T) {
116116
Config: testAccVirtualMachineTemplateNICAdd,
117117
Check: resource.ComposeTestCheckFunc(
118118
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
119-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "1"),
119+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "2"),
120120
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_ip", "172.16.100.131"),
121121
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_model", "virtio"),
122122
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_virtio_queues", "2"),
@@ -130,7 +130,7 @@ func TestAccVirtualMachineTemplateNIC(t *testing.T) {
130130
Config: testAccVirtualMachineTemplateNIC,
131131
Check: resource.ComposeTestCheckFunc(
132132
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "name", "test-virtual_machine"),
133-
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "1"),
133+
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.#", "2"),
134134
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_ip", "172.16.100.131"),
135135
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_model", "virtio"),
136136
resource.TestCheckResourceAttr("opennebula_virtual_machine.test", "template_nic.0.computed_virtio_queues", "2"),
@@ -445,6 +445,9 @@ var testAccVirtualMachineTemplateConfigMultipleNICs = testNICVNetResources + `
445445
network_id = opennebula_virtual_network.network2.id
446446
ip = "172.16.100.113"
447447
}
448+
nic {
449+
network_mode_auto = true
450+
}
448451
nic {
449452
network_id = opennebula_virtual_network.network1.id
450453
ip = "172.16.100.133"
@@ -497,6 +500,9 @@ var testAccVirtualMachineTemplateConfigMultipleNICsOrderedUpdate = testNICVNetRe
497500
network_id = opennebula_virtual_network.network2.id
498501
ip = "172.16.100.113"
499502
}
503+
nic {
504+
network_mode_auto = true
505+
}
500506
nic {
501507
network_id = opennebula_virtual_network.network1.id
502508
ip = "172.16.100.133"
@@ -568,6 +574,9 @@ resource "opennebula_template" "template" {
568574
model = "virtio"
569575
virtio_queues = "2"
570576
}
577+
nic {
578+
network_mode_auto = true
579+
}
571580
572581
os {
573582
arch = "x86_64"

opennebula/shared_schemas.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ func nicFields() map[string]*schema.Schema {
207207
},
208208
"network_id": {
209209
Type: schema.TypeInt,
210-
Required: true,
210+
Optional: true,
211+
Default: -1,
211212
},
212213
"network": {
213214
Type: schema.TypeString,
@@ -224,6 +225,18 @@ func nicFields() map[string]*schema.Schema {
224225
Type: schema.TypeInt,
225226
},
226227
},
228+
"network_mode_auto": {
229+
Type: schema.TypeBool,
230+
Optional: true,
231+
},
232+
"sched_requirements": {
233+
Type: schema.TypeString,
234+
Optional: true,
235+
},
236+
"sched_rank": {
237+
Type: schema.TypeString,
238+
Optional: true,
239+
},
227240
}
228241
}
229242

@@ -536,7 +549,10 @@ func makeNICVector(nicConfig map[string]interface{}) *shared.NIC {
536549
for k, v := range nicConfig {
537550

538551
if k == "network_id" {
539-
nic.Add(shared.NetworkID, strconv.Itoa(v.(int)))
552+
networkID := v.(int)
553+
if networkID != -1 {
554+
nic.Add(shared.NetworkID, strconv.Itoa(networkID))
555+
}
540556
continue
541557
}
542558

@@ -558,6 +574,15 @@ func makeNICVector(nicConfig map[string]interface{}) *shared.NIC {
558574
case "security_groups":
559575
nicSecGroups := ArrayToString(v.([]interface{}), ",")
560576
nic.Add(shared.SecurityGroups, nicSecGroups)
577+
case "network_mode_auto":
578+
if v.(bool) {
579+
nic.Add(shared.NetworkMode, "auto")
580+
}
581+
case "sched_requirements":
582+
nic.Add(shared.SchedRequirements, v.(string))
583+
case "sched_rank":
584+
nic.Add(shared.SchedRank, v.(string))
585+
561586
}
562587
}
563588

@@ -774,8 +799,17 @@ func flattenNIC(nic shared.NIC) map[string]interface{} {
774799
model, _ := nic.Get(shared.Model)
775800
virtioQueues, _ := nic.GetStr("VIRTIO_QUEUES")
776801
networkId, _ := nic.GetI(shared.NetworkID)
777-
securityGroupsArray, _ := nic.Get(shared.SecurityGroups)
778802

803+
networkModeBool := false
804+
networkMode, err := nic.Get(shared.NetworkMode)
805+
if err == nil && networkMode == "auto" {
806+
networkModeBool = true
807+
}
808+
809+
schedReqs, _ := nic.Get(shared.SchedRequirements)
810+
schedRank, _ := nic.Get(shared.SchedRank)
811+
812+
securityGroupsArray, _ := nic.Get(shared.SecurityGroups)
779813
if len(securityGroupsArray) > 0 {
780814
sgString := strings.Split(securityGroupsArray, ",")
781815
for _, s := range sgString {
@@ -785,14 +819,17 @@ func flattenNIC(nic shared.NIC) map[string]interface{} {
785819
}
786820

787821
return map[string]interface{}{
788-
"ip": ip,
789-
"mac": mac,
790-
"network_id": networkId,
791-
"physical_device": physicalDevice,
792-
"network": network,
793-
"model": model,
794-
"virtio_queues": virtioQueues,
795-
"security_groups": sg,
822+
"ip": ip,
823+
"mac": mac,
824+
"network_id": networkId,
825+
"physical_device": physicalDevice,
826+
"network": network,
827+
"model": model,
828+
"virtio_queues": virtioQueues,
829+
"security_groups": sg,
830+
"network_mode_auto": networkModeBool,
831+
"sched_requirements": schedReqs,
832+
"sched_rank": schedRank,
796833
}
797834
}
798835

website/docs/r/template.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ Minimum 1 item. Maximum 8 items.
162162
* `model` - (Optional) Nic model driver. Example: `virtio`.
163163
* `physical_device` - (Optional) Physical device hosting the virtual network.
164164
* `security_groups` - (Optional) List of security group IDs to use on the virtual network.
165+
* `network_mode_auto` - (Optional) A boolean letting the scheduler pick the Virtual Networks the VM NICs will be attached to.
166+
* `sched_requirements` - (Optional) A boolean expression to select virtual networks (evaluates to true) to attach the NIC.
167+
* `sched_rank` - (Optional) Arithmetic expression to sort the suitable Virtual Networks for this NIC.
165168

166169
Minimum 1 item. Maximum 8 items.
167170

website/docs/r/virtual_machine.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ A disk update will be triggered in adding or removing a `disk` section, or by a
151151
* `virtio_queues` - (Optional) Virtio multi-queue size. Only if `model` is `virtio`.
152152
* `physical_device` - (Optional) Physical device hosting the virtual network.
153153
* `security_groups` - (Optional) List of security group IDs to use on the virtual network.
154+
* `network_mode_auto` - (Optional) A boolean letting the scheduler pick the Virtual Networks the VM NICs will be attached to.
155+
* `sched_requirements` - (Optional) A boolean expression to select virtual networks (evaluates to true) to attach the NIC.
156+
* `sched_rank` - (Optional) Arithmetic expression to sort the suitable Virtual Networks for this NIC.
154157

155158
Minimum 1 item. Maximum 8 items.
156159

0 commit comments

Comments
 (0)