This commit is contained in:
asteam
2024-08-23 16:55:50 +03:00
parent 6f40af6a5f
commit 003e4d656e
524 changed files with 43376 additions and 432 deletions

View File

@@ -0,0 +1,585 @@
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/cloudbroker/vins"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/flattens"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSDataSource flattens data source for vins.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSDataSource(ctx context.Context, state *models.DataSourceVINSModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSDataSource")
diags := diag.Diagnostics{}
vinsId := uint64(state.VinsID.ValueInt64())
recordVins, diags := utilities.VINSDataSourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSDataSource: before flatten", map[string]any{"vins_id": vinsId})
id := uuid.New()
*state = models.DataSourceVINSModel{
VinsID: state.VinsID,
Reason: state.Reason,
Timeouts: state.Timeouts,
VNFDev: flattenVNFDev(ctx, &recordVins.VNFDev),
AccountID: types.Int64Value(int64(recordVins.AccountID)),
AccountName: types.StringValue(recordVins.AccountName),
CreatedBy: types.StringValue(recordVins.CreatedBy),
CreatedTime: types.Int64Value(int64(recordVins.CreatedTime)),
DefaultGW: types.StringValue(recordVins.DefaultGW),
DefaultQOS: flattenQOS(ctx, &recordVins.DefaultQOS),
DeletedBy: types.StringValue(recordVins.DeletedBy),
DeletedTime: types.Int64Value(int64(recordVins.DeletedTime)),
Description: types.StringValue(recordVins.Description),
GID: types.Int64Value(int64(recordVins.GID)),
GUID: types.Int64Value(int64(recordVins.GUID)),
Id: types.StringValue(id.String()),
LockStatus: types.StringValue(recordVins.LockStatus),
ManagerID: types.Int64Value(int64(recordVins.ManagerID)),
ManagerType: types.StringValue(recordVins.ManagerType),
Milestones: types.Int64Value(int64(recordVins.Milestones)),
Name: types.StringValue(recordVins.Name),
NetMask: types.Int64Value(int64(recordVins.NetMask)),
Network: types.StringValue(recordVins.Network),
PreReservationsNum: types.Int64Value(int64(recordVins.PreReservationsNum)),
Redundant: types.BoolValue(recordVins.Redundant),
RGID: types.Int64Value(int64(recordVins.RGID)),
RGName: types.StringValue(recordVins.RGName),
SecVNFDevID: types.Int64Value(int64(recordVins.SecVNFDevID)),
Status: types.StringValue(recordVins.Status),
UpdatedBy: types.StringValue(recordVins.UpdatedBy),
UpdatedTime: types.Int64Value(int64(recordVins.UpdatedTime)),
UserManaged: types.BoolValue(recordVins.UserManaged),
VNFs: flattenVNFs(ctx, &recordVins.VNFs),
VXLANID: types.Int64Value(int64(recordVins.VXLANID)),
}
tflog.Info(ctx, "flattens.VINSDataSource: after flatten", map[string]any{"vins_id": state.Id.ValueString()})
tflog.Info(ctx, "End flattens.VINSDataSource", map[string]any{"vins_id": state.Id.ValueString()})
return nil
}
// flattenVNFDev flattens vnfdev.
// Flatten errors are added to tflog.
func flattenVNFDev(ctx context.Context, vnfdev *vins.VNFDev) types.Object {
tflog.Info(ctx, "Start flattenVNFDev")
temp := models.RecordVNFDevModel{
CKey: types.StringValue(vnfdev.CKey),
AccountID: types.Int64Value(int64(vnfdev.AccountID)),
Capabilities: flattens.FlattenSimpleTypeToList(ctx, types.StringType, &vnfdev.Capabilities),
Config: flattenVNFDevConfig(ctx, &vnfdev.Config),
ConfigSaved: types.BoolValue(vnfdev.ConfigSaved),
CustomPreConfig: types.BoolValue(vnfdev.CustomPreConfig),
Description: types.StringValue(vnfdev.Description),
GID: types.Int64Value(int64(vnfdev.GID)),
GUID: types.Int64Value(int64(vnfdev.GUID)),
ID: types.Int64Value(int64(vnfdev.ID)),
Interfaces: flattenInterfaces(ctx, &vnfdev.Interfaces),
LockStatus: types.StringValue(vnfdev.LockStatus),
Meta: flattens.FlattenSimpleTypeToList(ctx, types.StringType, &vnfdev.Meta),
Milestones: types.Int64Value(int64(vnfdev.Milestones)),
Name: types.StringValue(vnfdev.Name),
Status: types.StringValue(vnfdev.Status),
TechStatus: types.StringValue(vnfdev.TechStatus),
Type: types.StringValue(vnfdev.Type),
VINS: flattens.FlattenSimpleTypeToList(ctx, types.Int64Type, &vnfdev.VINS),
}
res, err := types.ObjectValueFrom(ctx, models.ItemVNFDev, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenVNFDev struct to obj", err))
}
tflog.Info(ctx, "End flattenVNFDev")
return res
}
// flattenVNFDevConfig flattens config.
// Flatten errors are added to tflog.
func flattenVNFDevConfig(ctx context.Context, config *vins.Config) types.Object {
tflog.Info(ctx, "Start flattenVNFDevConfig")
temp := models.RecordVNFConfigModel{
MGMT: flattenMgmtVNFConfig(ctx, &config.MGMT),
Resources: flattenResourcesVNFConfig(ctx, &config.Resources),
}
res, err := types.ObjectValueFrom(ctx, models.ItemVNFConfig, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenVNFDevConfig struct to obj", err))
}
tflog.Info(ctx, "End flattenVNFDevConfig")
return res
}
// flattenMgmtVNFConfig flattens mgmt.
// Flatten errors are added to tflog.
func flattenMgmtVNFConfig(ctx context.Context, mgmt *vins.MGMT) types.Object {
tflog.Info(ctx, "Start flattenMgmtVNFConfig")
temp := models.RecordMGMTModel{
IPAddress: types.StringValue(mgmt.IPAddress),
Password: types.StringValue(mgmt.Password),
SSHKey: types.StringValue(mgmt.SSHKey),
User: types.StringValue(mgmt.User),
}
res, err := types.ObjectValueFrom(ctx, models.ItemMgmt, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenMgmtVNFConfig struct to obj", err))
}
tflog.Info(ctx, "End flattenMgmtVNFConfig")
return res
}
// flattenResourcesVNFConfig flattens resources.
// Flatten errors are added to tflog.
func flattenResourcesVNFConfig(ctx context.Context, resources *vins.Resources) types.Object {
tflog.Info(ctx, "Start flattenResourcesVNFConfig")
temp := models.RecordResourcesModel{
CPU: types.Int64Value(int64(resources.CPU)),
RAM: types.Int64Value(int64(resources.RAM)),
StackID: types.Int64Value(int64(resources.StackID)),
UUID: types.StringValue(resources.UUID),
}
res, err := types.ObjectValueFrom(ctx, models.ItemResources, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenResourcesVNFConfig struct to obj", err))
}
tflog.Info(ctx, "End flattenResourcesVNFConfig")
return res
}
// flattenInterfaces flattens interfaces.
// Flatten errors are added to tflog.
func flattenInterfaces(ctx context.Context, items *vins.ListInterfaces) types.List {
tflog.Info(ctx, "Start flattenInterfaces")
diags := diag.Diagnostics{}
tempSlice := make([]types.Object, 0, len(*items))
for _, item := range *items {
temp := models.VNFInterfaceModel{
ConnID: types.Int64Value(int64(item.ConnID)),
ConnType: types.StringValue(item.ConnType),
DefGW: types.StringValue(item.DefGW),
Enabled: types.BoolValue(item.Enabled),
FLIPGroupID: types.Int64Value(int64(item.FLIPGroupID)),
GUID: types.StringValue(item.GUID),
IPAddress: types.StringValue(item.IPAddress),
ListenSSH: types.BoolValue(item.ListenSSH),
MAC: types.StringValue(item.MAC),
Name: types.StringValue(item.Name),
NetID: types.Int64Value(int64(item.NetID)),
NetMask: types.Int64Value(int64(item.NetMask)),
NetType: types.StringValue(item.NetType),
NodeID: types.Int64Value(int64(item.NodeID)),
PCISlot: types.Int64Value(int64(item.PCISlot)),
QOS: flattenQOS(ctx, &item.QOS),
Target: types.StringValue(item.Target),
Type: types.StringValue(item.Type),
}
temp.VNFs, diags = types.ListValueFrom(ctx, types.Int64Type, item.VNFs)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("flattens.flattenInterfaces: cannot flatten item.VNFs to temp.VNFs", diags))
}
obj, err := types.ObjectValueFrom(ctx, models.ItemInterface, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenInterfaces struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemInterface}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenInterfaces", err))
}
tflog.Info(ctx, "End flattenInterfaces")
return res
}
// flattenQOS flattens QOS.
// Flatten errors are added to tflog.
func flattenQOS(ctx context.Context, qos *vins.QOS) types.Object {
tflog.Info(ctx, "Start flattenQOS")
temp := models.QOSModel{
ERate: types.Int64Value(int64(qos.ERate)),
GUID: types.StringValue(qos.GUID),
InBurst: types.Int64Value(int64(qos.InBurst)),
InRate: types.Int64Value(int64(qos.InRate)),
}
res, err := types.ObjectValueFrom(ctx, models.ItemQOS, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenQOS struct to obj", err))
}
tflog.Info(ctx, "End flattenQOS")
return res
}
// flattenVNFs flattens vnfs.
func flattenVNFs(ctx context.Context, vnfs *vins.RecordVNFs) types.Object {
tflog.Info(ctx, "Start flattenVNFs")
temp := models.RecordVNFsModel{
DHCP: flattenDHCP(ctx, &vnfs.DHCP),
GW: flattenGW(ctx, &vnfs.GW),
NAT: flattenNAT(ctx, &vnfs.NAT),
}
res, err := types.ObjectValueFrom(ctx, models.ItemVNFs, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenVNFs struct to obj", err))
}
tflog.Info(ctx, "End flattenVNFs")
return res
}
// flattenDHCP flattens dhcp.
// Flatten errors are added to tflog.
func flattenDHCP(ctx context.Context, dhcp *vins.RecordDHCP) types.Object {
tflog.Info(ctx, "Start flattenDHCP")
temp := models.RecordDHCPModel{
CKey: types.StringValue(dhcp.CKey),
AccountID: types.Int64Value(int64(dhcp.AccountID)),
Config: flattenDHCPConfig(ctx, &dhcp.Config),
CreatedTime: types.Int64Value(int64(dhcp.CreatedTime)),
Devices: flattenDevices(ctx, &dhcp.Devices),
GID: types.Int64Value(int64(dhcp.GID)),
GUID: types.Int64Value(int64(dhcp.GUID)),
DHCPID: types.Int64Value(int64(dhcp.ID)),
LockStatus: types.StringValue(dhcp.LockStatus),
Milestones: types.Int64Value(int64(dhcp.Milestones)),
OwnerID: types.Int64Value(int64(dhcp.OwnerID)),
OwnerType: types.StringValue(dhcp.OwnerType),
PureVirtual: types.BoolValue(dhcp.PureVirtual),
Routes: flattenRoutes(ctx, &dhcp.Routes),
Status: types.StringValue(dhcp.Status),
TechStatus: types.StringValue(dhcp.TechStatus),
Type: types.StringValue(dhcp.Type),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemDHCP, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDHCP struct to obj", diags))
}
tflog.Info(ctx, "End flattenDHCP")
return res
}
// flattenDHCPConfig flattens dhcp config.
// Flatten errors are added to tflog.
func flattenDHCPConfig(ctx context.Context, config *vins.VNFsConfig) types.Object {
tflog.Info(ctx, "Start flattenDHCPConfig")
diags := diag.Diagnostics{}
temp := models.RecordDHCPConfigModel{
DefaultGW: types.StringValue(config.DefaultGW),
IPEnd: types.StringValue(config.IPEnd),
IPStart: types.StringValue(config.IPStart),
Lease: types.Int64Value(int64(config.Lease)),
NetMask: types.Int64Value(int64(config.NetMask)),
Network: types.StringValue(config.Network),
Reservations: flattenReservations(ctx, &config.Reservations),
}
temp.DNS, diags = types.ListValueFrom(ctx, types.StringType, config.DNS)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("flattens.flattenDHCPConfig: cannot flatten config.DNS to temp.DNS", diags))
}
res, diags := types.ObjectValueFrom(ctx, models.ItemDHCPConfig, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenDHCPConfig struct to obj", diags))
}
tflog.Info(ctx, "End flattenDHCPConfig")
return res
}
// flattenReservations flattens dhcp config reservations.
// Flatten errors are added to tflog.
func flattenReservations(ctx context.Context, items *vins.ListReservations) types.List {
tflog.Info(ctx, "Start flattenReservations")
tempSlice := make([]types.Object, 0, len(*items))
for _, item := range *items {
temp := models.ReservationModel{
ClientType: types.StringValue(item.ClientType),
Description: types.StringValue(item.Description),
DomainName: types.StringValue(item.DomainName),
Hostname: types.StringValue(item.Hostname),
IP: types.StringValue(item.IP),
MAC: types.StringValue(item.MAC),
Type: types.StringValue(item.Type),
VMID: types.Int64Value(int64(item.VMID)),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemReservations, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenReservations struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemReservations}, tempSlice)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenReservations", diags))
}
tflog.Info(ctx, "End flattenReservations")
return res
}
// flattenDevices flattens devices.
// Flatten errors are added to tflog.
func flattenDevices(ctx context.Context, devices *vins.Devices) types.Object {
tflog.Info(ctx, "Start flattenDevices")
temp := models.DevicesModel{
Primary: flattenPrimaryDevices(ctx, &devices.Primary),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemDevices, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenDevices struct to obj", diags))
}
tflog.Info(ctx, "End flattenDevices")
return res
}
// flattenPrimaryDevices flattens primary devices.
// Flatten errors are added to tflog.
func flattenPrimaryDevices(ctx context.Context, primary *vins.Primary) types.Object {
tflog.Info(ctx, "Start flattenPrimaryDevices")
temp := models.PrimaryDevicesModel{
DevID: types.Int64Value(int64(primary.DevID)),
IFace01: types.StringValue(primary.IFace01),
IFace02: types.StringValue(primary.IFace02),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemPrimaryDevices, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenPrimaryDevices struct to obj", diags))
}
tflog.Info(ctx, "End flattenPrimaryDevices")
return res
}
// flattenRoutes flattens routes.
// Flatten errors are added to tflog.
func flattenRoutes(ctx context.Context, items *vins.ListRoutes) types.List {
tflog.Info(ctx, "Start flattenRoutes")
diags := diag.Diagnostics{}
tempSlice := make([]types.Object, 0, len(*items))
for _, item := range *items {
temp := models.RoutesModel{
Destination: types.StringValue(item.Destination),
Gateway: types.StringValue(item.Gateway),
GUID: types.StringValue(item.GUID),
RouteID: types.Int64Value(int64(item.ID)),
Netmask: types.StringValue(item.Netmask),
}
temp.ComputeIds, diags = types.ListValueFrom(ctx, types.Int64Type, item.ComputeIds)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("flattens.flattenRoutes: cannot flatten item.ComputeIds to temp.ComputeIds", diags))
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemRoute, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenRoutes struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemRoute}, tempSlice)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenRoutes", diags))
}
tflog.Info(ctx, "End flattenRoutes")
return res
}
// flattenGW flattens gw.
// Flatten errors are added to tflog.
func flattenGW(ctx context.Context, gw *vins.RecordGW) types.Object {
tflog.Info(ctx, "Start flattenGW")
diags := diag.Diagnostics{}
temp := models.RecordGWModel{
CKey: types.StringValue(gw.CKey),
AccountID: types.Int64Value(int64(gw.AccountID)),
Config: flattenGWConfig(ctx, &gw.Config),
CreatedTime: types.Int64Value(int64(gw.CreatedTime)),
Devices: flattenDevices(ctx, &gw.Devices),
GID: types.Int64Value(int64(gw.GID)),
GUID: types.Int64Value(int64(gw.GUID)),
GWID: types.Int64Value(int64(gw.ID)),
LockStatus: types.StringValue(gw.LockStatus),
Milestones: types.Int64Value(int64(gw.Milestones)),
OwnerID: types.Int64Value(int64(gw.OwnerID)),
OwnerType: types.StringValue(gw.OwnerType),
PureVirtual: types.BoolValue(gw.PureVirtual),
Routes: flattenRoutes(ctx, &gw.Routes),
Status: types.StringValue(gw.Status),
TechStatus: types.StringValue(gw.TechStatus),
Type: types.StringValue(gw.Type),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemGW, temp)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenGW struct to obj", diags))
}
tflog.Info(ctx, "End flattenGW")
return res
}
// flattenGWConfig flattens gw config.
// Flatten errors are added to tflog.
func flattenGWConfig(ctx context.Context, config *vins.GWConfig) types.Object {
tflog.Info(ctx, "Start flattenGWConfig")
diags := diag.Diagnostics{}
temp := models.RecordGWConfigModel{
DefaultGW: types.StringValue(config.DefaultGW),
ExtNetID: types.Int64Value(int64(config.ExtNetID)),
ExtNetIP: types.StringValue(config.ExtNetIP),
ExtNetMask: types.Int64Value(int64(config.ExtNetMask)),
QOS: flattenQOS(ctx, &config.QOS),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemGWConfig, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenGWConfig struct to obj", diags))
}
tflog.Info(ctx, "End flattenGWConfig")
return res
}
// flattenNAT flattens nat.
// Flatten errors are added to tflog.
func flattenNAT(ctx context.Context, nat *vins.RecordNAT) types.Object {
tflog.Info(ctx, "Start flattenNAT")
temp := models.RecordNATModel{
CKey: types.StringValue(nat.CKey),
AccountID: types.Int64Value(int64(nat.AccountID)),
Config: flattenNATConfig(ctx, &nat.Config),
CreatedTime: types.Int64Value(int64(nat.CreatedTime)),
Devices: flattenDevices(ctx, &nat.Devices),
GID: types.Int64Value(int64(nat.GID)),
GUID: types.Int64Value(int64(nat.GUID)),
NatID: types.Int64Value(int64(nat.ID)),
LockStatus: types.StringValue(nat.LockStatus),
Milestones: types.Int64Value(int64(nat.Milestones)),
OwnerID: types.Int64Value(int64(nat.OwnerID)),
OwnerType: types.StringValue(nat.OwnerType),
PureVirtual: types.BoolValue(nat.PureVirtual),
Routes: flattenRoutes(ctx, &nat.Routes),
Status: types.StringValue(nat.Status),
TechStatus: types.StringValue(nat.TechStatus),
Type: types.StringValue(nat.Type),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemNAT, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenNAT struct to obj", diags))
}
tflog.Info(ctx, "End flattenNAT")
return res
}
// flattenRules flattens nat rules.
// Flatten errors are added to tflog.
func flattenRules(ctx context.Context, items *vins.ListNATRule) types.List {
tflog.Info(ctx, "Start flattenRules")
tempSlice := make([]types.Object, 0, len(*items))
for _, item := range *items {
temp := models.NATRuleModel{
RuleID: types.Int64Value(int64(item.ID)),
LocalIP: types.StringValue(item.LocalIP),
LocalPort: types.Int64Value(int64(item.LocalPort)),
Protocol: types.StringValue(item.Protocol),
PublicPortEnd: types.Int64Value(int64(item.PublicPortEnd)),
PublicPortStart: types.Int64Value(int64(item.PublicPortStart)),
VMID: types.Int64Value(int64(item.VMID)),
VMName: types.StringValue(item.VMName),
}
obj, diags := types.ObjectValueFrom(ctx, models.ItemNATRule, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenRules struct to obj", diags))
}
tempSlice = append(tempSlice, obj)
}
res, diags := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemNATRule}, tempSlice)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenRules", diags))
}
tflog.Info(ctx, "End flattenRules")
return res
}
// flattenNATConfig flattens nat config.
// Flatten errors are added to tflog.
func flattenNATConfig(ctx context.Context, config *vins.NATConfig) types.Object {
tflog.Info(ctx, "Start flattenNATConfig")
diags := diag.Diagnostics{}
temp := models.NATConfigModel{
NetMask: types.Int64Value(int64(config.NetMask)),
Network: types.StringValue(config.Network),
Rules: flattenRules(ctx, &config.Rules),
}
res, diags := types.ObjectValueFrom(ctx, models.ItemNATConfig, temp)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattenNATConfig struct to obj", diags))
}
tflog.Info(ctx, "End flattenNATConfig")
return res
}

