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/decort-golang-sdk/pkg/cloudapi/image"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/utilities"
)
func DataSourceImage(ctx context.Context, state *models.RecordImageModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceImage", map[string]any{"image_id": state.ImageId.ValueInt64()})
diags := diag.Diagnostics{}
image, err := utilities.ImageDataSourceCheckPresence(ctx, state, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about image with ID %d", state.ImageId.ValueInt64()), err.Error())
return diags
}
id := uuid.New()
cdPresentedTo, _ := json.Marshal(image.CdPresentedTo)
*state = models.RecordImageModel{
ImageId: types.Int64Value(int64(image.ID)),
ShowAll: state.ShowAll,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
UNCPath: types.StringValue(image.UNCPath),
CKey: types.StringValue(image.CKey),
AccountID: types.Int64Value(int64(image.AccountID)),
Architecture: types.StringValue(image.Architecture),
BootType: types.StringValue(image.BootType),
Bootable: types.BoolValue(image.Bootable),
ComputeCIID: types.Int64Value(int64(image.ComputeCIID)),
CdPresentedTo: types.StringValue(string(cdPresentedTo)),
GID: types.Int64Value(int64(image.GID)),
DeletedTime: types.Int64Value(int64(image.DeletedTime)),
Description: types.StringValue(image.Description),
Enabled: types.BoolValue(image.Enabled),
GUID: types.Int64Value(int64(image.GUID)),
History: flattenHistory(ctx, &image.History),
HotResize: types.BoolValue(image.HotResize),
LastModified: types.Int64Value(int64(image.LastModified)),
LinkTo: types.Int64Value(int64(image.LinkTo)),
Milestones: types.Int64Value(int64(image.Milestones)),
ImageName: types.StringValue(image.Name),
ImageType: types.StringValue(image.Type),
NetworkInterfaceNaming: types.StringValue(image.NetworkInterfaceNaming),
Password: types.StringValue(image.Password),
PoolName: types.StringValue(image.Pool),
ProviderName: types.StringValue(image.ProviderName),
PurgeAttempts: types.Int64Value(int64(image.PurgeAttempts)),
ResID: types.StringValue(image.ResID),
RescueCD: types.BoolValue(image.RescueCD),
SepID: types.Int64Value(int64(image.SepID)),
Size: types.Int64Value(int64(image.Size)),
Status: types.StringValue(image.Status),
TechStatus: types.StringValue(image.TechStatus),
Username: types.StringValue(image.Username),
Version: types.StringValue(image.Version),
}
state.ACL, diags = types.ListValueFrom(ctx, types.StringType, image.ACL)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
}
state.Drivers, diags = types.ListValueFrom(ctx, types.StringType, image.Drivers)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
}
state.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, image.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenPresentTo", diags))
}
state.SharedWith, diags = types.ListValueFrom(ctx, types.Int64Type, image.SharedWith)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenSharedWith", diags))
}
tflog.Info(ctx, "End FlattenDataSourceImage", map[string]any{"image_id": state.ImageId.ValueInt64()})
return nil
}
func flattenHistory(ctx context.Context, history *image.ListHistories) []models.HistoryModel {
tflog.Info(ctx, "Start flattenHistory")
res := make([]models.HistoryModel, 0, len(*history))
for _, item := range *history {
temp := models.HistoryModel{
GUID: types.StringValue(item.GUID),
ID: types.Int64Value(int64(item.ID)),
Timestamp: types.Int64Value(int64(item.Timestamp)),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenHistory")
return res
}

View File

@@ -0,0 +1,91 @@
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/decort-golang-sdk/pkg/cloudapi/image"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/utilities"
)
func DataSourceImageList(ctx context.Context, state *models.ListImagesModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceImageList")
diags := diag.Diagnostics{}
imageList, err := utilities.ImageListCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Error get image list info", err.Error())
return diags
}
id := uuid.New()
*state = models.ListImagesModel{
SEPID: state.SEPID,
ByID: state.ByID,
Name: state.Name,
Status: state.Status,
Architecture: state.Architecture,
TypeImage: state.TypeImage,
ImageSize: state.ImageSize,
SEPName: state.SEPName,
PoolName: state.PoolName,
Public: state.Public,
HotResize: state.HotResize,
Bootable: state.Bootable,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Items: flattenItems(ctx, imageList),
EntryCount: types.Int64Value(int64(imageList.EntryCount)),
}
tflog.Info(ctx, "End FlattenDataSourceImageList")
return nil
}
func flattenItems(ctx context.Context, data *image.ListImages) []models.ItemImageModel {
tflog.Info(ctx, "Start flattenItems")
var diags diag.Diagnostics
res := make([]models.ItemImageModel, 0, len(data.Data))
for _, item := range data.Data {
temp := models.ItemImageModel{
AccountID: types.Int64Value(int64(item.AccountID)),
Architecture: types.StringValue(item.Architecture),
BootType: types.StringValue(item.BootType),
Bootable: types.BoolValue(item.Bootable),
CDROM: types.BoolValue(item.CDROM),
Description: types.StringValue(item.Description),
HotResize: types.BoolValue(item.HotResize),
ImageID: types.Int64Value(int64(item.ID)),
ImageType: types.StringValue(item.Type),
LinkTo: types.Int64Value(int64(item.LinkTo)),
ImageName: types.StringValue(item.Name),
PoolName: types.StringValue(item.Pool),
SepID: types.Int64Value(int64(item.SepID)),
Size: types.Int64Value(int64(item.Size)),
Status: types.StringValue(item.Status),
NetworkInterfaceNaming: types.StringValue(item.NetworkInterfaceNaming),
Username: types.StringValue(item.Username),
Virtual: types.BoolValue(item.Virtual),
}
temp.Drivers, diags = types.ListValueFrom(ctx, types.StringType, item.Drivers)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenItems")
return res
}

View File

@@ -0,0 +1,127 @@
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/image"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/utilities"
)
func ResourceImage(ctx context.Context, plan *models.ImageResourceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenResourceImage", map[string]any{"image_id": plan.Id.ValueString()})
diags := diag.Diagnostics{}
imageId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot parsed ID image from state %d", imageId), err.Error())
return diags
}
image, err := utilities.ImageResourceCheckPresence(ctx, imageId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about image with ID %d", image.ID), err.Error())
return diags
}
cdPresentedTo, _ := json.Marshal(image.CdPresentedTo)
*plan = models.ImageResourceModel{
ImageName: types.StringValue(image.Name),
URL: plan.URL,
BootType: types.StringValue(image.BootType),
ImageType: types.StringValue(image.Type),
HotResize: types.BoolValue(image.HotResize),
NetworkInterfaceNaming: types.StringValue(image.NetworkInterfaceNaming),
Username: types.StringValue(image.Username),
Password: types.StringValue(image.Password),
AccountID: types.Int64Value(int64(image.AccountID)),
UsernameDL: plan.UsernameDL,
PasswordDL: plan.PasswordDL,
SepID: types.Int64Value(int64(image.SepID)),
PoolName: types.StringValue(image.Pool),
Architecture: types.StringValue(image.Architecture),
Permanently: plan.Permanently,
ImageId: types.Int64Value(int64(image.ID)),
Timeouts: plan.Timeouts,
Id: types.StringValue(strconv.Itoa(int(image.ID))),
LastUpdated: plan.LastUpdated,
UNCPath: types.StringValue(image.UNCPath),
CKey: types.StringValue(image.CKey),
Bootable: types.BoolValue(image.Bootable),
ComputeCIID: types.Int64Value(int64(image.ComputeCIID)),
CdPresentedTo: types.StringValue(string(cdPresentedTo)),
GID: types.Int64Value(int64(image.GID)),
DeletedTime: types.Int64Value(int64(image.DeletedTime)),
Description: types.StringValue(image.Description),
Enabled: types.BoolValue(image.Enabled),
GUID: types.Int64Value(int64(image.GUID)),
History: flattenHistoryInResource(ctx, &image.History),
LastModified: types.Int64Value(int64(image.LastModified)),
LinkTo: types.Int64Value(int64(image.LinkTo)),
Milestones: types.Int64Value(int64(image.Milestones)),
ProviderName: types.StringValue(image.ProviderName),
PurgeAttempts: types.Int64Value(int64(image.PurgeAttempts)),
ResID: types.StringValue(image.ResID),
RescueCD: types.BoolValue(image.RescueCD),
Size: types.Int64Value(int64(image.Size)),
Status: types.StringValue(image.Status),
TechStatus: types.StringValue(image.TechStatus),
Version: types.StringValue(image.Version),
}
plan.ACL, diags = types.ListValueFrom(ctx, types.StringType, image.ACL)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
}
plan.Drivers, diags = types.ListValueFrom(ctx, types.StringType, image.Drivers)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
}
plan.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, image.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenPresentTo", diags))
}
plan.SharedWith, diags = types.ListValueFrom(ctx, types.Int64Type, image.SharedWith)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenSharedWith", diags))
}
tflog.Info(ctx, "End FlattenResourceImage", map[string]any{"image_id": plan.ImageId.ValueInt64()})
return nil
}
func flattenHistoryInResource(ctx context.Context, history *image.ListHistories) types.List {
tflog.Info(ctx, "Start flattenHistoryInResource")
tempSlice := make([]types.Object, 0, len(*history))
for _, item := range *history {
temp := models.HistoryInImageResourceModel{
GUID: types.StringValue(item.GUID),
ID: types.Int64Value(int64(item.ID)),
Timestamp: types.Int64Value(int64(item.Timestamp)),
}
obj, err := types.ObjectValueFrom(ctx, models.HistoryInResource, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenHistoryInResource struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.HistoryInResource}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenHistoryInResource", err))
}
tflog.Info(ctx, "End flattenHistoryInResource")
return res
}

