|
|
@ -1,6 +1,6 @@
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2021 Digital Energy Cloud Solutions LLC. All Rights Reserved.
|
|
|
|
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
|
|
|
|
Author: Sergey Shubin, <sergey.shubin@digitalenergy.online>, <svs1370@gmail.com>
|
|
|
|
Author: Stanislav Solovev, <spsolovev@digitalenergy.online>, <svs1370@gmail.com>
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
@ -26,131 +26,31 @@ package decort
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"net/url"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
|
|
|
// "github.com/hashicorp/terraform-plugin-sdk/helper/validation"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func utilityAccountCheckPresence(d *schema.ResourceData, m interface{}) (string, error) {
|
|
|
|
func utilityAccountCheckPresence(d *schema.ResourceData, m interface{}) (*AccountWithResources, error) {
|
|
|
|
|
|
|
|
account := &AccountWithResources{}
|
|
|
|
controller := m.(*ControllerCfg)
|
|
|
|
controller := m.(*ControllerCfg)
|
|
|
|
urlValues := &url.Values{}
|
|
|
|
urlValues := &url.Values{}
|
|
|
|
|
|
|
|
|
|
|
|
accId, argSet := d.GetOk("account_id")
|
|
|
|
urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int)))
|
|
|
|
if argSet {
|
|
|
|
|
|
|
|
// get Account right away by its ID
|
|
|
|
|
|
|
|
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 apiResp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apiResp, err := controller.decortAPICall("POST", AccountsListAPI, urlValues)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Debugf("%s", apiResp)
|
|
|
|
|
|
|
|
// log.Debugf("utilityAccountCheckPresence: ready to decode response body from %q", AccountsListAPI)
|
|
|
|
|
|
|
|
accList := AccountsListResp{}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(apiResp), &accList)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("utilityAccountCheckPresence: traversing decoded Json of length %d", len(accList))
|
|
|
|
|
|
|
|
for index, item := range accList {
|
|
|
|
|
|
|
|
// match by account name
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reencodedItem, err := json.Marshal(item)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(reencodedItem[:]), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "", fmt.Errorf("Cannot find account name %q", accName.(string))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func utilityGetAccountIdBySchema(d *schema.ResourceData, m interface{}) (int, error) {
|
|
|
|
log.Debugf("utilityAccountCheckPresence: load account")
|
|
|
|
/*
|
|
|
|
accountRaw, err := controller.decortAPICall("POST", accountGetAPI, urlValues)
|
|
|
|
This function expects schema that contains the following two elements:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"account_name": &schema.Schema{
|
|
|
|
|
|
|
|
Type: schema.TypeString,
|
|
|
|
|
|
|
|
Required: Optional,
|
|
|
|
|
|
|
|
Description: "Name of the account, ....",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"account_id": &schema.Schema{
|
|
|
|
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
|
|
|
|
Optional: true,
|
|
|
|
|
|
|
|
Description: "Unique ID of the account, ....",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Then it will check, which argument is set, and if account name is present, it will
|
|
|
|
|
|
|
|
initiate API calls to the DECORT cloud controller and try to match relevant account
|
|
|
|
|
|
|
|
by the name.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NOTE that for some resources (most notably, Resource Group) "account_name" attribute is
|
|
|
|
|
|
|
|
marked as "Computed: true", so the only way to fully identify Resource Group is to specify
|
|
|
|
|
|
|
|
"account_id", which is marked as "Required: true"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
accName, argSet := d.GetOk("account_name")
|
|
|
|
|
|
|
|
if !argSet {
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("Either non-empty account name or valid account ID must be specified")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller := m.(*ControllerCfg)
|
|
|
|
|
|
|
|
urlValues := &url.Values{}
|
|
|
|
|
|
|
|
apiResp, err := controller.decortAPICall("POST", AccountsListAPI, urlValues)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
model := AccountsListResp{}
|
|
|
|
err = json.Unmarshal([]byte(accountRaw), &account)
|
|
|
|
err = json.Unmarshal([]byte(apiResp), &model)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 == 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", accName.(string))
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|