View File

@@ -0,0 +1,55 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSAuditsDataSource flattens data source for vins audits.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSAuditsDataSource(ctx context.Context, state *models.DataSourceVINSAuditsModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSAuditsDataSource")
diags := diag.Diagnostics{}
vinsId := uint64(state.VinsID.ValueInt64())
audits, diags := utilities.VINSAuditsDataSourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSAuditsDataSource: before flatten", map[string]any{"vins_id": vinsId})
id := uuid.New()
*state = models.DataSourceVINSAuditsModel{
VinsID: state.VinsID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
}
items := make([]models.ItemAuditModel, 0, len(*audits))
for _, item := range *audits {
a := models.ItemAuditModel{
Call: types.StringValue(item.Call),
ResponseTime: types.Float64Value(item.ResponseTime),
StatusCode: types.Int64Value(int64(item.StatusCode)),
Timestamp: types.Float64Value(item.Timestamp),
User: types.StringValue(item.User),
}
items = append(items, a)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSAuditsDataSource", map[string]any{"vins_id": state.Id.ValueString()})
return nil
}

View File

@@ -0,0 +1,57 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSExtNetListDataSource flattens data source for vins ext net list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSExtNetListDataSource(ctx context.Context, state *models.DataSourceVINSExtNetListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSExtNetListDataSource")
diags := diag.Diagnostics{}
vinsId := uint64(state.VinsID.ValueInt64())
extnetList, diags := utilities.VINSExtNetListDataSourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSExtNetListDataSource: before flatten", map[string]any{"vins_id": vinsId})
id := uuid.New()
*state = models.DataSourceVINSExtNetListModel{
VinsID: state.VinsID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(extnetList.EntryCount)),
}
items := make([]models.ItemExtNetVinsModel, 0, len(extnetList.Data))
for _, item := range extnetList.Data {
en := models.ItemExtNetVinsModel{
DefaultGW: types.StringValue(item.DefaultGW),
ExtNetID: types.Int64Value(int64(item.ExtNetID)),
IP: types.StringValue(item.IP),
PrefixLen: types.Int64Value(int64(item.PrefixLen)),
Status: types.StringValue(item.Status),
TechStatus: types.StringValue(item.TechStatus),
}
items = append(items, en)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSExtNetListDataSource", map[string]any{"vins_id": state.Id.ValueString()})
return nil
}

View File

@@ -0,0 +1,58 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSIPListDataSource flattens data source for vins ip list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSIPListDataSource(ctx context.Context, state *models.DataSourceVINSIPListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSIPListDataSource")
diags := diag.Diagnostics{}
vinsId := uint64(state.VinsID.ValueInt64())
ipList, diags := utilities.VINSIPListDataSourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSIPListDataSource: before flatten", map[string]any{"vins_id": vinsId})
id := uuid.New()
*state = models.DataSourceVINSIPListModel{
VinsID: state.VinsID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(ipList.EntryCount)),
}
items := make([]models.ItemIPVinsModel, 0, len(ipList.Data))
for _, item := range ipList.Data {
ip := models.ItemIPVinsModel{
ClientType: types.StringValue(item.ClientType),
DomainName: types.StringValue(item.DomainName),
Hostname: types.StringValue(item.Hostname),
IP: types.StringValue(item.IP),
MAC: types.StringValue(item.MAC),
Type: types.StringValue(item.Type),
VMID: types.Int64Value(int64(item.VMID)),
}
items = append(items, ip)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSIPListDataSource", map[string]any{"vins_id": state.Id.ValueString()})
return nil
}

View File

@@ -0,0 +1,117 @@
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/cloudbroker/vins"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSListDataSource flattens data source for vins list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSListDataSource(ctx context.Context, state *models.DataSourceVINSListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSListDataSource")
diags := diag.Diagnostics{}
vinsList, diags := utilities.VINSListDataSourceCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSListDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceVINSListModel{
ByID: state.ByID,
Name: state.Name,
AccountID: state.AccountID,
RGID: state.RGID,
ExtIP: state.ExtIP,
VNFDevID: state.VNFDevID,
IncludeDeleted: state.IncludeDeleted,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(vinsList.EntryCount)),
}
items := make([]models.ItemVinsModel, 0, len(vinsList.Data))
for _, item := range vinsList.Data {
v := models.ItemVinsModel{
AccountID: types.Int64Value(int64(item.AccountID)),
AccountName: types.StringValue(item.AccountName),
CreatedBy: types.StringValue(item.CreatedBy),
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
DefaultGW: types.StringValue(item.DefaultGW),
DefaultQOS: flattenQOS(ctx, &item.DefaultQOS),
DeletedBy: types.StringValue(item.DeletedBy),
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
Description: types.StringValue(item.Description),
ExternalIP: types.StringValue(item.ExternalIP),
ExtnetID: types.Int64Value(int64(item.ExtnetId)),
FreeIPs: types.Int64Value(int64(item.FreeIPs)),
GID: types.Int64Value(int64(item.GID)),
GUID: types.Int64Value(int64(item.GUID)),
ID: types.Int64Value(int64(item.ID)),
LockStatus: types.StringValue(item.LockStatus),
ManagerID: types.Int64Value(int64(item.ManagerID)),
ManagerType: types.StringValue(item.ManagerType),
Milestones: types.Int64Value(int64(item.Milestones)),
Name: types.StringValue(item.Name),
Netmask: types.Int64Value(int64(item.NetMask)),
Network: types.StringValue(item.Network),
PreReservationsNum: types.Int64Value(int64(item.PreReservationsNum)),
PriVNFDevID: types.Int64Value(int64(item.PriVNFDevID)),
Redundant: types.BoolValue(item.Redundant),
RGID: types.Int64Value(int64(item.RGID)),
RGName: types.StringValue(item.RGName),
SecVNFDefID: types.Int64Value(int64(item.SecVNFDevID)),
Status: types.StringValue(item.Status),
UpdatedBy: types.StringValue(item.UpdatedBy),
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
UserManaged: types.BoolValue(item.UserManaged),
VNFs: flattenListVNFs(ctx, &item.VNFs),
VXLANID: types.Int64Value(int64(item.VXLANID)),
}
items = append(items, v)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSListDataSource")
return nil
}
// flattenListVNFs flattens ItemListVNFs.
// Flatten errors are added to tflog.
func flattenListVNFs(ctx context.Context, iv *vins.ItemVNFs) types.Object {
tflog.Info(ctx, "Start flattenListVNFs")
temp := models.ListVNFsModel{
DHCP: types.Int64Value(int64(iv.DHCP)),
DNS: types.Int64Value(int64(iv.DNS)),
FW: types.Int64Value(int64(iv.FW)),
GW: types.Int64Value(int64(iv.GW)),
NAT: types.Int64Value(int64(iv.NAT)),
VPN: types.Int64Value(int64(iv.VPN)),
}
res, err := types.ObjectValueFrom(ctx, models.ItemListVNFs, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenListVNFs struct to obj", err))
}
tflog.Info(ctx, "End flattenListVNFs")
return res
}

