Files
terraform-provider-decort/internal/service/cloudapi/kvmvm/flattens.go

870 lines
30 KiB
Go
Raw Normal View History

/*
2023-10-13 13:28:19 +03:00
Copyright (c) 2019-2023 Digital Energy Cloud Solutions LLC. All Rights Reserved.
Authors:
Petr Krutov, <petr.krutov@digitalenergy.online>
Stanislav Solovev, <spsolovev@digitalenergy.online>
Kasim Baybikov, <kmbaybikov@basistech.ru>
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
*/
2023-01-24 17:50:38 +03:00
package kvmvm
import (
"encoding/json"
2023-03-10 12:42:15 +03:00
"sort"
"strconv"
2023-01-24 17:50:38 +03:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
2023-05-04 10:08:25 +03:00
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/status"
2023-01-24 17:50:38 +03:00
)
2023-05-04 10:08:25 +03:00
func flattenDisks(disks []compute.InfoDisk) []map[string]interface{} {
2023-01-24 17:50:38 +03:00
res := make([]map[string]interface{}, 0)
2023-03-10 12:42:15 +03:00
for _, disk := range disks {
temp := map[string]interface{}{
2025-05-21 16:38:25 +03:00
// "bus_number": disk.BusNumber,
"disk_id": disk.ID,
// "pci_slot": disk.PCISlot,
2023-03-10 12:42:15 +03:00
}
res = append(res, temp)
}
return res
}
2023-05-04 10:08:25 +03:00
func flattenQOS(qos compute.QOS) []map[string]interface{} {
2023-03-10 12:42:15 +03:00
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
"e_rate": qos.ERate,
"guid": qos.GUID,
"in_brust": qos.InBurst,
"in_rate": qos.InRate,
}
res = append(res, temp)
return res
}
2023-05-04 10:08:25 +03:00
func flattenInterfaces(interfaces compute.ListInterfaces) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(interfaces))
2023-03-10 12:42:15 +03:00
for _, interfaceItem := range interfaces {
temp := map[string]interface{}{
2024-11-12 13:41:38 +03:00
"bus_number": interfaceItem.BusNumber,
"conn_id": interfaceItem.ConnID,
"conn_type": interfaceItem.ConnType,
"def_gw": interfaceItem.DefGW,
"enabled": interfaceItem.Enabled,
"flip_group_id": interfaceItem.FLIPGroupID,
"guid": interfaceItem.GUID,
"ip_address": interfaceItem.IPAddress,
"listen_ssh": interfaceItem.ListenSSH,
"mac": interfaceItem.MAC,
"mtu": interfaceItem.MTU,
"name": interfaceItem.Name,
"net_id": interfaceItem.NetID,
"netmask": interfaceItem.NetMask,
"net_type": interfaceItem.NetType,
"node_id": interfaceItem.NodeID,
"pci_slot": interfaceItem.PCISlot,
"qos": flattenQOS(interfaceItem.QOS),
"target": interfaceItem.Target,
"type": interfaceItem.Type,
"vnfs": interfaceItem.VNFs,
"libvirt_settings": flattenLibvirtSettings(interfaceItem.LibvirtSettings),
2023-03-10 12:42:15 +03:00
}
res = append(res, temp)
}
return res
}
2024-11-12 13:41:38 +03:00
func flattenLibvirtSettings(libvirtSettings compute.LibvirtSettings) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
"guid": libvirtSettings.GUID,
"txmode": libvirtSettings.TXMode,
"ioeventfd": libvirtSettings.IOEventFD,
"event_idx": libvirtSettings.EventIDx,
"queues": libvirtSettings.Queues,
"rx_queue_size": libvirtSettings.RXQueueSize,
"tx_queue_size": libvirtSettings.TXQueueSize,
}
res = append(res, temp)
return res
}
2023-05-04 10:08:25 +03:00
func flattenSnapSets(snapSets compute.ListSnapSets) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(snapSets))
2023-03-10 12:42:15 +03:00
for _, snapSet := range snapSets {
temp := map[string]interface{}{
"disks": snapSet.Disks,
"guid": snapSet.GUID,
"label": snapSet.Label,
"timestamp": snapSet.Timestamp,
}
res = append(res, temp)
}
return res
}
func flattenTags(tags map[string]string) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(tags))
2023-03-10 12:42:15 +03:00
for key, val := range tags {
temp := map[string]interface{}{
"key": key,
"val": val,
}
res = append(res, temp)
}
return res
}
2023-05-04 10:08:25 +03:00
func flattenListRules(listRules compute.ListRules) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(listRules))
2023-03-10 12:42:15 +03:00
for _, rule := range listRules {
temp := map[string]interface{}{
"guid": rule.GUID,
"key": rule.Key,
"mode": rule.Mode,
"policy": rule.Policy,
"topology": rule.Topology,
"value": rule.Value,
}
res = append(res, temp)
}
return res
}
2023-05-04 10:08:25 +03:00
func flattenListACL(listAcl compute.ListACL) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(listAcl))
2023-03-10 12:42:15 +03:00
for _, acl := range listAcl {
temp := map[string]interface{}{
2023-05-04 10:08:25 +03:00
"explicit": acl.Explicit,
2023-03-10 12:42:15 +03:00
"guid": acl.GUID,
"right": acl.Right,
"status": acl.Status,
"type": acl.Type,
"user_group_id": acl.UserGroupID,
}
res = append(res, temp)
}
return res
}
2023-05-04 10:08:25 +03:00
2023-07-26 13:32:39 +03:00
func flattenComputeList(computes *compute.ListComputes) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(computes.Data))
2023-07-26 13:32:39 +03:00
for _, compute := range computes.Data {
2023-08-28 13:02:41 +03:00
customFields, _ := json.Marshal(compute.CustomFields)
2023-03-10 12:42:15 +03:00
devices, _ := json.Marshal(compute.Devices)
temp := map[string]interface{}{
"acl": flattenListACL(compute.ACL),
"account_id": compute.AccountID,
"account_name": compute.AccountName,
"affinity_label": compute.AffinityLabel,
"affinity_rules": flattenListRules(compute.AffinityRules),
"affinity_weight": compute.AffinityWeight,
"anti_affinity_rules": flattenListRules(compute.AntiAffinityRules),
"arch": compute.Architecture,
2024-12-27 12:00:59 +03:00
"auto_start_w_node": compute.AutoStart,
2023-03-10 12:42:15 +03:00
"boot_order": compute.BootOrder,
2024-05-31 14:05:21 +03:00
"bootdisk_size": compute.BootDiskSize,
2024-11-12 13:41:38 +03:00
"chipset": compute.Chipset,
2024-05-31 14:05:21 +03:00
"cd_image_id": compute.CdImageId,
2023-03-10 12:42:15 +03:00
"clone_reference": compute.CloneReference,
"clones": compute.Clones,
"computeci_id": compute.ComputeCIID,
2024-05-31 14:05:21 +03:00
"cpu_pin": compute.CPUPin,
2023-03-10 12:42:15 +03:00
"cpus": compute.CPU,
"created_by": compute.CreatedBy,
"created_time": compute.CreatedTime,
2023-08-28 13:02:41 +03:00
"custom_fields": string(customFields),
"deleted_by": compute.DeletedBy,
"deleted_time": compute.DeletedTime,
"desc": compute.Description,
"devices": string(devices),
"disks": flattenDisks(compute.Disks),
"driver": compute.Driver,
"gid": compute.GID,
"guid": compute.GUID,
2024-05-31 14:05:21 +03:00
"hp_backed": compute.HPBacked,
2023-08-28 13:02:41 +03:00
"compute_id": compute.ID,
"image_id": compute.ImageID,
"interfaces": flattenInterfaces(compute.Interfaces),
"lock_status": compute.LockStatus,
"manager_id": compute.ManagerID,
"manager_type": compute.ManagerType,
"migrationjob": compute.MigrationJob,
"milestones": compute.Milestones,
"name": compute.Name,
2024-03-26 12:17:33 +03:00
"need_reboot": compute.NeedReboot,
2024-05-31 14:05:21 +03:00
"numa_affinity": compute.NumaAffinity,
"numa_node_id": compute.NumaNodeId,
2025-05-21 16:38:25 +03:00
// "pinned": compute.Pinned,
"preferred_cpu": compute.PreferredCPU,
"ram": compute.RAM,
"reference_id": compute.ReferenceID,
"registered": compute.Registered,
"res_name": compute.ResName,
"reserved_node_cpus": compute.ReservedNodeCpus,
"rg_id": compute.RGID,
"rg_name": compute.RGName,
"snap_sets": flattenSnapSets(compute.SnapSets),
"stateless_sep_id": compute.StatelessSepID,
"stateless_sep_type": compute.StatelessSepType,
"status": compute.Status,
"tags": flattenTags(compute.Tags),
"tech_status": compute.TechStatus,
"total_disk_size": compute.TotalDiskSize,
"updated_by": compute.UpdatedBy,
"updated_time": compute.UpdatedTime,
"user_managed": compute.UserManaged,
"vgpus": compute.VGPUs,
"vins_connected": compute.VINSConnected,
"virtual_image_id": compute.VirtualImageID,
"loader_type": compute.LoaderType,
"boot_type": compute.BootType,
"hot_resize": compute.HotResize,
"network_interface_naming": compute.NetworkInterfaceNaming,
2023-03-10 12:42:15 +03:00
}
res = append(res, temp)
}
return res
}
2023-05-04 10:08:25 +03:00
func flattenBootDisk(bootDisk *compute.ItemComputeDisk) []map[string]interface{} {
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
2025-05-21 16:38:25 +03:00
"disk_name": bootDisk.Name,
"disk_id": bootDisk.ID,
"disk_type": bootDisk.Type,
"sep_id": bootDisk.SepID,
"shareable": bootDisk.Shareable,
"size_max": bootDisk.SizeMax,
"size_used": bootDisk.SizeUsed,
"pool": bootDisk.Pool,
"desc": bootDisk.Description,
"image_id": bootDisk.ImageID,
"size": bootDisk.SizeMax,
"present_to": bootDisk.PresentTo,
2023-05-04 10:08:25 +03:00
}
res = append(res, temp)
return res
}
2024-05-31 14:05:21 +03:00
func flattenComputeDisksDemo(disksList compute.ListComputeDisks, disksBlocks, extraDisks []interface{}, bootDiskId uint64) []map[string]interface{} {
2023-03-10 12:42:15 +03:00
res := make([]map[string]interface{}, 0, len(disksList))
2024-05-31 14:05:21 +03:00
if len(disksBlocks) == 0 {
return res
}
sort.Slice(disksList, func(i, j int) bool {
return disksList[i].ID < disksList[j].ID
})
indexDataDisks := 0
2023-01-24 17:50:38 +03:00
for _, disk := range disksList {
2023-08-22 13:15:17 +03:00
if disk.ID == bootDiskId || findInExtraDisks(uint(disk.ID), extraDisks) { //skip main bootdisk and extraDisks
2023-01-24 17:50:38 +03:00
continue
}
2024-03-26 12:17:33 +03:00
2024-05-31 14:05:21 +03:00
pernamentlyValue := disksBlocks[indexDataDisks].(map[string]interface{})["permanently"].(bool)
2024-03-26 12:17:33 +03:00
2023-01-24 17:50:38 +03:00
temp := map[string]interface{}{
2024-03-26 12:17:33 +03:00
"disk_name": disk.Name,
"disk_id": disk.ID,
"disk_type": disk.Type,
"sep_id": disk.SepID,
"shareable": disk.Shareable,
"size_max": disk.SizeMax,
"size_used": disk.SizeUsed,
"pool": disk.Pool,
"desc": disk.Description,
"image_id": disk.ImageID,
"size": disk.SizeMax,
2024-05-31 14:05:21 +03:00
"permanently": pernamentlyValue,
2025-05-21 16:38:25 +03:00
"present_to": disk.PresentTo,
2023-01-24 17:50:38 +03:00
}
res = append(res, temp)
2024-05-31 14:05:21 +03:00
indexDataDisks++
2023-01-24 17:50:38 +03:00
}
2023-08-29 16:26:37 +03:00
2023-01-24 17:50:38 +03:00
return res
}
2024-10-08 12:20:56 +03:00
func flattenNetwork(networks []interface{}, interfaces compute.ListInterfaces) []map[string]interface{} {
2023-03-10 12:42:15 +03:00
res := make([]map[string]interface{}, 0, len(interfaces))
2023-07-26 13:32:39 +03:00
2023-03-10 12:42:15 +03:00
for _, network := range interfaces {
temp := map[string]interface{}{
"net_id": network.NetID,
"net_type": network.NetType,
"ip_address": network.IPAddress,
"mac": network.MAC,
2024-12-04 12:13:55 +03:00
"mtu": network.MTU,
2024-10-08 12:20:56 +03:00
"weight": flattenNetworkWeight(networks, network.NetID, network.NetType),
2023-03-10 12:42:15 +03:00
}
res = append(res, temp)
}
return res
}
2024-10-08 12:20:56 +03:00
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
}
2023-05-04 10:08:25 +03:00
func findBootDisk(disks compute.ListComputeDisks) *compute.ItemComputeDisk {
2023-03-10 12:42:15 +03:00
for _, disk := range disks {
2023-08-22 13:15:17 +03:00
if disk.Type == "B" {
2023-03-10 12:42:15 +03:00
return &disk
}
}
2024-05-31 14:05:21 +03:00
return &compute.ItemComputeDisk{}
2023-03-10 12:42:15 +03:00
}
2024-09-25 13:43:26 +03:00
func flattenCompute(d *schema.ResourceData, computeRec compute.RecordCompute, pciList *compute.ListPCIDevices) error {
2023-01-24 17:50:38 +03:00
// This function expects that compFacts string contains response from API compute/get,
// i.e. detailed information about compute instance.
//
// NOTE: this function modifies ResourceData argument - as such it should never be called
// from resourceComputeExists(...) method
2023-05-04 10:08:25 +03:00
log.Debugf("flattenCompute: ID %d, RG ID %d", computeRec.ID, computeRec.RGID)
2023-03-10 12:42:15 +03:00
2023-05-04 10:08:25 +03:00
devices, _ := json.Marshal(computeRec.Devices)
bootDisk := findBootDisk(computeRec.Disks)
2023-03-10 12:42:15 +03:00
//check extraDisks, ipa_type, is,
2023-05-04 10:08:25 +03:00
d.SetId(strconv.FormatUint(computeRec.ID, 10))
2023-08-29 16:26:37 +03:00
// d.Set("acl", flattenACL(computeRec.ACL))
2023-05-04 10:08:25 +03:00
d.Set("account_id", computeRec.AccountID)
d.Set("account_name", computeRec.AccountName)
d.Set("affinity_weight", computeRec.AffinityWeight)
d.Set("arch", computeRec.Architecture)
2024-12-27 12:00:59 +03:00
d.Set("auto_start_w_node", computeRec.AutoStart)
2023-05-04 10:08:25 +03:00
d.Set("boot_order", computeRec.BootOrder)
2024-03-26 12:17:33 +03:00
// we intentionally use the SizeMax field, do not change it until the BootDiskSize field is fixed on the platform
2023-06-01 17:40:47 +03:00
d.Set("boot_disk_size", bootDisk.SizeMax)
2023-05-04 10:08:25 +03:00
d.Set("boot_disk", flattenBootDisk(bootDisk))
2023-03-10 12:42:15 +03:00
d.Set("boot_disk_id", bootDisk.ID)
2024-05-31 14:05:21 +03:00
d.Set("cd_image_id", computeRec.CdImageId)
2023-03-10 12:42:15 +03:00
d.Set("sep_id", bootDisk.SepID)
d.Set("pool", bootDisk.Pool)
2024-11-12 13:41:38 +03:00
d.Set("chipset", computeRec.Chipset)
2023-05-04 10:08:25 +03:00
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)
2023-08-28 13:02:41 +03:00
// d.Set("custom_fields", flattenCustomFields(computeRec.CustomFields))
2023-05-04 10:08:25 +03:00
d.Set("deleted_by", computeRec.DeletedBy)
d.Set("deleted_time", computeRec.DeletedTime)
d.Set("description", computeRec.Description)
2023-03-10 12:42:15 +03:00
d.Set("devices", string(devices))
2024-05-31 14:05:21 +03:00
err := d.Set("disks", flattenComputeDisksDemo(computeRec.Disks, d.Get("disks").([]interface{}), d.Get("extra_disks").(*schema.Set).List(), bootDisk.ID))
2023-01-24 17:50:38 +03:00
if err != nil {
return err
}
2023-05-04 10:08:25 +03:00
d.Set("driver", computeRec.Driver)
d.Set("cpu", computeRec.CPU)
d.Set("gid", computeRec.GID)
d.Set("guid", computeRec.GUID)
d.Set("compute_id", computeRec.ID)
if computeRec.VirtualImageID != 0 {
d.Set("image_id", computeRec.VirtualImageID)
2023-01-24 17:50:38 +03:00
} else {
2023-05-04 10:08:25 +03:00
d.Set("image_id", computeRec.ImageID)
2023-03-10 12:42:15 +03:00
}
2023-05-04 10:08:25 +03:00
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("name", computeRec.Name)
2024-03-26 12:17:33 +03:00
d.Set("need_reboot", computeRec.NeedReboot)
2024-05-31 14:05:21 +03:00
d.Set("numa_node_id", computeRec.NumaNodeId)
2023-05-04 10:08:25 +03:00
d.Set("natable_vins_id", computeRec.NatableVINSID)
d.Set("natable_vins_ip", computeRec.NatableVINSIP)
d.Set("natable_vins_name", computeRec.NatableVINSName)
d.Set("natable_vins_network", computeRec.NatableVINSNetwork)
d.Set("natable_vins_network_name", computeRec.NatableVINSNetworkName)
if err := d.Set("os_users", parseOsUsers(computeRec.OSUsers)); err != nil {
2023-03-10 12:42:15 +03:00
return err
2023-01-24 17:50:38 +03:00
}
2025-05-21 16:38:25 +03:00
// d.Set("pinned", computeRec.Pinned)
2025-02-07 11:30:15 +03:00
d.Set("preferred_cpu", computeRec.PreferredCPU)
2023-05-04 10:08:25 +03:00
d.Set("ram", computeRec.RAM)
d.Set("reference_id", computeRec.ReferenceID)
d.Set("registered", computeRec.Registered)
d.Set("res_name", computeRec.ResName)
2024-05-31 14:05:21 +03:00
d.Set("reserved_node_cpus", computeRec.ReservedNodeCpus)
2023-05-04 10:08:25 +03:00
d.Set("rg_id", computeRec.RGID)
d.Set("rg_name", computeRec.RGName)
d.Set("snap_sets", flattenSnapSets(computeRec.SnapSets))
d.Set("stateless_sep_id", computeRec.StatelessSepID)
d.Set("stateless_sep_type", computeRec.StatelessSepType)
d.Set("status", computeRec.Status)
2023-12-19 16:37:50 +03:00
// d.Set("tags", flattenTags(computeRec.Tags))
2023-05-04 10:08:25 +03:00
d.Set("tech_status", computeRec.TechStatus)
d.Set("updated_by", computeRec.UpdatedBy)
d.Set("updated_time", computeRec.UpdatedTime)
d.Set("user_managed", computeRec.UserManaged)
2024-12-27 12:00:59 +03:00
d.Set("vnc_password", computeRec.VNCPassword)
2025-05-21 16:38:25 +03:00
d.Set("vgpus", flattenVGPUs(computeRec.VGPUs))
2023-05-04 10:08:25 +03:00
d.Set("virtual_image_id", computeRec.VirtualImageID)
d.Set("virtual_image_name", computeRec.VirtualImageName)
2025-05-21 16:38:25 +03:00
d.Set("loader_type", computeRec.LoaderType)
d.Set("boot_type", computeRec.BootType)
d.Set("hot_resize", computeRec.HotResize)
d.Set("network_interface_naming", computeRec.NetworkInterfaceNaming)
2023-03-10 12:42:15 +03:00
2023-01-24 17:50:38 +03:00
d.Set("enabled", false)
2023-05-04 10:08:25 +03:00
if computeRec.Status == status.Enabled {
2023-01-24 17:50:38 +03:00
d.Set("enabled", true)
}
d.Set("started", false)
2023-05-04 10:08:25 +03:00
if computeRec.TechStatus == "STARTED" {
2023-01-24 17:50:38 +03:00
d.Set("started", true)
}
2024-10-08 12:20:56 +03:00
d.Set("network", flattenNetwork(d.Get("network").(*schema.Set).List(), computeRec.Interfaces))
2024-09-25 13:43:26 +03:00
d.Set("pci_devices", flattenPCI(*pciList))
2023-01-24 17:50:38 +03:00
2023-03-10 12:42:15 +03:00
return nil
}
2023-05-04 10:08:25 +03:00
func flattenACL(acl compute.RecordACL) []map[string]interface{} {
2023-03-10 12:42:15 +03:00
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
"account_acl": flattenListACL(acl.AccountACL),
"compute_acl": flattenListACL(acl.ComputeACL),
"rg_acl": flattenListACL(acl.RGACL),
}
res = append(res, temp)
return res
}
2023-05-04 10:08:25 +03:00
func flattenAffinityRules(affinityRules compute.ListRules) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(affinityRules))
2023-03-10 12:42:15 +03:00
for _, affinityRule := range affinityRules {
temp := map[string]interface{}{
"guid": affinityRule.GUID,
"key": affinityRule.Key,
"mode": affinityRule.Mode,
"policy": affinityRule.Policy,
"topology": affinityRule.Topology,
"value": affinityRule.Value,
2023-01-24 17:50:38 +03:00
}
2023-03-10 12:42:15 +03:00
res = append(res, temp)
2023-01-24 17:50:38 +03:00
}
2023-03-10 12:42:15 +03:00
return res
}
2023-05-04 10:08:25 +03:00
func flattenIotune(iotune compute.IOTune) []map[string]interface{} {
2023-03-10 12:42:15 +03:00
res := make([]map[string]interface{}, 0)
temp := map[string]interface{}{
"read_bytes_sec": iotune.ReadBytesSec,
"read_bytes_sec_max": iotune.ReadBytesSecMax,
"read_iops_sec": iotune.ReadIOPSSec,
"read_iops_sec_max": iotune.ReadIOPSSecMax,
"size_iops_sec": iotune.SizeIOPSSec,
"total_bytes_sec": iotune.TotalBytesSec,
"total_bytes_sec_max": iotune.TotalBytesSecMax,
"total_iops_sec": iotune.TotalIOPSSec,
"total_iops_sec_max": iotune.TotalIOPSSecMax,
"write_bytes_sec": iotune.WriteBytesSec,
"write_bytes_sec_max": iotune.WriteBytesSecMax,
"write_iops_sec": iotune.WriteIOPSSec,
"write_iops_sec_max": iotune.WriteIOPSSecMax,
}
res = append(res, temp)
return res
}
2023-05-04 10:08:25 +03:00
func flattenSnapshots(snapshots compute.SnapshotExtendList) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(snapshots))
2023-03-10 12:42:15 +03:00
for _, snapshot := range snapshots {
temp := map[string]interface{}{
"guid": snapshot.GUID,
"label": snapshot.Label,
"res_id": snapshot.ResID,
2023-07-26 13:32:39 +03:00
"reference_id": snapshot.ReferenceID,
2023-03-10 12:42:15 +03:00
"snap_set_guid": snapshot.SnapSetGUID,
"snap_set_time": snapshot.SnapSetTime,
"timestamp": snapshot.TimeStamp,
2023-01-24 17:50:38 +03:00
}
2023-03-10 12:42:15 +03:00
res = append(res, temp)
2023-01-24 17:50:38 +03:00
}
2023-03-10 12:42:15 +03:00
return res
}
2023-05-04 10:08:25 +03:00
func flattenListComputeDisks(disks compute.ListComputeDisks) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(disks))
2023-03-10 12:42:15 +03:00
for _, disk := range disks {
acl, _ := json.Marshal(disk.ACL)
temp := map[string]interface{}{
"_ckey": disk.CKey,
"acl": string(acl),
"account_id": disk.AccountID,
"boot_partition": disk.BootPartition,
2024-11-12 13:41:38 +03:00
"bus_number": disk.BusNumber,
2023-03-10 12:42:15 +03:00
"created_time": disk.CreatedTime,
"deleted_time": disk.DeletedTime,
"description": disk.Description,
"destruction_time": disk.DestructionTime,
"disk_path": disk.DiskPath,
"gid": disk.GID,
"guid": disk.GUID,
"disk_id": disk.ID,
"image_id": disk.ImageID,
"images": disk.Images,
"iotune": flattenIotune(disk.IOTune),
"iqn": disk.IQN,
"login": disk.Login,
"milestones": disk.Milestones,
"name": disk.Name,
"order": disk.Order,
"params": disk.Params,
"parent_id": disk.ParentID,
"passwd": disk.Passwd,
"pci_slot": disk.PCISlot,
"pool": disk.Pool,
"present_to": disk.PresentTo,
"purge_time": disk.PurgeTime,
2024-05-31 14:05:21 +03:00
"replication": flattenDiskReplication(disk.Replication),
2023-03-10 12:42:15 +03:00
"reality_device_number": disk.RealityDeviceNumber,
"res_id": disk.ResID,
"role": disk.Role,
"sep_id": disk.SepID,
"shareable": disk.Shareable,
2025-05-21 16:38:25 +03:00
"size_available": disk.SizeAvailable,
2023-03-10 12:42:15 +03:00
"size_max": disk.SizeMax,
"size_used": disk.SizeUsed,
"snapshots": flattenSnapshots(disk.Snapshots),
"status": disk.Status,
"tech_status": disk.TechStatus,
"type": disk.Type,
"vmid": disk.VMID,
}
res = append(res, temp)
2023-01-24 17:50:38 +03:00
}
2023-03-10 12:42:15 +03:00
return res
}
2024-05-31 14:05:21 +03:00
func flattenDiskReplication(rep compute.ItemReplication) []map[string]interface{} {
res := []map[string]interface{}{
{
"disk_id": rep.DiskID,
"pool_id": rep.PoolID,
"role": rep.Role,
"self_volume_id": rep.SelfVolumeID,
"storage_id": rep.StorageID,
"volume_id": rep.VolumeID,
},
}
return res
}
2023-07-26 13:32:39 +03:00
func flattenCustomFields(customFields map[string]interface{}) string {
encoded, _ := json.Marshal(customFields)
return string(encoded)
2023-03-10 12:42:15 +03:00
}
2023-07-26 13:32:39 +03:00
2023-05-04 10:08:25 +03:00
func flattenOsUsers(osUsers compute.ListOSUser) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(osUsers))
2023-03-10 12:42:15 +03:00
for _, user := range osUsers {
temp := map[string]interface{}{
"guid": user.GUID,
"login": user.Login,
"password": user.Password,
"public_key": user.PubKey,
}
res = append(res, temp)
}
return res
}
2024-09-25 13:43:26 +03:00
func flattenDataCompute(d *schema.ResourceData, computeRec compute.RecordCompute, pciList *compute.ListPCIDevices) {
2023-05-04 10:08:25 +03:00
devices, _ := json.Marshal(computeRec.Devices)
userdata, _ := json.Marshal(computeRec.Userdata)
d.Set("acl", flattenACL(computeRec.ACL))
d.Set("account_id", computeRec.AccountID)
d.Set("account_name", computeRec.AccountName)
d.Set("affinity_label", computeRec.AffinityLabel)
d.Set("affinity_rules", flattenAffinityRules(computeRec.AffinityRules))
d.Set("affinity_weight", computeRec.AffinityWeight)
d.Set("anti_affinity_rules", flattenListRules(computeRec.AntiAffinityRules))
2024-12-27 12:00:59 +03:00
d.Set("auto_start_w_node", computeRec.AutoStart)
2023-05-04 10:08:25 +03:00
d.Set("arch", computeRec.Architecture)
2024-11-12 13:41:38 +03:00
d.Set("chipset", computeRec.Chipset)
2023-05-04 10:08:25 +03:00
d.Set("boot_order", computeRec.BootOrder)
2024-05-31 14:05:21 +03:00
d.Set("bootdisk_size", computeRec.BootDiskSize)
d.Set("cd_image_id", computeRec.CdImageId)
2023-05-04 10:08:25 +03:00
d.Set("clone_reference", computeRec.CloneReference)
d.Set("clones", computeRec.Clones)
d.Set("computeci_id", computeRec.ComputeCIID)
2024-05-31 14:05:21 +03:00
d.Set("cpu_pin", computeRec.CPUPin)
2023-05-04 10:08:25 +03:00
d.Set("cpus", computeRec.CPU)
d.Set("created_by", computeRec.CreatedBy)
d.Set("created_time", computeRec.CreatedTime)
2023-08-28 13:02:41 +03:00
d.Set("custom_fields", flattenCustomFields(computeRec.CustomFields))
2023-05-04 10:08:25 +03:00
d.Set("deleted_by", computeRec.DeletedBy)
d.Set("deleted_time", computeRec.DeletedTime)
d.Set("desc", computeRec.Description)
2023-03-10 12:42:15 +03:00
d.Set("devices", string(devices))
2023-05-04 10:08:25 +03:00
d.Set("disks", flattenListComputeDisks(computeRec.Disks))
d.Set("driver", computeRec.Driver)
d.Set("gid", computeRec.GID)
d.Set("guid", computeRec.GUID)
2024-05-31 14:05:21 +03:00
d.Set("hp_backed", computeRec.HPBacked)
2023-05-04 10:08:25 +03:00
d.Set("compute_id", computeRec.ID)
d.Set("image_id", computeRec.ImageID)
2023-12-19 16:37:50 +03:00
d.Set("image_name", computeRec.ImageName)
2023-05-04 10:08:25 +03:00
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("name", computeRec.Name)
2024-03-26 12:17:33 +03:00
d.Set("need_reboot", computeRec.NeedReboot)
2024-05-31 14:05:21 +03:00
d.Set("numa_affinity", computeRec.NumaAffinity)
d.Set("numa_node_id", computeRec.NumaNodeId)
2023-05-04 10:08:25 +03:00
d.Set("natable_vins_id", computeRec.NatableVINSID)
d.Set("natable_vins_ip", computeRec.NatableVINSIP)
d.Set("natable_vins_name", computeRec.NatableVINSName)
d.Set("natable_vins_network", computeRec.NatableVINSNetwork)
d.Set("natable_vins_network_name", computeRec.NatableVINSNetworkName)
d.Set("os_users", flattenOsUsers(computeRec.OSUsers))
2025-05-21 16:38:25 +03:00
// d.Set("pinned", computeRec.Pinned)
2025-02-07 11:30:15 +03:00
d.Set("preferred_CPU", computeRec.PreferredCPU)
2023-05-04 10:08:25 +03:00
d.Set("ram", computeRec.RAM)
d.Set("reference_id", computeRec.ReferenceID)
d.Set("registered", computeRec.Registered)
d.Set("res_name", computeRec.ResName)
2024-05-31 14:05:21 +03:00
d.Set("reserved_node_cpus", computeRec.ReservedNodeCpus)
2023-05-04 10:08:25 +03:00
d.Set("rg_id", computeRec.RGID)
d.Set("rg_name", computeRec.RGName)
d.Set("snap_sets", flattenSnapSets(computeRec.SnapSets))
d.Set("stateless_sep_id", computeRec.StatelessSepID)
d.Set("stateless_sep_type", computeRec.StatelessSepType)
d.Set("status", computeRec.Status)
d.Set("tags", 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)
2023-03-10 12:42:15 +03:00
d.Set("userdata", string(userdata))
2024-12-27 12:00:59 +03:00
d.Set("vnc_password", computeRec.VNCPassword)
2025-05-21 16:38:25 +03:00
d.Set("vgpus", flattenVGPUs(computeRec.VGPUs))
2023-05-04 10:08:25 +03:00
d.Set("virtual_image_id", computeRec.VirtualImageID)
d.Set("virtual_image_name", computeRec.VirtualImageName)
2024-09-25 13:43:26 +03:00
d.Set("pci_devices", flattenPCI(*pciList))
2025-05-21 16:38:25 +03:00
d.Set("loader_type", computeRec.LoaderType)
d.Set("boot_type", computeRec.BootType)
d.Set("hot_resize", computeRec.HotResize)
d.Set("network_interface_naming", computeRec.NetworkInterfaceNaming)
2024-09-25 13:43:26 +03:00
}
func flattenPCI(pciList compute.ListPCIDevices) []uint64 {
res := make([]uint64, 0, len(pciList.Data))
for _, v := range pciList.Data {
res = append(res, v.ID)
}
return res
2023-03-10 12:42:15 +03:00
}
2023-05-04 10:08:25 +03:00
func flattenComputeAudits(computeAudits compute.ListAudits) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(computeAudits))
2023-03-10 12:42:15 +03:00
for _, computeAudit := range computeAudits {
temp := map[string]interface{}{
"call": computeAudit.Call,
"responsetime": computeAudit.ResponseTime,
"statuscode": computeAudit.StatusCode,
"timestamp": computeAudit.Timestamp,
"user": computeAudit.User,
}
res = append(res, temp)
}
return res
}
2023-07-26 13:32:39 +03:00
func flattenPfwList(computePfws *compute.ListPFWs) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(computePfws.Data))
2023-07-26 13:32:39 +03:00
for _, computePfw := range computePfws.Data {
2023-03-10 12:42:15 +03:00
temp := map[string]interface{}{
"pfw_id": computePfw.ID,
"local_ip": computePfw.LocalIP,
"local_port": computePfw.LocalPort,
"protocol": computePfw.Protocol,
"public_port_end": computePfw.PublicPortEnd,
"public_port_start": computePfw.PublicPortStart,
"vm_id": computePfw.VMID,
}
res = append(res, temp)
}
return res
}
2023-07-26 13:32:39 +03:00
func flattenUserList(d *schema.ResourceData, userList *compute.ListUsers) {
d.Set("account_acl", flattenListACL(userList.Data.AccountACL))
d.Set("compute_acl", flattenListACL(userList.Data.ComputeACL))
d.Set("rg_acl", flattenListACL(userList.Data.RGACL))
2023-03-10 12:42:15 +03:00
}
2023-05-04 10:08:25 +03:00
func flattenComputeGetAudits(computeAudits compute.ListShortAudits) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(computeAudits))
2023-03-10 12:42:15 +03:00
for _, computeAudit := range computeAudits {
temp := map[string]interface{}{
"epoch": computeAudit.Epoch,
"message": computeAudit.Message,
}
res = append(res, temp)
}
return res
2023-01-24 17:50:38 +03:00
}
2023-03-26 22:00:30 +03:00
2023-05-04 10:08:25 +03:00
func flattenSnapshotUsage(computeSnapshotUsages compute.ListUsageSnapshots) []map[string]interface{} {
2023-10-13 13:28:19 +03:00
res := make([]map[string]interface{}, 0, len(computeSnapshotUsages))
2023-05-04 10:08:25 +03:00
for _, computeUsage := range computeSnapshotUsages {
2023-03-26 22:00:30 +03:00
temp := map[string]interface{}{
"count": computeUsage.Count,
"stored": computeUsage.Stored,
"label": computeUsage.Label,
"timestamp": computeUsage.Timestamp,
}
res = append(res, temp)
}
return res
}
2023-10-13 13:28:19 +03:00
2023-12-19 16:37:50 +03:00
// func flattenSnapshotList(computeSnapshotUsages *compute.ListSnapShots) []map[string]interface{} {
// res := make([]map[string]interface{}, 0, len(computeSnapshotUsages.Data))
// for _, computeUsage := range computeSnapshotUsages.Data {
// temp := map[string]interface{}{
// "disks": computeUsage.Disks,
// "guid": computeUsage.GUID,
// "label": computeUsage.Label,
// "timestamp": computeUsage.Timestamp,
// }
// res = append(res, temp)
// }
// return res
// }
2023-10-13 13:28:19 +03:00
2024-05-31 14:05:21 +03:00
func flattenVGPU(vgpuList []compute.ItemVGPU) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(vgpuList))
for _, dev := range vgpuList {
temp := map[string]interface{}{
"account_id": dev.AccountID,
"created_time": dev.CreatedTime,
"deleted_time": dev.DeletedTime,
"gid": dev.GID,
"guid": dev.GUID,
"vgpu_id": dev.ID,
"last_claimed_by": dev.LastClaimedBy,
"last_update_time": dev.LastUpdateTime,
"mode": dev.Mode,
"pci_slot": dev.PCISlot,
"pgpuid": dev.PGPUID,
"profile_id": dev.ProfileID,
"ram": dev.RAM,
"reference_id": dev.ReferenceID,
"rg_id": dev.RGID,
"status": dev.Status,
"type": dev.Type,
"vm_id": dev.VMID,
2023-10-13 13:28:19 +03:00
}
2024-05-31 14:05:21 +03:00
res = append(res, temp)
2023-10-13 13:28:19 +03:00
}
2024-05-31 14:05:21 +03:00
return res
2023-10-13 13:28:19 +03:00
}
2024-05-31 14:05:21 +03:00
func flattenPCIDevice(deviceList []compute.ItemPCIDevice) []map[string]interface{} {
res := make([]map[string]interface{}, 0, len(deviceList))
for _, dev := range deviceList {
temp := map[string]interface{}{
"compute_id": dev.ComputeID,
"description": dev.Description,
"guid": dev.GUID,
"hwpath": dev.HwPath,
"device_id": dev.ID,
"name": dev.Name,
"rg_id": dev.RGID,
"stack_id": dev.StackID,
"status": dev.Status,
"system_name": dev.SystemName,
2023-10-13 13:28:19 +03:00
}
2024-05-31 14:05:21 +03:00
res = append(res, temp)
2023-10-13 13:28:19 +03:00
}
2024-05-31 14:05:21 +03:00
return res
2023-12-19 16:37:50 +03:00
}
2025-05-21 16:38:25 +03:00
func flattenVGPUs(vgpus []compute.VGPUItem) []map[string]interface{} {
res := make([]map[string]interface{}, len(vgpus))
for i, vgpu := range vgpus {
res[i] = map[string]interface{}{
"id": int(vgpu.ID),
"gid": int(vgpu.GID),
"type": vgpu.Type,
"mode": vgpu.Mode,
"status": vgpu.Status,
"profile_id": vgpu.ProfileID,
"ram": int(vgpu.RAM),
"last_update_time": int(vgpu.LastUpdateTime),
"created_time": int(vgpu.CreatedTime),
"deleted_time": int(vgpu.DeletedTime),
"vmid": int(vgpu.VMID),
"pgpuid": int(vgpu.PGPuid),
"reference_id": vgpu.ReferenceID,
"account_id": int(vgpu.AccountID),
"rg_id": int(vgpu.RgID),
"last_claimed_by": int(vgpu.LastClaimedBy),
"pci_slot": int(vgpu.PCISlot),
"bus_number": int(vgpu.BusNumber),
"guid": int(vgpu.GUID),
}
}
return res
}