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,51 @@
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/cloudapi/stack/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/utilities"
)
// StackDataSource flattens data source for stack.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func StackDataSource(ctx context.Context, state *models.InfoStackModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.StackDataSource")
diags := diag.Diagnostics{}
id := uint64(state.StackID.ValueInt64())
record, err := utilities.StackCheckPresence(ctx, id, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about stack with ID %v", id), err.Error())
return diags
}
tflog.Info(ctx, "flattens.StackDataSource: before flatten", map[string]any{"stack_id": state.StackID.ValueInt64(), "record": record})
*state = models.InfoStackModel{
StackID: state.StackID,
Timeouts: state.Timeouts,
CPUAllocationRatio: types.Float64Value(record.CPUAllocationRatio),
Descr: types.StringValue(record.Descr),
Drivers: flattens.FlattenSimpleTypeToList(ctx, types.StringType, record.Drivers),
MemAllocationRatio: types.Float64Value(record.MemAllocationRatio),
Name: types.StringValue(record.Name),
Status: types.StringValue(record.Status),
Type: types.StringValue(record.Type),
}
tflog.Info(ctx, "flattens.StackDataSource: after flatten", map[string]any{"stack_id": state.StackID.ValueInt64()})
tflog.Info(ctx, "End flattens.StackDataSource", map[string]any{"stack_id": state.StackID.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,61 @@
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/cloudapi/stack/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/stack/utilities"
)
// StackListDataSource flattens data source for stack list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func StackListDataSource(ctx context.Context, state *models.ListStacksModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.StackListDataSource")
diags := diag.Diagnostics{}
record, err := utilities.StackListCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Cannot get info about stack list", err.Error())
return diags
}
tflog.Info(ctx, "flattens.StackListDataSource: before flatten", map[string]any{"record": record})
*state = models.ListStacksModel{
//optional fields
Timeouts: state.Timeouts,
ByID: state.ByID,
Name: state.Name,
Status: state.Status,
Type: state.Type,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
//compute fields
EntryCount: types.Int64Value(int64(record.EntryCount)),
}
items := make([]models.ItemStackModel, 0, len(record.Data))
for _, item := range record.Data {
i := models.ItemStackModel{
ID: types.Int64Value(int64(item.ID)),
Name: types.StringValue(item.Name),
Status: types.StringValue(item.Status),
Type: types.StringValue(item.Type),
}
items = append(items, i)
}
state.Data = items
tflog.Info(ctx, "flattens.StackListDataSource: after flatten")
tflog.Info(ctx, "End flattens.StackListDataSource")
return nil
}