View File

@@ -0,0 +1,88 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSListDeletedDataSource flattens data source for vins list deleted.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSListDeletedDataSource(ctx context.Context, state *models.DataSourceVINSListDeletedModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSListDeletedDataSource")
diags := diag.Diagnostics{}
vinsList, diags := utilities.VINSListDeletedDataSourceCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSListDeletedDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceVINSListDeletedModel{
ByID: state.ByID,
Name: state.Name,
AccountID: state.AccountID,
RGID: state.RGID,
ExtIP: state.ExtIP,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(vinsList.EntryCount)),
}
items := make([]models.ItemVinsDeletedModel, 0, len(vinsList.Data))
for _, item := range vinsList.Data {
v := models.ItemVinsDeletedModel{
AccountID: types.Int64Value(int64(item.AccountID)),
AccountName: types.StringValue(item.AccountName),
CreatedBy: types.StringValue(item.CreatedBy),
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
DefaultGW: types.StringValue(item.DefaultGW),
DefaultQOS: flattenQOS(ctx, &item.DefaultQOS),
DeletedBy: types.StringValue(item.DeletedBy),
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
Description: types.StringValue(item.Description),
ExternalIP: types.StringValue(item.ExternalIP),
GID: types.Int64Value(int64(item.GID)),
GUID: types.Int64Value(int64(item.GUID)),
ID: types.Int64Value(int64(item.ID)),
LockStatus: types.StringValue(item.LockStatus),
ManagerID: types.Int64Value(int64(item.ManagerID)),
ManagerType: types.StringValue(item.ManagerType),
Milestones: types.Int64Value(int64(item.Milestones)),
Name: types.StringValue(item.Name),
Netmask: types.Int64Value(int64(item.NetMask)),
Network: types.StringValue(item.Network),
PreReservationsNum: types.Int64Value(int64(item.PreReservationsNum)),
PriVNFDevID: types.Int64Value(int64(item.PriVNFDevID)),
Redundant: types.BoolValue(item.Redundant),
RGID: types.Int64Value(int64(item.RGID)),
RGName: types.StringValue(item.RGName),
SecVNFDefID: types.Int64Value(int64(item.SecVNFDevID)),
Status: types.StringValue(item.Status),
UpdatedBy: types.StringValue(item.UpdatedBy),
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
UserManaged: types.BoolValue(item.UserManaged),
VNFs: flattenListVNFs(ctx, &item.VNFs),
VXLANID: types.Int64Value(int64(item.VXLANID)),
}
items = append(items, v)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSListDeletedDataSource")
return nil
}

