Selectively beautify code based on lint reports
This commit is contained in:
@@ -24,4 +24,11 @@ Visit https://github.com/rudecs/terraform-provider-decort for full source code p
|
|||||||
|
|
||||||
package decort
|
package decort
|
||||||
|
|
||||||
const LimitMaxVinsPerResgroup=4
|
// 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
|
||||||
@@ -107,15 +107,15 @@ func parseComputeInterfaces(ifaces []InterfaceRecord) []interface{} {
|
|||||||
return result // this result will be used to d.Set("interfaces",) item of dataSourceCompute schema
|
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,
|
// This function expects that comp_facts string contains response from API compute/get,
|
||||||
// i.e. detailed information about compute instance.
|
// i.e. detailed information about compute instance.
|
||||||
//
|
//
|
||||||
// NOTE: this function modifies ResourceData argument - as such it should never be called
|
// NOTE: this function modifies ResourceData argument - as such it should never be called
|
||||||
// from resourceComputeExists(...) method
|
// from resourceComputeExists(...) method
|
||||||
model := ComputeGetResp{}
|
model := ComputeGetResp{}
|
||||||
log.Debugf("flattenCompute: ready to unmarshal string %q", comp_facts)
|
log.Debugf("flattenCompute: ready to unmarshal string %q", compFacts)
|
||||||
err := json.Unmarshal([]byte(comp_facts), &model)
|
err := json.Unmarshal([]byte(compFacts), &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -163,15 +163,15 @@ func flattenCompute(d *schema.ResourceData, comp_facts string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dataSourceComputeRead(d *schema.ResourceData, m interface{}) error {
|
func dataSourceComputeRead(d *schema.ResourceData, m interface{}) error {
|
||||||
comp_facts, err := utilityComputeCheckPresence(d, m)
|
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||||
if comp_facts == "" {
|
if compFacts == "" {
|
||||||
// if empty string is returned from utilityComputeCheckPresence then there is no
|
// if empty string is returned from utilityComputeCheckPresence then there is no
|
||||||
// such Compute and err tells so - just return it to the calling party
|
// such Compute and err tells so - just return it to the calling party
|
||||||
d.SetId("") // ensure ID is empty
|
d.SetId("") // ensure ID is empty
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return flattenCompute(d, comp_facts)
|
return flattenCompute(d, compFacts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dataSourceCompute() *schema.Resource {
|
func dataSourceCompute() *schema.Resource {
|
||||||
|
|||||||
@@ -94,18 +94,18 @@ func resourceComputeCreate(d *schema.ResourceData, m interface{}) error {
|
|||||||
// by separate API calls)
|
// by separate API calls)
|
||||||
d.Partial(true)
|
d.Partial(true)
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
url_values.Add("cloudspaceId", fmt.Sprintf("%d", machine.ResGroupID))
|
urlValues.Add("cloudspaceId", fmt.Sprintf("%d", machine.ResGroupID))
|
||||||
url_values.Add("name", machine.Name)
|
urlValues.Add("name", machine.Name)
|
||||||
url_values.Add("description", machine.Description)
|
urlValues.Add("description", machine.Description)
|
||||||
url_values.Add("vcpus", fmt.Sprintf("%d", machine.Cpu))
|
urlValues.Add("vcpus", fmt.Sprintf("%d", machine.Cpu))
|
||||||
url_values.Add("memory", fmt.Sprintf("%d", machine.Ram))
|
urlValues.Add("memory", fmt.Sprintf("%d", machine.Ram))
|
||||||
url_values.Add("imageId", fmt.Sprintf("%d", machine.ImageID))
|
urlValues.Add("imageId", fmt.Sprintf("%d", machine.ImageID))
|
||||||
url_values.Add("disksize", fmt.Sprintf("%d", machine.BootDisk.Size))
|
urlValues.Add("disksize", fmt.Sprintf("%d", machine.BootDisk.Size))
|
||||||
if len(machine.SshKeys) > 0 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -218,8 +218,8 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error {
|
|||||||
log.Printf("resourceComputeRead: called for VM name %q, ResGroupID %d",
|
log.Printf("resourceComputeRead: called for VM name %q, ResGroupID %d",
|
||||||
d.Get("name").(string), d.Get("rgid").(int))
|
d.Get("name").(string), d.Get("rgid").(int))
|
||||||
|
|
||||||
comp_facts, err := utilityComputeCheckPresence(d, m)
|
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||||
if comp_facts == "" {
|
if compFacts == "" {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = flattenCompute(d, comp_facts); err != nil {
|
if err = flattenCompute(d, compFacts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("resourceComputeRead: after flattenCompute: VM ID %s, VM name %q, ResGroupID %d",
|
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
|
// Not all parameters, that we may need, are returned by machines/get API
|
||||||
// Continue with further reading of VM subresource parameters:
|
// Continue with further reading of VM subresource parameters:
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// Obtain information on external networks
|
// Obtain information on external networks
|
||||||
url_values.Add("machineId", d.Id())
|
urlValues.Add("machineId", d.Id())
|
||||||
body_string, err := controller.decortAPICall("POST", VmExtNetworksListAPI, url_values)
|
body_string, err := controller.decortAPICall("POST", VmExtNetworksListAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -271,10 +271,10 @@ func resourceComputeRead(d *schema.ResourceData, m interface{}) error {
|
|||||||
//
|
//
|
||||||
// Obtain information on port forwards
|
// Obtain information on port forwards
|
||||||
/*
|
/*
|
||||||
url_values.Add("cloudspaceId", fmt.Sprintf("%d", d.Get("rgid")))
|
urlValues.Add("cloudspaceId", fmt.Sprintf("%d", d.Get("rgid")))
|
||||||
url_values.Add("machineId", d.Id())
|
urlValues.Add("machineId", d.Id())
|
||||||
pfw_list := PortforwardsResp{}
|
pfw_list := PortforwardsResp{}
|
||||||
body_string, err := controller.decortAPICall("POST", PortforwardsListAPI, url_values)
|
body_string, err := controller.decortAPICall("POST", PortforwardsListAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -301,24 +301,24 @@ func resourceComputeUpdate(d *schema.ResourceData, m interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func resourceComputeDelete(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
|
// NOTE: this method destroys target Compute instance with flag "permanently", so
|
||||||
// restore destroyed VM
|
// there is no way to restore destroyed Compute
|
||||||
log.Printf("resourceComputeDelete: called for VM name %q, ResGroupID %d",
|
log.Printf("resourceComputeDelete: called for VM name %q, ResGroupID %d",
|
||||||
d.Get("name").(string), d.Get("rgid").(int))
|
d.Get("name").(string), d.Get("rgid").(int))
|
||||||
|
|
||||||
comp_facts, err := utilityComputeCheckPresence(d, m)
|
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||||
if comp_facts == "" {
|
if compFacts == "" {
|
||||||
// the target VM does not exist - in this case according to Terraform best practice
|
// the target Compute does not exist - in this case according to Terraform best practice
|
||||||
// we exit from Destroy method without error
|
// we exit from Destroy method without error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
params := &url.Values{}
|
params := &url.Values{}
|
||||||
params.Add("machineId", d.Id())
|
params.Add("computeId", d.Id())
|
||||||
params.Add("permanently", "true")
|
params.Add("permanently", "true")
|
||||||
|
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
comp_facts, err = controller.decortAPICall("POST", MachineDeleteAPI, params)
|
compFacts, err = controller.decortAPICall("POST", ComputeDeleteAPI, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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",
|
log.Printf("resourceComputeExist: called for VM name %q, ResGroupID %d",
|
||||||
d.Get("name").(string), d.Get("rgid").(int))
|
d.Get("name").(string), d.Get("rgid").(int))
|
||||||
|
|
||||||
comp_facts, err := utilityComputeCheckPresence(d, m)
|
compFacts, err := utilityComputeCheckPresence(d, m)
|
||||||
if comp_facts == "" {
|
if compFacts == "" {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@@ -363,123 +363,132 @@ func resourceCompute() *schema.Resource {
|
|||||||
"name": {
|
"name": {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
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,
|
Type: schema.TypeInt,
|
||||||
Required: true,
|
Required: true,
|
||||||
ValidateFunc: validation.IntAtLeast(1),
|
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": {
|
"cpu": {
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
Required: true,
|
Required: true,
|
||||||
ValidateFunc: validation.IntBetween(1, 64),
|
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": {
|
"ram": {
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
Required: true,
|
Required: true,
|
||||||
ValidateFunc: validation.IntAtLeast(512),
|
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": {
|
"image_id": {
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: 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": {
|
"boot_disk_size": {
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeInt,
|
||||||
Required: true,
|
Optional: true,
|
||||||
MaxItems: 1,
|
Description: "Size of the boot disk on this compute instance.",
|
||||||
Elem: &schema.Resource{
|
|
||||||
Schema: diskSubresourceSchema(),
|
|
||||||
},
|
|
||||||
Description: "Specification for a boot disk on this virtual machine.",
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"data_disks": {
|
"extra_disks": {
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
MaxItems: 12,
|
MaxItems: MaxExtraDisksPerCompute,
|
||||||
Elem: &schema.Resource{
|
Elem: &schema.Schema{
|
||||||
Schema: diskSubresourceSchema(),
|
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.",
|
||||||
|
},
|
||||||
|
|
||||||
|
"ssh_keys": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Optional: true,
|
||||||
|
MaxItems: MaxSshKeysPerCompute,
|
||||||
|
Elem: &schema.Resource{
|
||||||
|
Schema: sshSubresourceSchemaMake(),
|
||||||
|
},
|
||||||
|
Description: "SSH keys to authorize on this compute instance.",
|
||||||
|
},
|
||||||
|
|
||||||
|
"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.",
|
||||||
|
},
|
||||||
|
|
||||||
|
"disks": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Resource{
|
||||||
|
Schema: dataSourceDiskSchemaMake(), // ID, type, name, size, account ID, SEP ID, SEP type, pool, status, tech status, compute ID, image ID
|
||||||
|
},
|
||||||
|
Description: "Detailed specification for all disks attached to this compute instance (including bood disk).",
|
||||||
|
},
|
||||||
|
|
||||||
|
"interfaces": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Resource{
|
||||||
|
Schema: interfaceSubresourceSchemaMake(),
|
||||||
|
},
|
||||||
|
Description: "Specification for the virtual NICs configured on this compute instance.",
|
||||||
},
|
},
|
||||||
|
|
||||||
"guest_logins": {
|
"guest_logins": {
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
Elem: &schema.Resource{
|
Elem: &schema.Resource{
|
||||||
Schema: loginsSubresourceSchema(),
|
Schema: loginsSubresourceSchemaMake(),
|
||||||
},
|
},
|
||||||
Description: "Specification for guest logins on this virtual machine.",
|
Description: "Specification for guest logins on this compute instance.",
|
||||||
},
|
},
|
||||||
|
|
||||||
"networks": {
|
"status": {
|
||||||
Type: schema.TypeList,
|
|
||||||
Optional: true,
|
|
||||||
MaxItems: 8,
|
|
||||||
Elem: &schema.Resource{
|
|
||||||
Schema: networkSubresourceSchema(),
|
|
||||||
},
|
|
||||||
Description: "Specification for the networks to connect this virtual machine to.",
|
|
||||||
},
|
|
||||||
|
|
||||||
"nics": {
|
|
||||||
Type: schema.TypeList,
|
|
||||||
Computed: true,
|
|
||||||
MaxItems: 8,
|
|
||||||
Elem: &schema.Resource{
|
|
||||||
Schema: nicSubresourceSchema(),
|
|
||||||
},
|
|
||||||
Description: "Specification for the virutal NICs allocated to this virtual machine.",
|
|
||||||
},
|
|
||||||
|
|
||||||
"ssh_keys": {
|
|
||||||
Type: schema.TypeList,
|
|
||||||
Optional: true,
|
|
||||||
MaxItems: 12,
|
|
||||||
Elem: &schema.Resource{
|
|
||||||
Schema: sshSubresourceSchema(),
|
|
||||||
},
|
|
||||||
Description: "SSH keys to authorize on this virtual machine.",
|
|
||||||
},
|
|
||||||
|
|
||||||
"port_forwards": {
|
|
||||||
Type: schema.TypeList,
|
|
||||||
Optional: true,
|
|
||||||
MaxItems: 12,
|
|
||||||
Elem: &schema.Resource{
|
|
||||||
Schema: portforwardSubresourceSchema(),
|
|
||||||
},
|
|
||||||
Description: "Specification for the port forwards to configure for this virtual machine.",
|
|
||||||
},
|
|
||||||
|
|
||||||
"description": {
|
|
||||||
Type: schema.TypeString,
|
|
||||||
Optional: true,
|
|
||||||
Description: "Description of this virtual machine.",
|
|
||||||
},
|
|
||||||
|
|
||||||
"user": {
|
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
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,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
Sensitive: true,
|
Description: "Current technical status of this compute instance.",
|
||||||
Description: "Default password for the guest OS login on this virtual machine.",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,57 +37,57 @@ import (
|
|||||||
|
|
||||||
func utilityAccountCheckPresence(d *schema.ResourceData, m interface{}) (string, error) {
|
func utilityAccountCheckPresence(d *schema.ResourceData, m interface{}) (string, error) {
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
|
|
||||||
acc_id, arg_set := d.GetOk("account_id")
|
accId, argSet := d.GetOk("account_id")
|
||||||
if arg_set {
|
if argSet {
|
||||||
// get Account right away by its ID
|
// get Account right away by its ID
|
||||||
log.Debugf("utilityAccountCheckPresence: locating Account by its ID %d", acc_id.(int))
|
log.Debugf("utilityAccountCheckPresence: locating Account by its ID %d", accId.(int))
|
||||||
url_values.Add("accountId", fmt.Sprintf("%d", acc_id.(int)))
|
urlValues.Add("accountId", fmt.Sprintf("%d", accId.(int)))
|
||||||
api_resp, err := controller.decortAPICall("POST", AccountsGetAPI, url_values)
|
apiResp, err := controller.decortAPICall("POST", AccountsGetAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return api_resp, nil
|
return apiResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
acc_name, arg_set := d.GetOk("name")
|
accName, argSet := d.GetOk("name")
|
||||||
if !arg_set {
|
if !argSet {
|
||||||
// neither ID nor name - no account for you!
|
// 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 {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// log.Debugf("%s", api_resp)
|
// log.Debugf("%s", apiResp)
|
||||||
// log.Debugf("utilityAccountCheckPresence: ready to decode response body from %q", AccountsListAPI)
|
// log.Debugf("utilityAccountCheckPresence: ready to decode response body from %q", AccountsListAPI)
|
||||||
acc_list := AccountsListResp{}
|
accList := AccountsListResp{}
|
||||||
err = json.Unmarshal([]byte(api_resp), &acc_list)
|
err = json.Unmarshal([]byte(apiResp), &accList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("utilityAccountCheckPresence: traversing decoded Json of length %d", len(acc_list))
|
log.Debugf("utilityAccountCheckPresence: traversing decoded Json of length %d", len(accList))
|
||||||
for index, item := range acc_list {
|
for index, item := range accList {
|
||||||
// match by account name
|
// 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",
|
log.Debugf("utilityAccountCheckPresence: match account name %q / ID %d at index %d",
|
||||||
item.Name, item.ID, index)
|
item.Name, item.ID, index)
|
||||||
|
|
||||||
// NB: unlike accounts/get API, accounts/list API returns abridged set of account info,
|
// NB: unlike accounts/get API, accounts/list API returns abridged set of account info,
|
||||||
// for instance it does not return quotas
|
// for instance it does not return quotas
|
||||||
|
|
||||||
reencoded_item, err := json.Marshal(item)
|
reencodedItem, err := json.Marshal(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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) {
|
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")
|
accId, argSet := d.GetOk("account_id")
|
||||||
if arg_set {
|
if argSet {
|
||||||
if account_id.(int) > 0 {
|
if accId.(int) > 0 {
|
||||||
return account_id.(int), nil
|
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")
|
accName, argSet := d.GetOk("account_name")
|
||||||
if !arg_set {
|
if !argSet {
|
||||||
return 0, fmt.Errorf("Non-empty account name or positive account ID must be specified.")
|
return 0, fmt.Errorf("Either non-empty account name or positive account ID must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
body_string, err := controller.decortAPICall("POST", AccountsListAPI, url_values)
|
apiResp, err := controller.decortAPICall("POST", AccountsListAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
model := AccountsListResp{}
|
model := AccountsListResp{}
|
||||||
err = json.Unmarshal([]byte(body_string), &model)
|
err = json.Unmarshal([]byte(apiResp), &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
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))
|
log.Debugf("utilityGetAccountIdBySchema: traversing decoded Json of length %d", len(model))
|
||||||
for index, item := range model {
|
for index, item := range model {
|
||||||
// need to match Account by name
|
// 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",
|
log.Debugf("utilityGetAccountIdBySchema: match Account name %q / ID %d at index %d",
|
||||||
item.Name, item.ID, index)
|
item.Name, item.ID, index)
|
||||||
return item.ID, nil
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,17 +39,17 @@ import (
|
|||||||
|
|
||||||
func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error {
|
func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error {
|
||||||
for index, disk := range mcfg.DataDisks {
|
for index, disk := range mcfg.DataDisks {
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
// url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
// urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
||||||
url_values.Add("accountId", fmt.Sprintf("%d", mcfg.TenantID))
|
urlValues.Add("accountId", fmt.Sprintf("%d", mcfg.TenantID))
|
||||||
url_values.Add("gid", fmt.Sprintf("%d", mcfg.GridID))
|
urlValues.Add("gid", fmt.Sprintf("%d", mcfg.GridID))
|
||||||
url_values.Add("name", fmt.Sprintf("%s", disk.Label))
|
urlValues.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))
|
urlValues.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))
|
urlValues.Add("size", fmt.Sprintf("%d", disk.Size))
|
||||||
url_values.Add("type", "D")
|
urlValues.Add("type", "D")
|
||||||
// url_values.Add("iops", )
|
// urlValues.Add("iops", )
|
||||||
|
|
||||||
disk_id_resp, err := ctrl.decortAPICall("POST", DiskCreateAPI, url_values)
|
disk_id_resp, err := ctrl.decortAPICall("POST", DiskCreateAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to create disk - partial resource update
|
// failed to create disk - partial resource update
|
||||||
return err
|
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
|
// 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
|
// we can attempt attaching the disk to the VM
|
||||||
url_values = &url.Values{}
|
urlValues = &url.Values{}
|
||||||
// url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
// urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
||||||
url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
||||||
url_values.Add("diskId", disk_id_resp)
|
urlValues.Add("diskId", disk_id_resp)
|
||||||
_, err = ctrl.decortAPICall("POST", DiskAttachAPI, url_values)
|
_, err = ctrl.decortAPICall("POST", DiskAttachAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to attach disk - partial resource update
|
// failed to attach disk - partial resource update
|
||||||
return err
|
return err
|
||||||
@@ -81,14 +81,14 @@ func (ctrl *ControllerCfg) utilityVmDisksProvision(mcfg *MachineConfig) error {
|
|||||||
|
|
||||||
func (ctrl *ControllerCfg) utilityVmPortforwardsProvision(mcfg *MachineConfig) error {
|
func (ctrl *ControllerCfg) utilityVmPortforwardsProvision(mcfg *MachineConfig) error {
|
||||||
for _, rule := range mcfg.PortForwards {
|
for _, rule := range mcfg.PortForwards {
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
||||||
url_values.Add("cloudspaceId", fmt.Sprintf("%d", mcfg.ResGroupID))
|
urlValues.Add("cloudspaceId", fmt.Sprintf("%d", mcfg.ResGroupID))
|
||||||
url_values.Add("publicIp", mcfg.ExtIP) // this may be obsoleted by Resource group implementation
|
urlValues.Add("publicIp", mcfg.ExtIP) // this may be obsoleted by Resource group implementation
|
||||||
url_values.Add("publicPort", fmt.Sprintf("%d", rule.ExtPort))
|
urlValues.Add("publicPort", fmt.Sprintf("%d", rule.ExtPort))
|
||||||
url_values.Add("localPort", fmt.Sprintf("%d", rule.IntPort))
|
urlValues.Add("localPort", fmt.Sprintf("%d", rule.IntPort))
|
||||||
url_values.Add("protocol", rule.Proto)
|
urlValues.Add("protocol", rule.Proto)
|
||||||
_, err := ctrl.decortAPICall("POST", PortforwardingCreateAPI, url_values)
|
_, err := ctrl.decortAPICall("POST", PortforwardingCreateAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to create port forward rule - partial resource update
|
// failed to create port forward rule - partial resource update
|
||||||
return err
|
return err
|
||||||
@@ -99,10 +99,10 @@ func (ctrl *ControllerCfg) utilityVmPortforwardsProvision(mcfg *MachineConfig) e
|
|||||||
|
|
||||||
func (ctrl *ControllerCfg) utilityVmNetworksProvision(mcfg *MachineConfig) error {
|
func (ctrl *ControllerCfg) utilityVmNetworksProvision(mcfg *MachineConfig) error {
|
||||||
for _, net := range mcfg.Networks {
|
for _, net := range mcfg.Networks {
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
url_values.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
urlValues.Add("machineId", fmt.Sprintf("%d", mcfg.ID))
|
||||||
url_values.Add("externalNetworkId", fmt.Sprintf("%d", net.NetworkID))
|
urlValues.Add("externalNetworkId", fmt.Sprintf("%d", net.NetworkID))
|
||||||
_, err := ctrl.decortAPICall("POST", AttachExternalNetworkAPI, url_values)
|
_, err := ctrl.decortAPICall("POST", AttachExternalNetworkAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// failed to attach network - partial resource update
|
// failed to attach network - partial resource update
|
||||||
return err
|
return err
|
||||||
@@ -128,58 +128,58 @@ func utilityComputeCheckPresence(d *schema.ResourceData, m interface{}) (string,
|
|||||||
//
|
//
|
||||||
|
|
||||||
controller := m.(*ControllerCfg)
|
controller := m.(*ControllerCfg)
|
||||||
url_values := &url.Values{}
|
urlValues := &url.Values{}
|
||||||
|
|
||||||
compute_id, arg_set := d.GetOk("compute_id")
|
computeId, argSet := d.GetOk("compute_id")
|
||||||
if arg_set {
|
if argSet {
|
||||||
// compute ID is specified, try to get compute instance straight by this ID
|
// 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))
|
log.Debugf("utilityComputeCheckPresence: locating compute by its ID %d", computeId.(int))
|
||||||
url_values.Add("computeId", fmt.Sprintf("%d", compute_id.(int)))
|
urlValues.Add("computeId", fmt.Sprintf("%d", computeId.(int)))
|
||||||
compute_facts, err := controller.decortAPICall("POST", ComputeGetAPI, url_values)
|
computeFacts, err := controller.decortAPICall("POST", ComputeGetAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return compute_facts, nil
|
return computeFacts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
compute_name, arg_set := d.GetOk("name")
|
computeName, argSet := d.GetOk("name")
|
||||||
if !arg_set {
|
if !argSet {
|
||||||
return "", fmt.Errorf("Cannot locate compute instance if name is empty and no compute ID specified.")
|
return "", fmt.Errorf("Cannot locate compute instance if name is empty and no compute ID specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
rg_id, arg_set := d.GetOk("rg_id")
|
rgId, argSet := d.GetOk("rg_id")
|
||||||
if !arg_set {
|
if !argSet {
|
||||||
return "", fmt.Errorf("Cannot locate compute by name %s if no resource group ID is set", compute_name.(string))
|
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))
|
urlValues.Add("rgId", fmt.Sprintf("%d", rgId))
|
||||||
api_resp, err := controller.decortAPICall("POST", RgListComputesAPI, url_values)
|
apiResp, err := controller.decortAPICall("POST", RgListComputesAPI, urlValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("utilityComputeCheckPresence: ready to unmarshal string %q", api_resp)
|
log.Debugf("utilityComputeCheckPresence: ready to unmarshal string %q", apiResp)
|
||||||
|
|
||||||
comp_list := RgListComputesResp{}
|
computeList := RgListComputesResp{}
|
||||||
err = json.Unmarshal([]byte(api_resp), &comp_list)
|
err = json.Unmarshal([]byte(apiResp), &computeList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Printf("%#v", comp_list)
|
// log.Printf("%#v", computeList)
|
||||||
log.Debugf("utilityComputeCheckPresence: traversing decoded JSON of length %d", len(comp_list))
|
log.Debugf("utilityComputeCheckPresence: traversing decoded JSON of length %d", len(computeList))
|
||||||
for index, item := range comp_list {
|
for index, item := range computeList {
|
||||||
// need to match Compute by name, skip Computes with the same name in DESTROYED satus
|
// 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)
|
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
|
// we found the Compute we need - now get detailed information via compute/get API
|
||||||
get_url_values := &url.Values{}
|
cgetValues := &url.Values{}
|
||||||
get_url_values.Add("computeId", fmt.Sprintf("%d", item.ID))
|
cgetValues.Add("computeId", fmt.Sprintf("%d", item.ID))
|
||||||
api_resp, err = controller.decortAPICall("POST", ComputeGetAPI, get_url_values)
|
apiResp, err = controller.decortAPICall("POST", ComputeGetAPI, cgetValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return api_resp, nil
|
return apiResp, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user