1.0.1
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
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/terraform-provider-dynamix/internal/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/utilities"
|
||||
)
|
||||
|
||||
// VFPoolDataSource flattens data source for vfpool.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func VFPoolDataSource(ctx context.Context, state *models.ItemVFPoolModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.VFPoolDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
vfPoolID := uint64(state.VFPoolID.ValueInt64())
|
||||
|
||||
record, err := utilities.VFPoolCheckPresence(ctx, vfPoolID, c)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about VFpool with ID %v", vfPoolID), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolDataSource: before flatten", map[string]any{"record": record})
|
||||
|
||||
*state = models.ItemVFPoolModel{
|
||||
//required fields
|
||||
VFPoolID: state.VFPoolID,
|
||||
|
||||
//optional fields
|
||||
Timeouts: state.Timeouts,
|
||||
|
||||
//compute fields
|
||||
AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, record.AccountAccess),
|
||||
|
||||
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
|
||||
Description: types.StringValue(record.Description),
|
||||
GID: types.Int64Value(int64(record.GID)),
|
||||
GUID: types.Int64Value(int64(record.GUID)),
|
||||
Name: types.StringValue(record.Name),
|
||||
RGAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, record.RGAccess),
|
||||
Status: types.StringValue(record.Status),
|
||||
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
|
||||
}
|
||||
|
||||
items := make([]models.VFSModel, 0, len(record.VFS))
|
||||
for _, vfsItem := range record.VFS {
|
||||
i := models.VFSModel{
|
||||
NodeID: types.Int64Value(int64(vfsItem.NodeID)),
|
||||
}
|
||||
|
||||
vfList := make([]models.VFItemModel, 0, len(vfsItem.VFList))
|
||||
for _, vfItem := range vfsItem.VFList {
|
||||
vfI := models.VFItemModel{
|
||||
NicName: types.StringValue(vfItem.NicName),
|
||||
}
|
||||
|
||||
vfInfoList := make([]models.VFSInfoItemModel, 0, len(vfItem.VFSInfo))
|
||||
for _, vfsInfoItem := range vfItem.VFSInfo {
|
||||
vfsInfoI := models.VFSInfoItemModel{
|
||||
ID: types.Int64Value(int64(vfsInfoItem.ID)),
|
||||
Claimed: types.BoolValue(vfsInfoItem.Claimed),
|
||||
VMID: types.Int64Value(int64(vfsInfoItem.VMID)),
|
||||
}
|
||||
|
||||
vfInfoList = append(vfInfoList, vfsInfoI)
|
||||
|
||||
}
|
||||
|
||||
vfI.VFSInfo = vfInfoList
|
||||
vfList = append(vfList, vfI)
|
||||
|
||||
}
|
||||
|
||||
i.VFList = vfList
|
||||
items = append(items, i)
|
||||
}
|
||||
state.VFS = items
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolDataSource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.VFPoolDataSource")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
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/flattens"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/utilities"
|
||||
)
|
||||
|
||||
// VFPoolListDataSource flattens data source for vfpool list.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func VFPoolListDataSource(ctx context.Context, state *models.ListVFPoolModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.VFPoolListDataSource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
record, err := utilities.VFPoolListCheckPresence(ctx, state, c)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot get info about fvpool list", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolListDataSource: before flatten", map[string]any{"record": record})
|
||||
|
||||
*state = models.ListVFPoolModel{
|
||||
//optional fields
|
||||
Timeouts: state.Timeouts,
|
||||
ByID: state.ByID,
|
||||
Name: state.Name,
|
||||
Description: state.Description,
|
||||
Status: state.Status,
|
||||
AccountAccess: state.AccountAccess,
|
||||
RgAccess: state.RgAccess,
|
||||
SortBy: state.SortBy,
|
||||
Page: state.Page,
|
||||
Size: state.Size,
|
||||
|
||||
//compute fields
|
||||
EntryCount: types.Int64Value(int64(record.EntryCount)),
|
||||
}
|
||||
|
||||
data := make([]models.ItemVFPoolListModel, 0, len(record.Data))
|
||||
for _, item := range record.Data {
|
||||
i := models.ItemVFPoolListModel{
|
||||
VFPoolID: types.Int64Value(int64(item.ID)),
|
||||
AccountAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.AccountAccess),
|
||||
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
|
||||
Description: types.StringValue(item.Description),
|
||||
GID: types.Int64Value(int64(item.GID)),
|
||||
GUID: types.Int64Value(int64(item.GUID)),
|
||||
Name: types.StringValue(item.Name),
|
||||
RGAccess: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, item.RGAccess),
|
||||
Status: types.StringValue(item.Status),
|
||||
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
|
||||
}
|
||||
items := make([]models.VFSListModel, 0, len(item.VFS))
|
||||
for _, item := range item.VFS {
|
||||
i := models.VFSListModel{
|
||||
NodeID: types.Int64Value(int64(item.NodeID)),
|
||||
}
|
||||
|
||||
vfList := make([]models.VFItemListModel, 0, len(item.VFList))
|
||||
for _, vfItem := range item.VFList {
|
||||
vfI := models.VFItemListModel{
|
||||
NicName: types.StringValue(vfItem.NicName),
|
||||
}
|
||||
|
||||
vfInfoList := make([]models.VFSInfoItemListModel, 0, len(vfItem.VFSInfo))
|
||||
for _, vfInfoItem := range vfItem.VFSInfo {
|
||||
vfInfoI := models.VFSInfoItemListModel{
|
||||
ID: types.Int64Value(int64(vfInfoItem.ID)),
|
||||
Claimed: types.BoolValue(vfInfoItem.Claimed),
|
||||
VMID: types.Int64Value(int64(vfInfoItem.VMID)),
|
||||
}
|
||||
|
||||
vfInfoList = append(vfInfoList, vfInfoI)
|
||||
|
||||
}
|
||||
|
||||
vfI.VFSInfo = vfInfoList
|
||||
vfList = append(vfList, vfI)
|
||||
|
||||
}
|
||||
|
||||
i.VFList = vfList
|
||||
items = append(items, i)
|
||||
}
|
||||
|
||||
i.VFS = items
|
||||
data = append(data, i)
|
||||
}
|
||||
state.Items = data
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolListDataSource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.VFPoolListDataSource")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package flattens
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"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/vfpool"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vfpool/utilities"
|
||||
)
|
||||
|
||||
// VFPoolResource flattens data source for vfpool.
|
||||
// Return error in case data source is not found on the platform.
|
||||
// Flatten errors are added to tflog.
|
||||
func VFPoolResource(ctx context.Context, state *models.ResourceItemVFPoolModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Start flattens.VFPoolResource")
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
vfPoolID := uint64(state.VFPoolID.ValueInt64())
|
||||
|
||||
if vfPoolID == 0 {
|
||||
id, err := strconv.Atoi(state.ID.ValueString())
|
||||
if err != nil {
|
||||
diags.AddError(
|
||||
"flattens.VFPoolResource: cannot parse resource ID from state",
|
||||
err.Error())
|
||||
return diags
|
||||
}
|
||||
vfPoolID = uint64(id)
|
||||
}
|
||||
|
||||
record, err := utilities.ResourceVFPoolCheckPresence(ctx, vfPoolID, c)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about VFpool with ID %v", vfPoolID), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolResource: before flatten", map[string]any{"record": record})
|
||||
|
||||
*state = models.ResourceItemVFPoolModel{
|
||||
//required fields
|
||||
Name: state.Name,
|
||||
|
||||
//optional fields
|
||||
Description: state.Description,
|
||||
AccountAccess: state.AccountAccess,
|
||||
RGAccess: state.RGAccess,
|
||||
Config: state.Config,
|
||||
Enable: state.Enable,
|
||||
Timeouts: state.Timeouts,
|
||||
|
||||
//compute fields
|
||||
VFPoolID: types.Int64Value(int64(record.ID)),
|
||||
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
|
||||
GID: types.Int64Value(int64(record.GID)),
|
||||
GUID: types.Int64Value(int64(record.GUID)),
|
||||
Status: types.StringValue(record.Status),
|
||||
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
|
||||
VFS: flattenVFS(ctx, record.VFS),
|
||||
ID: types.StringValue(strconv.Itoa(int(vfPoolID))),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "flattens.VFPoolResource: after flatten")
|
||||
|
||||
tflog.Info(ctx, "End flattens.VFPoolResource")
|
||||
return nil
|
||||
}
|
||||
|
||||
func flattenVFS(ctx context.Context, items []vfpool.VFS) types.List {
|
||||
tflog.Info(ctx, "Start flattenVFS")
|
||||
tempSlice := make([]types.Object, 0, len(items))
|
||||
for _, v := range items {
|
||||
temp := models.ResourceIVFSModel{
|
||||
NodeID: types.Int64Value(int64(v.NodeID)),
|
||||
VFList: flattenVFList(ctx, v.VFList),
|
||||
}
|
||||
obj, diags := types.ObjectValueFrom(ctx, models.ResourceIVFS, temp)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFS struct to obj", diags))
|
||||
}
|
||||
tempSlice = append(tempSlice, obj)
|
||||
}
|
||||
|
||||
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ResourceIVFS}, tempSlice)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFS", diags))
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattenVFS")
|
||||
return res
|
||||
}
|
||||
|
||||
func flattenVFList(ctx context.Context, items vfpool.VFList) types.List {
|
||||
tflog.Info(ctx, "Start flattenVFList")
|
||||
tempSlice := make([]types.Object, 0, len(items))
|
||||
for _, v := range items {
|
||||
temp := models.ResourceIVFItemModel{
|
||||
NicName: types.StringValue(v.NicName),
|
||||
VFSInfo: flattenVFSInfo(ctx, v.VFSInfo),
|
||||
}
|
||||
obj, diags := types.ObjectValueFrom(ctx, models.ResourceIVFItem, temp)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFList struct to obj", diags))
|
||||
}
|
||||
tempSlice = append(tempSlice, obj)
|
||||
}
|
||||
|
||||
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ResourceIVFItem}, tempSlice)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFList", diags))
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattenVFList")
|
||||
return res
|
||||
}
|
||||
|
||||
func flattenVFSInfo(ctx context.Context, items vfpool.VFSInfoList) types.List {
|
||||
tflog.Info(ctx, "Start flattenVFSInfo")
|
||||
tempSlice := make([]types.Object, 0, len(items))
|
||||
for _, v := range items {
|
||||
temp := models.ResourceIVFSInfoItemModel{
|
||||
ID: types.Int64Value(int64(v.ID)),
|
||||
Claimed: types.BoolValue(v.Claimed),
|
||||
VMID: types.Int64Value(int64(v.VMID)),
|
||||
}
|
||||
obj, diags := types.ObjectValueFrom(ctx, models.ResourceIVFSInfoItem, temp)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFSInfo struct to obj", diags))
|
||||
}
|
||||
tempSlice = append(tempSlice, obj)
|
||||
}
|
||||
|
||||
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ResourceIVFSInfoItem}, tempSlice)
|
||||
if diags != nil {
|
||||
tflog.Error(ctx, fmt.Sprint("Error flattenVFSInfo", diags))
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "End flattenVFSInfo")
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user