From eba3f411bdc31d8ad722c9d238100c9507ca85fd Mon Sep 17 00:00:00 2001 From: Sergey Shubin svs1370 Date: Wed, 17 Mar 2021 17:08:36 +0300 Subject: [PATCH] Implement Import function for Compute resource --- decort/data_source_compute.go | 16 ++++++++-------- decort/resource_compute.go | 6 +++++- decort/utility_compute.go | 25 ++++++++++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/decort/data_source_compute.go b/decort/data_source_compute.go index 88b209b..d9fb211 100644 --- a/decort/data_source_compute.go +++ b/decort/data_source_compute.go @@ -112,8 +112,8 @@ func parseComputeDisks(disks []DiskRecord) []interface{} { elem["sep_id"] = value.SepID elem["sep_type"] = value.SepType elem["pool"] = value.Pool - elem["status"] = value.Status - elem["tech_status"] = value.TechStatus + // elem["status"] = value.Status + // elem["tech_status"] = value.TechStatus elem["compute_id"] = value.ComputeID result[i] = elem } @@ -417,6 +417,7 @@ func dataSourceCompute() *schema.Resource { Description: "User-defined text description of this compute instance.", }, + /* "status": { Type: schema.TypeString, Computed: true, @@ -429,12 +430,11 @@ func dataSourceCompute() *schema.Resource { Description: "Current technical status of this compute instance.", }, - /* - "internal_ip": { - Type: schema.TypeString, - Computed: true, - Description: "Internal IP address of this Compute.", - }, + "internal_ip": { + Type: schema.TypeString, + Computed: true, + Description: "Internal IP address of this Compute.", + }, */ }, } diff --git a/decort/resource_compute.go b/decort/resource_compute.go index 0d37cf0..1ac8f36 100644 --- a/decort/resource_compute.go +++ b/decort/resource_compute.go @@ -230,6 +230,10 @@ func resourceCompute() *schema.Resource { Delete: resourceComputeDelete, Exists: resourceComputeExists, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ Create: &Timeout180s, Read: &Timeout30s, @@ -369,7 +373,6 @@ func resourceCompute() *schema.Resource { }, Description: "Specification for guest logins on this compute instance.", }, - */ "status": { Type: schema.TypeString, @@ -382,6 +385,7 @@ func resourceCompute() *schema.Resource { Computed: true, Description: "Current technical status of this compute instance.", }, + */ }, } } diff --git a/decort/utility_compute.go b/decort/utility_compute.go index ff14d83..41bce81 100644 --- a/decort/utility_compute.go +++ b/decort/utility_compute.go @@ -28,6 +28,7 @@ import ( "encoding/json" "fmt" "net/url" + "strconv" log "github.com/sirupsen/logrus" @@ -197,11 +198,24 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string, controller := m.(*ControllerCfg) urlValues := &url.Values{} - computeId, argSet := d.GetOk("compute_id") // NB: compute_id is NOT present in computeResource schema! - if argSet { + // make it possible to use "read" & "check presence" functions with comptue ID set so + // that Import of Compute resource is possible + idSet := false + theId, err := strconv.Atoi(d.Id()) + if err != nil || theId <= 0 { + computeId, argSet := d.GetOk("compute_id") // NB: compute_id is NOT present in computeResource schema! + if argSet { + theId = computeId.(int) + idSet = true + } + } else { + idSet = true + } + + if idSet { // compute ID is specified, try to get compute instance straight by this ID - log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", computeId.(int)) - urlValues.Add("computeId", fmt.Sprintf("%d", computeId.(int))) + log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", theId) + urlValues.Add("computeId", fmt.Sprintf("%d", theId)) computeFacts, err := controller.decortAPICall("POST", ComputeGetAPI, urlValues) if err != nil { return "", err @@ -209,6 +223,8 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string, return computeFacts, nil } + // ID was not set in the schema upon entering this function - work through Compute name + // and RG ID computeName, argSet := d.GetOk("name") if !argSet { return "", fmt.Errorf("Cannot locate compute instance if name is empty and no compute ID specified") @@ -251,5 +267,4 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string, } return "", nil // there should be no error if Compute does not exist - // return "", fmt.Errorf("Cannot find Compute name %q in resource group ID %d", name, rgid) }