View File

@@ -0,0 +1,58 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSNATRuleListDataSource flattens data source for vins nat rule list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSNATRuleListDataSource(ctx context.Context, state *models.DataSourceVINSNATRuleListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSNATRuleListDataSource")
diags := diag.Diagnostics{}
natRulesList, diags := utilities.VINSNATRuleListDataSourceCheckPresence(ctx, state, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSNATRuleListDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceVINSNATRuleListModel{
VinsID: state.VinsID,
Reason: state.Reason,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(natRulesList.EntryCount)),
}
items := make([]models.ItemVINSNATRuleModel, 0, len(natRulesList.Data))
for _, item := range natRulesList.Data {
v := models.ItemVINSNATRuleModel{
ID: types.Int64Value(int64(item.ID)),
LocalIP: types.StringValue(item.LocalIP),
LocalPort: types.Int64Value(int64(item.LocalPort)),
Protocol: types.StringValue(item.Protocol),
PublicPortEnd: types.Int64Value(int64(item.PublicPortEnd)),
PublicPortStart: types.Int64Value(int64(item.PublicPortStart)),
VMID: types.Int64Value(int64(item.VMID)),
VMName: types.StringValue(item.VMName),
}
items = append(items, v)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSNATRuleListDataSource")
return nil
}

View File

@@ -0,0 +1,51 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSStaticRouteDataSource flattens data source for vins static route.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSStaticRouteDataSource(ctx context.Context, state *models.DataSourceVINSStaticRouteModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSStaticRouteDataSource")
diags := diag.Diagnostics{}
route, diags := utilities.VINSStaticRouteDataSourceCheckPresence(ctx, uint64(state.VinsID.ValueInt64()), uint64(state.RouteID.ValueInt64()), c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSStaticRouteDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceVINSStaticRouteModel{
VinsID: state.VinsID,
RouteID: state.RouteID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Destination: types.StringValue(route.Destination),
Gateway: types.StringValue(route.Gateway),
GUID: types.StringValue(route.GUID),
Netmask: types.StringValue(route.Netmask),
}
state.ComputeIds, diags = types.ListValueFrom(ctx, types.Int64Type, route.ComputeIds)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattens.VINSStaticRouteDataSource: cannot flatten route.ComputeIds to state.ComputeIds", diags))
}
tflog.Info(ctx, "End flattens.VINSStaticRouteDataSource")
return nil
}

View File

@@ -0,0 +1,63 @@
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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSStaticRouteListDataSource flattens data source for vins static route list.
// Return error in case data source is not found on the platform.
// Flatten errors are added to tflog.
func VINSStaticRouteListDataSource(ctx context.Context, state *models.DataSourceVINSStaticRouteListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSStaticRouteListDataSource")
diags := diag.Diagnostics{}
vinsId := uint64(state.VinsID.ValueInt64())
routesList, diags := utilities.VINSStaticRouteListDataSourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSStaticRouteListDataSource: before flatten")
id := uuid.New()
*state = models.DataSourceVINSStaticRouteListModel{
VinsID: state.VinsID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
EntryCount: types.Int64Value(int64(routesList.EntryCount)),
}
items := make([]models.ItemVinsStaticRouteModel, 0, len(routesList.Data))
for _, item := range routesList.Data {
v := models.ItemVinsStaticRouteModel{
Destination: types.StringValue(item.Destination),
Gateway: types.StringValue(item.Gateway),
GUID: types.StringValue(item.GUID),
ID: types.Int64Value(int64(item.ID)),
Netmask: types.StringValue(item.Netmask),
}
v.ComputeIds, diags = types.ListValueFrom(ctx, types.Int64Type, item.ComputeIds)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error flattens.VINSStaticRouteListDataSource: cannot flatten item.ComputeIds to v.ComputeIds", diags))
}
items = append(items, v)
}
state.Items = items
tflog.Info(ctx, "End flattens.VINSStaticRouteListDataSource")
return nil
}

