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,77 @@
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/k8ci"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8ciList(ctx context.Context, state *models.K8ciListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8ciList")
diags := diag.Diagnostics{}
k8ciList, err := utilities.K8ciListCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Error get k8ci list info", err.Error())
return diags
}
id := uuid.New()
*state = models.K8ciListModel{
ByID: state.ByID,
Name: state.Name,
Status: state.Status,
WorkerDriver: state.WorkerDriver,
MasterDriver: state.MasterDriver,
NetworkPlugins: state.NetworkPlugins,
IncludeDisabled: state.IncludeDisabled,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Items: flattenK8ciItems(ctx, k8ciList),
EntryCount: types.Int64Value(int64(k8ciList.EntryCount)),
}
tflog.Info(ctx, "End FlattenDataSourceK8ciList")
return nil
}
func flattenK8ciItems(ctx context.Context, data *k8ci.ListK8CI) []models.ItemInListK8ciModel {
tflog.Info(ctx, "Start flattenK8ciItems")
var diags diag.Diagnostics
res := make([]models.ItemInListK8ciModel, 0, len(data.Data))
for _, item := range data.Data {
temp := models.ItemInListK8ciModel{
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
Description: types.StringValue(item.Description),
K8CIID: types.Int64Value(int64(item.ID)),
LBImageID: types.Int64Value(int64(item.LBImageID)),
K8CIName: types.StringValue(item.Name),
Status: types.StringValue(item.Status),
Version: types.StringValue(item.Version),
}
temp.NetworkPlugins, diags = types.ListValueFrom(ctx, types.StringType, item.NetworkPlugins)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flatten NetworkPlugins", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenK8ciItems")
return res
}

View File

