This commit is contained in:
2024-10-08 12:20:56 +03:00
parent 6eb6546722
commit a59ec3611b
297 changed files with 31952 additions and 124 deletions

View File

@@ -286,7 +286,7 @@ func flattenComputeDisksDemo(disksList compute.ListComputeDisks, disksBlocks, ex
return res
}
func flattenNetwork(interfaces compute.ListInterfaces) []map[string]interface{} {
func flattenNetwork(networks []interface{}, interfaces compute.ListInterfaces) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(interfaces))
for _, network := range interfaces {
@@ -295,12 +295,24 @@ func flattenNetwork(interfaces compute.ListInterfaces) []map[string]interface{}
"net_type": network.NetType,
"ip_address": network.IPAddress,
"mac": network.MAC,
"weight": flattenNetworkWeight(networks, network.NetID, network.NetType),
}
res = append(res, temp)
}
return res
}
func flattenNetworkWeight(networks []interface{}, netID uint64, netType string) int {
for _, network := range networks {
ns := network.(map[string]interface{})
if ns["net_id"].(int) == int(netID) && ns["net_type"].(string) == netType {
weight := ns["weight"].(int)
return weight
}
}
return 0
}
func findBootDisk(disks compute.ListComputeDisks) *compute.ItemComputeDisk {
for _, disk := range disks {
if disk.Type == "B" {
@@ -408,7 +420,7 @@ func flattenCompute(d *schema.ResourceData, computeRec compute.RecordCompute, pc
d.Set("started", true)
}
d.Set("network", flattenNetwork(computeRec.Interfaces))
d.Set("network", flattenNetwork(d.Get("network").(*schema.Set).List(), computeRec.Interfaces))
d.Set("pci_devices", flattenPCI(*pciList))
return nil

View File

@@ -151,6 +151,12 @@ func networkSubresourceSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "MAC address associated with this connection. MAC address is assigned automatically.",
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Description: "weight the network if you need to sort network list, the smallest attach first. zero or null weight attach last",
},
}
return rets
}

View File

