From a63a35ca93d274ae8c17bffefa46378454836ef0 Mon Sep 17 00:00:00 2001 From: Sergey Shubin svs1370 Date: Tue, 5 Oct 2021 17:47:08 +0300 Subject: [PATCH] Switch to native Set methods for network definition blok, hardcode disk type D --- decort/data_source_disk.go | 14 +--- decort/resource_disk.go | 33 ++++---- decort/utility_compute.go | 162 ++++++++----------------------------- 3 files changed, 47 insertions(+), 162 deletions(-) diff --git a/decort/data_source_disk.go b/decort/data_source_disk.go index 9889509..8236f4a 100644 --- a/decort/data_source_disk.go +++ b/decort/data_source_disk.go @@ -67,18 +67,6 @@ func flattenDisk(d *schema.ResourceData, disk_facts string) error { // d.Set("compute_id", model.ComputeID) d.Set("description", model.Desc) - // d.Set("status", model.Status) - // d.Set("tech_status", model.TechStatus) - - /* we do not manage snapshots via Terraform yet (and probably, never will), so - // keep this block commented out for a while - if len(model.Snapshots) > 0 { - log.Debugf("flattenDisk: calling flattenDiskSnapshots") - if err = d.Set("nics", flattenDiskSnapshots(model.Snapshots)); err != nil { - return err - } - } - */ return nil } @@ -137,7 +125,7 @@ func dataSourceDiskSchemaMake() map[string]*schema.Schema { "type": { Type: schema.TypeString, Computed: true, - Description: "Type of this disk.", + Description: "Type of this disk. E.g. D for data disks, B for boot.", }, "description": { diff --git a/decort/resource_disk.go b/decort/resource_disk.go index d0225cc..a19bf08 100644 --- a/decort/resource_disk.go +++ b/decort/resource_disk.go @@ -46,7 +46,7 @@ func resourceDiskCreate(d *schema.ResourceData, m interface{}) error { urlValues.Add("gid", fmt.Sprintf("%d", DefaultGridID)) // we use default Grid ID, which was obtained along with DECORT Controller init urlValues.Add("name", d.Get("name").(string)) urlValues.Add("size", fmt.Sprintf("%d", d.Get("size").(int))) - urlValues.Add("type", d.Get("type").(string)) + urlValues.Add("type", "D") // NOTE: only disks of Data type are managed via plugin urlValues.Add("sep_id", fmt.Sprintf("%d", d.Get("sep_id").(int))) urlValues.Add("pool", d.Get("pool").(string)) @@ -128,10 +128,14 @@ func resourceDiskUpdate(d *schema.ResourceData, m interface{}) error { d.SetPartial("name") } + /* + NOTE: plugin will manage disks of type "Data" only, and type cannot be changed once disk is created + oldType, newType := d.GetChange("type") if oldType.(string) != newType.(string) { return fmt.Errorf("resourceDiskUpdate: Disk ID %s - changing type of existing disk not allowed", d.Id()) } + */ d.Partial(false) @@ -233,6 +237,8 @@ func resourceDiskSchemaMake() map[string]*schema.Schema { Description: "Size of the disk in GB. Note, that existing disks can only be grown in size.", }, + /* We moved "type" attribute to computed attributes section, as plugin manages disks of only + one type - "D", e.g. data disks. "type": { Type: schema.TypeString, Optional: true, @@ -241,6 +247,7 @@ func resourceDiskSchemaMake() map[string]*schema.Schema { ValidateFunc: validation.StringInSlice([]string{"B", "D"}, false), Description: "Optional type of this disk. Defaults to D, i.e. data disk. Cannot be changed for existing disks.", }, + */ "description": { Type: schema.TypeString, @@ -262,6 +269,12 @@ func resourceDiskSchemaMake() map[string]*schema.Schema { Description: "ID of the image, which this disk was cloned from (if ever cloned).", }, + "type": { + Type: schema.TypeString, + Computed: true, + Description: "Type of this disk.", + }, + "sep_type": { Type: schema.TypeString, Computed: true, @@ -277,24 +290,6 @@ func resourceDiskSchemaMake() map[string]*schema.Schema { }, Description: "List of user-created snapshots for this disk." }, - - "status": { - Type: schema.TypeString, - Computed: true, - Description: "Current model status of this disk.", - }, - - "tech_status": { - Type: schema.TypeString, - Computed: true, - Description: "Current technical status of this disk.", - }, - - "compute_id": { - Type: schema.TypeInt, - Computed: true, - Description: "ID of the compute instance where this disk is attached to, or 0 for unattached disk.", - }, */ } diff --git a/decort/utility_compute.go b/decort/utility_compute.go index 0ff544d..8767a25 100644 --- a/decort/utility_compute.go +++ b/decort/utility_compute.go @@ -40,6 +40,8 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD // d is filled with data according to computeResource schema, so extra disks config is retrieved via "extra_disks" key // If do_delta is true, this function will identify changes between new and existing specs for extra disks and try to // update compute configuration accordingly + // Otherwise it will apply whatever is found in the new set of "extra_disks" right away. + // Primary use of do_delta=false is when calling this function from compute Create handler. // Note that this function will not abort on API errors, but will continue to configure (attach / detach) other individual // disks via atomic API calls. However, it will not retry failed manipulation on the same disk. @@ -48,18 +50,6 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD // NB: as of rc-1.25 "extra_disks" are TypeSet with the elem of TypeInt old_set, new_set := d.GetChange("extra_disks") - /* - old_disks := make([]interface{},0,0) - if old_set != nil { - old_disks = old_set.([]interface{}) - } - - new_disks := make([]interface{},0,0) - if new_set != nil { - new_disks = new_set.([]interface{}) - } - */ - apiErrCount := 0 var lastSavedError error @@ -90,41 +80,7 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD } detach_set := old_set.(*schema.Set).Difference(new_set.(*schema.Set)) - /* - var attach_list, detach_list []int - match := false - for _, oDisk := range old_disks { - match = false - for _, nDisk := range new_disks { - if oDisk.(int) == nDisk.(int) { - match = true - break - } - } - if !match { - detach_list = append(detach_list, oDisk.(int)) - } - } - */ log.Debugf("utilityComputeExtraDisksConfigure: detach set has %d items for Compute ID %s", detach_set.Len(), d.Id()) - - /* - for _, nDisk := range new_disks { - match = false - for _, oDisk := range old_disks { - if nDisk.(int) == oDisk.(int) { - match = true - break - } - } - if !match { - attach_list = append(attach_list, nDisk.(int)) - } - } - */ - attach_set := new_set.(*schema.Set).Difference(old_set.(*schema.Set)) - log.Debugf("utilityComputeExtraDisksConfigure: attach set has %d items for Compute ID %s", attach_set.Len(), d.Id()) - for _, diskId := range detach_set.List() { urlValues := &url.Values{} urlValues.Add("computeId", d.Id()) @@ -132,12 +88,14 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD _, err := ctrl.decortAPICall("POST", ComputeDiskDetachAPI, urlValues) if err != nil { // failed to detach disk - there will be partial resource update - log.Debugf("utilityComputeExtraDisksConfigure: failed to detach disk ID %d from Compute ID %s: %s", diskId.(int), d.Id(), err) + log.Errorf("utilityComputeExtraDisksConfigure: failed to detach disk ID %d from Compute ID %s: %s", diskId.(int), d.Id(), err) apiErrCount++ lastSavedError = err } } + attach_set := new_set.(*schema.Set).Difference(old_set.(*schema.Set)) + log.Debugf("utilityComputeExtraDisksConfigure: attach set has %d items for Compute ID %s", attach_set.Len(), d.Id()) for _, diskId := range attach_set.List() { urlValues := &url.Values{} urlValues.Add("computeId", d.Id()) @@ -145,7 +103,7 @@ func (ctrl *ControllerCfg) utilityComputeExtraDisksConfigure(d *schema.ResourceD _, err := ctrl.decortAPICall("POST", ComputeDiskAttachAPI, urlValues) if err != nil { // failed to attach disk - there will be partial resource update - log.Debugf("utilityComputeExtraDisksConfigure: failed to attach disk ID %d to Compute ID %s: %s", diskId.(int), d.Id(), err) + log.Errorf("utilityComputeExtraDisksConfigure: failed to attach disk ID %d to Compute ID %s: %s", diskId.(int), d.Id(), err) apiErrCount++ lastSavedError = err } @@ -164,34 +122,22 @@ func (ctrl *ControllerCfg) utilityComputeNetworksConfigure(d *schema.ResourceDat // "d" is filled with data according to computeResource schema, so extra networks config is retrieved via "network" key // If do_delta is true, this function will identify changes between new and existing specs for network and try to // update compute configuration accordingly + // Otherwise it will apply whatever is found in the new set of "network" right away. + // Primary use of do_delta=false is when calling this function from compute Create handler. - /* - argVal, argSet := d.GetOk("network") - if !argSet || len(argVal.([]interface{})) < 1 { - return nil - } - net_list := argVal.([]interface{}) // network is ar array of maps; for keys see func networkSubresourceSchemaMake() definition - */ - old_set, new_set := d.GetChange("network") - oldNets := make([]interface{},0,0) - if old_set != nil { - oldNets = old_set.(*schema.Set).List() // network set is ar array of maps; for keys see func networkSubresourceSchemaMake() definition - } - - newNets := make([]interface{},0,0) - if new_set != nil { - newNets = new_set.(*schema.Set).List() // network set is ar array of maps; for keys see func networkSubresourceSchemaMake() definition - } - apiErrCount := 0 var lastSavedError error if !do_delta { - for _, net := range newNets { + if new_set.(*schema.Set).Len() < 1 { + return nil + } + + for _, runner := range new_set.(*schema.Set).List() { urlValues := &url.Values{} - net_data := net.(map[string]interface{}) + net_data := runner.(map[string]interface{}) urlValues.Add("computeId", d.Id()) urlValues.Add("netType", net_data["net_type"].(string)) urlValues.Add("netId", fmt.Sprintf("%d", net_data["net_id"].(int))) @@ -215,84 +161,40 @@ func (ctrl *ControllerCfg) utilityComputeNetworksConfigure(d *schema.ResourceDat return nil } - var attachList, detachList []ComputeNetMgmtRecord - match := false - - for _, oRunner := range oldNets { - match = false - oSpecs := oRunner.(map[string]interface{}) - for _, nRunner := range newNets { - nSpecs := nRunner.(map[string]interface{}) - if oSpecs["net_id"].(int) == nSpecs["net_id"].(int) && oSpecs["net_type"].(string) == nSpecs["net_type"].(string) { - match = true - break - } - } - if !match { - newItem := ComputeNetMgmtRecord{ - ID: oSpecs["net_id"].(int), - Type: oSpecs["net_type"].(string), - IPAddress: oSpecs["ip_address"].(string), - MAC: oSpecs["mac"].(string), - } - detachList = append(detachList, newItem) - } - } - log.Debugf("utilityComputeNetworksConfigure: detach list has %d items for Compute ID %s", len(detachList), d.Id()) - - for _, nRunner := range newNets { - match = false - nSpecs := nRunner.(map[string]interface{}) - for _, oRunner := range oldNets { - oSpecs := oRunner.(map[string]interface{}) - if nSpecs["net_id"].(int) == oSpecs["net_id"].(int) && nSpecs["net_type"].(string) == oSpecs["net_type"].(string) { - match = true - break - } - } - if !match { - newItem := ComputeNetMgmtRecord{ - ID: nSpecs["net_id"].(int), - Type: nSpecs["net_type"].(string), - } - if nSpecs["ip_address"] != nil { - newItem.IPAddress = nSpecs["ip_address"].(string) - } else { - newItem.IPAddress = "" // make sure it is empty, if not coming from the schema - } - attachList = append(attachList, newItem) - } - } - log.Debugf("utilityComputeNetworksConfigure: attach list has %d items for Compute ID %s", len(attachList), d.Id()) - - for _, netRec := range detachList { + detach_set := old_set.(*schema.Set).Difference(new_set.(*schema.Set)) + log.Debugf("utilityComputeNetworksConfigure: detach set has %d items for Compute ID %s", detach_set.Len(), d.Id()) + for _, runner := range detach_set.List() { urlValues := &url.Values{} + net_data := runner.(map[string]interface{}) urlValues.Add("computeId", d.Id()) - urlValues.Add("ipAddr", netRec.IPAddress) - urlValues.Add("mac", netRec.MAC) + urlValues.Add("ipAddr", net_data["ip_address"].(string)) + urlValues.Add("mac", net_data["mac"].(string)) _, err := ctrl.decortAPICall("POST", ComputeNetDetachAPI, urlValues) if err != nil { // failed to detach this network - there will be partial resource update - log.Debugf("utilityComputeNetworksConfigure: failed to detach net ID %d of type %s from Compute ID %s: %s", - netRec.ID, netRec.Type, d.Id(), err) + log.Errorf("utilityComputeNetworksConfigure: failed to detach net ID %d of type %s from Compute ID %s: %s", + net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err) apiErrCount++ lastSavedError = err } } - for _, netRec := range attachList { + attach_set := new_set.(*schema.Set).Difference(old_set.(*schema.Set)) + log.Debugf("utilityComputeNetworksConfigure: attach set has %d items for Compute ID %s", attach_set.Len(), d.Id()) + for _, runner := range attach_set.List() { urlValues := &url.Values{} + net_data := runner.(map[string]interface{}) urlValues.Add("computeId", d.Id()) - urlValues.Add("netId", fmt.Sprintf("%d",netRec.ID)) - urlValues.Add("netType", netRec.Type) - if netRec.IPAddress != "" { - urlValues.Add("ipAddr", netRec.IPAddress) + urlValues.Add("netId", fmt.Sprintf("%d",net_data["net_id"].(int))) + urlValues.Add("netType", net_data["net_type"].(string)) + if net_data["ip_address"].(string) != "" { + urlValues.Add("ipAddr", net_data["ip_address"].(string)) } _, err := ctrl.decortAPICall("POST", ComputeNetAttachAPI, urlValues) if err != nil { // failed to attach this network - there will be partial resource update - log.Debugf("utilityComputeNetworksConfigure: failed to attach net ID %d of type %s from Compute ID %s: %s", - netRec.ID, netRec.Type, d.Id(), err) + log.Errorf("utilityComputeNetworksConfigure: failed to attach net ID %d of type %s to Compute ID %s: %s", + net_data["net_id"].(int), net_data["net_type"].(string), d.Id(), err) apiErrCount++ lastSavedError = err }