527 lines
15 KiB
Go
527 lines
15 KiB
Go
/*
|
|
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
|
|
Authors:
|
|
Petr Krutov, <petr.krutov@digitalenergy.online>
|
|
Stanislav Solovev, <spsolovev@digitalenergy.online>
|
|
Sergey Kisil, <svkisil@digitalenergy.online>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
/*
|
|
Terraform DECORT provider - manage resources provided by DECORT (Digital Energy Cloud
|
|
Orchestration Technology) with Terraform by Hashicorp.
|
|
|
|
Source code: https://repository.basistech.ru/BASIS/terraform-provider-decort
|
|
|
|
Please see README.md to learn where to place source code so that it
|
|
builds seamlessly.
|
|
|
|
Documentation: https://repository.basistech.ru/BASIS/terraform-provider-decort/wiki
|
|
*/
|
|
|
|
package node
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
log "github.com/sirupsen/logrus"
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/node"
|
|
)
|
|
|
|
func flattenNode(d *schema.ResourceData, item *node.RecordNode) {
|
|
log.Debugf("flattenNode: decoded node id %d", d.Get("node_id").(int))
|
|
|
|
d.Set("auto_start", item.AutoStart)
|
|
d.Set("auto_start_count", item.AutoStartCount)
|
|
d.Set("consumption", flattenConsumption(item.Consumption))
|
|
d.Set("cpu_info", flattenCpuInfo(item.CpuInfo))
|
|
d.Set("cpu_allocation_ratio", item.CPUAllocationRatio)
|
|
d.Set("mem_allocation_ratio", item.MemAllocationRatio)
|
|
d.Set("description", item.Description)
|
|
d.Set("dpdk", flattenDPDKItem(item.DPDK))
|
|
d.Set("gid", item.GID)
|
|
d.Set("ipaddr", item.IPAddr)
|
|
d.Set("isolated_cpus", flattenNodeItem(item.IsolatedCpus))
|
|
d.Set("name", item.Name)
|
|
d.Set("need_reboot", item.NeedReboot)
|
|
d.Set("net_addr", flattenGetNetAddr(item.NetAddr))
|
|
d.Set("network_mode", item.NetworkMode)
|
|
d.Set("openvswitch_bridges", item.OpenvSwitchBridges)
|
|
d.Set("nic_info", flattenNicInfo(item.NicInfo))
|
|
d.Set("numa_topology", flattenNumaTopology(item.NumaTopology))
|
|
d.Set("reserved_cpus", flattenNodeItem(item.ReservedCPUs))
|
|
d.Set("roles", item.Roles)
|
|
d.Set("sdn_hypervisor_name", item.SDNHypervisorName)
|
|
d.Set("sriov_enabled", item.SriovEnabled)
|
|
d.Set("node_id", item.ID)
|
|
d.Set("status", item.Status)
|
|
d.Set("to_active", flattenRole(item.ToActive))
|
|
d.Set("to_installing", flattenRole(item.ToInstalling))
|
|
d.Set("to_maintenance", flattenRole(item.ToMaintenance))
|
|
d.Set("to_restricted", flattenRole(item.ToRestricted))
|
|
d.Set("version", item.Version)
|
|
d.Set("zone_id", item.ZoneID)
|
|
d.Set("usable_cpus", item.UsableCPUs)
|
|
}
|
|
|
|
func flattenConsumption(info node.ConsumptionInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
tempRes := map[string]interface{}{
|
|
"hostname": info.Hostname,
|
|
}
|
|
|
|
tempConsumed := map[string]interface{}{
|
|
"ram": info.Consumed.RAM,
|
|
"computes": info.Consumed.Computes,
|
|
"routers": info.Consumed.Routers,
|
|
"vcpu": info.Consumed.VCPU,
|
|
}
|
|
tempRes["consumed"] = []map[string]interface{}{
|
|
tempConsumed,
|
|
}
|
|
|
|
tempFree := map[string]interface{}{
|
|
"ram": info.Free.RAM,
|
|
"vcpu": info.Free.VCPU,
|
|
}
|
|
tempRes["free"] = []map[string]interface{}{
|
|
tempFree,
|
|
}
|
|
|
|
tempReserved := map[string]interface{}{
|
|
"ram": info.Reserved.RAM,
|
|
}
|
|
tempRes["reserved"] = []map[string]interface{}{
|
|
tempReserved,
|
|
}
|
|
|
|
tempTotal := map[string]interface{}{
|
|
"ram": info.Total.RAM,
|
|
}
|
|
tempRes["total"] = []map[string]interface{}{
|
|
tempTotal,
|
|
}
|
|
|
|
res[0] = tempRes
|
|
return res
|
|
}
|
|
|
|
func flattenNodeList(nodes *node.ListNodes) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(nodes.Data))
|
|
for _, item := range nodes.Data {
|
|
temp := map[string]interface{}{
|
|
"additional_pkgs": flattenNodeItem(item.AdditionalPkgs),
|
|
"auto_start": item.AutoStart,
|
|
"auto_start_count": item.AutoStartCount,
|
|
"cpu_info": flattenCpuInfo(item.CpuInfo),
|
|
"description": item.Description,
|
|
"dpdk": flattenDPDKItem(item.DPDK),
|
|
"gid": item.GID,
|
|
"guid": item.GUID,
|
|
"hostkey": item.HostKey,
|
|
"node_id": item.ID,
|
|
"ipaddr": item.IPAddr,
|
|
"isolated_cpus": flattenNodeItem(item.IsolatedCpus),
|
|
"lastcheck": item.LastCheck,
|
|
"machine_guid": item.MachineGUID,
|
|
"mainboard_sn": item.MainboardSN,
|
|
"memory": item.Memory,
|
|
"milestones": item.Milestones,
|
|
"model": item.Model,
|
|
"name": item.Name,
|
|
"need_reboot": item.NeedReboot,
|
|
"net_addr": flattenNetAddr(item.NetAddr),
|
|
"network_mode": item.NetworkMode,
|
|
"nic_info": flattenNicInfo(item.NicInfo),
|
|
"node_uuid": item.NodeUUID,
|
|
"numa_topology": flattenNumaTopology(item.NumaTopology),
|
|
"peer_backup": item.PeerBackup,
|
|
"peer_log": item.PeerLog,
|
|
"peer_stats": item.PeerStats,
|
|
"pgpus": item.Pgpus,
|
|
"public_keys": item.PublicKeys,
|
|
"release": item.Release,
|
|
"reserved_cpus": flattenNodeItem(item.ReservedCPUs),
|
|
"roles": item.Roles,
|
|
"sdn_hypervisor_name": item.SDNHypervisorName,
|
|
"seps": item.Seps,
|
|
"serial_num": item.SerialNum,
|
|
"sriov_enabled": item.SriovEnabled,
|
|
"status": item.Status,
|
|
"tags": item.Tags,
|
|
"type": item.Type,
|
|
"uefi_firmware_file": item.UEFIFirmwareFile,
|
|
"usable_cpus": item.UsableCPUs,
|
|
"version": item.Version,
|
|
"zone_id": item.ZoneID,
|
|
"openvswitch_bridges": item.OpenvSwitchBridges,
|
|
"api_url": item.APIUrl,
|
|
"drivers": item.Drivers,
|
|
"old_compat_lvm_id": item.OldCompatLVMID,
|
|
"cpu_allocation_ratio": item.CPUAllocationRatio,
|
|
"mem_allocation_ratio": item.MemAllocationRatio,
|
|
"packages": flattenPackages(item.Packages),
|
|
"pci_devices": flattenNodePCIDevices(item.PCIDevices),
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenNumaTopology(info node.NumaTopologyInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
tempRes := map[string]interface{}{
|
|
"node_num": info.NodeNum,
|
|
}
|
|
resNodes := make([]map[string]interface{}, 0, len(info.Nodes))
|
|
for _, item := range info.Nodes {
|
|
memoryTemp := []map[string]interface{}{
|
|
{
|
|
"one_g": item.Memory.OneG,
|
|
"one_g_free": item.Memory.OneGFree,
|
|
"one_g_reserved": item.Memory.OneGReserved,
|
|
"one_g_available": item.Memory.OneGAvailable,
|
|
"one_g_used": item.Memory.OneGUsed,
|
|
"one_g_dpdk_reserved": item.Memory.OneGDPDKReserved,
|
|
"two_m": item.Memory.TwoM,
|
|
"two_m_free": item.Memory.TwoMFree,
|
|
"two_m_reserved": item.Memory.TwoMReserved,
|
|
"two_m_available": item.Memory.TwoMAvailable,
|
|
"two_m_used": item.Memory.TwoMUsed,
|
|
"total": item.Memory.Total,
|
|
},
|
|
}
|
|
temp := map[string]interface{}{
|
|
"cpu_list": item.CPUList,
|
|
"memory": memoryTemp,
|
|
}
|
|
resNodes = append(resNodes, temp)
|
|
}
|
|
tempRes["nodes"] = resNodes
|
|
res[0] = tempRes
|
|
return res
|
|
}
|
|
|
|
func flattenNicInfo(infos node.ListNicInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(infos))
|
|
for _, item := range infos {
|
|
temp := map[string]interface{}{
|
|
"driver": item.Driver,
|
|
"max_vfs": item.MaxVFS,
|
|
"numa_node": item.NumaNode,
|
|
"num_vfs": item.NumVFS,
|
|
"os_name": item.OSName,
|
|
"pci_slot": item.PCISlot,
|
|
"vf_list": flattenVFList(item.VFList),
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenVFList(vfList []interface{}) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(vfList))
|
|
for _, v := range vfList {
|
|
vConv := v.(map[string]interface{})
|
|
temp := map[string]interface{}{
|
|
"fn_id": vConv["fnId"],
|
|
"pci_slot": vConv["pciSlot"],
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenNetAddr(addresses node.ListNetAddr) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(addresses))
|
|
for _, item := range addresses {
|
|
temp := map[string]interface{}{
|
|
"cidr": item.CIDR,
|
|
"index": item.Index,
|
|
"ip": item.IP,
|
|
"mac": item.Mac,
|
|
"mtu": item.MTU,
|
|
"name": item.Name,
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenGetNetAddr(address node.NetAddr) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
temp := map[string]interface{}{
|
|
"ip": address.IP,
|
|
"name": address.Name,
|
|
}
|
|
res[0] = temp
|
|
return res
|
|
}
|
|
|
|
func flattenCpuInfo(info node.CpuInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
temp := map[string]interface{}{
|
|
"clock_speed": info.ClockSpeed,
|
|
"core_count": info.CoreCount,
|
|
"phys_count": info.PhysCount,
|
|
"flags": info.Flags,
|
|
"model_name": info.ModelName,
|
|
"thread_count": info.ThreadCount,
|
|
}
|
|
res[0] = temp
|
|
return res
|
|
}
|
|
|
|
func flattenNodeItem(m []interface{}) []string {
|
|
output := []string{}
|
|
for _, item := range m {
|
|
switch d := item.(type) {
|
|
case string:
|
|
output = append(output, d)
|
|
case int:
|
|
output = append(output, strconv.Itoa(d))
|
|
case int64:
|
|
output = append(output, strconv.FormatInt(d, 10))
|
|
case float64:
|
|
output = append(output, strconv.FormatInt(int64(d), 10))
|
|
default:
|
|
output = append(output, "")
|
|
}
|
|
}
|
|
return output
|
|
}
|
|
|
|
func flattenPackages(pkgs map[string]node.PackageInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(pkgs))
|
|
|
|
for _, p := range pkgs {
|
|
temp := map[string]interface{}{
|
|
"ver": p.Ver,
|
|
"size": p.InstalledSize,
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenDPDKItem(dpdk node.DPDK) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
bridges := make([]map[string]interface{}, 1)
|
|
backplane := make([]map[string]interface{}, 1)
|
|
backplane[0] = map[string]interface{}{
|
|
"interfaces": dpdk.Bridges.Backplane1.Interfaces,
|
|
"numa_node": dpdk.Bridges.Backplane1.NumaNode,
|
|
}
|
|
|
|
bridges[0] = map[string]interface{}{
|
|
"backplane1": backplane,
|
|
}
|
|
|
|
res[0] = map[string]interface{}{
|
|
"bridges": bridges,
|
|
"hp_memory": dpdk.HPMemory,
|
|
"pmd_cpu": dpdk.PMDCPU,
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenRole(role node.Role) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 1)
|
|
temp := map[string]interface{}{
|
|
"actor": role.Actor,
|
|
"reason": role.Reason,
|
|
"time": role.Time,
|
|
}
|
|
res[0] = temp
|
|
return res
|
|
}
|
|
|
|
func flattenSystemNetworksInfo(m map[string]node.SystemNetworkInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(m))
|
|
for name, info := range m {
|
|
res = append(res, map[string]interface{}{
|
|
"name": name,
|
|
"mtu": info.MTU,
|
|
"speed": info.Speed,
|
|
"bridge_id": info.BridgeID,
|
|
"bport_id": info.BPortID,
|
|
"mac": info.MAC,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenOVSNetworksInfo(items []node.OVSNetworkInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(items))
|
|
for _, item := range items {
|
|
res = append(res, map[string]interface{}{
|
|
"bridge_name": item.BridgeName,
|
|
"bridge_tag": item.BridgeTag,
|
|
"interface_uuid": item.InterfaceUUID,
|
|
"interface_type": item.InterfaceType,
|
|
"interface_name": item.InterfaceName,
|
|
"interface_mtu": item.InterfaceMTU,
|
|
"interface_ip": item.InterfaceIP,
|
|
"interface_mac": item.InterfaceMAC,
|
|
"interface_peer": item.InterfacePeer,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenLibvirtNetworksInfo(items []node.LibvirtNetworkInfo) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(items))
|
|
for _, item := range items {
|
|
res = append(res, map[string]interface{}{
|
|
"vm_name": item.VMName,
|
|
"interface": item.Interface,
|
|
"interface_type": item.InterfaceType,
|
|
"interface_source": item.InterfaceSource,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenNetworkTopology(t node.NetworkTopology) []map[string]interface{} {
|
|
return []map[string]interface{}{
|
|
{
|
|
"interfaces": flattenTopologyInterfaces(t.Interfaces),
|
|
},
|
|
}
|
|
}
|
|
|
|
func flattenTopologyInterfaces(m map[string]node.TopologyInterface) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(m))
|
|
for name, iface := range m {
|
|
res = append(res, map[string]interface{}{
|
|
"name": name,
|
|
"type": iface.Type,
|
|
"mtu": iface.MTU,
|
|
"speed": iface.Speed,
|
|
"vlans": iface.VLANs,
|
|
"bridge_id": iface.BridgeID,
|
|
"peer": iface.Peer,
|
|
"uuid": iface.UUID,
|
|
"connected_interfaces": iface.ConnectedInterfaces,
|
|
"connections": flattenTopologyConnections(iface.Connections),
|
|
"bridge_info": flattenTopologyBridgeInfo(iface.BridgeInfo),
|
|
"bridge_connections": flattenTopologyBridgeRefs(iface.BridgeConnections),
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenTopologyConnections(c node.TopologyInterfaceConnections) []map[string]interface{} {
|
|
vms := make([]map[string]interface{}, 0, len(c.VMs))
|
|
for _, vm := range c.VMs {
|
|
vms = append(vms, map[string]interface{}{
|
|
"name": vm.Name,
|
|
"vm_interface": vm.VMInterface,
|
|
"vm_interface_type": vm.VMInterfaceType,
|
|
"connection_type": vm.ConnectionType,
|
|
"via_bridge": vm.ViaBridge,
|
|
})
|
|
}
|
|
|
|
bridges := make([]map[string]interface{}, 0, len(c.Bridges))
|
|
for _, b := range c.Bridges {
|
|
portInfo := flattenTopologyPortInfo(b.PortInfo)
|
|
bridges = append(bridges, map[string]interface{}{
|
|
"name": b.Name,
|
|
"type": b.Type,
|
|
"via": b.Via,
|
|
"bport_id": b.BPortID,
|
|
"port_info": portInfo,
|
|
})
|
|
}
|
|
|
|
return []map[string]interface{}{
|
|
{
|
|
"vms": vms,
|
|
"bridges": bridges,
|
|
},
|
|
}
|
|
}
|
|
|
|
func flattenTopologyPortInfo(p *node.TopologyPortInfo) []map[string]interface{} {
|
|
if p == nil {
|
|
return nil
|
|
}
|
|
return []map[string]interface{}{
|
|
{
|
|
"name": p.Name,
|
|
"type": p.Type,
|
|
"mtu": p.MTU,
|
|
"vlan": p.VLAN,
|
|
"uuid": p.UUID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func flattenTopologyBridgeInfo(b *node.TopologyBridgeInfo) []map[string]interface{} {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
ports := make([]map[string]interface{}, 0, len(b.Ports))
|
|
for _, p := range b.Ports {
|
|
ports = append(ports, map[string]interface{}{
|
|
"name": p.Name,
|
|
"type": p.Type,
|
|
"mtu": p.MTU,
|
|
"vlan": p.VLAN,
|
|
"uuid": p.UUID,
|
|
"ip": p.IP,
|
|
"mac": p.MAC,
|
|
})
|
|
}
|
|
return []map[string]interface{}{
|
|
{
|
|
"type": b.Type,
|
|
"ports": ports,
|
|
},
|
|
}
|
|
}
|
|
|
|
func flattenTopologyBridgeRefs(refs []node.TopologyBridgeRef) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(refs))
|
|
for _, r := range refs {
|
|
res = append(res, map[string]interface{}{
|
|
"name": r.Name,
|
|
"via": r.Via,
|
|
"vlan": r.VLAN,
|
|
})
|
|
}
|
|
return res
|
|
}
|
|
|
|
func flattenNodePCIDevices(items []node.ItemPCIDevice) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(items))
|
|
for _, item := range items {
|
|
res = append(res, map[string]interface{}{
|
|
"hw_path": item.HWPath,
|
|
"current_driver": item.CurrentDriver,
|
|
"numa_node": item.NUMANode,
|
|
"product_id": item.ProductID,
|
|
"product_name": item.ProductName,
|
|
"vendor_id": item.VendorID,
|
|
"vendor_name": item.VendorName,
|
|
"iommu_group": item.IOMMUGroup,
|
|
})
|
|
}
|
|
return res
|
|
}
|