You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
6.3 KiB
220 lines
6.3 KiB
package kvmvm
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sort"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
|
log "github.com/sirupsen/logrus"
|
|
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute"
|
|
)
|
|
|
|
func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute) error {
|
|
log.Debugf("flattenCompute: ID %d, RG ID %d", computeRec.ID, computeRec.RGID)
|
|
|
|
customFields, _ := json.Marshal(computeRec.CustomFields)
|
|
devices, _ := json.Marshal(computeRec.Devices)
|
|
|
|
bootDisk := findBootDisk(computeRec.Disks)
|
|
|
|
d.Set("account_id", computeRec.AccountID)
|
|
d.Set("account_name", computeRec.AccountName)
|
|
d.Set("affinity_label", computeRec.AffinityLabel)
|
|
d.Set("affinity_weight", computeRec.AffinityWeight)
|
|
d.Set("affinity_rules", flattenAffinityRules(computeRec.AffinityRules))
|
|
d.Set("anti_affinity_rules", flattenAffinityRules(computeRec.AntiAffinityRules))
|
|
d.Set("arch", computeRec.Arch)
|
|
d.Set("boot_order", computeRec.BootOrder)
|
|
d.Set("boot_disk_size", computeRec.BootDiskSize)
|
|
d.Set("clone_reference", computeRec.CloneReference)
|
|
d.Set("clones", computeRec.Clones)
|
|
d.Set("computeci_id", computeRec.ComputeCIID)
|
|
d.Set("created_by", computeRec.CreatedBy)
|
|
d.Set("created_time", computeRec.CreatedTime)
|
|
d.Set("custom_fields", string(customFields))
|
|
d.Set("deleted_by", computeRec.DeletedBy)
|
|
d.Set("deleted_time", computeRec.DeletedTime)
|
|
d.Set("description", computeRec.Description)
|
|
d.Set("devices", string(devices))
|
|
d.Set("disks",
|
|
flattenComputeDisks(
|
|
computeRec.Disks,
|
|
d.Get("extra_disks").(*schema.Set).List(),
|
|
bootDisk.ID,
|
|
),
|
|
)
|
|
d.Set("gid", computeRec.GID)
|
|
d.Set("guid", computeRec.GUID)
|
|
d.Set("compute_id", computeRec.ID)
|
|
d.Set("image_id", computeRec.ImageID)
|
|
d.Set("interfaces", flattenInterfaces(computeRec.Interfaces))
|
|
d.Set("lock_status", computeRec.LockStatus)
|
|
d.Set("manager_id", computeRec.ManagerID)
|
|
d.Set("manager_type", computeRec.ManagerType)
|
|
d.Set("migrationjob", computeRec.MigrationJob)
|
|
d.Set("milestones", computeRec.Milestones)
|
|
d.Set("os_users", flattenOSUsers(computeRec.OSUsers))
|
|
d.Set("pinned", computeRec.Pinned)
|
|
d.Set("reference_id", computeRec.ReferenceID)
|
|
d.Set("registered", computeRec.Registered)
|
|
d.Set("res_name", computeRec.ResName)
|
|
d.Set("rg_name", computeRec.RGName)
|
|
d.Set("snap_sets", flattenSnapSets(computeRec.SnapSets))
|
|
d.Set("stack_id", computeRec.StackID)
|
|
d.Set("stack_name", computeRec.StackName)
|
|
d.Set("stateless_sep_id", computeRec.StatelessSEPID)
|
|
d.Set("stateless_sep_type", computeRec.StatelessSEPType)
|
|
d.Set("status", computeRec.Status)
|
|
d.Set("tags", flattenTags(computeRec.Tags))
|
|
d.Set("tech_status", computeRec.TechStatus)
|
|
d.Set("updated_by", computeRec.UpdatedBy)
|
|
d.Set("updated_time", computeRec.UpdatedTime)
|
|
d.Set("user_managed", computeRec.UserManaged)
|
|
d.Set("vgpus", computeRec.VGPUs)
|
|
d.Set("virtual_image_id", computeRec.VirtualImageID)
|
|
|
|
return nil
|
|
}
|
|
|
|
func flattenTags(tags map[string]interface{}) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(tags))
|
|
|
|
for k, v := range tags {
|
|
res = append(res, map[string]interface{}{
|
|
"key": k,
|
|
"val": v,
|
|
})
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenSnapSets(snaps compute.ListSnapshots) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(snaps))
|
|
|
|
for _, snap := range snaps {
|
|
res = append(res, map[string]interface{}{
|
|
"disks": snap.Disks,
|
|
"guid": snap.GUID,
|
|
"label": snap.Label,
|
|
"timestamp": snap.Timestamp,
|
|
})
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenOSUsers(users compute.ListOSUsers) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(users))
|
|
|
|
for _, user := range users {
|
|
res = append(res, map[string]interface{}{
|
|
"guid": user.GUID,
|
|
"login": user.Login,
|
|
"password": user.Password,
|
|
"public_key": user.PubKey,
|
|
})
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenInterfaces(ifaces compute.ListInterfaces) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(ifaces))
|
|
|
|
for _, iface := range ifaces {
|
|
res = append(res, map[string]interface{}{
|
|
"conn_id": iface.ConnID,
|
|
"conn_type": iface.ConnType,
|
|
"def_gw": iface.DefGW,
|
|
"flip_group_id": iface.FLIPGroupID,
|
|
"guid": iface.GUID,
|
|
"ip_address": iface.IPAddress,
|
|
"listen_ssh": iface.ListenSSH,
|
|
"mac": iface.MAC,
|
|
"name": iface.Name,
|
|
"net_id": iface.NetID,
|
|
"netmask": iface.NetMask,
|
|
"net_type": iface.NetType,
|
|
"pci_slot": iface.PCISlot,
|
|
"qos": flattenQOS(iface.QOS),
|
|
"target": iface.Target,
|
|
"type": iface.Type,
|
|
"vnfs": iface.VNFs,
|
|
})
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func flattenQOS(qos compute.QOS) []map[string]interface{} {
|
|
return []map[string]interface{}{
|
|
{
|
|
"e_rate": qos.ERate,
|
|
"guid": qos.GUID,
|
|
"in_brust": qos.InBurst,
|
|
"in_rate": qos.InRate,
|
|
},
|
|
}
|
|
}
|
|
|
|
func flattenComputeDisks(disksList compute.ListDisks, extraDisks []interface{}, bootDiskId uint64) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(disksList))
|
|
for _, disk := range disksList {
|
|
if disk.ID == bootDiskId || findInExtraDisks(uint(disk.ID), extraDisks) { //skip main bootdisk and extraDisks
|
|
continue
|
|
}
|
|
temp := map[string]interface{}{
|
|
"disk_name": disk.Name,
|
|
"size": disk.SizeMax,
|
|
"sep_id": disk.SEPID,
|
|
"disk_type": disk.Type,
|
|
"pool": disk.Pool,
|
|
"desc": disk.Description,
|
|
"image_id": disk.ImageID,
|
|
"disk_id": disk.ID,
|
|
"shareable": disk.Shareable,
|
|
"size_used": disk.SizeUsed,
|
|
}
|
|
res = append(res, temp)
|
|
}
|
|
sort.Slice(res, func(i, j int) bool {
|
|
return res[i]["disk_id"].(uint64) < res[j]["disk_id"].(uint64)
|
|
})
|
|
return res
|
|
}
|
|
|
|
func findInExtraDisks(diskId uint, extraDisks []interface{}) bool {
|
|
for _, ExtraDisk := range extraDisks {
|
|
if diskId == uint(ExtraDisk.(int)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func findBootDisk(disks compute.ListDisks) *compute.ItemDisk {
|
|
for _, disk := range disks {
|
|
if disk.Type == "B" {
|
|
return &disk
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func flattenAffinityRules(rules compute.ListRules) []map[string]interface{} {
|
|
res := make([]map[string]interface{}, 0, len(rules))
|
|
|
|
for _, rule := range rules {
|
|
res = append(res, map[string]interface{}{
|
|
"topology": rule.Topology,
|
|
"policy": rule.Policy,
|
|
"mode": rule.Mode,
|
|
"key": rule.Key,
|
|
"value": rule.Value,
|
|
})
|
|
}
|
|
|
|
return res
|
|
}
|