Skip to content

Commit

Permalink
WIP: F #477: add nic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Nov 17, 2023
1 parent 9ded40f commit 868f202
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
55 changes: 51 additions & 4 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ func nicComputedVMFields() map[string]*schema.Schema {
Type: schema.TypeInt,
},
},
//"computed_network_mode_auto": {
// Type: schema.TypeBool,
// Computed: true,
//},
//"computed_sched_requirements": {
// Type: schema.TypeString,
// Computed: true,
//},
//"computed_sched_rank": {
// Type: schema.TypeString,
// Computed: true,
//},
}

}
Expand All @@ -123,6 +135,10 @@ func templateNICVMSchema() *schema.Schema {
Type: schema.TypeString,
Computed: true,
},
//"network_mode_auto": {
// Type: schema.TypeBool,
// Computed: true,
//},
}),
},
}
Expand Down Expand Up @@ -902,6 +918,21 @@ func flattenVMNICComputed(NICConfig map[string]interface{}, NIC shared.NIC) map[
NICMap["security_groups"] = NICMap["computed_security_groups"]
}

networkMode, err := NIC.Get(shared.NetworkMode)
if err == nil && networkMode == "auto" {
NICMap["network_mode_auto"] = true
}

schedReqs, err := NIC.Get(shared.SchedRequirements)
if err == nil {
NICMap["sched_requirements"] = schedReqs
}

schedRank, err := NIC.Get(shared.SchedRank)
if err == nil {
NICMap["sched_rank"] = schedRank
}

return NICMap
}

Expand Down Expand Up @@ -940,6 +971,10 @@ func matchNIC(NICConfig map[string]interface{}, NIC shared.NIC) bool {

model, _ := NIC.Get(shared.Model)
virtioQueues, _ := NIC.GetStr("VIRTIO_QUEUES")
schedRequirements, _ := NIC.Get(shared.SchedRequirements)
schedRank, _ := NIC.Get(shared.SchedRank)
networkMode, _ := NIC.Get(shared.NetworkMode)

securityGroupsArray, _ := NIC.Get(shared.SecurityGroups)

if NICConfig["security_groups"] != nil && len(NICConfig["security_groups"].([]interface{})) > 0 {
Expand Down Expand Up @@ -976,7 +1011,10 @@ func matchNIC(NICConfig map[string]interface{}, NIC shared.NIC) bool {
emptyOrEqual(NICConfig["mac"], mac) &&
emptyOrEqual(NICConfig["physical_device"], physicalDevice) &&
emptyOrEqual(NICConfig["model"], model) &&
emptyOrEqual(NICConfig["virtio_queues"], virtioQueues)
emptyOrEqual(NICConfig["virtio_queues"], virtioQueues) &&
emptyOrEqual(NICConfig["sched_requirements"], schedRequirements) &&
emptyOrEqual(NICConfig["sched_rank"], schedRank) &&
(NICConfig["network_mode_auto"].(bool) == false || networkMode == "auto")
}

func matchNICComputed(NICConfig map[string]interface{}, NIC shared.NIC) bool {
Expand Down Expand Up @@ -1051,8 +1089,13 @@ NICLoop:
match = true
nicMap = flattenVMNICComputed(nicConfig, nic)

networkID, _ := nic.GetI(shared.NetworkID)
nicMap["network_id"] = networkID
networkIDCfg := nicConfig["network_id"].(int)
if networkIDCfg == -1 {
nicMap["network_id"] = -1
} else {
networkID, _ := nic.GetI(shared.NetworkID)
nicMap["network_id"] = networkID
}

nicList = append(nicList, nicMap)

Expand Down Expand Up @@ -1863,7 +1906,11 @@ func updateNIC(ctx context.Context, d *schema.ResourceData, meta interface{}) er
"security_groups",
"model",
"virtio_queues",
"physical_device")
"physical_device",
"network_mode_auto",
"sched_requirements",
"sched_rank",
)

// in case of NICs updated in the middle of the NIC list
// they would be reattached at the end of the list (we don't have in place XML-RPC update method).
Expand Down
29 changes: 27 additions & 2 deletions opennebula/shared_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func nicFields() map[string]*schema.Schema {
},
"network_id": {
Type: schema.TypeInt,
Required: true,
Optional: true,
Default: -1,
},
"network": {
Type: schema.TypeString,
Expand All @@ -224,6 +225,18 @@ func nicFields() map[string]*schema.Schema {
Type: schema.TypeInt,
},
},
"network_mode_auto": {
Type: schema.TypeBool,
Optional: true,
},
"sched_requirements": {
Type: schema.TypeString,
Optional: true,
},
"sched_rank": {
Type: schema.TypeString,
Optional: true,
},
}
}

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

if k == "network_id" {
nic.Add(shared.NetworkID, strconv.Itoa(v.(int)))
networkID := v.(int)
if networkID != -1 {
nic.Add(shared.NetworkID, strconv.Itoa(networkID))
}
continue
}

Expand All @@ -558,6 +574,15 @@ func makeNICVector(nicConfig map[string]interface{}) *shared.NIC {
case "security_groups":
nicSecGroups := ArrayToString(v.([]interface{}), ",")
nic.Add(shared.SecurityGroups, nicSecGroups)
case "network_mode_auto":
if v.(bool) {
nic.Add(shared.NetworkMode, "auto")
}
case "sched_requirements":
nic.Add(shared.SchedRequirements, v.(string))
case "sched_rank":
nic.Add(shared.SchedRank, v.(string))

}
}

Expand Down

0 comments on commit 868f202

Please sign in to comment.