From 9f5b4ab771b7200f29b47f02ace3b16472ef653b Mon Sep 17 00:00:00 2001 From: stSolo Date: Fri, 27 May 2022 13:48:58 +0300 Subject: [PATCH] Add a data for account rg list --- decort/data_source_account_rg_list.go | 294 ++++++++++++++++++++++++++ decort/models_api.go | 31 ++- decort/provider.go | 1 + decort/utility_account_rg_list.go | 56 +++++ samples/README.md | 2 + samples/data_account_rg_list/main.tf | 37 ++++ 6 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 decort/data_source_account_rg_list.go create mode 100644 decort/utility_account_rg_list.go create mode 100644 samples/data_account_rg_list/main.tf diff --git a/decort/data_source_account_rg_list.go b/decort/data_source_account_rg_list.go new file mode 100644 index 0000000..40e2284 --- /dev/null +++ b/decort/data_source_account_rg_list.go @@ -0,0 +1,294 @@ +/* +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 flattenAccountRGList(argl AccountRGList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, arg := range argl { + temp := map[string]interface{}{ + "computes": flattenAccRGComputes(arg.Computes), + "resources": flattenAccRGResources(arg.Resources), + "created_by": arg.CreatedBy, + "created_time": arg.CreatedTime, + "deleted_by": arg.DeletedBy, + "deleted_time": arg.DeletedTime, + "rg_id": arg.RGID, + "milestones": arg.Milestones, + "rg_name": arg.RGName, + "status": arg.Status, + "updated_by": arg.UpdatedBy, + "updated_time": arg.UpdatedTime, + "vinses": arg.Vinses, + } + res = append(res, temp) + } + return res + +} + +func flattenAccRGComputes(argc AccountRGComputes) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + temp := map[string]interface{}{ + "started": argc.Started, + "stopped": argc.Stopped, + } + res = append(res, temp) + return res +} + +func flattenAccRGResources(argr AccountRGResources) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + temp := map[string]interface{}{ + "consumed": flattenAccResource(argr.Consumed), + "limits": flattenAccResource(argr.Limits), + "reserved": flattenAccResource(argr.Reserved), + } + res = append(res, temp) + return res +} + +func dataSourceAccountRGListRead(d *schema.ResourceData, m interface{}) error { + accountRGList, err := utilityAccountRGListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountRGList(accountRGList)) + + return nil +} + +func dataSourceAccountRGListSchemaMake() map[string]*schema.Schema { + res := map[string]*schema.Schema{ + "account_id": { + Type: schema.TypeInt, + Required: true, + Description: "ID of the account", + }, + "items": { + Type: schema.TypeList, + Computed: true, + Description: "Search Result", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "computes": { + Type: schema.TypeList, + MaxItems: 1, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "started": { + Type: schema.TypeInt, + Computed: true, + }, + "stopped": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + "resources": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "consumed": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cpu": { + Type: schema.TypeInt, + Computed: true, + }, + "disksize": { + Type: schema.TypeInt, + Computed: true, + }, + "extips": { + Type: schema.TypeInt, + Computed: true, + }, + "exttraffic": { + Type: schema.TypeInt, + Computed: true, + }, + "gpu": { + Type: schema.TypeInt, + Computed: true, + }, + "ram": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + + "limits": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cpu": { + Type: schema.TypeInt, + Computed: true, + }, + "disksize": { + Type: schema.TypeInt, + Computed: true, + }, + "extips": { + Type: schema.TypeInt, + Computed: true, + }, + "exttraffic": { + Type: schema.TypeInt, + Computed: true, + }, + "gpu": { + Type: schema.TypeInt, + Computed: true, + }, + "ram": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + "reserved": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cpu": { + Type: schema.TypeInt, + Computed: true, + }, + "disksize": { + Type: schema.TypeInt, + Computed: true, + }, + "extips": { + Type: schema.TypeInt, + Computed: true, + }, + "exttraffic": { + Type: schema.TypeInt, + Computed: true, + }, + "gpu": { + Type: schema.TypeInt, + Computed: true, + }, + "ram": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + }, + }, + + "created_by": { + Type: schema.TypeString, + Computed: true, + }, + "created_time": { + Type: schema.TypeInt, + Computed: true, + }, + "deleted_by": { + Type: schema.TypeString, + Computed: true, + }, + "deleted_time": { + Type: schema.TypeInt, + Computed: true, + }, + "rg_id": { + Type: schema.TypeInt, + Computed: true, + }, + "milestones": { + Type: schema.TypeInt, + Computed: true, + }, + "rg_name": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "updated_by": { + Type: schema.TypeString, + Computed: true, + }, + "updated_time": { + Type: schema.TypeInt, + Computed: true, + }, + "vinses": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountRGList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountRGListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountRGListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index 80b5ffd..69745b4 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -1008,7 +1008,7 @@ const accountListCSAPI = "/cloudapi/account/listCS" const accountListDeletedAPI = "/cloudapi/account/listDeleted" const accountListDisksAPI = "/restmachine/cloudapi/account/listDisks" const accountListFlipGroupsAPI = "/cloudapi/account/listFlipGroups" -const accountListRGAPI = "/cloudapi/account/listRG" +const accountListRGAPI = "/restmachine/cloudapi/account/listRG" const accountListTemplatesAPI = "/cloudapi/account/listTemplates" const accountListVinsAPI = "/restmachine/cloudapi/account/listVins" const accountListVMsAPI = "/cloudapi/account/listVMs" @@ -1156,3 +1156,32 @@ type AccountAudit struct { } type AccountAuditsList []AccountAudit + +type AccountRGComputes struct { + Started int `json:"Started"` + Stopped int `json:"Stopped"` +} + +type AccountRGResources struct { + Consumed Resource `json:"Consumed"` + Limits Resource `json:"Limits"` + Reserved Resource `json:"Reserved"` +} + +type AccountRG struct { + Computes AccountRGComputes `json:"Computes"` + Resources AccountRGResources `json:"Resources"` + CreatedBy string `json:"createdBy"` + CreatedTime int `json:"createdTime"` + DeletedBy string `json:"deletedBy"` + DeletedTime int `json:"deletedTime"` + RGID int `json:"id"` + Milestones int `json:"milestones"` + RGName string `json:"name"` + Status string `json:"status"` + UpdatedBy string `json:"updatedBy"` + UpdatedTime int `json:"updatedTime"` + Vinses int `json:"vinses"` +} + +type AccountRGList []AccountRG diff --git a/decort/provider.go b/decort/provider.go index 7145229..aba1acc 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -145,6 +145,7 @@ func Provider() *schema.Provider { "decort_account_disks_list": dataSourceAccountDisksList(), "decort_account_vins_list": dataSourceAccountVinsList(), "decort_account_audits_list": dataSourceAccountAuditsList(), + "decort_account_rg_list": dataSourceAccountRGList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_rg_list.go b/decort/utility_account_rg_list.go new file mode 100644 index 0000000..0145b3a --- /dev/null +++ b/decort/utility_account_rg_list.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 utilityAccountRGListCheckPresence(d *schema.ResourceData, m interface{}) (AccountRGList, error) { + accountRGList := AccountRGList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountRGListCheckPresence: load account list") + accountRGListRaw, err := controller.decortAPICall("POST", accountListRGAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountRGListRaw), &accountRGList) + if err != nil { + return nil, err + } + + return accountRGList, nil +} diff --git a/samples/README.md b/samples/README.md index 3b091f7..20326fc 100644 --- a/samples/README.md +++ b/samples/README.md @@ -26,6 +26,7 @@ - account_vins_list - account_audits_list - account + - account_rg_list - resources: - image - virtual_image @@ -37,6 +38,7 @@ - pcidevice - sep - sep_config + - account ## Как пользоваться примерами 1. Установить terraform diff --git a/samples/data_account_rg_list/main.tf b/samples/data_account_rg_list/main.tf new file mode 100644 index 0000000..48d5418 --- /dev/null +++ b/samples/data_account_rg_list/main.tf @@ -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 = + controller_url = "https://ds1.digitalenergy.online" + #oauth2_url = + oauth2_url = "https://sso.digitalenergy.online" + allow_unverified_ssl = true +} + +data "decort_account_rg_list" "argl" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 88366 +} + +output "test" { + value = data.decort_account_rg_list.argl +}