From 96c4175e74ccf68e9518228223ddbeadb51981d7 Mon Sep 17 00:00:00 2001 From: stSolo Date: Fri, 27 May 2022 18:23:38 +0300 Subject: [PATCH] Add data for account reserved units --- decort/data_account_reserved_units.go | 98 +++++++++++++++++++++ decort/models_api.go | 1 + decort/provider.go | 1 + decort/utility_account_reserved_units.go | 56 ++++++++++++ samples/README.md | 1 + samples/data_account_reserved_units/main.tf | 38 ++++++++ 6 files changed, 195 insertions(+) create mode 100644 decort/data_account_reserved_units.go create mode 100644 decort/utility_account_reserved_units.go create mode 100644 samples/data_account_reserved_units/main.tf diff --git a/decort/data_account_reserved_units.go b/decort/data_account_reserved_units.go new file mode 100644 index 0000000..b4d48c9 --- /dev/null +++ b/decort/data_account_reserved_units.go @@ -0,0 +1,98 @@ +/* +Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved. +Author: Stanislav Solovev, + +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 dataSourceAccountReservedUnitsRead(d *schema.ResourceData, m interface{}) error { + accountReservedUnits, err := utilityAccountReservedUnitsCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("cu_c", accountReservedUnits.CUC) + d.Set("cu_d", accountReservedUnits.CUD) + d.Set("cu_i", accountReservedUnits.CUI) + d.Set("cu_m", accountReservedUnits.CUM) + d.Set("cu_np", accountReservedUnits.CUNP) + d.Set("gpu_units", accountReservedUnits.GpuUnits) + + return nil +} + +func dataSourceAccountReservedUnitsSchemaMake() 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 dataSourceAccountReservedUnits() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountReservedUnitsRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountReservedUnitsSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index b321d0e..ce6f97f 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -1004,6 +1004,7 @@ const accountEnableAPI = "/restmachine/cloudapi/account/enable" const accountGetAPI = "/restmachine/cloudapi/account/get" const accountGetConsumedUnitsAPI = "/restmachine/cloudapi/account/getConsumedAccountUnits" const accountGetConsumedUnitsByTypeAPI = "/restmachine/cloudapi/account/getConsumedCloudUnitsByType" +const accountGetReservedUnitsAPI = "/restmachine/cloudapi/account/getReservedAccountUnits" const accountListAPI = "/restmachine/cloudapi/account/list" const accountListComputesAPI = "/restmachine/cloudapi/account/listComputes" const accountListCSAPI = "/cloudapi/account/listCS" diff --git a/decort/provider.go b/decort/provider.go index a12a61c..66f2ce6 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -148,6 +148,7 @@ func Provider() *schema.Provider { "decort_account_rg_list": dataSourceAccountRGList(), "decort_account_consumed_units": dataSourceAccountConsumedUnits(), "decort_account_consumed_units_by_type": dataSourceAccountConsumedUnitsByType(), + "decort_account_reserved_units": dataSourceAccountReservedUnits(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_reserved_units.go b/decort/utility_account_reserved_units.go new file mode 100644 index 0000000..31bd4b9 --- /dev/null +++ b/decort/utility_account_reserved_units.go @@ -0,0 +1,56 @@ +/* +Copyright (c) 2019-2022 Digital Energy Cloud Solutions LLC. All Rights Reserved. +Author: Stanislav Solovev, , + +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 utilityAccountReservedUnitsCheckPresence(d *schema.ResourceData, m interface{}) (*ResourceLimits, error) { + accountReservedUnits := &ResourceLimits{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountReservedUnitsCheckPresence: load units") + accountReservedUnitsRaw, err := controller.decortAPICall("POST", accountGetReservedUnitsAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountReservedUnitsRaw), accountReservedUnits) + if err != nil { + return nil, err + } + + return accountReservedUnits, nil +} diff --git a/samples/README.md b/samples/README.md index dbc389e..85e8dc0 100644 --- a/samples/README.md +++ b/samples/README.md @@ -29,6 +29,7 @@ - account_rg_list - account_counsumed_units - account_counsumed_units_by_type + - account_reserved_units - resources: - image - virtual_image diff --git a/samples/data_account_reserved_units/main.tf b/samples/data_account_reserved_units/main.tf new file mode 100644 index 0000000..f86a3dc --- /dev/null +++ b/samples/data_account_reserved_units/main.tf @@ -0,0 +1,38 @@ +/* +Пример использования +Получение информии о зарезервированных вычислительных мощностях + +*/ +#Расскомментируйте этот код, +#и внесите необходимые правки в версию и путь, +#чтобы работать с установленным вручную (не через hashicorp provider registry) провайдером +/* +terraform { + required_providers { + decort = { + version = "1.1" + source = "digitalenergy.online/decort/decort" + } + } +} +*/ + +provider "decort" { + authenticator = "oauth2" + #controller_url = + controller_url = "https://ds1.digitalenergy.online" + #oauth2_url = + oauth2_url = "https://sso.digitalenergy.online" + allow_unverified_ssl = true +} + +data "decort_account_reserved_units" "aru" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 88366 +} + +output "test" { + value = data.decort_account_reserved_units.aru +}