View File

@@ -0,0 +1,161 @@
package flattens
import (
"context"
"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/vins"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSResource flattens resource for vins.
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func VINSResource(ctx context.Context, plan *models.ResourceVINSModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSResource")
diags := diag.Diagnostics{}
vinsId, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
if err != nil {
diags.AddError("Cannot parse vins ID from state", err.Error())
return diags
}
recordVins, diags := utilities.VINSResourceCheckPresence(ctx, vinsId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSResource: before flatten", map[string]any{"vins_id": vinsId})
*plan = models.ResourceVINSModel{
Name: types.StringValue(recordVins.Name),
RGID: plan.RGID,
AccountID: plan.AccountID,
IPCIDR: plan.IPCIDR,
PreReservationsNum: plan.PreReservationsNum,
Description: plan.Description,
GID: plan.GID,
DNS: plan.DNS,
Enable: plan.Enable,
Permanently: plan.Permanently,
Force: plan.Force,
Restore: plan.Restore,
VnfdevRestart: plan.VnfdevRestart,
VnfdevRedeploy: plan.VnfdevRedeploy,
ExtNet: plan.ExtNet,
IP: plan.IP,
NatRule: plan.NatRule,
Timeouts: plan.Timeouts,
VinsID: types.Int64Value(int64(vinsId)),
Id: types.StringValue(strconv.Itoa(int(recordVins.ID))),
LastUpdated: plan.LastUpdated,
VNFDev: flattenVNFDev(ctx, &recordVins.VNFDev),
AccountName: types.StringValue(recordVins.AccountName),
CreatedBy: types.StringValue(recordVins.CreatedBy),
CreatedTime: types.Int64Value(int64(recordVins.CreatedTime)),
DefaultGW: types.StringValue(recordVins.DefaultGW),
DefaultQOS: flattenQOS(ctx, &recordVins.DefaultQOS),
DeletedBy: types.StringValue(recordVins.DeletedBy),
DeletedTime: types.Int64Value(int64(recordVins.DeletedTime)),
GUID: types.Int64Value(int64(recordVins.GUID)),
LockStatus: types.StringValue(recordVins.LockStatus),
ManagerID: types.Int64Value(int64(recordVins.ManagerID)),
ManagerType: types.StringValue(recordVins.ManagerType),
Milestones: types.Int64Value(int64(recordVins.Milestones)),
NetMask: types.Int64Value(int64(recordVins.NetMask)),
Network: types.StringValue(recordVins.Network),
Redundant: types.BoolValue(recordVins.Redundant),
RGName: types.StringValue(recordVins.RGName),
SecVNFDevID: types.Int64Value(int64(recordVins.SecVNFDevID)),
Status: types.StringValue(recordVins.Status),
UpdatedBy: types.StringValue(recordVins.UpdatedBy),
UpdatedTime: types.Int64Value(int64(recordVins.UpdatedTime)),
UserManaged: types.BoolValue(recordVins.UserManaged),
VNFs: flattenVNFs(ctx, &recordVins.VNFs),
VXLANID: types.Int64Value(int64(recordVins.VXLANID)),
}
if plan.RGID.IsUnknown() {
plan.RGID = types.Int64Value(int64(recordVins.RGID))
}
if plan.AccountID.IsUnknown() {
plan.AccountID = types.Int64Value(int64(recordVins.AccountID))
}
if plan.GID.IsUnknown() {
plan.GID = types.Int64Value(int64(recordVins.GID))
}
if plan.PreReservationsNum.IsUnknown() {
plan.PreReservationsNum = types.Int64Value(int64(recordVins.PreReservationsNum))
}
if plan.Description.IsUnknown() {
plan.Description = types.StringValue(recordVins.Description)
}
if plan.DNS.IsNull() {
plan.DNS = types.SetNull(types.StringType)
}
if !plan.NatRule.IsNull() {
plan.NatRule = flattenNatRule(ctx, plan, &recordVins.VNFs.NAT.Config.Rules)
}
tflog.Info(ctx, "flattens.VINSResource: after flatten", map[string]any{"vins_id": plan.Id.ValueString()})
tflog.Info(ctx, "End flattens.VINSResource", map[string]any{"vins_id": plan.Id.ValueString()})
return nil
}
// flattenNatRule flattens nat rule parameters:
// - rule_id (computed),
// - int_port, ext_port_end, proto (optional & computed).
// Flatten errors are added to tflog.
func flattenNatRule(ctx context.Context, plan *models.ResourceVINSModel, rules *vins.ListNATRule) types.List {
tflog.Info(ctx, "Start flattenRuleIdInNatRule")
diags := diag.Diagnostics{}
itemsNatRulePlan := make([]models.NatRuleResourceModel, 0, len(plan.NatRule.Elements()))
diags.Append(plan.NatRule.ElementsAs(ctx, &itemsNatRulePlan, false)...)
if diags.HasError() {
tflog.Error(ctx, "flattenRuleIdInNatRule: cannot populate itemsNatRulePlan with plan.NatRule list elements")
}
for i, natRule := range itemsNatRulePlan {
ruleFromPlatform := natRule.GetNatRule(*rules)
if ruleFromPlatform == nil {
tflog.Error(ctx, fmt.Sprintf("flattenRuleIdInNatRule: rule_id can not be flatten for natRule %v because such nat_rule does not exist", natRule))
continue
}
itemsNatRulePlan[i].RuleID = types.Int64Value(int64(ruleFromPlatform.ID))
if itemsNatRulePlan[i].IntPort.ValueInt64() == 0 {
itemsNatRulePlan[i].IntPort = types.Int64Value(int64(ruleFromPlatform.LocalPort))
}
if itemsNatRulePlan[i].ExtPortEnd.ValueInt64() == 0 {
itemsNatRulePlan[i].ExtPortEnd = types.Int64Value(int64(ruleFromPlatform.PublicPortEnd))
}
if itemsNatRulePlan[i].Proto.ValueString() == "" {
itemsNatRulePlan[i].Proto = types.StringValue(ruleFromPlatform.Protocol)
}
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemNatRuleResource}, itemsNatRulePlan)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenRuleIdInNatRule", err))
}
tflog.Info(ctx, "End flattenRuleIdInNatRule")
return res
}

