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,116 @@
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/service/cloudapi/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
// BServiceDataSource flattens data source for bservice.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServiceDataSource(ctx context.Context, state *models.RecordBasicServiceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServiceDataSource")
diags := diag.Diagnostics{}
serivceId := uint64(state.ServiceId.ValueInt64())
record, err := utilities.BServiceDataSourceCheckPresence(ctx, serivceId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about bservice with ID %v", serivceId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServiceDataSource: before flatten", map[string]any{"service_id": serivceId, "record": record})
*state = models.RecordBasicServiceModel{
ServiceId: state.ServiceId,
Timeouts: state.Timeouts,
AccountID: types.Int64Value(int64(record.AccountID)),
AccountName: types.StringValue(record.AccountName),
BaseDomain: types.StringValue(record.BaseDomain),
CPUTotal: types.Int64Value(int64(record.CPUTotal)),
CreatedBy: types.StringValue(record.CreatedBy),
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
DeletedBy: types.StringValue(record.DeletedBy),
DeletedTime: types.Int64Value(int64(record.DeletedTime)),
DiskTotal: types.Int64Value(int64(record.DiskTotal)),
GID: types.Int64Value(int64(record.GID)),
GUID: types.Int64Value(int64(record.GUID)),
Milestones: types.Int64Value(int64(record.Milestones)),
Name: types.StringValue(record.Name),
ParentSrvID: types.Int64Value(int64(record.ParentSrvID)),
ParentSrvType: types.StringValue(record.ParentSrvType),
RAMTotal: types.Int64Value(int64(record.RAMTotal)),
RGID: types.Int64Value(int64(record.RGID)),
RGName: types.StringValue(record.RGName),
SSHKey: types.StringValue(record.SSHKey),
SSHUser: types.StringValue(record.SSHUser),
Status: types.StringValue(record.Status),
TechStatus: types.StringValue(record.TechStatus),
UpdatedBy: types.StringValue(record.UpdatedBy),
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
UserManaged: types.BoolValue(record.UserManaged),
}
computesList := make([]models.ItemComputeModel, 0, len(record.Computes))
for _, v := range record.Computes {
temp := models.ItemComputeModel{
AccountID: types.Int64Value(int64(v.AccountID)),
Architecture: types.StringValue(v.Architecture),
CompGroupID: types.Int64Value(int64(v.CompGroupID)),
CompGroupName: types.StringValue(v.CompGroupName),
CompGroupRole: types.StringValue(v.CompGroupRole),
ID: types.Int64Value(int64(v.ID)),
Name: types.StringValue(v.Name),
RGID: types.Int64Value(int64(v.RGID)),
StackID: types.Int64Value(int64(v.StackID)),
Status: types.StringValue(v.Status),
TechStatus: types.StringValue(v.TechStatus),
}
computesList = append(computesList, temp)
}
state.Computes = computesList
groupsList := make([]models.ItemGroupModel, 0, len(record.Groups))
for _, v := range record.Groups {
temp := models.ItemGroupModel{
Computes: types.Int64Value(int64(v.Computes)),
Consistency: types.BoolValue(v.Consistency),
ID: types.Int64Value(int64(v.ID)),
Name: types.StringValue(v.Name),
Status: types.StringValue(v.Status),
TechStatus: types.StringValue(v.TechStatus),
}
groupsList = append(groupsList, temp)
}
state.Groups = groupsList
snapshotsList := make([]models.ItemSnapshotModel, 0, len(record.Snapshots))
for _, v := range record.Snapshots {
temp := models.ItemSnapshotModel{
GUID: types.StringValue(v.GUID),
Label: types.StringValue(v.Label),
Timestamp: types.Int64Value(int64(v.Timestamp)),
Valid: types.BoolValue(v.Valid),
}
snapshotsList = append(snapshotsList, temp)
}
state.Snapshots = snapshotsList
tflog.Info(ctx, "flattens.BServiceDataSource: after flatten", map[string]any{"service_id": state.ServiceId.ValueInt64()})
tflog.Info(ctx, "End flattens.BServiceDataSource", map[string]any{"service_id": state.ServiceId.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,75 @@
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/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
// BServicesDeletedListDataSource flattens data source for a list of basic services.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServicesDeletedListDataSource(ctx context.Context, state *models.ListBasicServicesDelModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServicesDeletedListDataSource")
diags := diag.Diagnostics{}
// Fetch the list of basic services from the API
recordList, err := utilities.BServiceDeletedListDataSourceCheckPresence(ctx, *state, c)
if err != nil {
diags.AddError("Cannot get list of basic services", err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServicesDeletedListDataSource: before flatten", map[string]any{"record_list": recordList})
// Flatten the fetched data into the state
var itemList []models.ItemBasicServiceDelModel
for _, record := range recordList.Data {
item := models.ItemBasicServiceDelModel{
AccountID: types.Int64Value(int64(record.AccountID)),
AccountName: types.StringValue(record.AccountName),
BaseDomain: types.StringValue(record.BaseDomain),
CreatedBy: types.StringValue(record.CreatedBy),
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
DeletedBy: types.StringValue(record.DeletedBy),
DeletedTime: types.Int64Value(int64(record.DeletedTime)),
GID: types.Int64Value(int64(record.GID)),
GUID: types.Int64Value(int64(record.GUID)),
ID: types.Int64Value(int64(record.ID)),
Name: types.StringValue(record.Name),
ParentSrvID: types.Int64Value(int64(record.ParentSrvID)),
ParentSrvType: types.StringValue(record.ParentSrvType),
RGID: types.Int64Value(int64(record.RGID)),
RGName: types.StringValue(record.RGName),
SSHUser: types.StringValue(record.SSHUser),
Status: types.StringValue(record.Status),
TechStatus: types.StringValue(record.TechStatus),
UpdatedBy: types.StringValue(record.UpdatedBy),
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
UserManaged: types.BoolValue(record.UserManaged),
}
// Handle groups as a list of strings
groupList := make([]types.Int64, len(record.Groups))
for i, group := range record.Groups {
groupList[i] = types.Int64Value(int64(group))
}
itemList = append(itemList, item)
}
// Update state with the flattened data
state.Data = itemList
state.EntryCount = types.Int64Value(int64(len(itemList)))
tflog.Info(ctx, "flattens.BServicesDeletedListDataSource: after flatten", map[string]any{"entry_count": state.EntryCount.ValueInt64()})
tflog.Info(ctx, "End flattens.BServicesDeletedListDataSource")
return nil
}

View File

@@ -0,0 +1,118 @@
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/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
// BServiceGroupDataSource flattens data source for a group.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServiceGroupDataSource(ctx context.Context, state *models.RecordGroupModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServiceGroupDataSource")
diags := diag.Diagnostics{}
serviceId := uint64(state.ServiceID.ValueInt64())
record, err := utilities.BServiceGroupDataSourceCheckPresence(ctx, *state, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about group with ID %v", serviceId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServiceGroupDataSource: before flatten", map[string]any{"service_id": serviceId, "record": record})
*state = models.RecordGroupModel{
ServiceID: state.ServiceID,
ID: state.ID,
Timeouts: state.Timeouts,
AccountID: types.Int64Value(int64(record.AccountID)),
AccountName: types.StringValue(record.AccountName),
Consistency: types.BoolValue(record.Consistency),
CPU: types.Int64Value(int64(record.CPU)),
CreatedBy: types.StringValue(record.CreatedBy),
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
DeletedBy: types.StringValue(record.DeletedBy),
DeletedTime: types.Int64Value(int64(record.DeletedTime)),
Disk: types.Int64Value(int64(record.Disk)),
Driver: types.StringValue(record.Driver),
GID: types.Int64Value(int64(record.GID)),
GUID: types.Int64Value(int64(record.GUID)),
ImageID: types.Int64Value(int64(record.ImageID)),
Milestones: types.Int64Value(int64(record.Milestones)),
Name: types.StringValue(record.Name),
RAM: types.Int64Value(int64(record.RAM)),
RGID: types.Int64Value(int64(record.RGID)),
RGName: types.StringValue(record.RGName),
Role: types.StringValue(record.Role),
SEPID: types.Int64Value(int64(record.SEPID)),
SeqNo: types.Int64Value(int64(record.SeqNo)),
Status: types.StringValue(record.Status),
TechStatus: types.StringValue(record.TechStatus),
TimeoutStart: types.Int64Value(int64(record.TimeoutStart)),
UpdatedBy: types.StringValue(record.UpdatedBy),
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
}
// Handle computes
computesList := make([]models.ItemGroupComputeModel, 0, len(record.Computes))
for _, v := range record.Computes {
ipAddresses := make([]types.String, len(v.IPAddresses))
for i, ip := range v.IPAddresses {
ipAddresses[i] = types.StringValue(ip)
}
osUsers := make([]models.ItemOSUserModel, len(v.OSUsers))
for j, user := range v.OSUsers {
osUsers[j] = models.ItemOSUserModel{
Login: types.StringValue(user.Login),
Password: types.StringValue(user.Password),
}
}
temp := models.ItemGroupComputeModel{
ID: types.Int64Value(int64(v.ID)),
IPAddresses: flattens.FlattenSimpleTypeToList(ctx, types.StringType, ipAddresses),
Name: types.StringValue(v.Name),
OSUsers: osUsers,
}
computesList = append(computesList, temp)
}
state.Computes = computesList
// Handle ExtNets
extNetsList := make([]types.Int64, len(record.ExtNets))
for i, extNet := range record.ExtNets {
extNetsList[i] = types.Int64Value(int64(extNet))
}
state.ExtNets = flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, extNetsList)
// Handle Parents
parentsList := make([]types.Int64, len(record.Parents))
for i, parent := range record.Parents {
parentsList[i] = types.Int64Value(int64(parent))
}
state.Parents = flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, parentsList)
// Handle VINSes
vinsesList := make([]types.Int64, len(record.VINSes))
for i, vins := range record.VINSes {
vinsesList[i] = types.Int64Value(int64(vins))
}
state.VINSes = flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, vinsesList)
tflog.Info(ctx, "flattens.BServiceGroupDataSource: after flatten", map[string]any{"service_id": state.ID.ValueInt64()})
tflog.Info(ctx, "End flattens.BServiceGroupDataSource", map[string]any{"service_id": state.ID.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,75 @@
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/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
// BServicesListDataSource flattens data source for a list of basic services.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServicesListDataSource(ctx context.Context, state *models.ListBasicServicesModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServicesListDataSource")
diags := diag.Diagnostics{}
// Fetch the list of basic services from the API
recordList, err := utilities.BServiceListDataSourceCheckPresence(ctx, *state, c)
if err != nil {
diags.AddError("Cannot get list of basic services", err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServicesListDataSource: before flatten", map[string]any{"record_list": recordList})
// Flatten the fetched data into the state
var itemList []models.ItemBasicServiceModel
for _, record := range recordList.Data {
item := models.ItemBasicServiceModel{
AccountID: types.Int64Value(int64(record.AccountID)),
AccountName: types.StringValue(record.AccountName),
BaseDomain: types.StringValue(record.BaseDomain),
CreatedBy: types.StringValue(record.CreatedBy),
CreatedTime: types.Int64Value(int64(record.CreatedTime)),
DeletedBy: types.StringValue(record.DeletedBy),
DeletedTime: types.Int64Value(int64(record.DeletedTime)),
GID: types.Int64Value(int64(record.GID)),
GUID: types.Int64Value(int64(record.GUID)),
ID: types.Int64Value(int64(record.ID)),
Name: types.StringValue(record.Name),
ParentSrvID: types.Int64Value(int64(record.ParentSrvID)),
ParentSrvType: types.StringValue(record.ParentSrvType),
RGID: types.Int64Value(int64(record.RGID)),
RGName: types.StringValue(record.RGName),
SSHUser: types.StringValue(record.SSHUser),
Status: types.StringValue(record.Status),
TechStatus: types.StringValue(record.TechStatus),
UpdatedBy: types.StringValue(record.UpdatedBy),
UpdatedTime: types.Int64Value(int64(record.UpdatedTime)),
UserManaged: types.BoolValue(record.UserManaged),
}
// Handle groups as a list of strings
groupList := make([]types.Int64, len(record.Groups))
for i, group := range record.Groups {
groupList[i] = types.Int64Value(int64(group))
}
itemList = append(itemList, item)
}
// Update state with the flattened data
state.Data = itemList
state.EntryCount = types.Int64Value(int64(len(itemList)))
tflog.Info(ctx, "flattens.BServicesListDataSource: after flatten", map[string]any{"entry_count": state.EntryCount.ValueInt64()})
tflog.Info(ctx, "End flattens.BServicesListDataSource")
return nil
}

View File

@@ -0,0 +1,56 @@
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/service/cloudapi/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
// BServiceSnapshotListDataSource flattens data source for account.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func BServiceSnapshotListDataSource(ctx context.Context, state *models.ListInfoSnapshotsModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServiceSnapshotListDataSource")
diags := diag.Diagnostics{}
serivceId := uint64(state.ServiceID.ValueInt64())
record, err := utilities.BServiceSnapshotListDataSourceCheckPresence(ctx, serivceId, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about bservice with ID %v", serivceId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServiceSnapshotListDataSource: before flatten", map[string]any{"service_id": serivceId, "record": record})
*state = models.ListInfoSnapshotsModel{
ServiceID: state.ServiceID,
Timeouts: state.Timeouts,
EntryCount: types.Int64Value(int64(record.EntryCount)),
}
dataList := make([]models.ItemSnapshotsModel, 0, len(record.Data))
for _, v := range record.Data {
temp := models.ItemSnapshotsModel{
GUID: types.StringValue(v.GUID),
Label: types.StringValue(v.Label),
Timestamp: types.Int64Value(int64(v.Timestamp)),
Valid: types.BoolValue(v.Valid),
}
dataList = append(dataList, temp)
}
state.Data = dataList
tflog.Info(ctx, "flattens.BServiceSnapshotListDataSource: after flatten", map[string]any{"service_id": state.ServiceID.ValueInt64()})
tflog.Info(ctx, "End flattens.BServiceSnapshotListDataSource", map[string]any{"service_id": state.ServiceID.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,144 @@
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/cloudapi/bservice"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
func BServiceResource(ctx context.Context, state *models.RecordBasicServiceResourceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.BServiceResource")
diags := diag.Diagnostics{}
serviceId := uint64(state.ServiceId.ValueInt64())
if serviceId == 0 {
id, err := strconv.Atoi(state.ID.ValueString())
if err != nil {
diags.AddError(
"flattens.BServiceResource: cannot parse resource ID from state",
err.Error())
return diags
}
serviceId = uint64(id)
}
recordBService, err := utilities.BServiceResourceCheckPresence(ctx, serviceId, c)
if err != nil {
diags.AddError(fmt.Sprintf("flattens.BServiceResource: Cannot get info about resource with ID %v", serviceId), err.Error())
return diags
}
tflog.Info(ctx, "flattens.BServiceResource: before flatten", map[string]any{"service_id": serviceId, "recordBService": recordBService})
*state = models.RecordBasicServiceResourceModel{
Name: state.Name,
RGID: state.RGID,
Permanently: state.Permanently,
Enable: state.Enable,
Restore: state.Restore,
Start: state.Start,
Snapshots: state.Snapshots,
Timeouts: state.Timeouts,
SSHKey: types.StringValue(recordBService.SSHKey),
SSHUser: types.StringValue(recordBService.SSHUser),
ServiceId: types.Int64Value(int64(recordBService.ID)),
AccountID: types.Int64Value(int64(recordBService.AccountID)),
Computes: flattenComputes(ctx, recordBService.Computes),
Groups: flattenGroups(ctx, recordBService.Groups),
AccountName: types.StringValue(recordBService.Name),
BaseDomain: types.StringValue(recordBService.BaseDomain),
CPUTotal: types.Int64Value(int64(recordBService.CPUTotal)),
CreatedBy: types.StringValue(recordBService.CreatedBy),
CreatedTime: types.Int64Value(int64(recordBService.CreatedTime)),
DeletedBy: types.StringValue(recordBService.DeletedBy),
DeletedTime: types.Int64Value(int64(recordBService.DeletedTime)),
DiskTotal: types.Int64Value(int64(recordBService.DiskTotal)),
GID: types.Int64Value(int64(recordBService.GID)),
GUID: types.Int64Value(int64(recordBService.GUID)),
Milestones: types.Int64Value(int64(recordBService.Milestones)),
ParentSrvID: types.Int64Value(int64(recordBService.ParentSrvID)),
ParentSrvType: types.StringValue(recordBService.ParentSrvType),
RAMTotal: types.Int64Value(int64(recordBService.RAMTotal)),
RGName: types.StringValue(recordBService.RGName),
Status: types.StringValue(recordBService.Status),
TechStatus: types.StringValue(recordBService.TechStatus),
UpdatedBy: types.StringValue(recordBService.UpdatedBy),
UpdatedTime: types.Int64Value(int64(recordBService.UpdatedTime)),
UserManaged: types.BoolValue(recordBService.UserManaged),
ID: types.StringValue(strconv.Itoa(int(serviceId))),
}
tflog.Info(ctx, "flattens.BServiceResource: after flatten", map[string]any{"service_id": state.ID.ValueString()})
tflog.Info(ctx, "End flattens.BServiceResource", map[string]any{"service_id": state.ID.ValueString()})
return nil
}
func flattenComputes(ctx context.Context, items bservice.ListComputes) types.List {
tflog.Info(ctx, "Start flattenComputes")
tempSlice := make([]types.Object, 0, len(items))
for _, v := range items {
temp := models.ItemComputeResourceModel{
AccountID: types.Int64Value(int64(v.AccountID)),
Architecture: types.StringValue(v.Architecture),
CompGroupID: types.Int64Value(int64(v.CompGroupID)),
CompGroupName: types.StringValue(v.CompGroupName),
CompGroupRole: types.StringValue(v.CompGroupRole),
ID: types.Int64Value(int64(v.ID)),
Name: types.StringValue(v.Name),
RGID: types.Int64Value(int64(v.RGID)),
StackID: types.Int64Value(int64(v.StackID)),
Status: types.StringValue(v.Status),
TechStatus: types.StringValue(v.TechStatus),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemComputeResource, 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.ItemComputeResource}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenComputes", diags))
}
tflog.Info(ctx, "End flattenComputes")
return res
}
func flattenGroups(ctx context.Context, items bservice.ListGroups) types.List {
tflog.Info(ctx, "Start flattenGroups")
tempSlice := make([]types.Object, 0, len(items))
for _, v := range items {
temp := models.ItemGroupResourceModel{
Computes: types.Int64Value(int64(v.Computes)),
Consistency: types.BoolValue(v.Consistency),
ID: types.Int64Value(int64(v.ID)),
Name: types.StringValue(v.Name),
Status: types.StringValue(v.Status),
TechStatus: types.StringValue(v.TechStatus),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemGroupResource, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenGroups struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemGroupResource}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenGroups", diags))
}
tflog.Info(ctx, "End flattenGroups")
return res
}

View File

@@ -0,0 +1,151 @@
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/cloudapi/bservice"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/flattens"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/bservice/utilities"
)
func BServiceGroupResource(ctx context.Context, plan *models.ResourceRecordGroupModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start BServiceGroupResource", map[string]any{"service_id": plan.ServiceID.ValueInt64(), "compgroup_id": plan.CompgroupID.ValueInt64()})
diags := diag.Diagnostics{}
serviceID := plan.ServiceID.ValueInt64()
comgroupID := plan.CompgroupID.ValueInt64()
if serviceID == 0 {
id, err := strconv.Atoi(plan.SID.ValueString())
if err != nil {
diags.AddError(
"flattens.BServiceGroupResource: cannot parse resource ID from state",
err.Error())
return diags
}
serviceID = int64(id)
}
if comgroupID == 0 {
id, err := strconv.Atoi(plan.ID.ValueString())
if err != nil {
diags.AddError(
"flattens.BServiceGroupResource: cannot parse resource ID from state",
err.Error())
return diags
}
comgroupID = int64(id)
}
recordResourceGroup, err := utilities.BServiceGroupResourceCheckPresence(ctx, uint64(serviceID), uint64(comgroupID), c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about bservice group with service_id %d and compgroup_id %d", serviceID, comgroupID), err.Error())
return diags
}
*plan = models.ResourceRecordGroupModel{
ServiceID: plan.ServiceID,
CompCount: plan.CompCount,
Name: plan.Name,
CPU: plan.CPU,
RAM: plan.RAM,
Disk: plan.Disk,
ImageID: plan.ImageID,
Driver: plan.Driver,
SEPID: types.Int64Value(int64(recordResourceGroup.SEPID)),
SepPool: types.StringValue(recordResourceGroup.PoolName),
CloudInit: plan.CloudInit,
Role: types.StringValue(recordResourceGroup.Role),
TimeoutStart: types.Int64Value(int64(recordResourceGroup.TimeoutStart)),
VINSes: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordResourceGroup.VINSes),
ExtNets: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordResourceGroup.ExtNets),
Mode: plan.Mode,
Start: plan.Start,
ForceStop: plan.ForceStop,
ForceUpdate: plan.ForceUpdate,
Parents: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, recordResourceGroup.Parents),
RemoveComputes: plan.RemoveComputes,
CompgroupID: plan.CompgroupID,
ID: types.StringValue(strconv.Itoa(int(plan.CompgroupID.ValueInt64()))),
SID: types.StringValue(strconv.Itoa(int(plan.ServiceID.ValueInt64()))),
Timeouts: plan.Timeouts,
AccountID: types.Int64Value(int64(recordResourceGroup.AccountID)),
AccountName: types.StringValue(recordResourceGroup.AccountName),
Computes: flattenGroupComputes(ctx, recordResourceGroup.Computes),
Consistency: types.BoolValue(recordResourceGroup.Consistency),
CreatedBy: types.StringValue(recordResourceGroup.CreatedBy),
CreatedTime: types.Int64Value(int64(recordResourceGroup.CreatedTime)),
DeletedBy: types.StringValue(recordResourceGroup.DeletedBy),
DeletedTime: types.Int64Value(int64(recordResourceGroup.DeletedTime)),
GID: types.Int64Value(int64(recordResourceGroup.GID)),
GUID: types.Int64Value(int64(recordResourceGroup.GUID)),
Milestones: types.Int64Value(int64(recordResourceGroup.Milestones)),
RGID: types.Int64Value(int64(recordResourceGroup.RGID)),
RGName: types.StringValue(recordResourceGroup.RGName),
SeqNo: types.Int64Value(int64(recordResourceGroup.SeqNo)),
Status: types.StringValue(recordResourceGroup.Status),
TechStatus: types.StringValue(recordResourceGroup.TechStatus),
UpdatedBy: types.StringValue(recordResourceGroup.UpdatedBy),
UpdatedTime: types.Int64Value(int64(recordResourceGroup.UpdatedTime)),
}
tflog.Info(ctx, "End BServiceGroupResource", map[string]any{"service_id": plan.ServiceID.ValueInt64(), "compgroup_id": plan.CompgroupID.ValueInt64()})
return nil
}
func flattenGroupComputes(ctx context.Context, items bservice.ListGroupComputes) types.List {
tflog.Info(ctx, "Start flattenGroupComputes")
tempSlice := make([]types.Object, 0, len(items))
for _, v := range items {
temp := models.ResourceItemGroupComputeModel{
ID: types.Int64Value(int64(v.ID)),
Name: types.StringValue(v.Name),
IPAddresses: flattens.FlattenSimpleTypeToList(ctx, types.StringType, v.IPAddresses),
OSUsers: flattenOSuser(ctx, v.OSUsers),
}
obj, diags := types.ObjectValueFrom(ctx, models.ResourceItemGroupCompute, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenGroupComputes struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ResourceItemGroupCompute}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenGroupComputes", diags))
}
tflog.Info(ctx, "End flattenGroupComputes")
return res
}
func flattenOSuser(ctx context.Context, items bservice.ListOSUsers) types.List {
tflog.Info(ctx, "Start flattenOSuser")
tempSlice := make([]types.Object, 0, len(items))
for _, v := range items {
temp := models.ResourceItemOSUserModel{
Login: types.StringValue(v.Login),
Password: types.StringValue(v.Password),
}
obj, diags := types.ObjectValueFrom(ctx, models.ResourceItemOSUser, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenOSuser struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ResourceItemOSUser}, tempSlice)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenOSuser", diags))
}
tflog.Info(ctx, "End flattenOSuser")
return res
}