This commit is contained in:
asteam
2024-07-25 14:33:38 +03:00
commit 6f40af6a5f
946 changed files with 98335 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
package flattens
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskDataSource flattens data source for disk.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskDataSource(ctx context.Context, state *models.DataSourceDiskModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskDataSource")
diags := diag.Diagnostics{}
diskId := uint64(state.DiskID.ValueInt64())
recordDisk, err := utilities.DataSourceDiskCheckPresence(ctx, diskId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about disk with ID %v", diskId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.DiskDataSource: before flatten", map[string]any{"disk_id": diskId, "recordDisk": recordDisk})
id := uuid.New()
diskAcl, _ := json.Marshal(recordDisk.ACL)
*state = models.DataSourceDiskModel{
DiskID: state.DiskID,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
ACL: types.StringValue(string(diskAcl)),
Computes: flattenComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
GID: types.Int64Value(int64(recordDisk.GID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
Name: types.StringValue(recordDisk.Name),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepID: types.Int64Value(int64(recordDisk.SepID)),
SepType: types.StringValue(recordDisk.SepType),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
state.Images, diags = types.ListValueFrom(ctx, types.StringType, recordDisk.Images)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskDataSource: cannot flatten recordDisk.Images to state.Images", diags))
}
state.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, recordDisk.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskDataSource: cannot flatten recordDisk.PresentTo to state.PresentTo", diags))
}
iotune := models.IOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.DiskDataSource iotune struct to obj", diags))
}
state.IOTune = obj
tflog.Info(ctx, "flattens.DiskDataSource: end flatten", map[string]any{"disk_id": state.DiskID.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,128 @@
package flattens
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskListDataSource flattens data source for disk list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskListDataSource(ctx context.Context, state *models.DataSourceDiskListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskListDataSource")
diags := diag.Diagnostics{}
diskList, diags := utilities.DataSourceDiskListCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskListDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDiskListModel{
ByID: state.ByID,
Name: state.Name,
AccountName: state.AccountName,
DiskMaxSize: state.DiskMaxSize,
Status: state.Status,
Shared: state.Shared,
AccountID: state.AccountID,
Type: state.Type,
SEPID: state.SEPID,
PoolName: state.PoolName,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(diskList.EntryCount)),
}
items := make([]models.ItemDiskModel, 0, diskList.EntryCount)
for _, recordDisk := range diskList.Data {
diskAcl, _ := json.Marshal(recordDisk.ACL)
d := models.ItemDiskModel{
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
ACL: types.StringValue(string(diskAcl)),
Computes: flattenComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
GID: types.Int64Value(int64(recordDisk.GID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
DiskId: types.Int64Value(int64(recordDisk.ID)),
DiskName: types.StringValue(recordDisk.Name),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepID: types.Int64Value(int64(recordDisk.SepID)),
SepType: types.StringValue(recordDisk.SepType),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
d.Images, diags = types.ListValueFrom(ctx, types.StringType, recordDisk.Images)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListDataSource: cannot flatten recordDisk.Images to d.Images", diags))
}
d.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, recordDisk.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListDataSource: cannot flatten recordDisk.PresentTo to d.PresentTo", diags))
}
iotune := models.IOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.DiskListDataSource iotune struct to obj", diags))
}
d.IOTune = obj
items = append(items, d)
}
state.Items = items
tflog.Info(ctx, "flattens.DiskListDataSource: end flatten")
return nil
}

View File

@@ -0,0 +1,125 @@
package flattens
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskListDeletedDataSource flattens data source for disk list deleted.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskListDeletedDataSource(ctx context.Context, state *models.DataSourceDiskListDeletedModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskListDeletedDataSource")
diags := diag.Diagnostics{}
diskList, diags := utilities.DataSourceDiskListDeletedCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskListDeletedDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDiskListDeletedModel{
ByID: state.ByID,
Name: state.Name,
AccountName: state.AccountName,
DiskMaxSize: state.DiskMaxSize,
Shared: state.Shared,
AccountID: state.AccountID,
Type: state.Type,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(diskList.EntryCount)),
}
items := make([]models.ItemDiskModel, 0, diskList.EntryCount)
for _, recordDisk := range diskList.Data {
diskAcl, _ := json.Marshal(recordDisk.ACL)
d := models.ItemDiskModel{
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
ACL: types.StringValue(string(diskAcl)),
Computes: flattenComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
GID: types.Int64Value(int64(recordDisk.GID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
DiskId: types.Int64Value(int64(recordDisk.ID)),
DiskName: types.StringValue(recordDisk.Name),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepID: types.Int64Value(int64(recordDisk.SepID)),
SepType: types.StringValue(recordDisk.SepType),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
d.Images, diags = types.ListValueFrom(ctx, types.StringType, recordDisk.Images)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListDeletedDataSource: cannot flatten recordDisk.Images to d.Images", diags))
}
d.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, recordDisk.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListDeletedDataSource: cannot flatten recordDisk.PresentTo to d.PresentTo", diags))
}
iotune := models.IOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.DiskListDeletedDataSource iotune struct to obj", diags))
}
d.IOTune = obj
items = append(items, d)
}
state.Items = items
tflog.Info(ctx, "flattens.DiskListDeletedDataSource: end flatten")
return nil
}

View File

@@ -0,0 +1,50 @@
package flattens
import (
"context"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskListTypesDataSource flattens data source for disk list types.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskListTypesDataSource(ctx context.Context, state *models.DataSourceDiskListTypesModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskListTypesDataSource")
diags := diag.Diagnostics{}
listTypes, diags := utilities.DataSourceDiskListTypesCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskListTypesDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDiskListTypesModel{
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(listTypes.EntryCount)),
}
state.Types, diags = types.ListValueFrom(ctx, types.StringType, listTypes.Data)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListTypesDataSource: cannot flatten listTypes.Data to state.Types", diags))
}
tflog.Info(ctx, "flattens.DiskListTypesDataSource: end flatten")
return nil
}

