From 9912b04c2a8c1fc3ebf457a20eb4509ed831806e Mon Sep 17 00:00:00 2001 From: stSolo Date: Tue, 24 May 2022 19:22:29 +0300 Subject: [PATCH] Add data function account-audits-list --- decort/data_source_account_audits_list.go | 114 ++++++++++++++++++++++ decort/models_api.go | 14 ++- decort/provider.go | 1 + decort/utility_account_audits_list.go | 56 +++++++++++ samples/README.md | 4 + samples/data_account_audits_list/main.tf | 40 ++++++++ 6 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 decort/data_source_account_audits_list.go create mode 100644 decort/utility_account_audits_list.go create mode 100644 samples/data_account_audits_list/main.tf diff --git a/decort/data_source_account_audits_list.go b/decort/data_source_account_audits_list.go new file mode 100644 index 0000000..3475527 --- /dev/null +++ b/decort/data_source_account_audits_list.go @@ -0,0 +1,114 @@ +/* +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 flattenAccountAuditsList(aal AccountAuditsList) []map[string]interface{} { + res := make([]map[string]interface{}, 0) + for _, aa := range aal { + temp := map[string]interface{}{ + "call": aa.Call, + "responsetime": aa.ResponseTime, + "statuscode": aa.StatusCode, + "timestamp": aa.Timestamp, + "user": aa.User, + } + res = append(res, temp) + } + return res + +} + +func dataSourceAccountAuditsListRead(d *schema.ResourceData, m interface{}) error { + accountAuditsList, err := utilityAccountAuditsListCheckPresence(d, m) + if err != nil { + return err + } + + id := uuid.New() + d.SetId(id.String()) + d.Set("items", flattenAccountAuditsList(accountAuditsList)) + + return nil +} + +func dataSourceAccountAuditsListSchemaMake() 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{ + "call": { + Type: schema.TypeString, + Computed: true, + }, + "responsetime": { + Type: schema.TypeFloat, + Computed: true, + }, + "statuscode": { + Type: schema.TypeInt, + Computed: true, + }, + "timestamp": { + Type: schema.TypeFloat, + Computed: true, + }, + "user": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + } + return res +} + +func dataSourceAccountAuditsList() *schema.Resource { + return &schema.Resource{ + SchemaVersion: 1, + + Read: dataSourceAccountAuditsListRead, + + Timeouts: &schema.ResourceTimeout{ + Read: &Timeout30s, + Default: &Timeout60s, + }, + + Schema: dataSourceAccountAuditsListSchemaMake(), + } +} diff --git a/decort/models_api.go b/decort/models_api.go index 9b100f6..d783626 100644 --- a/decort/models_api.go +++ b/decort/models_api.go @@ -993,7 +993,9 @@ type SepPool map[string]interface{} /////////////////////// const accountAddUserAPI = "/cloudapi/account/addUser" -const accountAuditsAPI = "/cloudapi/account/audits" + +//const accountAuditsAPI = "/restmachine/cloudapi/account/audits" +const accountAuditsAPI = "/restmachine/cloudbroker/account/audits" const accountCreateAPI = "/cloudapi/account/create" const accountDeleteAPI = "/cloudapi/account/delete" const accountDeleteAccountsAPI = "/cloudapi/account/deleteAccounts" @@ -1134,3 +1136,13 @@ type AccountVin struct { } type AccountVinsList []AccountVin + +type AccountAudit struct { + Call string `json:"call"` + ResponseTime float64 `json:"responsetime"` + StatusCode int `json:"statuscode"` + Timestamp float64 `json:"timestamp"` + User string `json:"user"` +} + +type AccountAuditsList []AccountAudit diff --git a/decort/provider.go b/decort/provider.go index 4f2f4ea..b2f9e45 100644 --- a/decort/provider.go +++ b/decort/provider.go @@ -143,6 +143,7 @@ func Provider() *schema.Provider { "decort_account_computes_list": dataSourceAccountComputesList(), "decort_account_disks_list": dataSourceAccountDisksList(), "decort_account_vins_list": dataSourceAccountVinsList(), + "decort_account_audits_list": dataSourceAccountAuditsList(), // "decort_pfw": dataSourcePfw(), }, diff --git a/decort/utility_account_audits_list.go b/decort/utility_account_audits_list.go new file mode 100644 index 0000000..283c513 --- /dev/null +++ b/decort/utility_account_audits_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 utilityAccountAuditsListCheckPresence(d *schema.ResourceData, m interface{}) (AccountAuditsList, error) { + accountAuditsList := AccountAuditsList{} + controller := m.(*ControllerCfg) + urlValues := &url.Values{} + + urlValues.Add("accountId", strconv.Itoa(d.Get("account_id").(int))) + + log.Debugf("utilityAccountAuditsListCheckPresence: load account list") + accountAuditsListRaw, err := controller.decortAPICall("POST", accountAuditsAPI, urlValues) + if err != nil { + return nil, err + } + + err = json.Unmarshal([]byte(accountAuditsListRaw), &accountAuditsList) + if err != nil { + return nil, err + } + + return accountAuditsList, nil +} diff --git a/samples/README.md b/samples/README.md index c4ff93c..7a61d08 100644 --- a/samples/README.md +++ b/samples/README.md @@ -21,6 +21,10 @@ - disk_list - rg_list - account_list + - account_computes_list + - account_disks_list + - account_vins_list + - account_audits_list - resources: - image - virtual_image diff --git a/samples/data_account_audits_list/main.tf b/samples/data_account_audits_list/main.tf new file mode 100644 index 0000000..a30b152 --- /dev/null +++ b/samples/data_account_audits_list/main.tf @@ -0,0 +1,40 @@ +/* +Пример использования +Получение информации об использовании аккаунта + +*/ +#Расскомментируйте этот код, +#и внесите необходимые правки в версию и путь, +#чтобы работать с установленным вручную (не через 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_audits_list" "aal" { + #id аккаунта + #обязательный параметр + #тип - число + account_id = 28096 + +} + +output "test" { + value = data.decort_account_audits_list.aal +}