Skip to content

Commit

Permalink
F #247 Add IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
frousselet committed May 23, 2022
1 parent 376ab4d commit f4f4c0d
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 84 deletions.
153 changes: 153 additions & 0 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func nicComputedVMFields() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"computed_ip6_global": {
Type: schema.TypeString,
Computed: true,
},
"computed_ip6_link": {
Type: schema.TypeString,
Computed: true,
},
"computed_mac": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -648,6 +656,8 @@ func flattenNICComputed(nic shared.NIC) map[string]interface{} {
ip, _ := nic.Get(shared.IP)
mac, _ := nic.Get(shared.MAC)
physicalDevice, _ := nic.GetStr("PHYDEV")
ip6Global, _ := nic.GetStr("IP6_GLOBAL")
ip6Link, _ := nic.GetStr("IP6_LINK")
network, _ := nic.Get(shared.Network)

model, _ := nic.Get(shared.Model)
Expand All @@ -664,6 +674,8 @@ func flattenNICComputed(nic shared.NIC) map[string]interface{} {
"nic_id": nicID,
"network": network,
"computed_ip": ip,
"computed_ip6_global": ip6Global,
"computed_ip6_link": ip6Link,
"computed_mac": mac,
"computed_physical_device": physicalDevice,
"computed_model": model,
Expand All @@ -679,6 +691,12 @@ func flattenVMNICComputed(NICConfig map[string]interface{}, NIC shared.NIC) map[
if len(NICConfig["ip"].(string)) > 0 {
NICMap["ip"] = NICMap["computed_ip"]
}
if len(NICConfig["ip6_global"].(string)) > 0 {
NICMap["ip6_global"] = NICMap["computed_ip6_global"]
}
if len(NICConfig["ip6_link"].(string)) > 0 {
NICMap["ip6_link"] = NICMap["computed_ip6_link"]
}
if len(NICConfig["mac"].(string)) > 0 {
NICMap["mac"] = NICMap["computed_mac"]
}
Expand Down Expand Up @@ -1045,10 +1063,145 @@ func resourceOpennebulaVirtualMachineUpdateCustom(d *schema.ResourceData, meta i
}
}

<<<<<<< Updated upstream
if customFunc != nil {
err = customFunc(d, meta)
if err != nil {
return err
=======
if d.HasChange("nic") {

log.Printf("[INFO] Update NIC configuration")

old, new := d.GetChange("nic")
attachedNicsCfg := old.([]interface{})
newNicsCfg := new.([]interface{})

timeout := d.Get("timeout").(int)

// get unique elements of each list of configs
// NOTE: diffListConfig relies on Set, so we may loose list ordering of NICs here
// it's why we reorder the attach list below
toDetach, toAttach := diffListConfig(newNicsCfg, attachedNicsCfg,
&schema.Resource{
Schema: nicFields(),
},
"network_id",
"ip",
"ip6Global",
"ip6Link",
"mac",
"security_groups",
"model",
"virtio_queues",
"physical_device")

// 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).
// keep_nic_order prevent this behavior adding more NICs to detach/attach to keep initial ordering
if d.Get("keep_nic_order").(bool) && len(toDetach) > 0 {

// retrieve the minimal nic ID to detach
firstNIC := toDetach[0].(map[string]interface{})
minID := firstNIC["nic_id"].(int)
for _, nicIf := range toDetach[1:] {
nicConfig := nicIf.(map[string]interface{})

nicID := nicConfig["nic_id"].(int)
if nicID < minID {
minID = nicID
}
}

// NICs with greater nic ID should be detached
oldNICLoop:
for _, nicIf := range attachedNicsCfg {
nicConfig := nicIf.(map[string]interface{})

// collect greater nic IDs
nicID := nicConfig["nic_id"].(int)
if nicID > minID {

// add the nic if not already present in toDetach
for _, nicDetachIf := range toDetach {
nicDetachConfig := nicDetachIf.(map[string]interface{})

// nic is already present
detachNICID := nicDetachConfig["nic_id"].(int)
if detachNICID == nicID {
continue oldNICLoop
}
}

// add the NIC to detach it
toDetach = append(toDetach, nicConfig)

// add the NIC to reattach it
toAttach = append(toAttach, nicConfig)
}
}

}

// reorder toAttach NIC list according to new nics list order
newNICtoAttach := make([]interface{}, len(toAttach))
i := 0
for _, newNICIf := range newNicsCfg {
newNIC := newNICIf.(map[string]interface{})
newNICSecGroup := newNIC["security_groups"].([]interface{})

for _, NICToAttachIf := range toAttach {
NIC := NICToAttachIf.(map[string]interface{})

// if NIC have the same attributes

// compare security_groups
NICSecGroup := NIC["security_groups"].([]interface{})

if ArrayToString(NICSecGroup, ",") != ArrayToString(newNICSecGroup, ",") {
continue
}

// compare other attributes
if (NIC["ip"] == newNIC["ip"] &&
NIC["mac"] == newNIC["mac"]) &&
NIC["model"] == newNIC["model"] &&
NIC["virtio_queues"] == newNIC["virtio_queues"] &&
NIC["physical_device"] == newNIC["physical_device"] {

newNICtoAttach[i] = NIC
i++
break
}
}
}
toAttach = newNICtoAttach

// Detach the nics
for _, nicIf := range toDetach {
nicConfig := nicIf.(map[string]interface{})

nicID := nicConfig["nic_id"].(int)

err := vmNICDetach(vmc, timeout, nicID)
if err != nil {
return fmt.Errorf("vm nic detach: %s", err)

}
}

// Attach the nics
for _, nicIf := range toAttach {

nicConfig := nicIf.(map[string]interface{})

nicTpl := makeNICVector(nicConfig)

_, err := vmNICAttach(vmc, timeout, nicTpl)
if err != nil {
return fmt.Errorf("vm nic attach: %s", err)
}
>>>>>>> Stashed changes
}
}

Expand Down
Loading

0 comments on commit f4f4c0d

Please sign in to comment.