View File

@@ -0,0 +1,82 @@
package flattens
import (
"context"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskListTypesDetailedDataSource flattens data source for disk list types detailed.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskListTypesDetailedDataSource(ctx context.Context, state *models.DataSourceDiskListTypesDetailedModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskListTypesDetailedDataSource")
diags := diag.Diagnostics{}
listTypes, diags := utilities.DataSourceDiskListTypesDetailedCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskListTypesDetailedDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDiskListTypesDetailedModel{
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(listTypes.EntryCount)),
}
items := make([]models.ItemDiskTypeDetailedModel, 0, len(listTypes.Data))
for _, typeListDetailed := range listTypes.Data {
typeMap := typeListDetailed.(map[string]interface{})
t := models.ItemDiskTypeDetailedModel{
SepID: types.Int64Value(int64(typeMap["sepId"].(float64))),
SepName: types.StringValue(typeMap["sepName"].(string)),
}
var pools []models.ItemPoolModel
poolsTemp := typeMap["pools"].([]interface{})
for _, pool := range poolsTemp {
poolsMap := pool.(map[string]interface{})
p := models.ItemPoolModel{
Name: types.StringValue(poolsMap["name"].(string)),
System: types.StringValue(poolsMap["system"].(string)),
}
p.Types, diags = types.ListValueFrom(ctx, types.StringType, flattenTypes(poolsMap["types"].([]interface{})))
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListTypesDetailedDataSource: cannot flatten poolsMap[\"types\"] to p.Types", diags))
}
pools = append(pools, p)
}
t.Pools = pools
items = append(items, t)
}
state.Items = items
tflog.Info(ctx, "flattens.DiskListTypesDetailedDataSource: end flatten")
return nil
}
func flattenTypes(typesInterface []interface{}) []string {
var typesList []string
for _, typ := range typesInterface {
typesList = append(typesList, typ.(string))
}
return typesList
}

View File

