diff --git a/decort/data_source_account_computes_list.go b/decort/data_source_account_computes_list.go new file mode 100644 index 0000000..b4b99ad --- /dev/null +++ b/decort/data_source_account_computes_list.go @@ -0,0 +1,189 @@ +/* +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 flattenAccountComputesList(acl AccountComputesList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, acc := range acl { + temp := map[string]interface{}{ + "account_id": acc.AccountId, + "account_name": acc.AccountName, + "cpus": acc.CPUs, + "created_by": acc.CreatedBy, + "created_time": acc.CreatedTime, + "deleted_by": acc.DeletedBy, + "deleted_time": acc.DeletedTime, + "compute_id": acc.ComputeId, + "compute_name": acc.ComputeName, + "ram": acc.RAM, + "registered": acc.Registered, + "rg_id": acc.RgId, + "rg_name": acc.RgName, + "status": acc.Status, + "tech_status": acc.TechStatus, + "total_disks_size": acc.TotalDisksSize, + "updated_by": acc.UpdatedBy, + "updated_time": acc.UpdatedTime, + "user_managed": acc.UserManaged, + "vins_connected": acc.VinsConnected, + } + res = append(res, temp) + } + return res + +} + +func dataSourceAccountComputesListRead(d *schema.ResourceData, m interface{}) error { + accountComputesList, err := utilityAccountComputesListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountComputesList(accountComputesList)) + + return nil +} + +func dataSourceAccountComputesListSchemaMake() 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, + }, + "cpus": { + 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, + }, + "compute_id": { + Type: schema.TypeInt, + Computed: true, + }, + "compute_name": { + Type: schema.TypeString, + Computed: true, + }, + "ram": { + Type: schema.TypeInt, + Computed: true, + }, + "registered": { + Type: schema.TypeBool, + Computed: true, + }, + "rg_id": { + Type: schema.TypeInt, + Computed: true, + }, + "rg_name": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tech_status": { + Type: schema.TypeString, + Computed: true, + }, + "total_disks_size": { + Type: schema.TypeInt, + Computed: true, + }, + "updated_by": { + Type: schema.TypeString, + Computed: true, + }, + "updated_time": { + Type: schema.TypeInt, + Computed: true, + }, + "user_managed": { + Type: schema.TypeBool, + Computed: true, + }, + "vins_connected": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountComputesList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountComputesListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountComputesListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index ea9dff4..e02ed2c 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -1008,9 +1008,11 @@ const accountGetConsumptionPostAPI = "/cloudapi/account/getConsumption" const accountGetReservedAccountUnitsAPI = "/cloudapi/account/getReservedAccountUnits" const accountGetStatsAPI = "/cloudapi/account/getStats" -//const accountListAPI = "/cloudapi/account/list" +//const accountListAPI = "/restmachine/cloudapi/account/list" const accountListAPI = "/restmachine/cloudbroker/account/list" -const accountListComputesAPI = "/cloudapi/account/listComputes" + +//const accountListComputesAPI = "/restmachine/cloudapi/account/listComputes" +const accountListComputesAPI = "/restmachine/cloudbroker/account/listComputes" const accountListCSAPI = "/cloudapi/account/listCS" const accountListDeletedAPI = "/cloudapi/account/listDeleted" const accountListDisksAPI = "/cloudapi/account/listDisks" @@ -1070,3 +1072,28 @@ type AccountWithResources struct { Account Resources Resources `json:"Resources"` } + +type AccountCompute struct { + AccountId int `json:"accountId"` + AccountName string `json:"accountName"` + CPUs int `json:"cpus"` + CreatedBy string `json:"createdBy"` + CreatedTime int `json:"createdTime"` + DeletedBy string `json:"deletedBy"` + DeletedTime int `json:"deletedTime"` + ComputeId int `json:"id"` + ComputeName string `json:"name"` + RAM int `json:"ram"` + Registered bool `json:"registered"` + RgId int `json:"rgId"` + RgName string `json:"rgName"` + Status string `json:"status"` + TechStatus string `json:"techStatus"` + TotalDisksSize int `json:"totalDisksSize"` + UpdatedBy string `json:"updatedBy"` + UpdatedTime int `json:"updatedTime"` + UserManaged bool `json:"userManaged"` + VinsConnected int `json:"vinsConnected"` +} + +type AccountComputesList []AccountCompute diff --git a/decort/provider.go b/decort/provider.go index 7c7887e..c2a5fa5 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -117,29 +117,30 @@ func Provider() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "decort_account": dataSourceAccount(), - "decort_resgroup": dataSourceResgroup(), - "decort_kvmvm": dataSourceCompute(), - "decort_image": dataSourceImage(), - "decort_disk": dataSourceDisk(), - "decort_vins": dataSourceVins(), - "decort_grid": dataSourceGrid(), - "decort_grid_list": dataSourceGridList(), - "decort_image_list": dataSourceImageList(), - "decort_image_list_stacks": dataSourceImageListStacks(), - "decort_snapshot_list": dataSourceSnapshotList(), - "decort_vgpu": dataSourceVGPU(), - "decort_pcidevice": dataSourcePcidevice(), - "decort_pcidevice_list": dataSourcePcideviceList(), - "decort_sep_list": dataSourceSepList(), - "decort_sep": dataSourceSep(), - "decort_sep_consumption": dataSourceSepConsumption(), - "decort_sep_disk_list": dataSourceSepDiskList(), - "decort_sep_config": dataSourceSepConfig(), - "decort_sep_pool": dataSourceSepPool(), - "decort_disk_list": dataSourceDiskList(), - "decort_rg_list": dataSourceRgList(), - "decort_account_list": dataSourceAccountList(), + "decort_account": dataSourceAccount(), + "decort_resgroup": dataSourceResgroup(), + "decort_kvmvm": dataSourceCompute(), + "decort_image": dataSourceImage(), + "decort_disk": dataSourceDisk(), + "decort_vins": dataSourceVins(), + "decort_grid": dataSourceGrid(), + "decort_grid_list": dataSourceGridList(), + "decort_image_list": dataSourceImageList(), + "decort_image_list_stacks": dataSourceImageListStacks(), + "decort_snapshot_list": dataSourceSnapshotList(), + "decort_vgpu": dataSourceVGPU(), + "decort_pcidevice": dataSourcePcidevice(), + "decort_pcidevice_list": dataSourcePcideviceList(), + "decort_sep_list": dataSourceSepList(), + "decort_sep": dataSourceSep(), + "decort_sep_consumption": dataSourceSepConsumption(), + "decort_sep_disk_list": dataSourceSepDiskList(), + "decort_sep_config": dataSourceSepConfig(), + "decort_sep_pool": dataSourceSepPool(), + "decort_disk_list": dataSourceDiskList(), + "decort_rg_list": dataSourceRgList(), + "decort_account_list": dataSourceAccountList(), + "decort_account_computes_list": dataSourceAccountComputesList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account.go b/decort/utility_account.go index f1eaf34..c9a6def 100644 --- a/decort/utility_account.go +++ b/decort/utility_account.go @@ -110,8 +110,8 @@ func utilityGetAccountIdBySchema(d *schema.ResourceData, m interface{}) (int, er initiate API calls to the DECORT cloud controller and try to match relevant account by the name. - NOTE that for some resources (most notably, Resource Group) "account_name" attribute is - marked as "Computed: true", so the only way to fully identify Resource Group is to specify + NOTE that for some resources (most notably, Resource Group) "account_name" attribute is + marked as "Computed: true", so the only way to fully identify Resource Group is to specify "account_id", which is marked as "Required: true" */ diff --git a/decort/utility_accout_computes_list.go b/decort/utility_accout_computes_list.go new file mode 100644 index 0000000..6c92590 --- /dev/null +++ b/decort/utility_accout_computes_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 utilityAccountComputesListCheckPresence(d *schema.ResourceData, m interface{}) (AccountComputesList, error) { + accountComputesList := AccountComputesList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountComputesListCheckPresence: load account list") + accountComputesListRaw, err := controller.decortAPICall("POST", accountListComputesAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountComputesListRaw), &accountComputesList) + if err != nil { + return nil, err + } + + return accountComputesList, nil +} diff --git a/samples/account_computes_list/main.tf b/samples/account_computes_list/main.tf new file mode 100644 index 0000000..26edae2 --- /dev/null +++ b/samples/account_computes_list/main.tf @@ -0,0 +1,39 @@ +/* +Пример использования +Получение списка computes, используемых аккаунтом + +*/ +#Расскомментируйте этот код, +#и внесите необходимые правки в версию и путь, +#чтобы работать с установленным вручную (не через 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_computes_list" "acl" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 1111 + +} + +output "test" { + value = data.decort_account_computes_list.acl +}