From 78258789ebfeaa06e3ea1982317736f8d4f3ec3b Mon Sep 17 00:00:00 2001 From: Sergey Shubin svs1370 Date: Thu, 29 Apr 2021 13:30:57 +0300 Subject: [PATCH] Change networks attribute type to TypeSet and fix compute resource state issue --- decort/data_source_compute.go | 35 ++++++++++++++--------------------- decort/models_api.go | 6 +++--- decort/resource_compute.go | 4 ++-- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/decort/data_source_compute.go b/decort/data_source_compute.go index 0ec6044..98c99c2 100644 --- a/decort/data_source_compute.go +++ b/decort/data_source_compute.go @@ -149,20 +149,15 @@ func parseBootDiskId(disks []DiskRecord) uint { // Parse the list of interfaces from compute/get response into a list of networks // attached to this compute -func parseComputeInterfacesToNetworks(ifaces []InterfaceRecord) []interface{} { +func parseComputeInterfacesToNetworks(ifaces []InterfaceRecord) []map[string]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 := make([]interface{}, length) - - if length == 0 { - return result - } - - elem := make(map[string]interface{}) + result := make([]map[string]interface{}, length, length) for i, value := range ifaces { + elem := make(map[string]interface{}) // Keys in this map should correspond to the Schema definition // as returned by networkSubresourceSchemaMake() elem["net_id"] = value.NetID @@ -170,30 +165,28 @@ func parseComputeInterfacesToNetworks(ifaces []InterfaceRecord) []interface{} { elem["ip_address"] = value.IPAddress elem["mac"] = value.MAC + // log.Debugf(" element %d: net_id=%d, net_type=%s", i, value.NetID, value.NetType) + result[i] = elem } return result } -func parseComputeInterfaces(ifaces []InterfaceRecord) []interface{} { +func parseComputeInterfaces(ifaces []InterfaceRecord) []map[string]interface{} { // return value was designed to d.Set("interfaces",) item of dataSourceCompute schema // However, this item was excluded from the schema as it is not directly // managed through Terraform length := len(ifaces) log.Debugf("parseComputeInterfaces: called for %d ifaces", length) - result := make([]interface{}, length) - - if length == 0 { - return result - } - - elem := make(map[string]interface{}) + result := make([]map[string]interface{}, length, length) for i, value := range ifaces { // Keys in this map should correspond to the Schema definition // as returned by dataSourceInterfaceSchemaMake() + elem := make(map[string]interface{}) + elem["net_id"] = value.NetID elem["net_type"] = value.NetType elem["ip_address"] = value.IPAddress @@ -225,7 +218,7 @@ func flattenCompute(d *schema.ResourceData, compFacts string) error { // NOTE: this function modifies ResourceData argument - as such it should never be called // from resourceComputeExists(...) method model := ComputeGetResp{} - log.Debugf("flattenCompute: ready to unmarshal string %q", compFacts) + log.Debugf("flattenCompute: ready to unmarshal string %s", compFacts) err := json.Unmarshal([]byte(compFacts), &model) if err != nil { return err @@ -248,8 +241,8 @@ func flattenCompute(d *schema.ResourceData, compFacts string) error { d.Set("boot_disk_id", parseBootDiskId(model.Disks)) // we may need boot disk ID in resize operations d.Set("image_id", model.ImageID) d.Set("description", model.Desc) - d.Set("status", model.Status) - d.Set("tech_status", model.TechStatus) + // d.Set("status", model.Status) + // d.Set("tech_status", model.TechStatus) if len(model.Disks) > 0 { log.Debugf("flattenCompute: calling parseComputeDisksToExtraDisks for %d disks", len(model.Disks)) @@ -401,9 +394,9 @@ func dataSourceCompute() *schema.Resource { */ "network": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, - MaxItems: MaxNetworksPerCompute, + // MaxItems: MaxNetworksPerCompute, Elem: &schema.Resource{ Schema: networkSubresourceSchemaMake(), }, diff --git a/decort/models_api.go b/decort/models_api.go index 09b0ea8..b92164c 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -356,7 +356,7 @@ type ComputeGetResp struct { ID uint `json:"id"` ImageID int `json:"imageId"` ImageName string `json:"imageName"` - Interfaces []InterfaceRecord `json:"interfaces` + Interfaces []InterfaceRecord `json:"interfaces"` LockStatus string `json:"lockStatus"` ManagerID int `json:"managerId"` ManagerType string `json:"manageType"` @@ -388,7 +388,7 @@ type ComputeGetResp struct { // type ImageRecord struct { AccountID uint `json:"accountId"` - Arch string `json:"architecture` + Arch string `json:"architecture"` BootType string `json:"bootType"` IsBootable bool `json:"bootable"` IsCdrom bool `json:"cdrom"` @@ -445,7 +445,7 @@ type AccountsListResp []AccountRecord // type PfwRecord struct { ID int `json:"id"` - LocalIP string `json:"localIp` + LocalIP string `json:"localIp"` LocalPort int `json:"localPort"` Protocol string `json:"protocol"` PublicPortEnd int `json:"publicPortEnd"` diff --git a/decort/resource_compute.go b/decort/resource_compute.go index f17356a..45e1e95 100644 --- a/decort/resource_compute.go +++ b/decort/resource_compute.go @@ -380,9 +380,9 @@ func resourceCompute() *schema.Resource { }, "network": { - Type: schema.TypeList, + Type: schema.TypeSet, Optional: true, - MaxItems: MaxNetworksPerCompute, + // MaxItems: MaxNetworksPerCompute, Elem: &schema.Resource{ Schema: networkSubresourceSchemaMake(), },