@@ -0,0 +1,132 @@
package flattens
import (
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskListUnattachedDataSource flattens data source for disk list unattached.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskListUnattachedDataSource(ctx context.Context, state *models.DataSourceDiskListUnattachedModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskListUnattachedDataSource")
diags := diag.Diagnostics{}
diskList, diags := utilities.DataSourceDiskListUnattachedCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskListUnattachedDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceDiskListUnattachedModel{
ByID: state.ByID,
AccountName: state.AccountName,
DiskMaxSize: state.DiskMaxSize,
Status: state.Status,
AccountID: state.AccountID,
SepID: state.SepID,
PoolName: state.PoolName,
Type: state.Type,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(diskList.EntryCount)),
}
items := make([]models.ItemDiskUnattachedModel, 0, diskList.EntryCount)
for _, recordDisk := range diskList.Data {
diskAcl, _ := json.Marshal(recordDisk.ACL)
d := models.ItemDiskUnattachedModel{
CKey: types.StringValue(recordDisk.CKey),
Meta: flattens.Meta(ctx, recordDisk.Meta),
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
ACL: types.StringValue(string(diskAcl)),
BootPartition: types.Int64Value(int64(recordDisk.BootPartition)),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
DiskPath: types.StringValue(recordDisk.DiskPath),
GID: types.Int64Value(int64(recordDisk.GID)),
GUID: types.Int64Value(int64(recordDisk.GUID)),
DiskId: types.Int64Value(int64(recordDisk.ID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
Iqn: types.StringValue(recordDisk.IQN),
Login: types.StringValue(recordDisk.Login),
Milestones: types.Int64Value(int64(recordDisk.Milestones)),
DiskName: types.StringValue(recordDisk.Name),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
Passwd: types.StringValue(recordDisk.Password),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PurgeAttempts: types.Int64Value(int64(recordDisk.PurgeAttempts)),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
RealityDeviceNumber: types.Int64Value(int64(recordDisk.RealityDeviceNumber)),
ReferenceID: types.StringValue(recordDisk.ReferenceID),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepID: types.Int64Value(int64(recordDisk.SEPID)),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
d.Images, diags = types.ListValueFrom(ctx, types.StringType, recordDisk.Images)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskListUnattachedDataSource: cannot flatten recordDisk.Images to d.Images", diags))
}
iotune := models.IOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.DiskListUnattachedDataSource iotune struct to obj", diags))
}
d.IOTune = obj
items = append(items, d)
}
state.Items = items
tflog.Info(ctx, "flattens.DiskListUnattachedDataSource: end flatten")
return nil
}

View File

@@ -0,0 +1,130 @@
package flattens
import (
"context"
"encoding/json"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskReplicationDataSource flattens data source for disk.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskReplicationDataSource(ctx context.Context, state *models.RecordDiskModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskReplicationDataSource")
diags := diag.Diagnostics{}
recordDisk, status, err := utilities.DataSourceDiskReplicationCheckPresence(ctx, state, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about disk"), err.Error())
return diags
}
tflog.Info(ctx, "flattens.DiskReplicationDataSource: before flatten")
diskAcl, _ := json.Marshal(recordDisk.ACL)
*state = models.RecordDiskModel{
DiskId: state.DiskId,
ID: state.ID,
Timeouts: state.Timeouts,
ACL: types.StringValue(string(diskAcl)),
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
Computes: flattenDRComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
GID: types.Int64Value(int64(recordDisk.GID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
Images: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordDisk.Images),
Name: types.StringValue(recordDisk.Name),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PresentTo: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordDisk.PresentTo),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
Replication: &models.ItemReplicationModel{},
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepType: types.StringValue(recordDisk.SepType),
SepID: types.Int64Value(int64(recordDisk.SepID)),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
StatusReplication: types.StringValue(*status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
iotune := models.DiskReplicationIOTune{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
state.IOTune = iotune
itemReplication := models.ItemReplicationModel{
DiskID: types.Int64Value(int64(recordDisk.Replication.DiskID)),
PoolID: types.StringValue(recordDisk.Replication.PoolID),
Role: types.StringValue(recordDisk.Replication.Role),
SelfVolumeID: types.StringValue(recordDisk.Replication.SelfVolumeID),
StorageID: types.StringValue(recordDisk.Replication.StorageID),
VolumeID: types.StringValue(recordDisk.Replication.VolumeID),
}
state.Replication = &itemReplication
tflog.Info(ctx, "flattens.ReplicationDiskDataSource: end flatten")
return nil
}
func flattenDRComputes(ctx context.Context, items map[string]string) types.List {
tflog.Info(ctx, "Start flattenDRComputes")
tempSlice := make([]types.Object, 0, len(items))
for id, name := range items {
temp := models.ItemComputeModel{
ComputeId: types.StringValue(id),
ComputeName: types.StringValue(name),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemCompute, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDRComputes struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemCompute}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDRComputes", diags))
}
tflog.Info(ctx, "End flattenDRComputes")
return res
}

View File

@@ -0,0 +1,50 @@
package flattens
import (
"context"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskSnapshotDataSource flattens data source for disk snapshot.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskSnapshotDataSource(ctx context.Context, state *models.DataSourceDiskSnapshotModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskSnapshotDataSource")
diskId := uint64(state.DiskID.ValueInt64())
item, diags := utilities.DataSourceDiskSnapshotCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskSnapshotDataSource: before flatten", map[string]any{"disk_id": diskId, "snapshot": item})
id := uuid.New()
*state = models.DataSourceDiskSnapshotModel{
DiskID: state.DiskID,
Label: state.Label,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
GUID: types.StringValue(item.GUID),
ResID: types.StringValue(item.ResID),
SnapSetGUID: types.StringValue(item.SnapSetGUID),
SnapSetTime: types.Int64Value(int64(item.SnapSetTime)),
TimeStamp: types.Int64Value(int64(item.TimeStamp)),
}
tflog.Info(ctx, "flattens.DiskSnapshotDataSource: end flatten", map[string]any{
"disk_id": state.DiskID.ValueInt64(),
"label": state.Label.ValueString(),
})
return nil
}

View File

@@ -0,0 +1,46 @@
package flattens
import (
"context"
"fmt"
"github.com/google/uuid"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskSnapshotListDataSource flattens data source for disk snapshot list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func DiskSnapshotListDataSource(ctx context.Context, state *models.DataSourceDiskSnapshotListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskSnapshotListDataSource")
diags := diag.Diagnostics{}
diskId := uint64(state.DiskID.ValueInt64())
snapshots, err := utilities.DiskSnapshotListCheckPresence(ctx, diskId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about disk snapshot list with disk ID %v", diskId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.DiskSnapshotListDataSource: before flatten", map[string]any{"disk_id": diskId, "snapshots": snapshots})
id := uuid.New()
*state = models.DataSourceDiskSnapshotListModel{
DiskID: state.DiskID,
Timeouts: state.Timeouts,
// computed fields
Id: types.StringValue(id.String()),
Items: flattenSnapshots(ctx, *snapshots),
}
tflog.Info(ctx, "flattens.DiskSnapshotListDataSource: end flatten", map[string]any{"disk_id": state.DiskID.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,189 @@
package flattens
import (
"context"
"encoding/json"
"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/cloudapi/disks"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskResource flattens resource for disk.
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func DiskResource(ctx context.Context, plan *models.ResourceDiskModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskResource")
diags := diag.Diagnostics{}
diskId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
if err != nil {
diags.AddError("flattens.DiskResource: Cannot parse disk ID from state", err.Error())
return diags
}
recordDisk, err := utilities.DiskCheckPresence(ctx, diskId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about disk with ID %v", diskId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.DiskResource: before flatten", map[string]any{"disk_id": diskId, "recordDisk": recordDisk})
diskAcl, _ := json.Marshal(recordDisk.ACL)
*plan = models.ResourceDiskModel{
// required fields
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
DiskName: types.StringValue(recordDisk.Name),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
GID: types.Int64Value(int64(recordDisk.GID)),
// optional fields
Description: plan.Description,
Pool: plan.Pool,
SEPID: plan.SEPID,
Type: plan.Type,
Detach: plan.Detach,
Permanently: plan.Permanently,
Reason: plan.Reason,
Shareable: plan.Shareable,
Timeouts: plan.Timeouts,
// computed fields
LastUpdated: plan.LastUpdated,
Id: types.StringValue(strconv.Itoa(int(recordDisk.ID))),
DiskId: types.Int64Value(int64(recordDisk.ID)),
AccountName: types.StringValue(recordDisk.AccountName),
ACL: types.StringValue(string(diskAcl)),
Computes: flattenComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepType: types.StringValue(recordDisk.SepType),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
TechStatus: types.StringValue(recordDisk.TechStatus),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
plan.Images, diags = types.ListValueFrom(ctx, types.StringType, recordDisk.Images)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskResource: cannot flatten recordDisk.Images to plan.Images", diags))
}
plan.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, recordDisk.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("flattens.DiskResource: cannot flatten recordDisk.PresentTo to plan.PresentTo", diags))
}
if plan.Description.IsUnknown() {
plan.Description = types.StringValue(recordDisk.Description)
}
if plan.Pool.IsUnknown() {
plan.Pool = types.StringValue(recordDisk.Pool)
}
if plan.SEPID.IsUnknown() {
plan.SEPID = types.Int64Value(int64(recordDisk.SepID))
}
if plan.Shareable.IsUnknown() {
plan.Shareable = types.BoolValue(recordDisk.Shareable)
}
if plan.Type.IsUnknown() {
plan.Type = types.StringValue(recordDisk.Type)
}
iotune := models.IOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.DiskResource iotune struct to obj", diags))
}
plan.IOTune = obj
tflog.Info(ctx, "flattens.DiskResource: after flatten", map[string]any{"disk_id": plan.Id.ValueString()})
tflog.Info(ctx, "End flattens.DiskResource")
return nil
}
func flattenComputes(ctx context.Context, items map[string]string) types.List {
tflog.Info(ctx, "Start flattenComputes")
tempSlice := make([]types.Object, 0, len(items))
for id, name := range items {
temp := models.ItemComputeModel{
ComputeId: types.StringValue(id),
ComputeName: types.StringValue(name),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemCompute, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenComputes struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemCompute}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenComputes", diags))
}
tflog.Info(ctx, "End flattenComputes")
return res
}
func flattenSnapshots(ctx context.Context, snapshots disks.ListSnapshots) types.List {
tflog.Info(ctx, "Start flattenSnapshots")
tempSlice := make([]types.Object, 0, len(snapshots))
for _, item := range snapshots {
temp := models.ItemSnapshotModel{
GUID: types.StringValue(item.GUID),
Label: types.StringValue(item.Label),
ResID: types.StringValue(item.ResID),
SnapSetGUID: types.StringValue(item.SnapSetGUID),
SnapSetTime: types.Int64Value(int64(item.SnapSetTime)),
TimeStamp: types.Int64Value(int64(item.TimeStamp)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemSnapshot, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenSnapshots struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemSnapshot}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenSnapshots", diags))
}
tflog.Info(ctx, "End flattenSnapshots")
return res
}

View File

@@ -0,0 +1,146 @@
package flattens
import (
"context"
"encoding/json"
"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/terraform-provider-dynamix/internal/flattens"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// ReplicationDiskresource flattens resource for disk.
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func DiskReplicationResource(ctx context.Context, state *models.ResourceRecordDiskReplicationModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskReplicationresource")
diags := diag.Diagnostics{}
recordDisk, status, err := utilities.ResourceDiskReplicationCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Cannot get info about disk", err.Error())
return diags
}
tflog.Info(ctx, "flattens.DiskReplicationresource: before flatten")
diskAcl, _ := json.Marshal(recordDisk.ACL)
*state = models.ResourceRecordDiskReplicationModel{
DiskId: state.DiskId,
Name: state.Name,
SepID: state.SepID,
ReplicationId: state.ReplicationId,
Timeouts: state.Timeouts,
PoolName: state.PoolName,
Pause: state.Pause,
Reverse: state.Reverse,
Start: state.Start,
Detach: state.Detach,
Permanently: state.Permanently,
Reason: state.Reason,
Id: types.StringValue(strconv.Itoa(int(recordDisk.Replication.DiskID))),
ACL: types.StringValue(string(diskAcl)),
AccountID: types.Int64Value(int64(recordDisk.AccountID)),
AccountName: types.StringValue(recordDisk.AccountName),
Computes: flattenRComputes(ctx, recordDisk.Computes),
CreatedTime: types.Int64Value(int64(recordDisk.CreatedTime)),
DeletedTime: types.Int64Value(int64(recordDisk.DeletedTime)),
DeviceName: types.StringValue(recordDisk.DeviceName),
Description: types.StringValue(recordDisk.Description),
DestructionTime: types.Int64Value(int64(recordDisk.DestructionTime)),
GID: types.Int64Value(int64(recordDisk.GID)),
ImageID: types.Int64Value(int64(recordDisk.ImageID)),
Images: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordDisk.Images),
Order: types.Int64Value(int64(recordDisk.Order)),
Params: types.StringValue(recordDisk.Params),
ParentID: types.Int64Value(int64(recordDisk.ParentID)),
PCISlot: types.Int64Value(int64(recordDisk.PCISlot)),
Pool: types.StringValue(recordDisk.Pool),
PresentTo: flattens.FlattenSimpleTypeToList(ctx, types.StringType, recordDisk.PresentTo),
PurgeTime: types.Int64Value(int64(recordDisk.PurgeTime)),
ResID: types.StringValue(recordDisk.ResID),
ResName: types.StringValue(recordDisk.ResName),
Role: types.StringValue(recordDisk.Role),
SepType: types.StringValue(recordDisk.SepType),
Shareable: types.BoolValue(recordDisk.Shareable),
SizeMax: types.Int64Value(int64(recordDisk.SizeMax)),
SizeUsed: types.Float64Value(recordDisk.SizeUsed),
Snapshots: flattenSnapshots(ctx, recordDisk.Snapshots),
Status: types.StringValue(recordDisk.Status),
StatusReplication: types.StringValue(*status),
TechStatus: types.StringValue(recordDisk.TechStatus),
Type: types.StringValue(recordDisk.Type),
VMID: types.Int64Value(int64(recordDisk.VMID)),
}
iotune := models.ResourceDiskReplicationIOTuneModel{
ReadBytesSec: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSec)),
ReadBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadBytesSecMax)),
ReadIOPSSec: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSec)),
ReadIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.ReadIOPSSecMax)),
SizeIOPSSec: types.Int64Value(int64(recordDisk.IOTune.SizeIOPSSec)),
TotalBytesSec: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSec)),
TotalBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalBytesSecMax)),
TotalIOPSSec: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSec)),
TotalIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.TotalIOPSSecMax)),
WriteBytesSec: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSec)),
WriteBytesSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteBytesSecMax)),
WriteIOPSSec: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSec)),
WriteIOPSSecMax: types.Int64Value(int64(recordDisk.IOTune.WriteIOPSSecMax)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ResourceDiskReplicationIOTune, iotune)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.ReplicationDiskresource iotune struct to obj", diags))
}
state.IOTune = obj
itemReplication := models.ResourceItemReplicationModel{
DiskID: types.Int64Value(int64(recordDisk.Replication.DiskID)),
PoolID: types.StringValue(recordDisk.Replication.PoolID),
Role: types.StringValue(recordDisk.Replication.Role),
SelfVolumeID: types.StringValue(recordDisk.Replication.SelfVolumeID),
StorageID: types.StringValue(recordDisk.Replication.StorageID),
VolumeID: types.StringValue(recordDisk.Replication.VolumeID),
}
obj, diags = types.ObjectValueFrom(ctx, models.ResourceItemReplication, itemReplication)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattens.ReplicationDiskresource Replication struct to obj", diags))
}
state.Replication = obj
tflog.Info(ctx, "flattens.ReplicationDiskresource: end flatten")
return nil
}
func flattenRComputes(ctx context.Context, items map[string]string) types.List {
tflog.Info(ctx, "Start flattenRComputes")
tempSlice := make([]types.Object, 0, len(items))
for id, name := range items {
temp := models.ItemComputeModel{
ComputeId: types.StringValue(id),
ComputeName: types.StringValue(name),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemCompute, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenRComputes struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemCompute}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenRComputes", diags))
}
tflog.Info(ctx, "End flattenRComputes")
return res
}

