From 422658542ceb05ccd548a117205ded0aa5639cdf Mon Sep 17 00:00:00 2001 From: Sergey Shubin svs1370 Date: Fri, 12 Feb 2021 18:10:02 +0300 Subject: [PATCH] Selectively beautify code based on lint reports --- decort/constants.go | 9 +- decort/data_source_compute.go | 12 +-- decort/resource_compute.go | 177 ++++++++++++++++++---------------- decort/utility_account.go | 66 ++++++------- decort/utility_compute.go | 108 ++++++++++----------- 5 files changed, 194 insertions(+), 178 deletions(-) diff --git a/decort/constants.go b/decort/constants.go index 1ed1d28..6757389 100644 --- a/decort/constants.go +++ b/decort/constants.go @@ -24,4 +24,11 @@ Visit https://github.com/rudecs/terraform-provider-decort for full source code p package decort -const LimitMaxVinsPerResgroup=4 \ No newline at end of file +// LimitMaxVinsPerResgroup set maximum number of VINs instances per Resource Group +const LimitMaxVinsPerResgroup=4 + +// MaxSshKeysPerCompute sets maximum number of user:ssh_key pairs to authorize when creating new compute +const MaxSshKeysPerCompute=12 + +// MaxExtraDisksPerCompute sets maximum number of extra disks that can be added when creating new compute +const MaxExtraDisksPerCompute=12 \ No newline at end of file diff --git a/decort/data_source_compute.go b/decort/data_source_compute.go index ee06865..ac51949 100644 --- a/decort/data_source_compute.go +++ b/decort/data_source_compute.go @@ -107,15 +107,15 @@ func parseComputeInterfaces(ifaces []InterfaceRecord) []interface{} { return result // this result will be used to d.Set("interfaces",) item of dataSourceCompute schema } -func flattenCompute(d *schema.ResourceData, comp_facts string) error { +func flattenCompute(d *schema.ResourceData, compFacts string) error { // This function expects that comp_facts 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 model := ComputeGetResp{} - log.Debugf("flattenCompute: ready to unmarshal string %q", comp_facts) - err := json.Unmarshal([]byte(comp_facts), &model) + log.Debugf("flattenCompute: ready to unmarshal string %q", compFacts) + err := json.Unmarshal([]byte(compFacts), &model) if err != nil { return err } @@ -163,15 +163,15 @@ func flattenCompute(d *schema.ResourceData, comp_facts string) error { } func dataSourceComputeRead(d *schema.ResourceData, m interface{}) error { - comp_facts, err := utilityComputeCheckPresence(d, m) - if comp_facts == "" { + compFacts, err := utilityComputeCheckPresence(d, m) + if compFacts == "" { // if empty string is returned from utilityComputeCheckPresence then there is no // such Compute and err tells so - just return it to the calling party d.SetId("") // ensure ID is empty return err } - return flattenCompute(d, comp_facts) + return flattenCompute(d, compFacts) } func dataSourceCompute() *schema.Resource { diff --git a/decort/resource_compute.go b/decort/resource_compute.go index 0a9fba5..627fe3f 100644 --- a/decort/resource_compute.go +++ b/decort/resource_compute.go @@ -94,18 +94,18 @@ func resourceComputeCreate(d *schema.ResourceData, m interface{}) error { // by separate API calls) d.Partial(true) controller := m.(*ControllerCfg) - url_values := &url.Values{} - url_values.Add("cloudspaceId", fmt.Sprintf("%d", machine.ResGroupID)) - url_values.Add("name", machine.Name) - url_values.Add("description", machine.Description) - url_values.Add("vcpus", fmt.Sprintf("%d", machine.Cpu)) - url_values.Add("memory", fmt.Sprintf("%d", machine.Ram)) - url_values.Add("imageId", fmt.Sprintf("%d", machine.ImageID)) - url_values.Add("disksize", fmt.Sprintf("%d", machine.BootDisk.Size)) + urlValues := &url.Values{} + urlValues.Add("cloudspaceId", fmt.Sprintf("%d", machine.ResGroupID)) + urlValues.Add("name", machine.Name) + urlValues.Add("description", machine.Description) + urlValues.Add("vcpus", fmt.Sprintf("%d", machine.Cpu)) + urlValues.Add("memory", fmt.Sprintf("%d", machine.Ram)) + urlValues.Add("imageId", fmt.Sprintf("%d", machine.ImageID)) + urlValues.Add("disksize", fmt.Sprintf("%d", machine.BootDisk.Size)) if len(machine.SshKeys) > 0 { - url_values.Add("userdata", makeSshKeysArgString(machine.SshKeys)) + urlValues.Add("userdata", makeSshKeysArgString(machine.SshKeys)) } - api_resp, err := controller.decortAPICall("POST", MachineCreateAPI, url_values) + api_resp, err := controller.decortAPICall("POST", MachineCreateAPI, urlValues) if err != nil { return err } @@ -218,8 +218,8 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error { log.Printf("resourceComputeRead: called for VM name %q, ResGroupID %d", d.Get("name").(string), d.Get("rgid").(int)) - comp_facts, err := utilityComputeCheckPresence(d, m) - if comp_facts == "" { + compFacts, err := utilityComputeCheckPresence(d, m) + if compFacts == "" { if err != nil { return err } @@ -227,7 +227,7 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error { return nil } - if err = flattenCompute(d, comp_facts); err != nil { + if err = flattenCompute(d, compFacts); err != nil { return err } log.Printf("resourceComputeRead: after flattenCompute: VM ID %s, VM name %q, ResGroupID %d", @@ -236,12 +236,12 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error { // Not all parameters, that we may need, are returned by machines/get API // Continue with further reading of VM subresource parameters: controller := m.(*ControllerCfg) - url_values := &url.Values{} + urlValues := &url.Values{} /* // Obtain information on external networks - url_values.Add("machineId", d.Id()) - body_string, err := controller.decortAPICall("POST", VmExtNetworksListAPI, url_values) + urlValues.Add("machineId", d.Id()) + body_string, err := controller.decortAPICall("POST", VmExtNetworksListAPI, urlValues) if err != nil { return err } @@ -271,10 +271,10 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error { // // Obtain information on port forwards /* - url_values.Add("cloudspaceId", fmt.Sprintf("%d", d.Get("rgid"))) - url_values.Add("machineId", d.Id()) + urlValues.Add("cloudspaceId", fmt.Sprintf("%d", d.Get("rgid"))) + urlValues.Add("machineId", d.Id()) pfw_list := PortforwardsResp{} - body_string, err := controller.decortAPICall("POST", PortforwardsListAPI, url_values) + body_string, err := controller.decortAPICall("POST", PortforwardsListAPI, urlValues) if err != nil { return err } @@ -301,24 +301,24 @@ func resourceComputeUpdate(d *schema.ResourceData, m interface{}) error { } func resourceComputeDelete(d *schema.ResourceData, m interface{}) error { - // NOTE: this method destroys target VM with flag "permanently", so there is no way to - // restore destroyed VM + // NOTE: this method destroys target Compute instance with flag "permanently", so + // there is no way to restore destroyed Compute log.Printf("resourceComputeDelete: called for VM name %q, ResGroupID %d", d.Get("name").(string), d.Get("rgid").(int)) - comp_facts, err := utilityComputeCheckPresence(d, m) - if comp_facts == "" { - // the target VM does not exist - in this case according to Terraform best practice + compFacts, err := utilityComputeCheckPresence(d, m) + if compFacts == "" { + // the target Compute does not exist - in this case according to Terraform best practice // we exit from Destroy method without error return nil } params := &url.Values{} - params.Add("machineId", d.Id()) + params.Add("computeId", d.Id()) params.Add("permanently", "true") controller := m.(*ControllerCfg) - comp_facts, err = controller.decortAPICall("POST", MachineDeleteAPI, params) + compFacts, err = controller.decortAPICall("POST", ComputeDeleteAPI, params) if err != nil { return err } @@ -331,8 +331,8 @@ func resourceComputeExists(d *schema.ResourceData, m interface{}) (bool, error) log.Printf("resourceComputeExist: called for VM name %q, ResGroupID %d", d.Get("name").(string), d.Get("rgid").(int)) - comp_facts, err := utilityComputeCheckPresence(d, m) - if comp_facts == "" { + compFacts, err := utilityComputeCheckPresence(d, m) + if compFacts == "" { if err != nil { return false, err } @@ -363,123 +363,132 @@ func resourceCompute() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - Description: "Name of this virtual machine. This parameter is case sensitive.", + Description: "Name of this compute. This parameter is case sensitive and must be unique in the resource group.", }, - "rgid": { + "rg_id": { Type: schema.TypeInt, Required: true, ValidateFunc: validation.IntAtLeast(1), - Description: "ID of the resource group where this virtual machine should be deployed.", + Description: "ID of the resource group where this compute should be deployed.", + }, + + "arch": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Hardware architecture of this compute instance.", }, "cpu": { Type: schema.TypeInt, Required: true, ValidateFunc: validation.IntBetween(1, 64), - Description: "Number of CPUs to allocate to this virtual machine.", + Description: "Number of CPUs to allocate to this compute instance.", }, "ram": { Type: schema.TypeInt, Required: true, ValidateFunc: validation.IntAtLeast(512), - Description: "Amount of RAM in MB to allocate to this virtual machine.", + Description: "Amount of RAM in MB to allocate to this compute instance.", }, "image_id": { Type: schema.TypeInt, Required: true, ForceNew: true, - Description: "ID of the OS image to base this virtual machine on.", + Description: "ID of the OS image to base this compute instance on.", }, - "boot_disk": { - Type: schema.TypeList, - Required: true, - MaxItems: 1, - Elem: &schema.Resource{ - Schema: diskSubresourceSchema(), - }, - Description: "Specification for a boot disk on this virtual machine.", + "boot_disk_size": { + Type: schema.TypeInt, + Optional: true, + Description: "Size of the boot disk on this compute instance.", }, - "data_disks": { + "extra_disks": { Type: schema.TypeList, Optional: true, - MaxItems: 12, - Elem: &schema.Resource{ - Schema: diskSubresourceSchema(), + MaxItems: MaxExtraDisksPerCompute, + Elem: &schema.Schema{ + Type: schema.TypeInt, }, - Description: "Specification for data disks on this virtual machine.", + Description: "Optional list of IDs of the extra disks to attach to this compute.", }, - "guest_logins": { + "ssh_keys": { Type: schema.TypeList, - Computed: true, + Optional: true, + MaxItems: MaxSshKeysPerCompute, Elem: &schema.Resource{ - Schema: loginsSubresourceSchema(), + Schema: sshSubresourceSchemaMake(), }, - Description: "Specification for guest logins on this virtual machine.", + Description: "SSH keys to authorize on this compute instance.", }, - "networks": { - Type: schema.TypeList, - Optional: true, - MaxItems: 8, - Elem: &schema.Resource{ - Schema: networkSubresourceSchema(), - }, - Description: "Specification for the networks to connect this virtual machine to.", + "description": { + Type: schema.TypeString, + Optional: true, + Description: "Description of this compute instance.", + }, + + // The rest are Compute properties, which are "computed" once it is created + "rg_name": { + Type: schema.TypeString, + Computed: true, + Description: "Name of the resource group where this compute instance is located.", + }, + + "account_id": { + Type: schema.TypeInt, + Computed: true, + Description: "ID of the account this compute instance belongs to.", + }, + + "account_name": { + Type: schema.TypeString, + Computed: true, + Description: "Name of the account this compute instance belongs to.", }, - "nics": { + "disks": { Type: schema.TypeList, Computed: true, - MaxItems: 8, Elem: &schema.Resource{ - Schema: nicSubresourceSchema(), + Schema: dataSourceDiskSchemaMake(), // ID, type, name, size, account ID, SEP ID, SEP type, pool, status, tech status, compute ID, image ID }, - Description: "Specification for the virutal NICs allocated to this virtual machine.", + Description: "Detailed specification for all disks attached to this compute instance (including bood disk).", }, - "ssh_keys": { + "interfaces": { Type: schema.TypeList, - Optional: true, - MaxItems: 12, + Computed: true, Elem: &schema.Resource{ - Schema: sshSubresourceSchema(), + Schema: interfaceSubresourceSchemaMake(), }, - Description: "SSH keys to authorize on this virtual machine.", + Description: "Specification for the virtual NICs configured on this compute instance.", }, - "port_forwards": { + "guest_logins": { Type: schema.TypeList, - Optional: true, - MaxItems: 12, + Computed: true, Elem: &schema.Resource{ - Schema: portforwardSubresourceSchema(), + Schema: loginsSubresourceSchemaMake(), }, - Description: "Specification for the port forwards to configure for this virtual machine.", - }, - - "description": { - Type: schema.TypeString, - Optional: true, - Description: "Description of this virtual machine.", + Description: "Specification for guest logins on this compute instance.", }, - "user": { + "status": { Type: schema.TypeString, Computed: true, - Description: "Default login name for the guest OS on this virtual machine.", + Description: "Current model status of this compute instance.", }, - "password": { + "tech_status": { Type: schema.TypeString, Computed: true, - Sensitive: true, - Description: "Default password for the guest OS login on this virtual machine.", + Description: "Current technical status of this compute instance.", }, }, } diff --git a/decort/utility_account.go b/decort/utility_account.go index b8ef7ea..9b1bbad 100644 --- a/decort/utility_account.go +++ b/decort/utility_account.go @@ -37,57 +37,57 @@ import ( func utilityAccountCheckPresence(d *schema.ResourceData, m interface{}) (string, error) { controller := m.(*ControllerCfg) - url_values := &url.Values{} + urlValues := &url.Values{} - acc_id, arg_set := d.GetOk("account_id") - if arg_set { + accId, argSet := d.GetOk("account_id") + if argSet { // get Account right away by its ID - log.Debugf("utilityAccountCheckPresence: locating Account by its ID %d", acc_id.(int)) - url_values.Add("accountId", fmt.Sprintf("%d", acc_id.(int))) - api_resp, err := controller.decortAPICall("POST", AccountsGetAPI, url_values) + log.Debugf("utilityAccountCheckPresence: locating Account by its ID %d", accId.(int)) + urlValues.Add("accountId", fmt.Sprintf("%d", accId.(int))) + apiResp, err := controller.decortAPICall("POST", AccountsGetAPI, urlValues) if err != nil { return "", err } - return api_resp, nil + return apiResp, nil } - acc_name, arg_set := d.GetOk("name") - if !arg_set { + accName, argSet := d.GetOk("name") + if !argSet { // neither ID nor name - no account for you! - return "", fmt.Errorf("Cannot check account presence if name is empty and no account ID specified.") + return "", fmt.Errorf("Cannot check account presence if name is empty and no account ID specified") } - api_resp, err := controller.decortAPICall("POST", AccountsListAPI, url_values) + apiResp, err := controller.decortAPICall("POST", AccountsListAPI, urlValues) if err != nil { return "", err } - // log.Debugf("%s", api_resp) + // log.Debugf("%s", apiResp) // log.Debugf("utilityAccountCheckPresence: ready to decode response body from %q", AccountsListAPI) - acc_list := AccountsListResp{} - err = json.Unmarshal([]byte(api_resp), &acc_list) + accList := AccountsListResp{} + err = json.Unmarshal([]byte(apiResp), &accList) if err != nil { return "", err } - log.Debugf("utilityAccountCheckPresence: traversing decoded Json of length %d", len(acc_list)) - for index, item := range acc_list { + log.Debugf("utilityAccountCheckPresence: traversing decoded Json of length %d", len(accList)) + for index, item := range accList { // match by account name - if item.Name == acc_name.(string) { + if item.Name == accName.(string) { log.Debugf("utilityAccountCheckPresence: match account name %q / ID %d at index %d", item.Name, item.ID, index) // NB: unlike accounts/get API, accounts/list API returns abridged set of account info, // for instance it does not return quotas - reencoded_item, err := json.Marshal(item) + reencodedItem, err := json.Marshal(item) if err != nil { return "", err } - return string(reencoded_item[:]), nil + return string(reencodedItem[:]), nil } } - return "", fmt.Errorf("Cannot find account name %q", acc_name.(string)) + return "", fmt.Errorf("Cannot find account name %q", accName.(string)) } func utilityGetAccountIdBySchema(d *schema.ResourceData, m interface{}) (int, error) { @@ -112,28 +112,28 @@ func utilityGetAccountIdBySchema(d *schema.ResourceData, m interface{}) (int, er */ - account_id, arg_set := d.GetOk("account_id") - if arg_set { - if account_id.(int) > 0 { - return account_id.(int), nil + accId, argSet := d.GetOk("account_id") + if argSet { + if accId.(int) > 0 { + return accId.(int), nil } - return 0, fmt.Errorf("Account ID must be positive.") + return 0, fmt.Errorf("Account ID must be positive") } - account_name, arg_set := d.GetOk("account_name") - if !arg_set { - return 0, fmt.Errorf("Non-empty account name or positive account ID must be specified.") + accName, argSet := d.GetOk("account_name") + if !argSet { + return 0, fmt.Errorf("Either non-empty account name or positive account ID must be specified") } controller := m.(*ControllerCfg) - url_values := &url.Values{} - body_string, err := controller.decortAPICall("POST", AccountsListAPI, url_values) + urlValues := &url.Values{} + apiResp, err := controller.decortAPICall("POST", AccountsListAPI, urlValues) if err != nil { return 0, err } model := AccountsListResp{} - err = json.Unmarshal([]byte(body_string), &model) + err = json.Unmarshal([]byte(apiResp), &model) if err != nil { return 0, err } @@ -141,12 +141,12 @@ func utilityGetAccountIdBySchema(d *schema.ResourceData, m interface{}) (int, er log.Debugf("utilityGetAccountIdBySchema: traversing decoded Json of length %d", len(model)) for index, item := range model { // need to match Account by name - if item.Name == account_name.(string) { + if item.Name == accName.(string) { log.Debugf("utilityGetAccountIdBySchema: match Account name %q / ID %d at index %d", item.Name, item.ID, index) return item.ID, nil } } - return 0, fmt.Errorf("Cannot find account %q for the current user. Check account name and your access rights", account_name.(string)) + return 0, fmt.Errorf("Cannot find account %q for the current user. Check account name and your access rights", accName.(string)) } diff --git a/decort/utility_compute.go b/decort/utility_compute.go index 302533a..e7cade6 100644 --- a/decort/utility_compute.go +++ b/decort/utility_compute.go @@ -39,17 +39,17 @@ import ( func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error { for index, disk := range mcfg.DataDisks { - url_values := &url.Values{} - // url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) - url_values.Add("accountId", fmt.Sprintf("%d", mcfg.TenantID)) - url_values.Add("gid", fmt.Sprintf("%d", mcfg.GridID)) - url_values.Add("name", fmt.Sprintf("%s", disk.Label)) - url_values.Add("description", fmt.Sprintf("Data disk for VM ID %d / VM Name: %s", mcfg.ID, mcfg.Name)) - url_values.Add("size", fmt.Sprintf("%d", disk.Size)) - url_values.Add("type", "D") - // url_values.Add("iops", ) - - disk_id_resp, err := ctrl.decortAPICall("POST", DiskCreateAPI, url_values) + urlValues := &url.Values{} + // urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) + urlValues.Add("accountId", fmt.Sprintf("%d", mcfg.TenantID)) + urlValues.Add("gid", fmt.Sprintf("%d", mcfg.GridID)) + urlValues.Add("name", fmt.Sprintf("%s", disk.Label)) + urlValues.Add("description", fmt.Sprintf("Data disk for VM ID %d / VM Name: %s", mcfg.ID, mcfg.Name)) + urlValues.Add("size", fmt.Sprintf("%d", disk.Size)) + urlValues.Add("type", "D") + // urlValues.Add("iops", ) + + disk_id_resp, err := ctrl.decortAPICall("POST", DiskCreateAPI, urlValues) if err != nil { // failed to create disk - partial resource update return err @@ -65,11 +65,11 @@ func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error { // now that we have disk created and stored its ID in the mcfg.DataDisks[index].ID // we can attempt attaching the disk to the VM - url_values = &url.Values{} - // url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) - url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) - url_values.Add("diskId", disk_id_resp) - _, err = ctrl.decortAPICall("POST", DiskAttachAPI, url_values) + urlValues = &url.Values{} + // urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) + urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) + urlValues.Add("diskId", disk_id_resp) + _, err = ctrl.decortAPICall("POST", DiskAttachAPI, urlValues) if err != nil { // failed to attach disk - partial resource update return err @@ -81,14 +81,14 @@ func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error { func (ctrl *ControllerCfg) utilityVmPortforwardsProvision(mcfg *MachineConfig) error { for _, rule := range mcfg.PortForwards { - url_values := &url.Values{} - url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) - url_values.Add("cloudspaceId", fmt.Sprintf("%d", mcfg.ResGroupID)) - url_values.Add("publicIp", mcfg.ExtIP) // this may be obsoleted by Resource group implementation - url_values.Add("publicPort", fmt.Sprintf("%d", rule.ExtPort)) - url_values.Add("localPort", fmt.Sprintf("%d", rule.IntPort)) - url_values.Add("protocol", rule.Proto) - _, err := ctrl.decortAPICall("POST", PortforwardingCreateAPI, url_values) + urlValues := &url.Values{} + urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) + urlValues.Add("cloudspaceId", fmt.Sprintf("%d", mcfg.ResGroupID)) + urlValues.Add("publicIp", mcfg.ExtIP) // this may be obsoleted by Resource group implementation + urlValues.Add("publicPort", fmt.Sprintf("%d", rule.ExtPort)) + urlValues.Add("localPort", fmt.Sprintf("%d", rule.IntPort)) + urlValues.Add("protocol", rule.Proto) + _, err := ctrl.decortAPICall("POST", PortforwardingCreateAPI, urlValues) if err != nil { // failed to create port forward rule - partial resource update return err @@ -99,10 +99,10 @@ func (ctrl *ControllerCfg) utilityVmPortforwardsProvision(mcfg *MachineConfig) e func (ctrl *ControllerCfg) utilityVmNetworksProvision(mcfg *MachineConfig) error { for _, net := range mcfg.Networks { - url_values := &url.Values{} - url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) - url_values.Add("externalNetworkId", fmt.Sprintf("%d", net.NetworkID)) - _, err := ctrl.decortAPICall("POST", AttachExternalNetworkAPI, url_values) + urlValues := &url.Values{} + urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID)) + urlValues.Add("externalNetworkId", fmt.Sprintf("%d", net.NetworkID)) + _, err := ctrl.decortAPICall("POST", AttachExternalNetworkAPI, urlValues) if err != nil { // failed to attach network - partial resource update return err @@ -128,58 +128,58 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string, // controller := m.(*ControllerCfg) - url_values := &url.Values{} + urlValues := &url.Values{} - compute_id, arg_set := d.GetOk("compute_id") - if arg_set { + computeId, argSet := d.GetOk("compute_id") + if argSet { // compute ID is specified, try to get compute instance straight by this ID - log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", compute_id.(int)) - url_values.Add("computeId", fmt.Sprintf("%d", compute_id.(int))) - compute_facts, err := controller.decortAPICall("POST", ComputeGetAPI, url_values) + log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", computeId.(int)) + urlValues.Add("computeId", fmt.Sprintf("%d", computeId.(int))) + computeFacts, err := controller.decortAPICall("POST", ComputeGetAPI, urlValues) if err != nil { return "", err } - return compute_facts, nil + return computeFacts, nil } - compute_name, arg_set := d.GetOk("name") - if !arg_set { - return "", fmt.Errorf("Cannot locate compute instance if name is empty and no compute ID specified.") + computeName, argSet := d.GetOk("name") + if !argSet { + return "", fmt.Errorf("Cannot locate compute instance if name is empty and no compute ID specified") } - rg_id, arg_set := d.GetOk("rg_id") - if !arg_set { - return "", fmt.Errorf("Cannot locate compute by name %s if no resource group ID is set", compute_name.(string)) + rgId, argSet := d.GetOk("rg_id") + if !argSet { + return "", fmt.Errorf("Cannot locate compute by name %s if no resource group ID is set", computeName.(string)) } - url_values.Add("rgId", fmt.Sprintf("%d", rg_id)) - api_resp, err := controller.decortAPICall("POST", RgListComputesAPI, url_values) + urlValues.Add("rgId", fmt.Sprintf("%d", rgId)) + apiResp, err := controller.decortAPICall("POST", RgListComputesAPI, urlValues) if err != nil { return "", err } - log.Debugf("utilityComputeCheckPresence: ready to unmarshal string %q", api_resp) + log.Debugf("utilityComputeCheckPresence: ready to unmarshal string %q", apiResp) - comp_list := RgListComputesResp{} - err = json.Unmarshal([]byte(api_resp), &comp_list) + computeList := RgListComputesResp{} + err = json.Unmarshal([]byte(apiResp), &computeList) if err != nil { return "", err } - // log.Printf("%#v", comp_list) - log.Debugf("utilityComputeCheckPresence: traversing decoded JSON of length %d", len(comp_list)) - for index, item := range comp_list { + // log.Printf("%#v", computeList) + log.Debugf("utilityComputeCheckPresence: traversing decoded JSON of length %d", len(computeList)) + for index, item := range computeList { // need to match Compute by name, skip Computes with the same name in DESTROYED satus - if item.Name == compute_name.(string) && item.Status != "DESTROYED" { + if item.Name == computeName.(string) && item.Status != "DESTROYED" { log.Debugf("utilityComputeCheckPresence: index %d, matched name %q", index, item.Name) // we found the Compute we need - now get detailed information via compute/get API - get_url_values := &url.Values{} - get_url_values.Add("computeId", fmt.Sprintf("%d", item.ID)) - api_resp, err = controller.decortAPICall("POST", ComputeGetAPI, get_url_values) + cgetValues := &url.Values{} + cgetValues.Add("computeId", fmt.Sprintf("%d", item.ID)) + apiResp, err = controller.decortAPICall("POST", ComputeGetAPI, cgetValues) if err != nil { return "", err } - return api_resp, nil + return apiResp, nil } }