From d527d9b47483405cd6e5d8454470232327f88907 Mon Sep 17 00:00:00 2001 From: stSolo Date: Tue, 24 May 2022 18:53:02 +0300 Subject: [PATCH] Add an account vins list --- decort/data_source_account_disks_list.go | 18 +- decort/data_source_account_vins_list.go | 174 ++++++++++++++++++ decort/models_api.go | 26 ++- decort/provider.go | 1 + decort/utility_account_vins_list.go | 56 ++++++ .../main.tf | 0 .../main.tf | 0 samples/data_account_vins_list/main.tf | 39 ++++ 8 files changed, 304 insertions(+), 10 deletions(-) create mode 100644 decort/data_source_account_vins_list.go create mode 100644 decort/utility_account_vins_list.go rename samples/{account_computes_list => data_account_computes_list}/main.tf (100%) rename samples/{account_disks_list => data_account_disks_list}/main.tf (100%) create mode 100644 samples/data_account_vins_list/main.tf diff --git a/decort/data_source_account_disks_list.go b/decort/data_source_account_disks_list.go index 489f8eb..6c33ced 100644 --- a/decort/data_source_account_disks_list.go +++ b/decort/data_source_account_disks_list.go @@ -31,14 +31,14 @@ import ( func flattenAccountDisksList(adl AccountDisksList) []map[string]interface{} { res := make([]map[string]interface{}, 0) - for _, acc := range adl { + for _, ad := range adl { temp := map[string]interface{}{ - "disk_id": acc.ID, - "disk_name": acc.Name, - "pool": acc.Pool, - "sep_id": acc.SepId, - "size_max": acc.SizeMax, - "type": acc.Type, + "disk_id": ad.ID, + "disk_name": ad.Name, + "pool": ad.Pool, + "sep_id": ad.SepId, + "size_max": ad.SizeMax, + "type": ad.Type, } res = append(res, temp) } @@ -46,7 +46,7 @@ func flattenAccountDisksList(adl AccountDisksList) []map[string]interface{} { } -func dataSourceAccountDiskssListRead(d *schema.ResourceData, m interface{}) error { +func dataSourceAccountDisksListRead(d *schema.ResourceData, m interface{}) error { accountDisksList, err := utilityAccountDisksListCheckPresence(d, m) if err != nil { return err @@ -107,7 +107,7 @@ func dataSourceAccountDisksList() *schema.Resource { return &schema.Resource{ SchemaVersion: 1, - Read: dataSourceAccountDiskssListRead, + Read: dataSourceAccountDisksListRead, Timeouts: &schema.ResourceTimeout{ Read: &Timeout30s, diff --git a/decort/data_source_account_vins_list.go b/decort/data_source_account_vins_list.go new file mode 100644 index 0000000..27307ff --- /dev/null +++ b/decort/data_source_account_vins_list.go @@ -0,0 +1,174 @@ +/* +Copyright (c) 2019-2021 Digital Energy Cloud Solutions LLC. All Rights Reserved. +Author: Sergey Shubin, , + +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 flattenAccountVinsList(avl AccountVinsList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, av := range avl { + temp := map[string]interface{}{ + "account_id": av.AccountId, + "account_name": av.AccountName, + "computes": av.Computes, + "created_by": av.CreatedBy, + "created_time": av.CreatedTime, + "deleted_by": av.DeletedBy, + "deleted_time": av.DeletedTime, + "external_ip": av.ExternalIP, + "vin_id": av.ID, + "vin_name": av.Name, + "network": av.Network, + "pri_vnf_dev_id": av.PriVnfDevId, + "rg_id": av.RgId, + "rg_name": av.RgName, + "status": av.Status, + "updated_by": av.UpdatedBy, + "updated_time": av.UpdatedTime, + } + res = append(res, temp) + } + return res + +} + +func dataSourceAccountVinsListRead(d *schema.ResourceData, m interface{}) error { + accountVinsList, err := utilityAccountVinsListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountVinsList(accountVinsList)) + + return nil +} + +func dataSourceAccountVinsListSchemaMake() 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{ + "account_id": { + Type: schema.TypeInt, + Computed: true, + }, + "account_name": { + Type: schema.TypeString, + Computed: true, + }, + "computes": { + 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, + }, + "external_ip": { + Type: schema.TypeString, + Computed: true, + }, + "vin_id": { + Type: schema.TypeInt, + Computed: true, + }, + "vin_name": { + Type: schema.TypeString, + Computed: true, + }, + "network": { + Type: schema.TypeString, + Computed: true, + }, + "pri_vnf_dev_id": { + Type: schema.TypeInt, + Computed: true, + }, + "rg_id": { + 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, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountVinsList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountVinsListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountVinsListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index 9c9b3a8..9b100f6 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -1021,7 +1021,9 @@ const accountListDisksAPI = "/restmachine/cloudbroker/account/listDisks" const accountListFlipGroupsAPI = "/cloudapi/account/listFlipGroups" const accountListRGAPI = "/cloudapi/account/listRG" const accountListTemplatesAPI = "/cloudapi/account/listTemplates" -const accountListVinsAPI = "/cloudapi/account/listVins" + +//const accountListVinsAPI = "/restmachine/cloudapi/account/listVins" +const accountListVinsAPI = "/restmachine/cloudbroker/account/listVins" const accountListVMsAPI = "/cloudapi/account/listVMs" const accountRestoreAPI = "/cloudapi/account/restore" const accountUpdateAPI = "/cloudapi/account/update" @@ -1110,3 +1112,25 @@ type AccountDisk struct { } type AccountDisksList []AccountDisk + +type AccountVin struct { + AccountId int `json:"accountId"` + AccountName string `json:"accountName"` + Computes int `json:"computes"` + CreatedBy string `json:"createdBy"` + CreatedTime int `json:"createdTime"` + DeletedBy string `json:"deletedBy"` + DeletedTime int `json:"deletedTime"` + ExternalIP string `json:"externalIP"` + ID int `json:"id"` + Name string `json:"name"` + Network string `json:"network"` + PriVnfDevId int `json:"priVnfDevId"` + RgId int `json:"rgId"` + RgName string `json:"rgName"` + Status string `json:"status"` + UpdatedBy string `json:"updatedBy"` + UpdatedTime int `json:"updatedTime"` +} + +type AccountVinsList []AccountVin diff --git a/decort/provider.go b/decort/provider.go index 98760cf..4f2f4ea 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -142,6 +142,7 @@ func Provider() *schema.Provider { "decort_account_list": dataSourceAccountList(), "decort_account_computes_list": dataSourceAccountComputesList(), "decort_account_disks_list": dataSourceAccountDisksList(), + "decort_account_vins_list": dataSourceAccountVinsList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_vins_list.go b/decort/utility_account_vins_list.go new file mode 100644 index 0000000..31291ff --- /dev/null +++ b/decort/utility_account_vins_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 utilityAccountVinsListCheckPresence(d *schema.ResourceData, m interface{}) (AccountVinsList, error) { + accountVinsList := AccountVinsList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountVinsListCheckPresence: load account list") + accountVinsListRaw, err := controller.decortAPICall("POST", accountListVinsAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountVinsListRaw), &accountVinsList) + if err != nil { + return nil, err + } + + return accountVinsList, nil +} diff --git a/samples/account_computes_list/main.tf b/samples/data_account_computes_list/main.tf similarity index 100% rename from samples/account_computes_list/main.tf rename to samples/data_account_computes_list/main.tf diff --git a/samples/account_disks_list/main.tf b/samples/data_account_disks_list/main.tf similarity index 100% rename from samples/account_disks_list/main.tf rename to samples/data_account_disks_list/main.tf diff --git a/samples/data_account_vins_list/main.tf b/samples/data_account_vins_list/main.tf new file mode 100644 index 0000000..da28996 --- /dev/null +++ b/samples/data_account_vins_list/main.tf @@ -0,0 +1,39 @@ +/* +Пример использования +Получение списка vins, используемых аккаунтом + +*/ +#Расскомментируйте этот код, +#и внесите необходимые правки в версию и путь, +#чтобы работать с установленным вручную (не через 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_vins_list" "avl" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 28096 + +} + +output "test" { + value = data.decort_account_vins_list.avl +}