View File

@@ -0,0 +1,65 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/cloudapi/disks/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/disks/utilities"
)
// DiskSnapshotResource flattens resource for disk snapshot.
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func DiskSnapshotResource(ctx context.Context, plan *models.ResourceDiskSnapshotModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.DiskSnapshotResource", map[string]any{
"disk_id": plan.DiskID.ValueInt64(),
"label": plan.Label.ValueString()})
recordSnapshot, diags := utilities.DiskSnapshotCheckPresence(ctx, plan, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.DiskSnapshotResource: before flatten", map[string]any{
"disk_id": plan.DiskID.ValueInt64(),
"label": plan.Label.ValueString(),
"recordDisk": recordSnapshot})
id := plan.Id
if !strings.Contains(id.ValueString(), "#") {
id = types.StringValue(fmt.Sprintf("%d#%s", plan.DiskID.ValueInt64(), plan.Label.ValueString()))
}
*plan = models.ResourceDiskSnapshotModel{
// required fields
DiskID: plan.DiskID,
Label: types.StringValue(recordSnapshot.Label),
// optional fields
Rollback: plan.Rollback,
TimeStamp: plan.TimeStamp,
Timeouts: plan.Timeouts,
// computed fields
Id: id,
GUID: types.StringValue(recordSnapshot.GUID),
ResID: types.StringValue(recordSnapshot.ResID),
SnapSetGUID: types.StringValue(recordSnapshot.SnapSetGUID),
SnapSetTime: types.Int64Value(int64(recordSnapshot.SnapSetTime)),
}
if plan.TimeStamp.IsUnknown() {
plan.TimeStamp = types.Int64Value(int64(recordSnapshot.TimeStamp))
}
tflog.Info(ctx, "flattens.DiskResource: after flatten", map[string]any{
"disk_id": plan.DiskID.ValueInt64(),
"label": plan.Label.ValueString()})
return nil
}