From 61b8765c82af2f4bf4e467539ec6c4ad7a988dfd Mon Sep 17 00:00:00 2001 From: stSolo Date: Fri, 27 May 2022 19:39:49 +0300 Subject: [PATCH] Add data for account templates lis --- decort/data_account_templates_list.go | 139 ++++++++++++++++++++ decort/models_api.go | 17 ++- decort/provider.go | 1 + decort/utility_account_templates_list.go | 56 ++++++++ samples/README.md | 1 + samples/data_account_templates_list/main.tf | 38 ++++++ 6 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 decort/data_account_templates_list.go create mode 100644 decort/utility_account_templates_list.go create mode 100644 samples/data_account_templates_list/main.tf diff --git a/decort/data_account_templates_list.go b/decort/data_account_templates_list.go new file mode 100644 index 0000000..4071088 --- /dev/null +++ b/decort/data_account_templates_list.go @@ -0,0 +1,139 @@ +/* +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 flattenAccountTemplatesList(atl AccountTemplatesList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, at := range atl { + temp := map[string]interface{}{ + "unc_path": at.UNCPath, + "account_id": at.AccountId, + "desc": at.Desc, + "template_id": at.ID, + "template_name": at.Name, + "public": at.Public, + "size": at.Size, + "status": at.Status, + "type": at.Type, + "username": at.Username, + } + res = append(res, temp) + } + return res + +} + +func dataSourceAccountTemplatesListRead(d *schema.ResourceData, m interface{}) error { + accountTemplatesList, err := utilityAccountTemplatesListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountTemplatesList(accountTemplatesList)) + + return nil +} + +func dataSourceAccountTemplatesListSchemaMake() 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{ + "unc_path": { + Type: schema.TypeString, + Computed: true, + }, + "account_id": { + Type: schema.TypeInt, + Computed: true, + }, + "desc": { + Type: schema.TypeString, + Computed: true, + }, + "template_id": { + Type: schema.TypeInt, + Computed: true, + }, + "template_name": { + Type: schema.TypeString, + Computed: true, + }, + "public": { + Type: schema.TypeBool, + Computed: true, + }, + "size": { + Type: schema.TypeInt, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "username": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountTemplatessList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountTemplatesListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountTemplatesListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index ce6f97f..cbdf434 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -1012,7 +1012,7 @@ const accountListDeletedAPI = "/cloudapi/account/listDeleted" const accountListDisksAPI = "/restmachine/cloudapi/account/listDisks" const accountListFlipGroupsAPI = "/cloudapi/account/listFlipGroups" const accountListRGAPI = "/restmachine/cloudapi/account/listRG" -const accountListTemplatesAPI = "/cloudapi/account/listTemplates" +const accountListTemplatesAPI = "/restmachine/cloudapi/account/listTemplates" const accountListVinsAPI = "/restmachine/cloudapi/account/listVins" const accountListVMsAPI = "/cloudapi/account/listVMs" const accountRestoreAPI = "/restmachine/cloudapi/account/restore" @@ -1188,3 +1188,18 @@ type AccountRG struct { } type AccountRGList []AccountRG + +type AccountTemplate struct { + UNCPath string `json:"UNCPath"` + AccountId int `json:"accountId"` + Desc string `json:"desc"` + ID int `json:"id"` + Name string `json:"name"` + Public bool `json:"public"` + Size int `json:"size"` + Status string `json:"status"` + Type string `json:"type"` + Username string `json:"username"` +} + +type AccountTemplatesList []AccountTemplate diff --git a/decort/provider.go b/decort/provider.go index 66f2ce6..0e48ce6 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -149,6 +149,7 @@ func Provider() *schema.Provider { "decort_account_consumed_units": dataSourceAccountConsumedUnits(), "decort_account_consumed_units_by_type": dataSourceAccountConsumedUnitsByType(), "decort_account_reserved_units": dataSourceAccountReservedUnits(), + "decort_account_templates_list": dataSourceAccountTemplatessList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_templates_list.go b/decort/utility_account_templates_list.go new file mode 100644 index 0000000..2dd3a2e --- /dev/null +++ b/decort/utility_account_templates_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 utilityAccountTemplatesListCheckPresence(d *schema.ResourceData, m interface{}) (AccountTemplatesList, error) { + accountTemplatesList := AccountTemplatesList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountTemplatesListCheckPresence: load") + accountTemplatesListRaw, err := controller.decortAPICall("POST", accountListTemplatesAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountTemplatesListRaw), &accountTemplatesList) + if err != nil { + return nil, err + } + + return accountTemplatesList, nil +} diff --git a/samples/README.md b/samples/README.md index 85e8dc0..291ac14 100644 --- a/samples/README.md +++ b/samples/README.md @@ -30,6 +30,7 @@ - account_counsumed_units - account_counsumed_units_by_type - account_reserved_units + - account_templates_list - resources: - image - virtual_image diff --git a/samples/data_account_templates_list/main.tf b/samples/data_account_templates_list/main.tf new file mode 100644 index 0000000..276fe68 --- /dev/null +++ b/samples/data_account_templates_list/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_templates_list" "atl" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 11111 +} + +output "test" { + value = data.decort_account_templates_list.atl +}