1.0.0
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/account"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/utilities"
|
||||
)
|
||||
|
||||
// AccountDataSource flattens data source for account.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func AccountDataSource(ctx context.Context, state *models.DataSourceAccountModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.CbAccountDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
ID := uint64(state.AccountID.ValueInt64())
|
||||
|
||||
recordAccount, err := utilities.AccountDataSourceCheckPresence(ctx, ID, c)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about account with ID %v", ID), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountDataSource: before flatten", map[string]any{"account_id": ID, "recordAccount": recordAccount})
|
||||
|
||||
*state = models.DataSourceAccountModel{
|
||||
AccountID: state.AccountID,
|
||||
Timeouts: state.Timeouts,
|
||||
DCLocation: types.StringValue(recordAccount.DCLocation),
|
||||
CKey: types.StringValue(recordAccount.CKey),
|
||||
ACL: flattenACLInAccount(ctx, recordAccount.ACL),
|
||||
Company: types.StringValue(recordAccount.Company),
|
||||
CompanyURL: types.StringValue(recordAccount.CompanyURL),
|
||||
CPUAllocationParameter: types.StringValue(recordAccount.CPUAllocationParameter),
|
||||
CPUAllocationRatio: types.Float64Value(recordAccount.CPUAllocationRatio),
|
||||
CreatedBy: types.StringValue(recordAccount.CreatedBy),
|
||||
CreatedTime: types.Int64Value(int64(recordAccount.CreatedTime)),
|
||||
DeactivationTime: types.Float64Value(recordAccount.DeactivationTime),
|
||||
DeletedBy: types.StringValue(recordAccount.DeletedBy),
|
||||
DeletedTime: types.Int64Value(int64(recordAccount.DeletedTime)),
|
||||
DisplayName: types.StringValue(recordAccount.DisplayName),
|
||||
GUID: types.Int64Value(int64(recordAccount.GUID)),
|
||||
Name: types.StringValue(recordAccount.Name),
|
||||
ResourceLimits: flattenResourceLimits(ctx, recordAccount.ResourceLimits),
|
||||
ResTypes: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordAccount.ResTypes),
|
||||
SendAccessEmails: types.BoolValue(recordAccount.SendAccessEmails),
|
||||
Status: types.StringValue(recordAccount.Status),
|
||||
UniqPools: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordAccount.UniqPools),
|
||||
UpdatedTime: types.Int64Value(int64(recordAccount.UpdatedTime)),
|
||||
Version: types.Int64Value(int64(recordAccount.Version)),
|
||||
VINS: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordAccount.VINS),
|
||||
}
|
||||
|
||||
state.ComputeFeatures, diags = types.ListValueFrom(ctx, types.StringType, recordAccount.ComputeFeatures)
|
||||
if diags.HasError() {
|
||||
tflog.Error(ctx, fmt.Sprint("flattens.AccountListDeletedDataSource: cannot flatten recordAccount.ComputeFeatures to state.ComputeFeatures", diags))
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountDataSource: after flatten", map[string]any{"account_id": state.AccountID.ValueInt64()})
|
||||
|
||||
tflog.Info(ctx, "End flattens.AccountDataSource", map[string]any{"account_id": state.AccountID.ValueInt64()})
|
||||
return nil
|
||||
}
|
||||
|
||||
func flattenResourceLimits(ctx context.Context, limits account.ResourceLimits) *models.ResourceLimitsModel {
|
||||
tflog.Info(ctx, "Start flattenResourceLimits")
|
||||
res := models.ResourceLimitsModel{
|
||||
CuC: types.Float64Value(limits.CuC),
|
||||
CuD: types.Float64Value(limits.CuD),
|
||||
CuI: types.Float64Value(limits.CuI),
|
||||
CuM: types.Float64Value(limits.CuM),
|
||||
CuDM: types.Float64Value(limits.CuDM),
|
||||
CuNP: types.Float64Value(limits.CuNP),
|
||||
GPUUnits: types.Float64Value(limits.GPUUnits),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattenResourceLimits")
|
||||
return &res
|
||||
}
|
||||
|
||||
func flattenACLInAccount(ctx context.Context, aclList []account.ACL) []models.ACLModel {
|
||||
tflog.Info(ctx, "Start flattenACLInAccount")
|
||||
res := make([]models.ACLModel, 0, len(aclList))
|
||||
for _, item := range aclList {
|
||||
temp := models.ACLModel{
|
||||
Explicit: types.BoolValue(item.Explicit),
|
||||
GUID: types.StringValue(item.GUID),
|
||||
Right: types.StringValue(item.Right),
|
||||
Status: types.StringValue(item.Status),
|
||||
Type: types.StringValue(item.Type),
|
||||
UserGroupID: types.StringValue(item.UserGroupID),
|
||||
}
|
||||
res = append(res, temp)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/account"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/utilities"
|
||||
)
|
||||
|
||||
// AccountListDataSource flattens data source for account.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func AccountListDataSource(ctx context.Context, state *models.DataSourceAccountListModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.AccountListDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
accList, err := utilities.AccountListDataSourceCheckPresence(ctx, state, c)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot get info about account list", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountListDataSource: before flatten")
|
||||
|
||||
id := uuid.New()
|
||||
*state = models.DataSourceAccountListModel{
|
||||
ByID: state.ByID,
|
||||
Name: state.Name,
|
||||
ACL: state.ACL,
|
||||
Page: state.Page,
|
||||
Size: state.Size,
|
||||
Timeouts: state.Timeouts,
|
||||
SortBy: state.SortBy,
|
||||
|
||||
Id: types.StringValue(id.String()),
|
||||
EntryCount: types.Int64Value(int64(accList.EntryCount)),
|
||||
}
|
||||
|
||||
items := make([]models.AccountListElementModel, 0, len(accList.Data))
|
||||
for _, item := range accList.Data {
|
||||
i := models.AccountListElementModel{
|
||||
DCLocation: types.StringValue(item.DCLocation),
|
||||
CKey: types.StringValue(item.CKey),
|
||||
ACL: flattenACLInAccountList(ctx, item.ACL),
|
||||
Company: types.StringValue(item.Company),
|
||||
CompanyURL: types.StringValue(item.CompanyURL),
|
||||
CPUAllocationParameter: types.StringValue(item.CPUAllocationParameter),
|
||||
CPUAllocationRatio: types.Float64Value(item.CPUAllocationRatio),
|
||||
CreatedBy: types.StringValue(item.CreatedBy),
|
||||
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
|
||||
DeactivationTime: types.Float64Value(item.DeactivationTime),
|
||||
DeletedBy: types.StringValue(item.DeletedBy),
|
||||
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
|
||||
DisplayName: types.StringValue(item.DisplayName),
|
||||
GUID: types.Int64Value(int64(item.GUID)),
|
||||
Name: types.StringValue(item.Name),
|
||||
ResourceLimits: flattenAccountListResourceLimits(ctx, item.ResourceLimits),
|
||||
ResTypes: flattenAccListSimpleType(ctx, types.StringType, item.ResTypes),
|
||||
SendAccessEmails: types.BoolValue(item.SendAccessEmails),
|
||||
Status: types.StringValue(item.Status),
|
||||
UniqPools: flattenAccListSimpleType(ctx, types.StringType, item.UniqPools),
|
||||
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
|
||||
Version: types.Int64Value(int64(item.Version)),
|
||||
VINS: flattenAccListSimpleType(ctx, types.Int64Type, item.VINS),
|
||||
}
|
||||
i.ComputeFeatures, diags = types.ListValueFrom(ctx, types.StringType, item.ComputeFeatures)
|
||||
if diags.HasError() {
|
||||
tflog.Error(ctx, fmt.Sprint("flattens.AccountListDataSource: cannot flatten item.ComputeFeatures to i.ComputeFeatures", diags))
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
state.Items = items
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountListDataSource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.AccountListDataSource")
|
||||
return nil
|
||||
}
|
||||
|
||||
func flattenAccountListResourceLimits(ctx context.Context, limits account.ResourceLimits) *models.AccountListResourceLimitsModel {
|
||||
tflog.Info(ctx, "Start flattenAccountListResourceLimits")
|
||||
res := models.AccountListResourceLimitsModel{
|
||||
CuC: types.Float64Value(limits.CuC),
|
||||
CuD: types.Float64Value(limits.CuD),
|
||||
CuI: types.Float64Value(limits.CuI),
|
||||
CuM: types.Float64Value(limits.CuM),
|
||||
CuDM: types.Float64Value(limits.CuDM),
|
||||
CuNP: types.Float64Value(limits.CuNP),
|
||||
GPUUnits: types.Float64Value(limits.GPUUnits),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattenAccountListResourceLimits")
|
||||
return &res
|
||||
}
|
||||
|
||||
func flattenACLInAccountList(ctx context.Context, aclList []account.ACL) []models.AccountListACLModel {
|
||||
tflog.Info(ctx, "Start flattenACLInAccountList")
|
||||
res := make([]models.AccountListACLModel, 0, len(aclList))
|
||||
for _, item := range aclList {
|
||||
temp := models.AccountListACLModel{
|
||||
Explicit: types.BoolValue(item.Explicit),
|
||||
GUID: types.StringValue(item.GUID),
|
||||
Right: types.StringValue(item.Right),
|
||||
Status: types.StringValue(item.Status),
|
||||
Type: types.StringValue(item.Type),
|
||||
UserGroupID: types.StringValue(item.UserGroupID),
|
||||
}
|
||||
res = append(res, temp)
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func flattenAccListSimpleType(ctx context.Context, elementType attr.Type, elements any) types.List {
|
||||
res, diags := types.ListValueFrom(ctx, elementType, elements)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenAccListSimpleType", diags))
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/account/utilities"
|
||||
)
|
||||
|
||||
// AccountVinsListDataSource flattens data source for account list.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func AccountVinsListDataSource(ctx context.Context, state *models.DataSourceAccountVinsListModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.AccountVinsListDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
accountVinsList, err := utilities.AccountVinsListCheck(ctx, state, c)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot get info about account list", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountVinsListDataSource: before flatten")
|
||||
|
||||
*state = models.DataSourceAccountVinsListModel{
|
||||
AccountID: state.AccountID,
|
||||
VinsID: state.VinsID,
|
||||
Name: state.Name,
|
||||
RGID: state.RGID,
|
||||
ExtIp: state.ExtIp,
|
||||
SortBy: state.SortBy,
|
||||
Page: state.Page,
|
||||
Size: state.Size,
|
||||
Timeouts: state.Timeouts,
|
||||
|
||||
EntryCount: types.Int64Value(int64(accountVinsList.EntryCount)),
|
||||
}
|
||||
items := make([]models.ItemVINSModel, 0, len(accountVinsList.Data))
|
||||
for _, item := range accountVinsList.Data {
|
||||
i := models.ItemVINSModel{
|
||||
AccountID: types.Int64Value(int64(item.ID)),
|
||||
AccountName: types.StringValue(item.Name),
|
||||
Computes: types.Int64Value(int64(item.Computes)),
|
||||
CreatedBy: types.StringValue(item.CreatedBy),
|
||||
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
|
||||
DeletedBy: types.StringValue(item.DeletedBy),
|
||||
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
|
||||
ExternalIP: types.StringValue(item.ExternalIP),
|
||||
ExtnetId: types.Int64Value(int64(item.ExtnetId)),
|
||||
FreeIPs: types.Int64Value(int64(item.FreeIPs)),
|
||||
ID: types.Int64Value(int64(item.ID)),
|
||||
Name: types.StringValue(item.Name),
|
||||
Network: types.StringValue(item.Network),
|
||||
PriVNFDevID: types.Int64Value(int64(item.PriVNFDevID)),
|
||||
RGID: types.Int64Value(int64(item.RGID)),
|
||||
RGName: types.StringValue(item.RGName),
|
||||
Status: types.StringValue(item.Status),
|
||||
UpdatedBy: types.StringValue(item.UpdatedBy),
|
||||
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
|
||||
}
|
||||
|
||||
items = append(items, i)
|
||||
}
|
||||
|
||||
state.Data = items
|
||||
|
||||
tflog.Info(ctx, "flattens.AccountVinsListDataSource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.AccountVinsListDataSource")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user