@ -2,11 +2,14 @@ package kvmvm
import (
import (
"encoding/json"
"encoding/json"
"fmt"
"sort"
"sort"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/compute"
"repository.basistech.ru/BASIS/terraform-provider-decort/internal/flattens"
)
)
func flattenCompute ( d * schema . ResourceData , computeRec * compute . RecordCompute ) error {
func flattenCompute ( d * schema . ResourceData , computeRec * compute . RecordCompute ) error {
@ -14,18 +17,35 @@ func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute) e
customFields , _ := json . Marshal ( computeRec . CustomFields )
customFields , _ := json . Marshal ( computeRec . CustomFields )
devices , _ := json . Marshal ( computeRec . Devices )
devices , _ := json . Marshal ( computeRec . Devices )
userData , _ := json . Marshal ( computeRec . Userdata )
bootDisk := findBootDisk ( computeRec . Disks )
bootDisk := findBootDisk ( computeRec . Disks )
//extra fields setting
if len ( computeRec . Disks ) > 0 {
log . Debugf ( "flattenCompute: calling parseComputeDisksToExtraDisks for %d disks" , len ( computeRec . Disks ) )
if err := d . Set ( "extra_disks" , parseComputeDisksToExtraDisks ( computeRec . Disks ) ) ; err != nil {
return err
}
}
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 {
return err
}
}
d . Set ( "account_id" , computeRec . AccountID )
d . Set ( "account_id" , computeRec . AccountID )
d . Set ( "account_name" , computeRec . AccountName )
d . Set ( "account_name" , computeRec . AccountName )
d . Set ( "acl" , flattenListACLInterface ( computeRec . ACL ) )
d . Set ( "affinity_label" , computeRec . AffinityLabel )
d . Set ( "affinity_label" , computeRec . AffinityLabel )
d . Set ( "affinity_weight" , computeRec . AffinityWeight )
d . Set ( "affinity_weight" , computeRec . AffinityWeight )
d . Set ( "affinity_rules" , flattenAffinityRules ( computeRec . AffinityRules ) )
d . Set ( "affinity_rules" , flattenAffinityRules ( computeRec . AffinityRules ) )
d . Set ( "anti_affinity_rules" , flattenAffinityRules ( computeRec . AntiAffinityRules ) )
d . Set ( "anti_affinity_rules" , flattenAffinityRules ( computeRec . AntiAffinityRules ) )
d . Set ( "arch" , computeRec . Arch )
d . Set ( "arch" , computeRec . Arch )
d . Set ( "boot_order" , computeRec . BootOrder )
d . Set ( "boot_order" , computeRec . BootOrder )
d . Set ( "boot_disk_id" , bootDisk . ID )
d . Set ( "boot_disk_size" , computeRec . BootDiskSize )
d . Set ( "boot_disk_size" , computeRec . BootDiskSize )
d . Set ( "cd_image_id" , computeRec . CdImageId )
d . Set ( "clone_reference" , computeRec . CloneReference )
d . Set ( "clone_reference" , computeRec . CloneReference )
d . Set ( "clones" , computeRec . Clones )
d . Set ( "clones" , computeRec . Clones )
d . Set ( "computeci_id" , computeRec . ComputeCIID )
d . Set ( "computeci_id" , computeRec . ComputeCIID )
@ -36,13 +56,11 @@ func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute) e
d . Set ( "deleted_time" , computeRec . DeletedTime )
d . Set ( "deleted_time" , computeRec . DeletedTime )
d . Set ( "description" , computeRec . Description )
d . Set ( "description" , computeRec . Description )
d . Set ( "devices" , string ( devices ) )
d . Set ( "devices" , string ( devices ) )
d . Set ( "disks" ,
err := d . Set ( "disks" , flattenComputeDisks ( computeRec . Disks , d . Get ( "extra_disks" ) . ( * schema . Set ) . List ( ) , bootDisk . ID ) )
flattenComputeDisks (
if err != nil {
computeRec . Disks ,
return err
d . Get ( "extra_disks" ) . ( * schema . Set ) . List ( ) ,
}
bootDisk . ID ,
d . Set ( "driver" , computeRec . Driver )
) ,
)
d . Set ( "gid" , computeRec . GID )
d . Set ( "gid" , computeRec . GID )
d . Set ( "guid" , computeRec . GUID )
d . Set ( "guid" , computeRec . GUID )
d . Set ( "compute_id" , computeRec . ID )
d . Set ( "compute_id" , computeRec . ID )
@ -53,6 +71,7 @@ func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute) e
d . Set ( "manager_type" , computeRec . ManagerType )
d . Set ( "manager_type" , computeRec . ManagerType )
d . Set ( "migrationjob" , computeRec . MigrationJob )
d . Set ( "migrationjob" , computeRec . MigrationJob )
d . Set ( "milestones" , computeRec . Milestones )
d . Set ( "milestones" , computeRec . Milestones )
d . Set ( "need_reboot" , computeRec . NeedReboot )
d . Set ( "os_users" , flattenOSUsers ( computeRec . OSUsers ) )
d . Set ( "os_users" , flattenOSUsers ( computeRec . OSUsers ) )
d . Set ( "pinned" , computeRec . Pinned )
d . Set ( "pinned" , computeRec . Pinned )
d . Set ( "reference_id" , computeRec . ReferenceID )
d . Set ( "reference_id" , computeRec . ReferenceID )
@ -69,6 +88,7 @@ func flattenCompute(d *schema.ResourceData, computeRec *compute.RecordCompute) e
d . Set ( "tech_status" , computeRec . TechStatus )
d . Set ( "tech_status" , computeRec . TechStatus )
d . Set ( "updated_by" , computeRec . UpdatedBy )
d . Set ( "updated_by" , computeRec . UpdatedBy )
d . Set ( "updated_time" , computeRec . UpdatedTime )
d . Set ( "updated_time" , computeRec . UpdatedTime )
d . Set ( "user_data" , string ( userData ) )
d . Set ( "user_managed" , computeRec . UserManaged )
d . Set ( "user_managed" , computeRec . UserManaged )
d . Set ( "vgpus" , computeRec . VGPUs )
d . Set ( "vgpus" , computeRec . VGPUs )
d . Set ( "virtual_image_id" , computeRec . VirtualImageID )
d . Set ( "virtual_image_id" , computeRec . VirtualImageID )
@ -175,6 +195,7 @@ func flattenComputeDisks(disksList compute.ListDisks, extraDisks []interface{},
"disk_id" : disk . ID ,
"disk_id" : disk . ID ,
"shareable" : disk . Shareable ,
"shareable" : disk . Shareable ,
"size_used" : disk . SizeUsed ,
"size_used" : disk . SizeUsed ,
"size_max" : disk . SizeMax ,
}
}
res = append ( res , temp )
res = append ( res , temp )
}
}
@ -193,15 +214,6 @@ func findInExtraDisks(diskId uint, extraDisks []interface{}) bool {
return false
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 { } {
func flattenAffinityRules ( rules compute . ListRules ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( rules ) )
res := make ( [ ] map [ string ] interface { } , 0 , len ( rules ) )
@ -217,3 +229,541 @@ func flattenAffinityRules(rules compute.ListRules) []map[string]interface{} {
return res
return res
}
}
func flattenComputeList ( computes * compute . ListComputes ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computes . Data ) )
for _ , computeItem := range computes . Data {
customFields , _ := json . Marshal ( computeItem . CustomFields )
devices , _ := json . Marshal ( computeItem . Devices )
userData , _ := json . Marshal ( computeItem . Userdata )
temp := map [ string ] interface { } {
"acl" : flattenListACLInterface ( computeItem . ACL ) ,
"account_id" : computeItem . AccountID ,
"account_name" : computeItem . AccountName ,
"affinity_label" : computeItem . AffinityLabel ,
"affinity_rules" : flattenListRules ( computeItem . AffinityRules ) ,
"affinity_weight" : computeItem . AffinityWeight ,
"anti_affinity_rules" : flattenListRules ( computeItem . AntiAffinityRules ) ,
"arch" : computeItem . Arch ,
"cd_image_id" : computeItem . CdImageId ,
"boot_order" : computeItem . BootOrder ,
"bootdisk_size" : computeItem . BootDiskSize ,
"clone_reference" : computeItem . CloneReference ,
"clones" : computeItem . Clones ,
"computeci_id" : computeItem . ComputeCIID ,
"cpus" : computeItem . CPUs ,
"created_by" : computeItem . CreatedBy ,
"created_time" : computeItem . CreatedTime ,
"custom_fields" : string ( customFields ) ,
"deleted_by" : computeItem . DeletedBy ,
"deleted_time" : computeItem . DeletedTime ,
"desc" : computeItem . Description ,
"devices" : string ( devices ) ,
"disks" : flattenDisks ( computeItem . Disks ) ,
"driver" : computeItem . Driver ,
"gid" : computeItem . GID ,
"guid" : computeItem . GUID ,
"compute_id" : computeItem . ID ,
"image_id" : computeItem . ImageID ,
"interfaces" : flattenInterfaces ( computeItem . Interfaces ) ,
"lock_status" : computeItem . LockStatus ,
"manager_id" : computeItem . ManagerID ,
"manager_type" : computeItem . ManagerType ,
"migrationjob" : computeItem . MigrationJob ,
"milestones" : computeItem . Milestones ,
"name" : computeItem . Name ,
"need_reboot" : computeItem . NeedReboot ,
"os_users" : flattenOSUsers ( computeItem . OSUsers ) ,
"pinned" : computeItem . Pinned ,
"ram" : computeItem . RAM ,
"reference_id" : computeItem . ReferenceID ,
"registered" : computeItem . Registered ,
"res_name" : computeItem . ResName ,
"rg_id" : computeItem . RGID ,
"rg_name" : computeItem . RGName ,
"snap_sets" : flattenSnapSets ( computeItem . SnapSets ) ,
"stack_id" : computeItem . StackID ,
"stateless_sep_id" : computeItem . StatelessSEPID ,
"stateless_sep_type" : computeItem . StatelessSEPType ,
"status" : computeItem . Status ,
"tags" : flattenTags ( computeItem . Tags ) ,
"tech_status" : computeItem . TechStatus ,
"total_disk_size" : computeItem . TotalDiskSize ,
"updated_by" : computeItem . UpdatedBy ,
"updated_time" : computeItem . UpdatedTime ,
"user_data" : string ( userData ) ,
"user_managed" : computeItem . UserManaged ,
"vgpus" : computeItem . VGPUs ,
"vins_connected" : computeItem . VINSConnected ,
"virtual_image_id" : computeItem . VirtualImageID ,
}
res = append ( res , temp )
}
return res
}
func flattenListACLInterface ( listAcl [ ] interface { } ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( listAcl ) )
for _ , aclInterface := range listAcl {
acl := aclInterface . ( map [ string ] interface { } )
temp := map [ string ] interface { } {
"explicit" : acl [ "explicit" ] ,
"guid" : acl [ "guid" ] ,
"right" : acl [ "right" ] ,
"status" : acl [ "status" ] ,
"type" : acl [ "type" ] ,
"user_group_id" : acl [ "user_group_id" ] ,
}
res = append ( res , temp )
}
return res
}
func flattenListACL ( listAcl compute . ListACL ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( listAcl ) )
for _ , acl := range listAcl {
temp := map [ string ] interface { } {
"explicit" : acl . Explicit ,
"guid" : acl . GUID ,
"right" : acl . Right ,
"status" : acl . Status ,
"type" : acl . Type ,
"user_group_id" : acl . UserGroupID ,
}
res = append ( res , temp )
}
return res
}
func flattenListComputeACL ( listAcl [ ] compute . ItemComputeACL ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( listAcl ) )
for _ , acl := range listAcl {
temp := map [ string ] interface { } {
"explicit" : acl . Explicit ,
"guid" : acl . GUID ,
"right" : acl . Right ,
"status" : acl . Status ,
"type" : acl . Type ,
"user_group_id" : acl . UserGroupID ,
}
res = append ( res , temp )
}
return res
}
func flattenListRules ( listRules compute . ListRules ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( listRules ) )
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
}
func flattenDisks ( disks [ ] compute . InfoDisk ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 )
for _ , disk := range disks {
temp := map [ string ] interface { } {
"disk_id" : disk . ID ,
"pci_slot" : disk . PCISlot ,
}
res = append ( res , temp )
}
return res
}
func flattenComputeAudits ( computeAudits compute . ListDetailedAudits ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computeAudits ) )
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
}
func flattenComputeGetAudits ( computeAudits compute . ListAudits ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computeAudits ) )
for _ , computeAudit := range computeAudits {
temp := map [ string ] interface { } {
"epoch" : computeAudit . Epoch ,
"message" : computeAudit . Message ,
}
res = append ( res , temp )
}
return res
}
func flattenPfwList ( computePfws * compute . ListPFW ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computePfws . Data ) )
for _ , computePfw := range computePfws . Data {
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
}
func flattenUserList ( d * schema . ResourceData , userList * compute . ListUsers ) {
d . Set ( "account_acl" , flattenListACL ( userList . Data . AccountACL ) )
d . Set ( "compute_acl" , flattenListComputeACL ( userList . Data . ComputeACL ) )
d . Set ( "rg_acl" , flattenListACL ( userList . Data . RGACL ) )
}
func flattenSnapshotList ( computeSnapshots * compute . ListSnapShot ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computeSnapshots . Data ) )
for _ , snp := range computeSnapshots . Data {
temp := map [ string ] interface { } {
"disks" : snp . Disks ,
"guid" : snp . GUID ,
"label" : snp . Label ,
"timestamp" : snp . Timestamp ,
}
res = append ( res , temp )
}
return res
}
func flattenAffinityRelations ( d * schema . ResourceData , ar * compute . RecordAffinityRelations ) {
d . Set ( "other_node" , flattenNodes ( ar . OtherNode ) )
d . Set ( "other_node_indirect" , flattenNodes ( ar . OtherNodeIndirect ) )
d . Set ( "other_node_indirect_soft" , flattenNodes ( ar . OtherNodeIndirectSoft ) )
d . Set ( "other_node_soft" , flattenNodes ( ar . OtherNodeSoft ) )
d . Set ( "same_node" , flattenNodes ( ar . SameNode ) )
d . Set ( "same_node_soft" , flattenNodes ( ar . SameNodeSoft ) )
}
func flattenSnapshotUsage ( computeSnapshotUsages compute . ListSnapshotUsage ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( computeSnapshotUsages ) )
for _ , computeUsage := range computeSnapshotUsages {
temp := map [ string ] interface { } {
"count" : computeUsage . Count ,
"stored" : computeUsage . Stored ,
"label" : computeUsage . Label ,
"timestamp" : computeUsage . Timestamp ,
}
res = append ( res , temp )
}
return res
}
func flattenPCIDevice ( deviceList [ ] compute . ItemPCIDevice ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( deviceList ) )
for _ , dev := range deviceList {
temp := map [ string ] interface { } {
"ckey" : dev . CKey ,
"meta" : flattens . FlattenMeta ( dev . Meta ) ,
"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 ,
}
res = append ( res , temp )
}
return res
}
func flattenVGPU ( m [ ] interface { } ) [ ] string {
var 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 flattenNodes ( m [ ] interface { } ) [ ] string {
var 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 flattenDataCompute ( d * schema . ResourceData , compFacts * compute . RecordCompute ) error {
// 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
log . Debugf ( "flattenCompute: ID %d, RG ID %d" , compFacts . ID , compFacts . RGID )
customFields , _ := json . Marshal ( compFacts . CustomFields )
devices , _ := json . Marshal ( compFacts . Devices )
userData , _ := json . Marshal ( compFacts . Userdata )
// general fields setting
d . SetId ( fmt . Sprintf ( "%d" , compFacts . ID ) )
d . Set ( "account_id" , compFacts . AccountID )
d . Set ( "account_name" , compFacts . AccountName )
d . Set ( "acl" , flattenListACLInterface ( compFacts . ACL ) )
d . Set ( "affinity_label" , compFacts . AffinityLabel )
d . Set ( "affinity_rules" , flattenAffinityRules ( compFacts . AffinityRules ) )
d . Set ( "affinity_weight" , compFacts . AffinityWeight )
d . Set ( "anti_affinity_rules" , flattenAffinityRules ( compFacts . AntiAffinityRules ) )
d . Set ( "arch" , compFacts . Arch )
d . Set ( "boot_order" , compFacts . BootOrder )
d . Set ( "cd_image_id" , compFacts . CdImageId )
d . Set ( "clone_reference" , compFacts . CloneReference )
d . Set ( "clones" , compFacts . Clones )
d . Set ( "computeci_id" , compFacts . ComputeCIID )
d . Set ( "cpus" , compFacts . CPUs )
d . Set ( "created_by" , compFacts . CreatedBy )
d . Set ( "created_time" , compFacts . CreatedTime )
d . Set ( "custom_fields" , string ( customFields ) )
d . Set ( "deleted_by" , compFacts . DeletedBy )
d . Set ( "deleted_time" , compFacts . DeletedTime )
d . Set ( "desc" , compFacts . Description )
d . Set ( "devices" , string ( devices ) )
d . Set ( "disks" , flattenDisk ( compFacts . Disks ) )
d . Set ( "driver" , compFacts . Driver )
d . Set ( "gid" , compFacts . GID )
d . Set ( "guid" , compFacts . GUID )
d . Set ( "image_id" , compFacts . ImageID )
d . Set ( "interfaces" , flattenInterfaces ( compFacts . Interfaces ) )
d . Set ( "lock_status" , compFacts . LockStatus )
d . Set ( "manager_id" , compFacts . ManagerID )
d . Set ( "manager_type" , compFacts . ManagerType )
d . Set ( "migrationjob" , compFacts . MigrationJob )
d . Set ( "milestones" , compFacts . Milestones )
d . Set ( "name" , compFacts . Name )
d . Set ( "need_reboot" , compFacts . NeedReboot )
d . Set ( "os_users" , flattenOSUsers ( compFacts . OSUsers ) )
d . Set ( "pinned" , compFacts . Pinned )
d . Set ( "ram" , compFacts . RAM )
d . Set ( "reference_id" , compFacts . ReferenceID )
d . Set ( "registered" , compFacts . Registered )
d . Set ( "res_name" , compFacts . ResName )
d . Set ( "rg_id" , compFacts . RGID )
d . Set ( "rg_name" , compFacts . RGName )
d . Set ( "snap_sets" , flattenSnapSets ( compFacts . SnapSets ) )
d . Set ( "stack_id" , compFacts . StackID )
d . Set ( "stack_name" , compFacts . StackName )
d . Set ( "stateless_sep_id" , compFacts . StatelessSEPID )
d . Set ( "stateless_sep_type" , compFacts . StatelessSEPType )
d . Set ( "status" , compFacts . Status )
d . Set ( "tags" , flattenTags ( compFacts . Tags ) )
d . Set ( "tech_status" , compFacts . TechStatus )
d . Set ( "updated_by" , compFacts . UpdatedBy )
d . Set ( "updated_time" , compFacts . UpdatedTime )
d . Set ( "user_data" , string ( userData ) )
d . Set ( "user_managed" , compFacts . UserManaged )
d . Set ( "vgpus" , compFacts . VGPUs )
d . Set ( "virtual_image_id" , compFacts . VirtualImageID )
//extra fields setting
bootDisk := findBootDisk ( compFacts . Disks )
if bootDisk != nil {
d . Set ( "boot_disk_size" , bootDisk . SizeMax )
d . Set ( "boot_disk_id" , bootDisk . ID ) // we may need boot disk ID in resize operations
d . Set ( "sep_id" , bootDisk . SEPID )
d . Set ( "pool" , bootDisk . Pool )
}
if len ( compFacts . Disks ) > 0 {
log . Debugf ( "flattenCompute: calling parseComputeDisksToExtraDisks for %d disks" , len ( compFacts . Disks ) )
if err := d . Set ( "extra_disks" , parseComputeDisksToExtraDisks ( compFacts . Disks ) ) ; err != nil {
return err
}
}
if len ( compFacts . Interfaces ) > 0 {
log . Debugf ( "flattenCompute: calling parseComputeInterfacesToNetworks for %d interfaces" , len ( compFacts . Interfaces ) )
if err := d . Set ( "network" , parseComputeInterfacesToNetworks ( compFacts . Interfaces ) ) ; err != nil {
return err
}
}
return nil
}
// Parse list of all disks from API compute/get into a list of "extra disks" attached to this compute
// Extra disks are all compute disks but a boot disk.
func parseComputeDisksToExtraDisks ( disks compute . ListDisks ) [ ] interface { } {
// this return value will be used to d.Set("extra_disks",) item of dataSourceCompute schema,
// which is a simple list of integer disk IDs excluding boot disk ID
length := len ( disks )
log . Debugf ( "parseComputeDisksToExtraDisks: called for %d disks" , length )
if length == 0 || ( length == 1 && disks [ 0 ] . Type == "B" ) {
// the disk list is empty (which is kind of strange - diskless compute?), or
// there is only one disk in the list and it is a boot disk;
// as we skip boot disks, the result will be of 0 length anyway
return make ( [ ] interface { } , 0 )
}
result := make ( [ ] interface { } , length - 1 )
idx := 0
for _ , value := range disks {
if value . Type == "B" {
// skip boot disk when iterating over the list of disks
continue
}
result [ idx ] = value . ID
idx ++
}
return result
}
// Parse the list of interfaces from compute/get response into a list of networks
// attached to this compute
func parseComputeInterfacesToNetworks ( 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 )
result := [ ] interface { } { }
for _ , value := range ifaces {
elem := make ( map [ string ] interface { } )
// Keys in this map should correspond to the Schema definition for "network"
elem [ "net_id" ] = value . NetID
elem [ "net_type" ] = value . NetType
elem [ "ip_address" ] = value . IPAddress
elem [ "mac" ] = value . MAC
result = append ( result , elem )
}
return result
}
func flattenDisk ( diskList compute . ListDisks ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 , len ( diskList ) )
for _ , disk := range diskList {
temp := map [ string ] interface { } {
"ckey" : disk . CKey ,
"meta" : flattens . FlattenMeta ( disk . Meta ) ,
"account_id" : disk . AccountID ,
"boot_partition" : disk . BootPartition ,
"created_time" : disk . CreatedTime ,
"deleted_time" : disk . DeletedTime ,
"desc" : 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 . Password ,
"pci_slot" : disk . PCISlot ,
"pool" : disk . Pool ,
"purge_attempts" : disk . PurgeAttempts ,
"present_to" : disk . PresentTo ,
"purge_time" : disk . PurgeTime ,
"reality_device_number" : disk . RealityDeviceNumber ,
"reference_id" : disk . ReferenceID ,
"res_id" : disk . ResID ,
"res_name" : disk . ResName ,
"role" : disk . Role ,
"sep_id" : disk . SEPID ,
"shareable" : disk . Shareable ,
"size_max" : disk . SizeMax ,
"size_used" : disk . SizeUsed ,
"snapshots" : flattendDiskSnapshotList ( disk . Snapshots ) ,
"status" : disk . Status ,
"tech_status" : disk . TechStatus ,
"type" : disk . Type ,
"vmid" : disk . VMID ,
}
res = append ( res , temp )
}
return res
}
func flattenIOTune ( iot compute . IOTune ) [ ] map [ string ] interface { } {
res := make ( [ ] map [ string ] interface { } , 0 )
temp := map [ string ] interface { } {
"read_bytes_sec" : iot . ReadBytesSec ,
"read_bytes_sec_max" : iot . ReadBytesSecMax ,
"read_iops_sec" : iot . ReadIOPSSec ,
"read_iops_sec_max" : iot . ReadIOPSSecMax ,
"size_iops_sec" : iot . SizeIOPSSec ,
"total_bytes_sec" : iot . TotalBytesSec ,
"total_bytes_sec_max" : iot . TotalBytesSecMax ,
"total_iops_sec" : iot . TotalIOPSSec ,
"total_iops_sec_max" : iot . TotalIOPSSecMax ,
"write_bytes_sec" : iot . WriteBytesSec ,
"write_bytes_sec_max" : iot . WriteBytesSecMax ,
"write_iops_sec" : iot . WriteIOPSSec ,
"write_iops_sec_max" : iot . WriteIOPSSecMax ,
}
res = append ( res , temp )
return res
}
func flattendDiskSnapshotList ( sl compute . ListDetailedSnapshots ) [ ] interface { } {
res := make ( [ ] interface { } , 0 )
for _ , snapshot := range sl {
temp := map [ string ] interface { } {
"guid" : snapshot . GUID ,
"label" : snapshot . Label ,
"res_id" : snapshot . ResID ,
"snap_set_guid" : snapshot . SnapSetGUID ,
"snap_set_time" : snapshot . SnapSetTime ,
"timestamp" : snapshot . TimeStamp ,
}
res = append ( res , temp )
}
return res
}