Add data for account consumed units
This commit is contained in:
98
decort/data_account_consumed_units.go
Normal file
98
decort/data_account_consumed_units.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
|
||||||
|
Author: Stanislav Solovev, <spsolovev@digitalenergy.online>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file is part of Terraform (by Hashicorp) provider for Digital Energy Cloud Orchestration
|
||||||
|
Technology platfom.
|
||||||
|
|
||||||
|
Visit https://github.com/rudecs/terraform-provider-decort for full source code package and updates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package decort
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dataSourceAccountConsumedUnitsRead(d *schema.ResourceData, m interface{}) error {
|
||||||
|
accountConsumedUnits, err := utilityAccountConsumedUnitsCheckPresence(d, m)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
id := uuid.New()
|
||||||
|
d.SetId(id.String())
|
||||||
|
d.Set("cu_c", accountConsumedUnits.CUC)
|
||||||
|
d.Set("cu_d", accountConsumedUnits.CUD)
|
||||||
|
d.Set("cu_i", accountConsumedUnits.CUI)
|
||||||
|
d.Set("cu_m", accountConsumedUnits.CUM)
|
||||||
|
d.Set("cu_np", accountConsumedUnits.CUNP)
|
||||||
|
d.Set("gpu_units", accountConsumedUnits.GpuUnits)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceAccountConsumedUnitsSchemaMake() map[string]*schema.Schema {
|
||||||
|
res := map[string]*schema.Schema{
|
||||||
|
"account_id": {
|
||||||
|
Type: schema.TypeInt,
|
||||||
|
Required: true,
|
||||||
|
Description: "ID of the account",
|
||||||
|
},
|
||||||
|
"cu_c": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"cu_d": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"cu_i": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"cu_m": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"cu_np": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
"gpu_units": {
|
||||||
|
Type: schema.TypeFloat,
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceAccountConsumedUnits() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
SchemaVersion: 1,
|
||||||
|
|
||||||
|
Read: dataSourceAccountConsumedUnitsRead,
|
||||||
|
|
||||||
|
Timeouts: &schema.ResourceTimeout{
|
||||||
|
Read: &Timeout30s,
|
||||||
|
Default: &Timeout60s,
|
||||||
|
},
|
||||||
|
|
||||||
|
Schema: dataSourceAccountConsumedUnitsSchemaMake(),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1002,6 +1002,7 @@ const accountDeleteUserAPI = "/restmachine/cloudapi/account/deleteUser"
|
|||||||
const accountDisableAPI = "/restmachine/cloudapi/account/disable"
|
const accountDisableAPI = "/restmachine/cloudapi/account/disable"
|
||||||
const accountEnableAPI = "/restmachine/cloudapi/account/enable"
|
const accountEnableAPI = "/restmachine/cloudapi/account/enable"
|
||||||
const accountGetAPI = "/restmachine/cloudapi/account/get"
|
const accountGetAPI = "/restmachine/cloudapi/account/get"
|
||||||
|
const accountGetConsumedUnitsAPI = "/restmachine/cloudapi/account/getConsumedAccountUnits"
|
||||||
const accountListAPI = "/restmachine/cloudapi/account/list"
|
const accountListAPI = "/restmachine/cloudapi/account/list"
|
||||||
const accountListComputesAPI = "/restmachine/cloudapi/account/listComputes"
|
const accountListComputesAPI = "/restmachine/cloudapi/account/listComputes"
|
||||||
const accountListCSAPI = "/cloudapi/account/listCS"
|
const accountListCSAPI = "/cloudapi/account/listCS"
|
||||||
|
|||||||
@@ -118,34 +118,35 @@ func Provider() *schema.Provider {
|
|||||||
},
|
},
|
||||||
|
|
||||||
DataSourcesMap: map[string]*schema.Resource{
|
DataSourcesMap: map[string]*schema.Resource{
|
||||||
"decort_account": dataSourceAccount(),
|
"decort_account": dataSourceAccount(),
|
||||||
"decort_resgroup": dataSourceResgroup(),
|
"decort_resgroup": dataSourceResgroup(),
|
||||||
"decort_kvmvm": dataSourceCompute(),
|
"decort_kvmvm": dataSourceCompute(),
|
||||||
"decort_image": dataSourceImage(),
|
"decort_image": dataSourceImage(),
|
||||||
"decort_disk": dataSourceDisk(),
|
"decort_disk": dataSourceDisk(),
|
||||||
"decort_vins": dataSourceVins(),
|
"decort_vins": dataSourceVins(),
|
||||||
"decort_grid": dataSourceGrid(),
|
"decort_grid": dataSourceGrid(),
|
||||||
"decort_grid_list": dataSourceGridList(),
|
"decort_grid_list": dataSourceGridList(),
|
||||||
"decort_image_list": dataSourceImageList(),
|
"decort_image_list": dataSourceImageList(),
|
||||||
"decort_image_list_stacks": dataSourceImageListStacks(),
|
"decort_image_list_stacks": dataSourceImageListStacks(),
|
||||||
"decort_snapshot_list": dataSourceSnapshotList(),
|
"decort_snapshot_list": dataSourceSnapshotList(),
|
||||||
"decort_vgpu": dataSourceVGPU(),
|
"decort_vgpu": dataSourceVGPU(),
|
||||||
"decort_pcidevice": dataSourcePcidevice(),
|
"decort_pcidevice": dataSourcePcidevice(),
|
||||||
"decort_pcidevice_list": dataSourcePcideviceList(),
|
"decort_pcidevice_list": dataSourcePcideviceList(),
|
||||||
"decort_sep_list": dataSourceSepList(),
|
"decort_sep_list": dataSourceSepList(),
|
||||||
"decort_sep": dataSourceSep(),
|
"decort_sep": dataSourceSep(),
|
||||||
"decort_sep_consumption": dataSourceSepConsumption(),
|
"decort_sep_consumption": dataSourceSepConsumption(),
|
||||||
"decort_sep_disk_list": dataSourceSepDiskList(),
|
"decort_sep_disk_list": dataSourceSepDiskList(),
|
||||||
"decort_sep_config": dataSourceSepConfig(),
|
"decort_sep_config": dataSourceSepConfig(),
|
||||||
"decort_sep_pool": dataSourceSepPool(),
|
"decort_sep_pool": dataSourceSepPool(),
|
||||||
"decort_disk_list": dataSourceDiskList(),
|
"decort_disk_list": dataSourceDiskList(),
|
||||||
"decort_rg_list": dataSourceRgList(),
|
"decort_rg_list": dataSourceRgList(),
|
||||||
"decort_account_list": dataSourceAccountList(),
|
"decort_account_list": dataSourceAccountList(),
|
||||||
"decort_account_computes_list": dataSourceAccountComputesList(),
|
"decort_account_computes_list": dataSourceAccountComputesList(),
|
||||||
"decort_account_disks_list": dataSourceAccountDisksList(),
|
"decort_account_disks_list": dataSourceAccountDisksList(),
|
||||||
"decort_account_vins_list": dataSourceAccountVinsList(),
|
"decort_account_vins_list": dataSourceAccountVinsList(),
|
||||||
"decort_account_audits_list": dataSourceAccountAuditsList(),
|
"decort_account_audits_list": dataSourceAccountAuditsList(),
|
||||||
"decort_account_rg_list": dataSourceAccountRGList(),
|
"decort_account_rg_list": dataSourceAccountRGList(),
|
||||||
|
"decort_account_consumed_units": dataSourceAccountConsumedUnits(),
|
||||||
// "decort_pfw": dataSourcePfw(),
|
// "decort_pfw": dataSourcePfw(),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
56
decort/utility_account_consumed_units.go
Normal file
56
decort/utility_account_consumed_units.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved.
|
||||||
|
Author: Stanislav Solovev, <spsolovev@digitalenergy.online>, <svs1370@gmail.com>
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file is part of Terraform (by Hashicorp) provider for Digital Energy Cloud Orchestration
|
||||||
|
Technology platfom.
|
||||||
|
|
||||||
|
Visit https://github.com/rudecs/terraform-provider-decort for full source code package and updates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package decort
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
func utilityAccountConsumedUnitsCheckPresence(d *schema.ResourceData, m interface{}) (*ResourceLimits, error) {
|
||||||
|
accountConsumedUnits := &ResourceLimits{}
|
||||||
|
controller := m.(*ControllerCfg)
|
||||||
|
urlValues := &url.Values{}
|
||||||
|
|
||||||
|
urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int)))
|
||||||
|
|
||||||
|
log.Debugf("utilityAccountConsumedUnitsCheckPresence: load account list")
|
||||||
|
accountConsumedUnitsRaw, err := controller.decortAPICall("POST", accountGetConsumedUnitsAPI, urlValues)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal([]byte(accountConsumedUnitsRaw), accountConsumedUnits)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return accountConsumedUnits, nil
|
||||||
|
}
|
||||||
@@ -27,6 +27,7 @@
|
|||||||
- account_audits_list
|
- account_audits_list
|
||||||
- account
|
- account
|
||||||
- account_rg_list
|
- account_rg_list
|
||||||
|
- account_counsumed_units
|
||||||
- resources:
|
- resources:
|
||||||
- image
|
- image
|
||||||
- virtual_image
|
- virtual_image
|
||||||
|
|||||||
37
samples/data_account_consumed_units/main.tf
Normal file
37
samples/data_account_consumed_units/main.tf
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Пример использования
|
||||||
|
Получение информации о расходуемых ресурсах аккаута
|
||||||
|
*/
|
||||||
|
#Расскомментируйте этот код,
|
||||||
|
#и внесите необходимые правки в версию и путь,
|
||||||
|
#чтобы работать с установленным вручную (не через hashicorp provider registry) провайдером
|
||||||
|
/*
|
||||||
|
terraform {
|
||||||
|
required_providers {
|
||||||
|
decort = {
|
||||||
|
version = "1.1"
|
||||||
|
source = "digitalenergy.online/decort/decort"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
provider "decort" {
|
||||||
|
authenticator = "oauth2"
|
||||||
|
#controller_url = <DECORT_CONTROLLER_URL>
|
||||||
|
controller_url = "https://ds1.digitalenergy.online"
|
||||||
|
#oauth2_url = <DECORT_SSO_URL>
|
||||||
|
oauth2_url = "https://sso.digitalenergy.online"
|
||||||
|
allow_unverified_ssl = true
|
||||||
|
}
|
||||||
|
|
||||||
|
data "decort_account_consumed_units" "acu" {
|
||||||
|
#id аккаунта
|
||||||
|
#обязательный параметр
|
||||||
|
#тип - число
|
||||||
|
account_id = 88366
|
||||||
|
}
|
||||||
|
|
||||||
|
output "test" {
|
||||||
|
value = data.decort_account_consumed_units.acu
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user