@@ -0,0 +1,235 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8s(ctx context.Context, state *models.RecordK8SDataSourceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8s", map[string]any{"k8s_id": state.K8SID.ValueInt64()})
diags := diag.Diagnostics{}
k8SID := uint64(state.K8SID.ValueInt64())
cluster, err := utilities.K8SCPResourceCheckPresence(ctx, k8SID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about cluster with ID %d", k8SID), err.Error())
return diags
}
k8sList, err := utilities.K8sListForResourceCheckPresence(ctx, k8SID, c)
if err != nil || len(k8sList.Data) == 0 {
diags.AddError(fmt.Sprintf("Cluster with ID %d not found in List", k8SID), err.Error())
return diags
}
masterComputeList := make([]*compute.RecordCompute, 0, len(cluster.K8SGroups.Masters.DetailedInfo))
workersComputeList := make([]*compute.RecordCompute, 0, len(cluster.K8SGroups.Workers))
for _, masterNode := range cluster.K8SGroups.Masters.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, masterNode.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about masterNode with ID %d", masterNode.ID), err.Error())
return diags
}
masterComputeList = append(masterComputeList, compute)
}
for _, worker := range cluster.K8SGroups.Workers {
for _, info := range worker.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, info.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about workerNode with ID %d", info.ID), err.Error())
return diags
}
workersComputeList = append(workersComputeList, compute)
}
}
id := uuid.New()
*state = models.RecordK8SDataSourceModel{
K8SID: state.K8SID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
ACL: flattenACLDataSource(ctx, &cluster.ACL),
AccountID: types.Int64Value(int64(cluster.AccountID)),
AccountName: types.StringValue(cluster.AccountName),
BServiceID: types.Int64Value(int64(cluster.BServiceID)),
K8CI: types.Int64Value(int64(cluster.CIID)),
CreatedBy: types.StringValue(cluster.CreatedBy),
CreatedTime: types.Int64Value(int64(cluster.CreatedTime)),
DeletedBy: types.StringValue(cluster.DeletedBy),
DeletedTime: types.Int64Value(int64(cluster.DeletedTime)),
K8CIName: types.StringValue(cluster.K8CIName),
Masters: flattenMasterGroup(ctx, &cluster.K8SGroups.Masters, masterComputeList),
Workers: flattenK8sGroup(ctx, &cluster.K8SGroups.Workers, workersComputeList),
LBID: types.Int64Value(int64(cluster.LBID)),
Name: types.StringValue(cluster.Name),
NetworkPlugin: types.StringValue(cluster.NetworkPlugin),
RGID: types.Int64Value(int64(cluster.RGID)),
RGName: types.StringValue(cluster.RGName),
Status: types.StringValue(cluster.Status),
TechStatus: types.StringValue(cluster.TechStatus),
UpdatedBy: types.StringValue(cluster.UpdatedBy),
UpdatedTime: types.Int64Value(int64(cluster.UpdatedTime)),
VinsId: types.Int64Value(int64(k8sList.Data[0].VINSID)),
}
if cluster.LBID != 0 {
lb, err := c.CloudAPI().LB().Get(ctx, lb.GetRequest{LBID: cluster.LBID})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Cannot get info about LB with ID %d", cluster.LBID)+err.Error())
}
state.ExtNetID = types.Int64Value(int64(lb.ExtNetID))
state.LBIP = types.StringValue(lb.PrimaryNode.FrontendIP)
}
kubeconfig, err := c.CloudAPI().K8S().GetConfig(ctx, k8s.GetConfigRequest{K8SID: k8SID})
if err != nil {
tflog.Error(ctx, "Could not get kubeconfig:"+err.Error())
}
state.Kubeconfig = types.StringValue(kubeconfig)
tflog.Info(ctx, "End FlattenDataSourceK8s", map[string]any{"k8s_id": state.K8SID.ValueInt64()})
return nil
}
func flattenMasterGroup(ctx context.Context, mastersGroup *k8s.MasterGroup, masters []*compute.RecordCompute) *models.MasterGroupDataSourceModel {
tflog.Info(ctx, "Start flattenMasterGroup")
res := models.MasterGroupDataSourceModel{
CPU: types.Int64Value(int64(mastersGroup.CPU)),
DetailedInfo: flattenDetailedInfoDataSource(ctx, &mastersGroup.DetailedInfo, masters),
Disk: types.Int64Value(int64(mastersGroup.Disk)),
MasterGroupId: types.Int64Value(int64(mastersGroup.ID)),
MasterGroupName: types.StringValue(mastersGroup.Name),
Num: types.Int64Value(int64(mastersGroup.Num)),
RAM: types.Int64Value(int64(mastersGroup.RAM)),
}
tflog.Info(ctx, "End flattenMasterGroup")
return &res
}
func flattenK8sGroup(ctx context.Context, k8SGroupList *k8s.ListK8SGroups, workers []*compute.RecordCompute) []models.ItemK8SGroupDataSourceModel {
tflog.Info(ctx, "Start flattenK8sGroup")
res := make([]models.ItemK8SGroupDataSourceModel, 0, len(*k8SGroupList))
var diags diag.Diagnostics
for _, k8sGroup := range *k8SGroupList {
labels := make([]string, 0)
for _, label := range k8sGroup.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
temp := models.ItemK8SGroupDataSourceModel{
CPU: types.Int64Value(int64(k8sGroup.CPU)),
DetailedInfo: flattenDetailedInfoDataSource(ctx, &k8sGroup.DetailedInfo, workers),
Disk: types.Int64Value(int64(k8sGroup.Disk)),
WorkerGroupID: types.Int64Value(int64(k8sGroup.ID)),
WorkerGroupName: types.StringValue(k8sGroup.Name),
Num: types.Int64Value(int64(k8sGroup.Num)),
RAM: types.Int64Value(int64(k8sGroup.RAM)),
}
temp.Annotations, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
temp.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
temp.Taints, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenK8sGroup")
return res
}
func flattenDetailedInfoDataSource(ctx context.Context, di *k8s.ListDetailedInfo, computes []*compute.RecordCompute) []models.ItemDetailedInfoDataSourceModel {
tflog.Info(ctx, "Start flattenDetailedInfoDataSource")
res := make([]models.ItemDetailedInfoDataSourceModel, 0, len(*di))
for i, detailedInfo := range *di {
temp := models.ItemDetailedInfoDataSourceModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
Interfaces: flattenInterfacesDataSource(ctx, &computes[i].Interfaces),
NatableVinsIp: types.StringValue(computes[i].NatableVINSIP),
NatableVinsNetwork: types.StringValue(computes[i].NatableVINSNetwork),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenDetailedInfoDataSource")
return res
}
func flattenInterfacesDataSource(ctx context.Context, interfaces *compute.ListInterfaces) []models.ItemInterfacesDataSourceModel {
tflog.Info(ctx, "Start flattenInterfacesDataSource")
res := make([]models.ItemInterfacesDataSourceModel, 0, len(*interfaces))
for _, interfaceCompute := range *interfaces {
temp := models.ItemInterfacesDataSourceModel{
DefGw: types.StringValue(interfaceCompute.DefGW),
IpAddress: types.StringValue(interfaceCompute.IPAddress),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenInterfacesDataSource")
return res
}
func flattenACLDataSource(ctx context.Context, acl *k8s.RecordACL) *models.RecordACLDataSourceModel {
tflog.Info(ctx, "Start flattenACLDataSource")
res := models.RecordACLDataSourceModel{
AccountACL: flattenACLItemsDataSource(ctx, &acl.AccountACL),
K8SACL: flattenACLItemsDataSource(ctx, &acl.K8SACL),
RGACL: flattenACLItemsDataSource(ctx, &acl.RGACL),
}
tflog.Info(ctx, "End flattenACLDataSource")
return &res
}
func flattenACLItemsDataSource(ctx context.Context, item *k8s.ListACL) []models.ItemACLDataSourceModel {
tflog.Info(ctx, "Start flattenACLItemsDataSource")
res := make([]models.ItemACLDataSourceModel, 0, len(*item))
for _, aclItem := range *item {
temp := models.ItemACLDataSourceModel{
Explicit: types.BoolValue(aclItem.Explicit),
GUID: types.StringValue(aclItem.GUID),
Right: types.StringValue(aclItem.Right),
Status: types.StringValue(aclItem.Status),
Type: types.StringValue(aclItem.Type),
UserGroupID: types.StringValue(aclItem.UserGroupID),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenACLItemsDataSource")
return res
}

View File

@@ -0,0 +1,81 @@
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/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8sComputes(ctx context.Context, state *models.K8SComputesModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sComputes", map[string]any{"k8s_id": state.K8SID.ValueInt64()})
diags := diag.Diagnostics{}
k8SID := uint64(state.K8SID.ValueInt64())
cluster, err := utilities.K8SCPResourceCheckPresence(ctx, k8SID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about cluster with ID %d", k8SID), err.Error())
return diags
}
id := uuid.New()
*state = models.K8SComputesModel{
K8SID: state.K8SID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Masters: flattenMasterComputes(ctx, cluster),
Workers: flattenWorkerComputes(ctx, cluster),
}
tflog.Info(ctx, "End FlattenDataSourceK8sComputes", map[string]any{"k8s_id": state.K8SID.ValueInt64()})
return nil
}
func flattenMasterComputes(ctx context.Context, cluster *k8s.RecordK8S) []models.ItemComputeModel {
tflog.Info(ctx, "Start flattenMasterComputes")
res := make([]models.ItemComputeModel, 0, len(cluster.K8SGroups.Masters.DetailedInfo))
for _, detailedInfo := range cluster.K8SGroups.Masters.DetailedInfo {
temp := models.ItemComputeModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
GroupName: types.StringValue(cluster.K8SGroups.Masters.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenMasterComputes")
return res
}
func flattenWorkerComputes(ctx context.Context, cluster *k8s.RecordK8S) []models.ItemComputeModel {
tflog.Info(ctx, "Start flattenWorkerComputes")
res := make([]models.ItemComputeModel, 0, len(cluster.K8SGroups.Workers))
for _, worker := range cluster.K8SGroups.Workers {
for _, comp := range worker.DetailedInfo {
temp := models.ItemComputeModel{
ComputeId: types.Int64Value(int64(comp.ID)),
Name: types.StringValue(comp.Name),
GroupName: types.StringValue(worker.Name),
Status: types.StringValue(comp.Status),
TechStatus: types.StringValue(comp.TechStatus),
}
res = append(res, temp)
}
}
tflog.Info(ctx, "End flattenWorkerComputes")
return res
}

View File

@@ -0,0 +1,162 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8sList(ctx context.Context, state *models.K8SListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sList")
diags := diag.Diagnostics{}
k8sList, err := utilities.K8sListCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Error get k8s list info", err.Error())
return diags
}
id := uuid.New()
*state = models.K8SListModel{
ByID: state.ByID,
Name: state.Name,
IPAddress: state.IPAddress,
RGID: state.RGID,
LBID: state.LBID,
BasicServiceID: state.BasicServiceID,
Status: state.Status,
TechStatus: state.TechStatus,
IncludeDeleted: state.IncludeDeleted,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Items: flattenItems(ctx, k8sList),
EntryCount: types.Int64Value(int64(k8sList.EntryCount)),
}
tflog.Info(ctx, "End FlattenDataSourceK8sList")
return nil
}
func flattenItems(ctx context.Context, data *k8s.ListK8SClusters) []models.ItemInListK8SModel {
tflog.Info(ctx, "Start flattenItems")
var diags diag.Diagnostics
res := make([]models.ItemInListK8SModel, 0, len(data.Data))
for _, item := range data.Data {
temp := models.ItemInListK8SModel{
AccountID: types.Int64Value(int64(item.AccountID)),
AccountName: types.StringValue(item.AccountName),
BServiceID: types.Int64Value(int64(item.BServiceID)),
CIID: types.Int64Value(int64(item.CIID)),
CreatedBy: types.StringValue(item.CreatedBy),
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
DeletedBy: types.StringValue(item.DeletedBy),
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
Description: types.StringValue(item.Description),
ExtNetID: types.Int64Value(int64(item.ExtNetID)),
GID: types.Int64Value(int64(item.GID)),
GUID: types.Int64Value(int64(item.GUID)),
K8SID: types.Int64Value(int64(item.ID)),
LBID: types.Int64Value(int64(item.LBID)),
Milestones: types.Int64Value(int64(item.Milestones)),
Name: types.StringValue(item.Name),
NetworkPlugin: types.StringValue(item.NetworkPlugin),
RGID: types.Int64Value(int64(item.RGID)),
RGName: types.StringValue(item.RGName),
ServiceAccount: &models.ServiceAccountInListModel{
GUID: types.StringValue(item.ServiceAccount.GUID),
Password: types.StringValue(item.ServiceAccount.Password),
Username: types.StringValue(item.ServiceAccount.Username),
},
Status: types.StringValue(item.Status),
TechStatus: types.StringValue(item.TechStatus),
UpdatedBy: types.StringValue(item.UpdatedBy),
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
VINSID: types.Int64Value(int64(item.VINSID)),
WorkersGroup: flattenWG(ctx, &item.WorkersGroup),
}
temp.ACL, diags = types.ListValueFrom(ctx, types.StringType, item.ACL)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenItems")
return res
}
func flattenWG(ctx context.Context, WG *k8s.ListK8SGroups) []models.ItemWGInListModel {
tflog.Info(ctx, "Start flattenWG")
res := make([]models.ItemWGInListModel, 0, len(*WG))
var diags diag.Diagnostics
for _, k8sGroup := range *WG {
labels := make([]string, 0)
for _, label := range k8sGroup.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
temp := models.ItemWGInListModel{
CPU: types.Int64Value(int64(k8sGroup.CPU)),
DetailedInfo: flattenDetailedInfoList(ctx, &k8sGroup.DetailedInfo),
Disk: types.Int64Value(int64(k8sGroup.Disk)),
GUID: types.StringValue(k8sGroup.GUID),
WorkerGroupID: types.Int64Value(int64(k8sGroup.ID)),
WorkerGroupName: types.StringValue(k8sGroup.Name),
Num: types.Int64Value(int64(k8sGroup.Num)),
RAM: types.Int64Value(int64(k8sGroup.RAM)),
}
temp.Annotations, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
temp.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
temp.Taints, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenWG")
return res
}
func flattenDetailedInfoList(ctx context.Context, di *k8s.ListDetailedInfo) []models.ItemDetailedInfoInListModel {
tflog.Info(ctx, "Start flattenDetailedInfoList")
res := make([]models.ItemDetailedInfoInListModel, 0, len(*di))
for _, info := range *di {
temp := models.ItemDetailedInfoInListModel{
Externalip: types.StringValue(info.Externalip),
ID: types.Int64Value(int64(info.ID)),
Name: types.StringValue(info.Name),
Status: types.StringValue(info.Status),
TechStatus: types.StringValue(info.TechStatus),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenDetailedInfoList")
return res
}

View File

@@ -0,0 +1,160 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8sListDeleted(ctx context.Context, state *models.K8SListDeletedModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sListDeleted")
diags := diag.Diagnostics{}
k8sList, err := utilities.K8sListDeletedCheckPresence(ctx, state, c)
if err != nil {
diags.AddError("Error get k8s list info", err.Error())
return diags
}
id := uuid.New()
*state = models.K8SListDeletedModel{
ByID: state.ByID,
Name: state.Name,
IPAddress: state.IPAddress,
RGID: state.RGID,
LBID: state.LBID,
BasicServiceID: state.BasicServiceID,
TechStatus: state.TechStatus,
SortBy: state.SortBy,
Page: state.Page,
Size: state.Size,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Items: flattenItemsDeleted(ctx, k8sList),
EntryCount: types.Int64Value(int64(k8sList.EntryCount)),
}
tflog.Info(ctx, "End FlattenDataSourceK8sListDeleted")
return nil
}
func flattenItemsDeleted(ctx context.Context, data *k8s.ListK8SClusters) []models.ItemInListDeletedK8SModel {
tflog.Info(ctx, "Start flattenItems")
var diags diag.Diagnostics
res := make([]models.ItemInListDeletedK8SModel, 0, len(data.Data))
for _, item := range data.Data {
temp := models.ItemInListDeletedK8SModel{
AccountID: types.Int64Value(int64(item.AccountID)),
AccountName: types.StringValue(item.AccountName),
BServiceID: types.Int64Value(int64(item.BServiceID)),
CIID: types.Int64Value(int64(item.CIID)),
CreatedBy: types.StringValue(item.CreatedBy),
CreatedTime: types.Int64Value(int64(item.CreatedTime)),
DeletedBy: types.StringValue(item.DeletedBy),
DeletedTime: types.Int64Value(int64(item.DeletedTime)),
Description: types.StringValue(item.Description),
ExtNetID: types.Int64Value(int64(item.ExtNetID)),
GID: types.Int64Value(int64(item.GID)),
GUID: types.Int64Value(int64(item.GUID)),
K8SID: types.Int64Value(int64(item.ID)),
LBID: types.Int64Value(int64(item.LBID)),
Milestones: types.Int64Value(int64(item.Milestones)),
Name: types.StringValue(item.Name),
NetworkPlugin: types.StringValue(item.NetworkPlugin),
RGID: types.Int64Value(int64(item.RGID)),
RGName: types.StringValue(item.RGName),
ServiceAccount: &models.ServiceAccountInListDeletedModel{
GUID: types.StringValue(item.ServiceAccount.GUID),
Password: types.StringValue(item.ServiceAccount.Password),
Username: types.StringValue(item.ServiceAccount.Username),
},
Status: types.StringValue(item.Status),
TechStatus: types.StringValue(item.TechStatus),
UpdatedBy: types.StringValue(item.UpdatedBy),
UpdatedTime: types.Int64Value(int64(item.UpdatedTime)),
VINSID: types.Int64Value(int64(item.VINSID)),
WorkersGroup: flattenWGDeleted(ctx, &item.WorkersGroup),
}
temp.ACL, diags = types.ListValueFrom(ctx, types.StringType, item.ACL)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenItems")
return res
}
func flattenWGDeleted(ctx context.Context, WG *k8s.ListK8SGroups) []models.ItemWGInListDeletedModel {
tflog.Info(ctx, "Start flattenWG")
res := make([]models.ItemWGInListDeletedModel, 0, len(*WG))
var diags diag.Diagnostics
for _, k8sGroup := range *WG {
labels := make([]string, 0)
for _, label := range k8sGroup.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
temp := models.ItemWGInListDeletedModel{
CPU: types.Int64Value(int64(k8sGroup.CPU)),
DetailedInfo: flattenDetailedInfoListDeleted(ctx, &k8sGroup.DetailedInfo),
Disk: types.Int64Value(int64(k8sGroup.Disk)),
GUID: types.StringValue(k8sGroup.GUID),
WorkerGroupID: types.Int64Value(int64(k8sGroup.ID)),
WorkerGroupName: types.StringValue(k8sGroup.Name),
Num: types.Int64Value(int64(k8sGroup.Num)),
RAM: types.Int64Value(int64(k8sGroup.RAM)),
}
temp.Annotations, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
temp.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
temp.Taints, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenWG")
return res
}
func flattenDetailedInfoListDeleted(ctx context.Context, di *k8s.ListDetailedInfo) []models.ItemDetailedInfoInListDeletedModel {
tflog.Info(ctx, "Start flattenDetailedInfoList")
res := make([]models.ItemDetailedInfoInListDeletedModel, 0, len(*di))
for _, info := range *di {
temp := models.ItemDetailedInfoInListDeletedModel{
Externalip: types.StringValue(info.Externalip),
ID: types.Int64Value(int64(info.ID)),
Name: types.StringValue(info.Name),
Status: types.StringValue(info.Status),
TechStatus: types.StringValue(info.TechStatus),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenDetailedInfoList")
return res
}

View File

@@ -0,0 +1,128 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8sWg(ctx context.Context, state *models.K8SWgModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sWg", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WorkerGroupID.ValueInt64()})
diags := diag.Diagnostics{}
k8SID := uint64(state.K8SID.ValueInt64())
wgID := uint64(state.WorkerGroupID.ValueInt64())
cluster, err := utilities.K8SCPResourceCheckPresence(ctx, k8SID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about cluster with ID %v", k8SID), err.Error())
return diags
}
curWg := k8s.ItemK8SGroup{}
for _, wg := range cluster.K8SGroups.Workers {
if wg.ID == wgID {
curWg = wg
break
}
}
if curWg.ID == 0 {
diags.AddError(fmt.Sprintf("WG with id %v in k8s cluster %v not found", wgID, k8SID), err.Error())
return diags
}
workersComputeList := make([]*compute.RecordCompute, 0, len(curWg.DetailedInfo))
for _, info := range curWg.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, info.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about workerNode with ID %v", info.ID), err.Error())
return diags
}
workersComputeList = append(workersComputeList, compute)
}
id := uuid.New()
*state = models.K8SWgModel{
K8SID: state.K8SID,
Timeouts: state.Timeouts,
WorkerGroupID: state.WorkerGroupID,
Id: types.StringValue(id.String()),
CPU: types.Int64Value(int64(curWg.CPU)),
DetailedInfo: flattenDetailedInfoWg(ctx, &curWg.DetailedInfo, workersComputeList),
Disk: types.Int64Value(int64(curWg.Disk)),
GUID: types.StringValue(curWg.GUID),
WorkerGroupName: types.StringValue(curWg.Name),
Num: types.Int64Value(int64(curWg.Num)),
RAM: types.Int64Value(int64(curWg.RAM)),
}
labels := make([]string, 0)
for _, label := range curWg.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
state.Annotations, diags = types.ListValueFrom(ctx, types.StringType, curWg.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
state.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
state.Taints, diags = types.ListValueFrom(ctx, types.StringType, curWg.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
tflog.Info(ctx, "End FlattenDataSourceK8sWg", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WorkerGroupID.ValueInt64()})
return nil
}
func flattenDetailedInfoWg(ctx context.Context, di *k8s.ListDetailedInfo, computes []*compute.RecordCompute) []models.ItemDetailedInfoInWgModel {
tflog.Info(ctx, "Start flattenDetailedInfoWg")
res := make([]models.ItemDetailedInfoInWgModel, 0, len(*di))
for i, detailedInfo := range *di {
temp := models.ItemDetailedInfoInWgModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
Interfaces: flattenInterfacesWg(ctx, &computes[i].Interfaces),
NatableVinsIp: types.StringValue(computes[i].NatableVINSIP),
NatableVinsNetwork: types.StringValue(computes[i].NatableVINSNetwork),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenDetailedInfoWg")
return res
}
func flattenInterfacesWg(ctx context.Context, interfaces *compute.ListInterfaces) []models.ItemInterfacesInWgModel {
tflog.Info(ctx, "Start flattenInterfacesWg")
res := make([]models.ItemInterfacesInWgModel, 0, len(*interfaces))
for _, interfaceCompute := range *interfaces {
temp := models.ItemInterfacesInWgModel{
DefGw: types.StringValue(interfaceCompute.DefGW),
IpAddress: types.StringValue(interfaceCompute.IPAddress),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenInterfacesWg")
return res
}

View File

@@ -0,0 +1,38 @@
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/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
)
func DataSourceK8sWgCloudInit(ctx context.Context, state *models.RecordK8SWgCloudInitDataSourceModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sWgCloudInit", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WgId.ValueInt64()})
diags := diag.Diagnostics{}
metaData, err := c.CloudAPI().K8S().GetWorkerNodesMetaData(ctx, k8s.GetWorkerNodesMetaDataRequest{K8SID: uint64(state.K8SID.ValueInt64()), WorkersGroupID: uint64(state.WgId.ValueInt64())})
if err != nil {
diags.AddError("Cannot get info about wg cloud init with error: ", err.Error())
return diags
}
id := uuid.New()
*state = models.RecordK8SWgCloudInitDataSourceModel{
K8SID: state.K8SID,
WgId: state.WgId,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
CloudInit: types.StringValue(metaData),
}
tflog.Info(ctx, "End FlattenDataSourceK8sWgCloudInit", map[string]any{"k8s_id": state.K8SID.ValueInt64(), "worker_group_id": state.WgId.ValueInt64()})
return nil
}

View File

@@ -0,0 +1,135 @@
package flattens
import (
"context"
"fmt"
"strings"
"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/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func DataSourceK8sWgList(ctx context.Context, state *models.K8SWgListModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenDataSourceK8sWgList")
diags := diag.Diagnostics{}
k8SID := uint64(state.K8SID.ValueInt64())
cluster, err := utilities.K8SCPResourceCheckPresence(ctx, k8SID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about cluster with ID %d", k8SID), err.Error())
return diags
}
workersComputeList := make(map[uint64][]*compute.RecordCompute)
for _, worker := range cluster.K8SGroups.Workers {
workersComputeList[worker.ID] = make([]*compute.RecordCompute, 0, len(worker.DetailedInfo))
for _, info := range worker.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, info.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about workerNode with ID %d", info.ID), err.Error())
return diags
}
workersComputeList[worker.ID] = append(workersComputeList[worker.ID], compute)
}
}
id := uuid.New()
*state = models.K8SWgListModel{
K8SID: state.K8SID,
Timeouts: state.Timeouts,
Id: types.StringValue(id.String()),
Items: flattenK8sWg(ctx, &cluster.K8SGroups.Workers, workersComputeList),
}
tflog.Info(ctx, "End FlattenDataSourceK8sWgList")
return nil
}
func flattenK8sWg(ctx context.Context, k8SGroupList *k8s.ListK8SGroups, workers map[uint64][]*compute.RecordCompute) []models.ItemWgListModel {
tflog.Info(ctx, "Start flattenK8sWg")
res := make([]models.ItemWgListModel, 0, len(*k8SGroupList))
var diags diag.Diagnostics
for _, k8sGroup := range *k8SGroupList {
labels := make([]string, 0)
for _, label := range k8sGroup.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
computes := workers[k8sGroup.ID]
temp := models.ItemWgListModel{
CPU: types.Int64Value(int64(k8sGroup.CPU)),
DetailedInfo: flattenDetailedInfoWgList(ctx, &k8sGroup.DetailedInfo, computes),
Disk: types.Int64Value(int64(k8sGroup.Disk)),
WorkerGroupID: types.Int64Value(int64(k8sGroup.ID)),
WorkerGroupName: types.StringValue(k8sGroup.Name),
Num: types.Int64Value(int64(k8sGroup.Num)),
RAM: types.Int64Value(int64(k8sGroup.RAM)),
}
temp.Annotations, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
temp.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
temp.Taints, diags = types.ListValueFrom(ctx, types.StringType, k8sGroup.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenK8sWg")
return res
}
func flattenDetailedInfoWgList(ctx context.Context, di *k8s.ListDetailedInfo, computes []*compute.RecordCompute) []models.ItemDetailedInfoDataSourceModel {
tflog.Info(ctx, "Start flattenDetailedInfoWgList")
res := make([]models.ItemDetailedInfoDataSourceModel, 0, len(*di))
for i, detailedInfo := range *di {
temp := models.ItemDetailedInfoDataSourceModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
Interfaces: flattenInterfacesWgList(ctx, &computes[i].Interfaces),
NatableVinsIp: types.StringValue(computes[i].NatableVINSIP),
NatableVinsNetwork: types.StringValue(computes[i].NatableVINSNetwork),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenDetailedInfoWgList")
return res
}
func flattenInterfacesWgList(ctx context.Context, interfaces *compute.ListInterfaces) []models.ItemInterfacesDataSourceModel {
tflog.Info(ctx, "Start flattenInterfacesWgList")
res := make([]models.ItemInterfacesDataSourceModel, 0, len(*interfaces))
for _, interfaceCompute := range *interfaces {
temp := models.ItemInterfacesDataSourceModel{
DefGw: types.StringValue(interfaceCompute.DefGW),
IpAddress: types.StringValue(interfaceCompute.IPAddress),
}
res = append(res, temp)
}
tflog.Info(ctx, "End flattenInterfacesWgList")
return res
}

View File

@@ -0,0 +1,231 @@
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/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/lb"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func K8SCPResource(ctx context.Context, plan *models.ResourceK8SCPModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenK8SCPResource", map[string]any{"k8s_id": plan.Id.ValueString()})
diags := diag.Diagnostics{}
k8SID, err := strconv.ParseUint(plan.Id.ValueString(), 10, 64)
if err != nil {
diags.AddError("Cannot parsed ID cluster from state", err.Error())
return diags
}
cluster, err := utilities.K8SCPResourceCheckPresence(ctx, k8SID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about cluster with ID %d", k8SID), err.Error())
return diags
}
k8sList, err := utilities.K8sListForResourceCheckPresence(ctx, k8SID, c)
if err != nil || len(k8sList.Data) == 0 {
diags.AddError(fmt.Sprintf("Cluster with ID %v not found in List", k8SID), err.Error())
return diags
}
masterComputeList := make([]*compute.RecordCompute, 0, len(cluster.K8SGroups.Masters.DetailedInfo))
for _, masterNode := range cluster.K8SGroups.Masters.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, masterNode.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about masterNode with ID %d", masterNode.ID), err.Error())
return diags
}
masterComputeList = append(masterComputeList, compute)
}
*plan = models.ResourceK8SCPModel{
Name: types.StringValue(cluster.Name),
RGID: types.Int64Value(int64(cluster.RGID)),
K8SCIID: types.Int64Value(int64(cluster.CIID)),
NetworkPlugin: types.StringValue(cluster.NetworkPlugin),
SEPID: plan.SEPID,
SEPPool: plan.SEPPool,
WithLB: plan.WithLB,
HighlyAvailable: plan.HighlyAvailable,
AdditionalSANs: plan.AdditionalSANs,
InitConfiguration: plan.InitConfiguration,
ClusterConfiguration: plan.ClusterConfiguration,
KubeletConfiguration: plan.KubeletConfiguration,
KubeProxyConfiguration: plan.KubeProxyConfiguration,
JoinConfiguration: plan.JoinConfiguration,
Description: plan.Description,
ExtNetOnly: plan.ExtNetOnly,
OidcCertificate: plan.OidcCertificate,
Start: plan.Start,
Enabled: plan.Enabled,
Permanently: plan.Permanently,
Restore: plan.Restore,
Timeouts: plan.Timeouts,
LBSysctlParams: plan.LBSysctlParams,
Id: types.StringValue(strconv.Itoa(int(cluster.ID))),
LastUpdated: plan.LastUpdated,
DetailedInfo: flattenDetailedInfo(ctx, &cluster.K8SGroups.Masters.DetailedInfo, masterComputeList),
MasterGroupId: types.Int64Value(int64(cluster.K8SGroups.Masters.ID)),
MasterGroupName: types.StringValue(cluster.K8SGroups.Masters.Name),
Num: types.Int64Value(int64(cluster.K8SGroups.Masters.Num)),
CPU: types.Int64Value(int64(cluster.K8SGroups.Masters.CPU)),
RAM: types.Int64Value(int64(cluster.K8SGroups.Masters.RAM)),
Disk: types.Int64Value(int64(cluster.K8SGroups.Masters.Disk)),
ACL: flattenACL(ctx, &cluster.ACL),
AccountID: types.Int64Value(int64(cluster.AccountID)),
AccountName: types.StringValue(cluster.AccountName),
BServiceID: types.Int64Value(int64(cluster.BServiceID)),
CreatedBy: types.StringValue(cluster.CreatedBy),
CreatedTime: types.Int64Value(int64(cluster.CreatedTime)),
DeletedBy: types.StringValue(cluster.DeletedBy),
DeletedTime: types.Int64Value(int64(cluster.DeletedTime)),
K8SID: types.Int64Value(int64(cluster.ID)),
K8CIName: types.StringValue(cluster.K8CIName),
LBID: types.Int64Value(int64(cluster.LBID)),
RGName: types.StringValue(cluster.RGName),
Status: types.StringValue(cluster.Status),
TechStatus: types.StringValue(cluster.TechStatus),
UpdatedBy: types.StringValue(cluster.UpdatedBy),
UpdatedTime: types.Int64Value(int64(cluster.UpdatedTime)),
VinsId: types.Int64Value(int64(k8sList.Data[0].VINSID)),
}
if plan.AdditionalSANs.IsNull() {
plan.AdditionalSANs = types.ListNull(types.StringType)
}
if cluster.LBID != 0 {
lb, err := c.CloudAPI().LB().Get(ctx, lb.GetRequest{LBID: cluster.LBID})
if err != nil {
tflog.Error(ctx, fmt.Sprintf("Cannot get info about LB with ID %d", cluster.LBID)+err.Error())
}
plan.ExtNetID = types.Int64Value(int64(lb.ExtNetID))
plan.LBIP = types.StringValue(lb.PrimaryNode.FrontendIP)
} else {
plan.ExtNetID = types.Int64Value(0)
}
kubeconfig, err := c.CloudAPI().K8S().GetConfig(ctx, k8s.GetConfigRequest{K8SID: k8SID})
if err != nil {
tflog.Error(ctx, "Could not get kubeconfig:"+err.Error())
}
plan.Kubeconfig = types.StringValue(kubeconfig)
tflog.Info(ctx, "End FlattenK8SCPResource", map[string]any{"k8s_id": plan.K8SID.ValueInt64()})
return nil
}
func flattenDetailedInfo(ctx context.Context, di *k8s.ListDetailedInfo, computes []*compute.RecordCompute) types.List {
tflog.Info(ctx, "Start flattenDetailedInfo")
tempSlice := make([]types.Object, 0, len(*di))
for i, detailedInfo := range *di {
temp := models.ItemDetailedInfoModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
Interfaces: flattenInterfaces(ctx, &computes[i].Interfaces),
NatableVinsIp: types.StringValue(computes[i].NatableVINSIP),
NatableVinsNetwork: types.StringValue(computes[i].NatableVINSNetwork),
}
obj, err := types.ObjectValueFrom(ctx, models.ItemDetailedInfo, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDetailedInfo struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemDetailedInfo}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDetailedInfo", err))
}
tflog.Info(ctx, "End flattenDetailedInfo")
return res
}
func flattenInterfaces(ctx context.Context, interfaces *compute.ListInterfaces) types.List {
tflog.Info(ctx, "Start flattenInterfaces")
tempSlice := make([]types.Object, 0, len(*interfaces))
for _, interfaceCompute := range *interfaces {
temp := models.ItemInterfacesModel{
DefGw: types.StringValue(interfaceCompute.DefGW),
IpAddress: types.StringValue(interfaceCompute.IPAddress),
}
obj, err := types.ObjectValueFrom(ctx, models.ItemInterfaces, 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.ItemInterfaces}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenInterfaces", err))
}
tflog.Info(ctx, "End flattenInterfaces")
return res
}
func flattenACL(ctx context.Context, acl *k8s.RecordACL) types.Object {
tflog.Info(ctx, "Start flattenACL")
temp := models.RecordACLModel{
AccountACL: flattenACLItems(ctx, &acl.AccountACL),
K8SACL: flattenACLItems(ctx, &acl.K8SACL),
RGACL: flattenACLItems(ctx, &acl.RGACL),
}
res, err := types.ObjectValueFrom(ctx, models.ListACL, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACL struct to obj", err))
}
tflog.Info(ctx, "End flattenACL")
return res
}
func flattenACLItems(ctx context.Context, item *k8s.ListACL) types.List {
tflog.Info(ctx, "Start flattenACLItems")
tempSlice := make([]types.Object, 0, len(*item))
for _, aclItem := range *item {
temp := models.ItemACLModel{
Explicit: types.BoolValue(aclItem.Explicit),
GUID: types.StringValue(aclItem.GUID),
Right: types.StringValue(aclItem.Right),
Status: types.StringValue(aclItem.Status),
Type: types.StringValue(aclItem.Type),
UserGroupID: types.StringValue(aclItem.UserGroupID),
}
obj, err := types.ObjectValueFrom(ctx, models.ItemAcl, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACLItems struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemAcl}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenACLItems", err))
}
tflog.Info(ctx, "End flattenACLItems")
return res
}

View File

@@ -0,0 +1,138 @@
package flattens
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/compute"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi/k8s"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/models"
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudapi/k8s/utilities"
)
func K8SWGResource(ctx context.Context, plan *models.ResourceK8SWGModel, c *decort.DecortClient) diag.Diagnostics {
tflog.Info(ctx, "Start FlattenK8SWGResource", map[string]any{"wg_id": plan.Id.ValueString()})
diags := diag.Diagnostics{}
wg, k8sId, err := utilities.K8SWGResourceCheckPresence(ctx, plan, c)
if err != nil {
diags.AddError("Cannot get info about wg ", err.Error())
return diags
}
workersComputeList := make([]*compute.RecordCompute, 0, len(wg.DetailedInfo))
for _, info := range wg.DetailedInfo {
compute, err := utilities.ComputeCheckPresence(ctx, info.ID, c)
if err != nil {
diags.AddError(fmt.Sprintf("Cannot get info about workerNode with ID %d", info.ID), err.Error())
return diags
}
workersComputeList = append(workersComputeList, compute)
}
*plan = models.ResourceK8SWGModel{
K8SID: types.Int64Value(int64(k8sId)),
Name: types.StringValue(wg.Name),
Num: types.Int64Value(int64(wg.Num)),
CPU: types.Int64Value(int64(wg.CPU)),
RAM: types.Int64Value(int64(wg.RAM)),
Disk: types.Int64Value(int64(wg.Disk)),
WorkerSEPID: plan.WorkerSEPID,
WorkerSEPPool: plan.WorkerSEPPool,
CloudInit: plan.CloudInit,
Timeouts: plan.Timeouts,
Id: types.StringValue(strconv.Itoa(int(wg.ID))),
WorkerGroupId: types.Int64Value(int64(wg.ID)),
LastUpdated: plan.LastUpdated,
DetailedInfo: flattenDetailedInfoInK8sWG(ctx, &wg.DetailedInfo, workersComputeList),
GUID: types.StringValue(wg.GUID),
}
labels := make([]string, 0)
for _, label := range wg.Labels {
if strings.HasPrefix(label, "workersGroupName") {
continue
}
labels = append(labels, label)
}
plan.Annotations, diags = types.ListValueFrom(ctx, types.StringType, wg.Annotations)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenAnnotations", diags))
}
plan.Labels, diags = types.ListValueFrom(ctx, types.StringType, labels)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenLabels", diags))
}
plan.Taints, diags = types.ListValueFrom(ctx, types.StringType, wg.Taints)
if diags != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenTaints", diags))
}
tflog.Info(ctx, "End FlattenK8SWGResource", map[string]any{"wg_id": plan.Id.ValueString()})
return nil
}
func flattenDetailedInfoInK8sWG(ctx context.Context, di *k8s.ListDetailedInfo, computes []*compute.RecordCompute) types.List {
tflog.Info(ctx, "Start flattenDetailedInfoInK8sWG")
tempSlice := make([]types.Object, 0, len(*di))
for i, detailedInfo := range *di {
temp := models.ItemDetailedInfoModel{
ComputeId: types.Int64Value(int64(detailedInfo.ID)),
Name: types.StringValue(detailedInfo.Name),
Status: types.StringValue(detailedInfo.Status),
TechStatus: types.StringValue(detailedInfo.TechStatus),
Interfaces: flattenInterfacesInK8sWG(ctx, &computes[i].Interfaces),
NatableVinsIp: types.StringValue(computes[i].NatableVINSIP),
NatableVinsNetwork: types.StringValue(computes[i].NatableVINSNetwork),
}
obj, err := types.ObjectValueFrom(ctx, models.ItemDetailedInfo, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDetailedInfoInK8sWG struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemDetailedInfo}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenDetailedInfoInK8sWG", err))
}
tflog.Info(ctx, "End flattenDetailedInfoInK8sWG")
return res
}
func flattenInterfacesInK8sWG(ctx context.Context, interfaces *compute.ListInterfaces) types.List {
tflog.Info(ctx, "Start flattenInterfacesInK8sWG")
tempSlice := make([]types.Object, 0, len(*interfaces))
for _, interfaceCompute := range *interfaces {
temp := models.ItemInterfacesModel{
DefGw: types.StringValue(interfaceCompute.DefGW),
IpAddress: types.StringValue(interfaceCompute.IPAddress),
}
obj, err := types.ObjectValueFrom(ctx, models.ItemInterfaces, temp)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenInterfacesInK8sWG struct to obj", err))
}
tempSlice = append(tempSlice, obj)
}
res, err := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: models.ItemInterfaces}, tempSlice)
if err != nil {
tflog.Error(ctx, fmt.Sprint("Error flattenInterfacesInK8sWG", err))
}
tflog.Info(ctx, "End flattenInterfacesInK8sWG")
return res
}