View File

@@ -0,0 +1,91 @@
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/terraform-provider-dynamix/internal/service/cloudapi/image/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/image/utilities"
)
func ResourceImageVirtual(ctx context.Context, plan *models.ImageVirtualResourceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenResourceImageVirtual", map[string]any{"image_id": plan.ImageId.ValueInt64()})
diags := diag.Diagnostics{}
imageId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot parsed ID image from state %d", imageId), err.Error())
return diags
}
image, err := utilities.ImageResourceCheckPresence(ctx, imageId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about image with ID %d", plan.ImageId.ValueInt64()), err.Error())
return diags
}
*plan = models.ImageVirtualResourceModel{
ImageName: types.StringValue(image.Name),
LinkTo: types.Int64Value(int64(image.LinkTo)),
Permanently: plan.Permanently,
Timeouts: plan.Timeouts,
Id: types.StringValue(strconv.Itoa(int(image.ID))),
LastUpdated: plan.LastUpdated,
UNCPath: types.StringValue(image.UNCPath),
CKey: types.StringValue(image.CKey),
AccountID: types.Int64Value(int64(image.AccountID)),
Architecture: types.StringValue(image.Architecture),
BootType: types.StringValue(image.BootType),
Bootable: types.BoolValue(image.Bootable),
ComputeCIID: types.Int64Value(int64(image.ComputeCIID)),
DeletedTime: types.Int64Value(int64(image.DeletedTime)),
Description: types.StringValue(image.Description),
Enabled: types.BoolValue(image.Enabled),
GID: types.Int64Value(int64(image.GID)),
GUID: types.Int64Value(int64(image.GUID)),
History: flattenHistoryInResource(ctx, &image.History),
HotResize: types.BoolValue(image.HotResize),
LastModified: types.Int64Value(int64(image.LastModified)),
Milestones: types.Int64Value(int64(image.Milestones)),
ImageId: types.Int64Value(int64(image.ID)),
ImageType: types.StringValue(image.Type),
Password: types.StringValue(image.Password),
PoolName: types.StringValue(image.Pool),
ProviderName: types.StringValue(image.ProviderName),
PurgeAttempts: types.Int64Value(int64(image.PurgeAttempts)),
ResID: types.StringValue(image.ResID),
RescueCD: types.BoolValue(image.RescueCD),
SepID: types.Int64Value(int64(image.SepID)),
Size: types.Int64Value(int64(image.Size)),
Status: types.StringValue(image.Status),
TechStatus: types.StringValue(image.TechStatus),
Username: types.StringValue(image.Username),
Version: types.StringValue(image.Version),
}
plan.ACL, diags = types.ListValueFrom(ctx, types.StringType, image.ACL)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
}
plan.Drivers, diags = types.ListValueFrom(ctx, types.StringType, image.Drivers)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDrivers", diags))
}
plan.PresentTo, diags = types.ListValueFrom(ctx, types.Int64Type, image.PresentTo)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenPresentTo", diags))
}
plan.SharedWith, diags = types.ListValueFrom(ctx, types.Int64Type, image.SharedWith)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenSharedWith", diags))
}
tflog.Info(ctx, "End FlattenResourceImageVirtual", map[string]any{"image_id": plan.ImageId.ValueInt64()})
return nil
}