@@ -35,6 +35,7 @@ package kvmvm
import (
"context"
"sort"
"strconv"
"strings"
@@ -149,6 +150,17 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
if networks.(*schema.Set).Len() > 0 {
ns := networks.(*schema.Set).List()
sort.Slice(ns, func(i, j int) bool {
weightI := ns[i].(map[string]interface{})["weight"].(int)
weightJ := ns[j].(map[string]interface{})["weight"].(int)
if weightI == 0 {
return false
}
if weightJ == 0 {
return true
}
return weightI < weightJ
})
interfaces := make([]kvmx86.Interface, 0)
for _, elem := range ns {
netInterfaceVal := elem.(map[string]interface{})
@@ -174,7 +186,17 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
if networks, ok := d.GetOk("network"); ok {
if networks.(*schema.Set).Len() > 0 {
ns := networks.(*schema.Set).List()
sort.Slice(ns, func(i, j int) bool {
weightI := ns[i].(map[string]interface{})["weight"].(int)
weightJ := ns[j].(map[string]interface{})["weight"].(int)
if weightI == 0 {
return false
}
if weightJ == 0 {
return true
}
return weightI < weightJ
})
interfaces := make([]kvmppc.Interface, 0)
for _, elem := range ns {
netInterfaceVal := elem.(map[string]interface{})
@@ -911,7 +933,7 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
if d.HasChange("network") {
err = utilityComputeNetworksConfigure(ctx, d, m, true, false, computeRec.ID) // pass do_delta = true to apply changes, if any
err = utilityComputeNetworksConfigure(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}

View File

@@ -35,6 +35,7 @@ package kvmvm
import (
"context"
"regexp"
"sort"
"strconv"
log "github.com/sirupsen/logrus"
@@ -171,65 +172,29 @@ func utilityComputeExtraDisksConfigure(ctx context.Context, d *schema.ResourceDa
return nil
}
func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}, do_delta bool, skip_zero bool, computeID uint64) error {
func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
old_set, new_set := d.GetChange("network")
oldSet, newSet := d.GetChange("network")
apiErrCount := 0
var lastSavedError error
if !do_delta {
if new_set.(*schema.Set).Len() < 1 {
return nil
}
for i, runner := range new_set.(*schema.Set).List() {
if i == 0 && skip_zero {
continue
}
net_data := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetAttachRequest{
ComputeID: computeId,
NetType: net_data["net_type"].(string),
NetID: uint64(net_data["net_id"].(int)),
}
ipaddr, ipSet := net_data["ip_address"]
if ipSet {
req.IPAddr = ipaddr.(string)
}
_, err := c.CloudAPI().Compute().NetAttach(ctx, req)
if err != nil {
apiErrCount++
lastSavedError = err
}
}
if apiErrCount > 0 {
log.Errorf("utilityComputeNetworksConfigure: there were %d error(s) when managing networks of Compute ID %s. Last error was: %s",
apiErrCount, d.Id(), lastSavedError)
return lastSavedError
}
return nil
}
detach_set := old_set.(*schema.Set).Difference(new_set.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: detach set has %d items for Compute ID %s", detach_set.Len(), d.Id())
for _, runner := range detach_set.List() {
net_data := runner.(map[string]interface{})
detachSet := oldSet.(*schema.Set).Difference(newSet.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: detach set has %d items for Compute ID %s", detachSet.Len(), d.Id())
for _, runner := range detachSet.List() {
netData := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetDetachRequest{
ComputeID: computeId,
IPAddr: net_data["ip_address"].(string),
MAC: net_data["mac"].(string),
IPAddr: netData["ip_address"].(string),
MAC: netData["mac"].(string),
}
_, err := c.CloudAPI().Compute().NetDetach(ctx, req)
if err != nil {
log.Errorf("utilityComputeNetworksConfigure: failed to detach net ID %d of type %s from Compute ID %s: %s",
net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err)
netData["net_id"].(int), netData["net_type"].(string), d.Id(), err)
apiErrCount++
lastSavedError = err
}
@@ -237,7 +202,7 @@ func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData
needStart := false
if d.Get("network").(*schema.Set).Len() == 1 || old_set.(*schema.Set).Len() < 1 {
if d.Get("network").(*schema.Set).Len() == 1 || oldSet.(*schema.Set).Len() < 1 {
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
if err := utilityComputeStop(ctx, computeId, m); err != nil {
apiErrCount++
@@ -246,25 +211,37 @@ func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData
needStart = true
}
attach_set := new_set.(*schema.Set).Difference(old_set.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: attach set has %d items for Compute ID %s", attach_set.Len(), d.Id())
for _, runner := range attach_set.List() {
net_data := runner.(map[string]interface{})
attachSet := newSet.(*schema.Set).Difference(oldSet.(*schema.Set))
attachList := attachSet.List()
sort.Slice(attachList, func(i, j int) bool {
weightI := attachList[i].(map[string]interface{})["weight"].(int)
weightJ := attachList[j].(map[string]interface{})["weight"].(int)
if weightI == 0 {
return false
}
if weightJ == 0 {
return true
}
return weightI < weightJ
})
log.Debugf("utilityComputeNetworksConfigure: attach set has %d items for Compute ID %s", attachSet.Len(), d.Id())
for _, runner := range attachList {
netData := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetAttachRequest{
ComputeID: computeId,
NetType: net_data["net_type"].(string),
NetID: uint64(net_data["net_id"].(int)),
NetType: netData["net_type"].(string),
NetID: uint64(netData["net_id"].(int)),
}
if net_data["ip_address"].(string) != "" {
req.IPAddr = net_data["ip_address"].(string)
if netData["ip_address"].(string) != "" {
req.IPAddr = netData["ip_address"].(string)
}
_, err := c.CloudAPI().Compute().NetAttach(ctx, req)
if err != nil {
log.Errorf("utilityComputeNetworksConfigure: failed to attach net ID %d of type %s to Compute ID %s: %s",
net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err)
netData["net_id"].(int), netData["net_type"].(string), d.Id(), err)
apiErrCount++
lastSavedError = err
}

View File

@@ -22,7 +22,7 @@ func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute, p
if len(computeRec.Interfaces) > 0 {
log.Debugf("flattenCompute: calling parseComputeInterfacesToNetworks for %d interfaces", len(computeRec.Interfaces))
if err := d.Set("network", parseComputeInterfacesToNetworks(computeRec.Interfaces)); err != nil {
if err := d.Set("network", parseComputeInterfacesToNetworks(d.Get("network").(*schema.Set).List(), computeRec.Interfaces)); err != nil {
return err
}
}
@@ -667,7 +667,7 @@ func flattenDataCompute(d *schema.ResourceData, compFacts *compute.RecordCompute
// Parse the list of interfaces from compute/get response into a list of networks
// attached to this compute
func parseComputeInterfacesToNetworks(ifaces compute.ListInterfaces) []interface{} {
func parseComputeInterfacesToNetworks(networks []interface{}, ifaces compute.ListInterfaces) []interface{} {
// return value will be used to d.Set("network") item of dataSourceCompute schema
length := len(ifaces)
log.Debugf("parseComputeInterfacesToNetworks: called for %d ifaces", length)
@@ -681,6 +681,7 @@ func parseComputeInterfacesToNetworks(ifaces compute.ListInterfaces) []interface
elem["net_type"] = value.NetType
elem["ip_address"] = value.IPAddress
elem["mac"] = value.MAC
elem["weight"] = flattenNetworkWeight(networks, value.NetID, value.NetType)
result = append(result, elem)
}
@@ -688,6 +689,17 @@ func parseComputeInterfacesToNetworks(ifaces compute.ListInterfaces) []interface
return result
}
func flattenNetworkWeight(networks []interface{}, netID uint64, netType string) int {
for _, network := range networks {
ns := network.(map[string]interface{})
if ns["net_id"].(int) == int(netID) && ns["net_type"].(string) == netType {
weight := ns["weight"].(int)
return weight
}
}
return 0
}
func flattenDisk(diskList compute.ListDisks) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(diskList))
for _, disk := range diskList {

View File

@@ -34,6 +34,7 @@ package kvmvm
import (
"context"
"sort"
"strconv"
"strings"
@@ -106,7 +107,19 @@ func resourceComputeCreate(ctx context.Context, d *schema.ResourceData, m interf
if networks, ok := d.GetOk("network"); ok {
if networks.(*schema.Set).Len() > 0 {
ns := networks.(*schema.Set).List()
log.Debugf("BEFORE SORT %v", ns)
sort.Slice(ns, func(i, j int) bool {
weightI := ns[i].(map[string]interface{})["weight"].(int)
weightJ := ns[j].(map[string]interface{})["weight"].(int)
if weightI == 0 {
return false
}
if weightJ == 0 {
return true
}
return weightI < weightJ
})
log.Debugf("AFTER SORT %v", ns)
interfacesX86 := make([]kvmx86.Interface, 0)
interfacesPPC := make([]kvmppc.Interface, 0)
for _, elem := range ns {
@@ -702,7 +715,7 @@ func resourceComputeUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
if d.HasChange("network") {
err = utilityComputeNetworksConfigure(ctx, d, m, true, false, computeRec.ID) // pass do_delta = true to apply changes, if any
err = utilityComputeNetworksConfigure(ctx, d, m) // pass do_delta = true to apply changes, if any
if err != nil {
return diag.FromErr(err)
}

View File

@@ -2923,6 +2923,12 @@ func resourceComputeSchemaMake() map[string]*schema.Schema {
Computed: true,
Description: "MAC address associated with this connection. MAC address is assigned automatically.",
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Description: "weight the network if you need to sort network list, the smallest attach first. zero or null weight attach last",
},
},
},
Description: "Optional network connection(s) for this compute. You may specify several network blocks, one for each connection.",

View File

@@ -581,65 +581,29 @@ func networkSubresIPAddreDiffSupperss(key, oldVal, newVal string, d *schema.Reso
return true // suppress difference
}
func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}, do_delta bool, skip_zero bool, computeID uint64) error {
func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData, m interface{}) error {
c := m.(*controller.ControllerCfg)
old_set, new_set := d.GetChange("network")
oldSet, newSet := d.GetChange("network")
apiErrCount := 0
var lastSavedError error
if !do_delta {
if new_set.(*schema.Set).Len() < 1 {
return nil
}
for i, runner := range new_set.(*schema.Set).List() {
if i == 0 && skip_zero {
continue
}
net_data := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetAttachRequest{
ComputeID: computeId,
NetType: net_data["net_type"].(string),
NetID: uint64(net_data["net_id"].(int)),
}
ipaddr, ipSet := net_data["ip_address"]
if ipSet {
req.IPAddr = ipaddr.(string)
}
_, err := c.CloudBroker().Compute().NetAttach(ctx, req)
if err != nil {
apiErrCount++
lastSavedError = err
}
}
if apiErrCount > 0 {
log.Errorf("utilityComputeNetworksConfigure: there were %d error(s) when managing networks of Compute ID %s. Last error was: %s",
apiErrCount, d.Id(), lastSavedError)
return lastSavedError
}
return nil
}
detach_set := old_set.(*schema.Set).Difference(new_set.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: detach set has %d items for Compute ID %s", detach_set.Len(), d.Id())
for _, runner := range detach_set.List() {
net_data := runner.(map[string]interface{})
detachSet := oldSet.(*schema.Set).Difference(newSet.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: detach set has %d items for Compute ID %s", detachSet.Len(), d.Id())
for _, runner := range detachSet.List() {
netData := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetDetachRequest{
ComputeID: computeId,
IPAddr: net_data["ip_address"].(string),
MAC: net_data["mac"].(string),
IPAddr: netData["ip_address"].(string),
MAC: netData["mac"].(string),
}
_, err := c.CloudBroker().Compute().NetDetach(ctx, req)
if err != nil {
log.Errorf("utilityComputeNetworksConfigure: failed to detach net ID %d of type %s from Compute ID %s: %s",
net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err)
netData["net_id"].(int), netData["net_type"].(string), d.Id(), err)
apiErrCount++
lastSavedError = err
}
@@ -647,7 +611,7 @@ func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData
needStart := false
if d.Get("network").(*schema.Set).Len() == 1 || old_set.(*schema.Set).Len() < 1 {
if d.Get("network").(*schema.Set).Len() == 1 || oldSet.(*schema.Set).Len() < 1 {
if err := utilityComputeStop(ctx, d, m); err != nil {
apiErrCount++
lastSavedError = err
@@ -655,25 +619,37 @@ func utilityComputeNetworksConfigure(ctx context.Context, d *schema.ResourceData
needStart = true
}
attach_set := new_set.(*schema.Set).Difference(old_set.(*schema.Set))
log.Debugf("utilityComputeNetworksConfigure: attach set has %d items for Compute ID %s", attach_set.Len(), d.Id())
for _, runner := range attach_set.List() {
net_data := runner.(map[string]interface{})
attachSet := newSet.(*schema.Set).Difference(oldSet.(*schema.Set))
attachList := attachSet.List()
sort.Slice(attachList, func(i, j int) bool {
weightI := attachList[i].(map[string]interface{})["weight"].(int)
weightJ := attachList[j].(map[string]interface{})["weight"].(int)
if weightI == 0 {
return false
}
if weightJ == 0 {
return true
}
return weightI < weightJ
})
log.Debugf("utilityComputeNetworksConfigure: attach set has %d items for Compute ID %s", attachSet.Len(), d.Id())
for _, runner := range attachList {
netData := runner.(map[string]interface{})
computeId, _ := strconv.ParseUint(d.Id(), 10, 64)
req := compute.NetAttachRequest{
ComputeID: computeId,
NetType: net_data["net_type"].(string),
NetID: uint64(net_data["net_id"].(int)),
NetType: netData["net_type"].(string),
NetID: uint64(netData["net_id"].(int)),
}
if net_data["ip_address"].(string) != "" {
req.IPAddr = net_data["ip_address"].(string)
if netData["ip_address"].(string) != "" {
req.IPAddr = netData["ip_address"].(string)
}
_, err := c.CloudBroker().Compute().NetAttach(ctx, req)
if err != nil {
log.Errorf("utilityComputeNetworksConfigure: failed to attach net ID %d of type %s to Compute ID %s: %s",
net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err)
netData["net_id"].(int), netData["net_type"].(string), d.Id(), err)
apiErrCount++
lastSavedError = err
}