View File

@@ -0,0 +1,68 @@
package flattens
import (
"context"
"fmt"
"time"
"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/cloudbroker/vins/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/vins/utilities"
)
// VINSStaticRouteResource flattens resource for vins static route.
// Return error in case resource is not found on the platform.
// Flatten errors are added to tflog.
func VINSStaticRouteResource(ctx context.Context, plan *models.ResourceVINSStaticRouteModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start flattens.VINSStaticRouteResource")
vinsId, routeId, diags := utilities.GetVinsIDAndRouteID(ctx, plan)
if diags.HasError() {
return diags
}
recordRoute, diags := utilities.VINSStaticRouteResourceCheckPresence(ctx, vinsId, routeId, c)
if diags.HasError() {
return diags
}
tflog.Info(ctx, "flattens.VINSStaticRouteResource: before flatten", map[string]any{"id": plan.Id.ValueString()})
*plan = models.ResourceVINSStaticRouteModel{
VinsID: types.Int64Value(int64(vinsId)),
Destination: types.StringValue(recordRoute.Destination),
Netmask: types.StringValue(recordRoute.Netmask),
Gateway: types.StringValue(recordRoute.Gateway),
Timeouts: plan.Timeouts,
RouteID: types.Int64Value(int64(routeId)),
ComputeIDs: plan.ComputeIDs,
Id: plan.Id,
LastUpdated: plan.LastUpdated,
GUID: types.StringValue(recordRoute.GUID),
}
if plan.ComputeIDs.IsUnknown() {
plan.ComputeIDs, diags = types.ListValueFrom(ctx, types.Int64Type, recordRoute.ComputeIds)
if diags.HasError() {
tflog.Error(ctx, fmt.Sprint("Error VINSStaticRouteResource: cannot flatten recordRoute.ComputeIds to plan.ComputeIDs", diags))
}
}
if plan.Id.IsUnknown() {
plan.Id = types.StringValue(fmt.Sprintf("%d#%d", vinsId, routeId))
}
if plan.LastUpdated.IsUnknown() {
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
}
tflog.Info(ctx, "flattens.VINSStaticRouteResource: after flatten", map[string]any{"id": plan.Id.ValueString()})
tflog.Info(ctx, "End flattens.VINSStaticRouteResource", map[string]any{"id": plan.Id.ValueString()})
return nil
}