diff --git a/decort/data_source_account_list.go b/decort/data_source_account_list.go new file mode 100644 index 0000000..c6f6919 --- /dev/null +++ b/decort/data_source_account_list.go @@ -0,0 +1,266 @@ +/* +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 flattenAccountList(al AccountList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, acc := range al { + temp := map[string]interface{}{ + "dc_location": acc.DCLocation, + "ckey": acc.CKey, + "meta": flattenMeta(acc.Meta), + "acl": flattenRgAcl(acc.Acl), + "company": acc.Company, + "companyurl": acc.CompanyUrl, + "created_by": acc.CreatedBy, + "created_time": acc.CreatedTime, + "deactivation_time": acc.DeactiovationTime, + "deleted_by": acc.DeletedBy, + "deleted_time": acc.DeletedTime, + "displayname": acc.DisplayName, + "guid": acc.GUID, + "account_id": acc.ID, + "account_name": acc.Name, + "resource_limits": flattenRgResourceLimits(acc.ResourceLimits), + "send_access_emails": acc.SendAccessEmails, + "service_account": acc.ServiceAccount, + "status": acc.Status, + "updated_time": acc.UpdatedTime, + "version": acc.Version, + "vins": acc.Vins, + } + res = append(res, temp) + } + return res + +} + +func dataSourceAccountListRead(d *schema.ResourceData, m interface{}) error { + accountList, err := utilityAccountListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountList(accountList)) + + return nil +} + +func dataSourceAccountListSchemaMake() map[string]*schema.Schema { + res := map[string]*schema.Schema{ + "page": { + Type: schema.TypeInt, + Optional: true, + Description: "Page number", + }, + "size": { + Type: schema.TypeInt, + Optional: true, + Description: "Page size", + }, + "items": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "dc_location": { + Type: schema.TypeString, + Computed: true, + }, + "ckey": { + Type: schema.TypeString, + Computed: true, + }, + "meta": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "acl": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "explicit": { + Type: schema.TypeBool, + Computed: true, + }, + "guid": { + Type: schema.TypeString, + Computed: true, + }, + "right": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "user_group_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "company": { + Type: schema.TypeString, + Computed: true, + }, + "companyurl": { + Type: schema.TypeString, + Computed: true, + }, + "created_by": { + Type: schema.TypeString, + Computed: true, + }, + "created_time": { + Type: schema.TypeInt, + Computed: true, + }, + "deactivation_time": { + Type: schema.TypeFloat, + Computed: true, + }, + "deleted_by": { + Type: schema.TypeString, + Computed: true, + }, + "deleted_time": { + Type: schema.TypeInt, + Computed: true, + }, + "displayname": { + Type: schema.TypeString, + Computed: true, + }, + "guid": { + Type: schema.TypeInt, + Computed: true, + }, + "account_id": { + Type: schema.TypeInt, + Computed: true, + }, + "account_name": { + Type: schema.TypeString, + Computed: true, + }, + "resource_limits": { + Type: schema.TypeList, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "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, + }, + }, + }, + }, + "send_access_emails": { + Type: schema.TypeBool, + Computed: true, + }, + "service_account": { + Type: schema.TypeBool, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "updated_time": { + Type: schema.TypeInt, + Computed: true, + }, + "version": { + Type: schema.TypeInt, + Computed: true, + }, + "vins": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index b3e6f86..ea9dff4 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -987,3 +987,86 @@ type SepConfig map[string]interface{} type SepList []Sep type SepPool map[string]interface{} + +/////////////////////// +///// ACCOUNTS //// +/////////////////////// + +const accountAddUserAPI = "/cloudapi/account/addUser" +const accountAuditsAPI = "/cloudapi/account/audits" +const accountCreateAPI = "/cloudapi/account/create" +const accountDeleteAPI = "/cloudapi/account/delete" +const accountDeleteAccountsAPI = "/cloudapi/account/deleteAccounts" +const accountDeleteUserAPI = "/cloudapi/account/deleteUser" +const accountDisableAPI = "/cloudapi/account/disable" +const accountEnableAPI = "/cloudapi/account/enable" +const accountGetAPI = "/cloudapi/account/get" +const accountGetConsumedAccountUnitsAPI = "/cloudapi/account/getConsumedAccountUnits" +const accountGetConsumedCloudUnitsByTypeAPI = "/cloudapi/account/getConsumedCloudUnitsByType" +const accountGetConsumptionGetAPI = "/cloudapi/account/getConsumption" +const accountGetConsumptionPostAPI = "/cloudapi/account/getConsumption" +const accountGetReservedAccountUnitsAPI = "/cloudapi/account/getReservedAccountUnits" +const accountGetStatsAPI = "/cloudapi/account/getStats" + +//const accountListAPI = "/cloudapi/account/list" +const accountListAPI = "/restmachine/cloudbroker/account/list" +const accountListComputesAPI = "/cloudapi/account/listComputes" +const accountListCSAPI = "/cloudapi/account/listCS" +const accountListDeletedAPI = "/cloudapi/account/listDeleted" +const accountListDisksAPI = "/cloudapi/account/listDisks" +const accountListFlipGroupsAPI = "/cloudapi/account/listFlipGroups" +const accountListRGAPI = "/cloudapi/account/listRG" +const accountListTemplatesAPI = "/cloudapi/account/listTemplates" +const accountListVinsAPI = "/cloudapi/account/listVins" +const accountListVMsAPI = "/cloudapi/account/listVMs" +const accountRestoreAPI = "/cloudapi/account/restore" +const accountUpdateAPI = "/cloudapi/account/update" +const accountUpdateUserAPI = "/cloudapi/account/updateUser" + +////Structs + +type Account struct { + DCLocation string `json:"DCLocation"` + CKey string `jspn:"_ckey"` + Meta []interface{} `json:"_meta"` + Acl []AccountAclRecord `json:"acl"` + Company string `json:"company"` + CompanyUrl string `json:"companyurl"` + CreatedBy string `jspn:"createdBy"` + CreatedTime int `json:"createdTime"` + DeactiovationTime float64 `json:"deactivationTime"` + DeletedBy string `json:"deletedBy"` + DeletedTime int `json:"deletedTime"` + DisplayName string `json:"displayname"` + GUID int `json:"guid"` + ID int `json:"id"` + Name string `json:"name"` + ResourceLimits ResourceLimits `json:"resourceLimits"` + SendAccessEmails bool `json:"sendAccessEmails"` + ServiceAccount bool `json:"serviceAccount"` + Status string `json:"status"` + UpdatedTime int `json:"updatedTime"` + Version int `json:"version"` + Vins []int `json:"vins"` +} + +type AccountList []Account + +type Resource struct { + CPU int `json:"cpu"` + Disksize int `json:"disksize"` + Extips int `json:"extips"` + Exttraffic int `json:"exttraffic"` + GPU int `json:"gpu"` + RAM int `json:"ram"` +} + +type Resources struct { + Current Resource `json:"Current"` + Reserved Resource `json:"Reserved"` +} + +type AccountWithResources struct { + Account + Resources Resources `json:"Resources"` +} diff --git a/decort/provider.go b/decort/provider.go index 00af955..7c7887e 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -139,6 +139,7 @@ func Provider() *schema.Provider { "decort_sep_pool": dataSourceSepPool(), "decort_disk_list": dataSourceDiskList(), "decort_rg_list": dataSourceRgList(), + "decort_account_list": dataSourceAccountList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_list.go b/decort/utility_account_list.go new file mode 100644 index 0000000..828ecf9 --- /dev/null +++ b/decort/utility_account_list.go @@ -0,0 +1,61 @@ +/* +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 utilityAccountListCheckPresence(d *schema.ResourceData, m interface{}) (AccountList, error) { + accountList := AccountList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + if page, ok := d.GetOk("page"); ok { + urlValues.Add("page", strconv.Itoa(page.(int))) + } + if size, ok := d.GetOk("size"); ok { + urlValues.Add("size", strconv.Itoa(size.(int))) + } + + log.Debugf("utilityAccountListCheckPresence: load account list") + accountListRaw, err := controller.decortAPICall("POST", accountListAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountListRaw), &accountList) + if err != nil { + return nil, err + } + + return accountList, nil +} diff --git a/samples/README.md b/samples/README.md index 6761a86..c4ff93c 100644 --- a/samples/README.md +++ b/samples/README.md @@ -20,6 +20,7 @@ - vgpu - disk_list - rg_list + - account_list - resources: - image - virtual_image diff --git a/samples/data_account_list/main.tf b/samples/data_account_list/main.tf new file mode 100644 index 0000000..722077a --- /dev/null +++ b/samples/data_account_list/main.tf @@ -0,0 +1,45 @@ +/* +Пример использования +Получение списка доступных аккаунтов + +*/ +#Расскомментируйте этот код, +#и внесите необходимые правки в версию и путь, +#чтобы работать с установленным вручную (не через 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_list" "al" { + #номер страницы для отображения + #опциональный параметр + #тип - число + #если не задан - выводятся все доступные данные + #page = 2 + + #размер страницы + #опциональный параметр + #тип - число + #если не задан - выводятся все доступные данные + #size = 3 +} + +output "test" { + value = data.decort_account_list.al +}