1.0.1
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
)
|
||||
|
||||
func LBDataSourceCheckPresence(ctx context.Context, lbId uint64, c *decort.DecortClient) (*lb.RecordLB, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBDataSourceCheckPresence: Get info about lb with ID - %v", lbId))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
recordLB, err := c.CloudBroker().LB().Get(ctx, lb.GetRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbId), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "LBDataSourceCheckPresence: response from CloudBroker().LB().Get", map[string]any{"lb_id": lbId, "response": recordLB})
|
||||
|
||||
return recordLB, nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func LBListDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceLBList, c *decort.DecortClient) (*lb.ListLB, error) {
|
||||
|
||||
listLBReq := lb.ListRequest{}
|
||||
|
||||
if !plan.ByID.IsNull() {
|
||||
listLBReq.ByID = uint64(plan.ByID.ValueInt64())
|
||||
}
|
||||
if !plan.Name.IsNull() {
|
||||
listLBReq.Name = plan.Name.ValueString()
|
||||
}
|
||||
if !plan.AccountID.IsNull() {
|
||||
listLBReq.AccountID = uint64(plan.AccountID.ValueInt64())
|
||||
}
|
||||
if !plan.RgID.IsNull() {
|
||||
listLBReq.RGID = uint64(plan.RgID.ValueInt64())
|
||||
}
|
||||
if !plan.TechStatus.IsNull() {
|
||||
listLBReq.TechStatus = plan.TechStatus.ValueString()
|
||||
}
|
||||
if !plan.Status.IsNull() {
|
||||
listLBReq.Status = plan.Status.ValueString()
|
||||
}
|
||||
if !plan.FrontIP.IsNull() {
|
||||
listLBReq.FrontIP = plan.FrontIP.ValueString()
|
||||
}
|
||||
if !plan.BackIP.IsNull() {
|
||||
listLBReq.BackIP = plan.BackIP.ValueString()
|
||||
}
|
||||
if plan.Status.IsNull() && !plan.IncludeDeleted.IsNull() {
|
||||
listLBReq.IncludeDeleted = plan.IncludeDeleted.ValueBool()
|
||||
}
|
||||
if !plan.SortBy.IsNull() {
|
||||
listLBReq.SortBy = plan.SortBy.ValueString()
|
||||
}
|
||||
if !plan.Page.IsNull() {
|
||||
listLBReq.Page = uint64(plan.Page.ValueInt64())
|
||||
}
|
||||
if !plan.Size.IsNull() {
|
||||
listLBReq.Size = uint64(plan.Size.ValueInt64())
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "LBListDataSourceCheckPresence: before call CloudBroker().LB().List", map[string]any{"response": listLBReq})
|
||||
lbList, err := c.CloudBroker().LB().List(ctx, listLBReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get info about data source list lb with error: %w", err)
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "LBListDataSourceCheckPresence: response from CloudBroker().LB().List", map[string]any{"response": lbList})
|
||||
|
||||
return lbList, err
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func LBListDeletedDataSourceCheckPresence(ctx context.Context, plan *models.DataSourceLBListDeleted, c *decort.DecortClient) (*lb.ListLB, error) {
|
||||
|
||||
req := lb.ListDeletedRequest{}
|
||||
|
||||
if !plan.ByID.IsNull() {
|
||||
req.ByID = uint64(plan.ByID.ValueInt64())
|
||||
}
|
||||
if !plan.Name.IsNull() {
|
||||
req.Name = plan.Name.ValueString()
|
||||
}
|
||||
if !plan.AccountID.IsNull() {
|
||||
req.AccountID = uint64(plan.AccountID.ValueInt64())
|
||||
}
|
||||
if !plan.RgID.IsNull() {
|
||||
req.RGID = uint64(plan.RgID.ValueInt64())
|
||||
}
|
||||
if !plan.TechStatus.IsNull() {
|
||||
req.TechStatus = plan.TechStatus.ValueString()
|
||||
}
|
||||
if !plan.FrontIP.IsNull() {
|
||||
req.FrontIP = plan.FrontIP.ValueString()
|
||||
}
|
||||
if !plan.BackIP.IsNull() {
|
||||
req.BackIP = plan.BackIP.ValueString()
|
||||
}
|
||||
if !plan.SortBy.IsNull() {
|
||||
req.SortBy = plan.SortBy.ValueString()
|
||||
}
|
||||
if !plan.Page.IsNull() {
|
||||
req.Page = uint64(plan.Page.ValueInt64())
|
||||
}
|
||||
if !plan.Size.IsNull() {
|
||||
req.Size = uint64(plan.Size.ValueInt64())
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "LBListDeletedDataSourceCheckPresence: before call CloudBroker().LB().ListDeleted", map[string]any{"response": req})
|
||||
lbDelList, err := c.CloudBroker().LB().ListDeleted(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get info about data source list lb with error: %w", err)
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "LBListDeletedDataSourceCheckPresence: response from CloudBroker().LB().ListDeleted", map[string]any{"response": lbDelList})
|
||||
|
||||
return lbDelList, err
|
||||
}
|
||||
352
internal/service/cloudbroker/lb/utilities/utility_resource_lb.go
Normal file
352
internal/service/cloudbroker/lb/utilities/utility_resource_lb.go
Normal file
@@ -0,0 +1,352 @@
|
||||
package utilities
|
||||
|
||||
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/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/status"
|
||||
)
|
||||
|
||||
func CreateResourceLB(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) (uint64, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("Start create ResourceLB: name %s", plan.Name.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
createReq := lb.CreateRequest{
|
||||
Name: plan.Name.ValueString(),
|
||||
RGID: uint64(plan.RGID.ValueInt64()),
|
||||
ExtNetID: uint64(plan.ExtNetID.ValueInt64()),
|
||||
VINSID: uint64(plan.VINSID.ValueInt64()),
|
||||
Start: plan.Start.ValueBool(),
|
||||
}
|
||||
|
||||
if plan.HAMode.IsUnknown() { // HAMode is optional & computed
|
||||
createReq.HighlyAvailable = false
|
||||
} else {
|
||||
createReq.HighlyAvailable = plan.HAMode.ValueBool()
|
||||
}
|
||||
|
||||
if !plan.Description.IsNull() { // Description is optional & computed
|
||||
createReq.Description = plan.Description.ValueString()
|
||||
}
|
||||
|
||||
if !plan.SysctlParams.IsNull() {
|
||||
result := make([]map[string]interface{}, 0, len(plan.SysctlParams.Elements()))
|
||||
for _, val := range plan.SysctlParams.Elements() {
|
||||
objVal := val.(types.Object)
|
||||
valMap := objVal.Attributes()
|
||||
mapKey := valMap["key"].(types.String).ValueString()
|
||||
mapVal := valMap["value"].(types.String).ValueString()
|
||||
tempMap := make(map[string]interface{})
|
||||
tempMap[mapKey] = mapVal
|
||||
result = append(result, tempMap)
|
||||
}
|
||||
createReq.SysctlParams = result
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "CreateResourceLB: before call CloudBroker().LB().Create", map[string]any{"req": createReq})
|
||||
|
||||
lbId, err := c.CloudBroker().LB().Create(ctx, createReq)
|
||||
if err != nil {
|
||||
diags.AddError("CreateResourceLB: unable to create LB", err.Error())
|
||||
return 0, diags
|
||||
}
|
||||
tflog.Info(ctx, "CreateResourceLB: LB created", map[string]any{"lb_id": lbId, "name": plan.Name.ValueString()})
|
||||
|
||||
return lbId, nil
|
||||
}
|
||||
|
||||
func LBResourceCheckPresence(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) (*lb.RecordLB, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBCheckPresence: Get info about LB with ID - %v", plan.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
lbItem, err := c.CloudBroker().LB().Get(ctx, lb.GetRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbId), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
return lbItem, nil
|
||||
}
|
||||
|
||||
func LBEnableDisable(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "EnableDisable lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
diags := diag.Diagnostics{}
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
if plan.Enable.IsNull() || plan.Enable.ValueBool() {
|
||||
tflog.Info(ctx, "Enable lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
_, err := c.CloudBroker().LB().Enable(ctx, lb.EnableRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError("EnableDisableLB: error to enable LB", err.Error())
|
||||
return diags
|
||||
}
|
||||
} else {
|
||||
tflog.Info(ctx, "Disable lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
_, err := c.CloudBroker().LB().Disable(ctx, lb.DisableRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError("EnableDisableLB: error to disable LB", err.Error())
|
||||
return diags
|
||||
}
|
||||
}
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBReadStatus(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Read status lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
lbItem, err := c.CloudBroker().LB().Get(ctx, lb.GetRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", lbItem), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
switch lbItem.Status {
|
||||
case status.Modeled:
|
||||
diags.AddError("Error:", fmt.Sprintf("The lb is in status: %s, please, contact support for more information", lbItem.Status))
|
||||
return diags
|
||||
case status.Deleted:
|
||||
if plan.Restore.ValueBool() || plan.Restore.IsNull() {
|
||||
diags = LBRestore(ctx, plan, c)
|
||||
if diags.HasError() {
|
||||
tflog.Error(ctx, "Error restore lb", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
} else {
|
||||
diags.AddError("LB in status Deleted:", "please clean state, or restore lb")
|
||||
return diags
|
||||
}
|
||||
if plan.Enable.ValueBool() || plan.Enable.IsNull() {
|
||||
diags = LBEnableDisable(ctx, plan, c)
|
||||
if diags.HasError() {
|
||||
tflog.Error(ctx, "Error enable/disable lb", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
if plan.Start.ValueBool() || plan.Start.IsNull() {
|
||||
diags = LBStartStop(ctx, plan, c)
|
||||
if diags.HasError() {
|
||||
tflog.Error(ctx, "Error start/stop lb", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
}
|
||||
}
|
||||
case status.Destroying:
|
||||
diags.AddError("Error:", fmt.Sprintf("The lb is in progress with status: %s", lbItem.Status))
|
||||
return diags
|
||||
case status.Destroyed:
|
||||
diags.AddError("Error:", "The resource cannot be updated because it has been destroyed")
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Read status lb successfully", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBRestore(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Restore lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().Restore(ctx, lb.RestoreRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot restore lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Restore lb successfully", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBStartStop(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "StartStop lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
diags := diag.Diagnostics{}
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
if plan.Enable.IsNull() || plan.Enable.ValueBool() {
|
||||
if plan.Start.ValueBool() || plan.Start.IsNull() {
|
||||
tflog.Info(ctx, "Start lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
_, err := c.CloudBroker().LB().Start(ctx, lb.StartRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot start lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
} else {
|
||||
tflog.Info(ctx, "Stop lb with ID", map[string]any{"lb_id": plan.ID.ValueString()})
|
||||
_, err := c.CloudBroker().LB().Stop(ctx, lb.StopRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot stop lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBUpdateHaMode(ctx context.Context, state *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Update ha mode from lb with ID", map[string]any{"id": state.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(state.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().HighlyAvailable(ctx, lb.HighlyAvailableRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot update ha mode from lb with ID - %s", state.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Update ha mode from LB with ID successfully", map[string]any{"id": state.ID.ValueString()})
|
||||
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBUpdateSysctlParams(ctx context.Context, plan *models.ResourceLBModel, state *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Update sysctl parameters from LB with ID", map[string]any{"id": state.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(state.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
sysctlParams := make([]map[string]interface{}, 0, len(plan.SysctlParams.Elements()))
|
||||
for _, val := range plan.SysctlParams.Elements() {
|
||||
objVal := val.(types.Object)
|
||||
valMap := objVal.Attributes()
|
||||
mapKey := valMap["key"].(types.String).ValueString()
|
||||
mapVal := valMap["value"].(types.String).ValueString()
|
||||
tempMap := make(map[string]interface{})
|
||||
tempMap[mapKey] = mapVal
|
||||
sysctlParams = append(sysctlParams, tempMap)
|
||||
}
|
||||
|
||||
req := lb.UpdateSysctParamsRequest{
|
||||
LBID: lbId,
|
||||
SysctlParams: sysctlParams,
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().UpdateSysctlParams(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot update sysctl parameters from LB with ID - %s", state.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Update sysctl parameters from LB with ID successfully", map[string]any{"id": state.ID.ValueString()})
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBUpdateDescription(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Update description from lb with ID", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().Update(ctx, lb.UpdateRequest{LBID: lbId, Description: plan.Description.ValueString()})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot update description from lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Update description from LB with ID successfully", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBRestart(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Restart lb with ID", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
req := lb.RestartRequest{
|
||||
LBID: lbId,
|
||||
Safe: false,
|
||||
}
|
||||
|
||||
if plan.Safe.ValueBool() || plan.Safe.IsNull() {
|
||||
req.Safe = true
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().Restart(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot restart lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Restart LB with ID successfully", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
return diags
|
||||
}
|
||||
|
||||
func LBConfigReset(ctx context.Context, plan *models.ResourceLBModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, "Reset config from lb with ID", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
lbId, err := strconv.ParseUint(plan.ID.ValueString(), 10, 64)
|
||||
if err != nil {
|
||||
diags.AddError("Cannot parsed ID lb from state", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
_, err = c.CloudBroker().LB().ConfigReset(ctx, lb.ConfigResetRequest{LBID: lbId})
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot reset config from lb with ID - %s", plan.ID.ValueString()), err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "Reset config from LB with ID successfully", map[string]any{"id": plan.ID.ValueString()})
|
||||
|
||||
return diags
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func CreateResourceLBBackend(ctx context.Context, plan *models.ResourceLBBackendModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("Start create ResourceLBBackend: backend_name %s", plan.Name.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
createReq := lb.BackendCreateRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
BackendName: plan.Name.ValueString(),
|
||||
}
|
||||
|
||||
if !plan.Algorithm.IsUnknown() { // Algorithm is optional & computed
|
||||
createReq.Algorithm = plan.Algorithm.ValueString()
|
||||
}
|
||||
|
||||
if !plan.Inter.IsUnknown() { // Inter is optional & computed
|
||||
createReq.Inter = uint64(plan.Inter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.DownInter.IsUnknown() { // DownInter is optional & computed
|
||||
createReq.DownInter = uint64(plan.DownInter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Rise.IsUnknown() { // Rise is optional & computed
|
||||
createReq.Rise = uint64(plan.Rise.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Fall.IsUnknown() { // Fall is optional & computed
|
||||
createReq.Fall = uint64(plan.Fall.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.SlowStart.IsUnknown() { // SlowStart is optional & computed
|
||||
createReq.SlowStart = uint64(plan.SlowStart.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxConn.IsUnknown() { // MaxConn is optional & computed
|
||||
createReq.MaxConn = uint64(plan.MaxConn.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxQueue.IsUnknown() { // MaxQueue is optional & computed
|
||||
createReq.MaxQueue = uint64(plan.MaxQueue.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Weight.IsUnknown() { // Weight is optional & computed
|
||||
createReq.Weight = uint64(plan.Weight.ValueInt64())
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "CreateResourceLBBackend: before call CloudBroker().LB().BackendCreate", map[string]any{"req": createReq})
|
||||
|
||||
resp, err := c.CloudBroker().LB().BackendCreate(ctx, createReq)
|
||||
if err != nil {
|
||||
diags.AddError("CreateResourceLBBackend: unable to create LB Backend", err.Error())
|
||||
return diags
|
||||
}
|
||||
tflog.Info(ctx, "CreateResourceLBBackend: LB Backend created", map[string]any{"responce": resp, "name": plan.Name.ValueString()})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LBBackendResourceCheckPresence(ctx context.Context, plan *models.ResourceLBBackendModel, c *decort.DecortClient) (*lb.ItemBackend, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBBackendCheckPresence: Get info about LB Backend with ID - %v", plan.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
req := lb.GetRequest{}
|
||||
bName := plan.Name.ValueString()
|
||||
|
||||
if !plan.LBID.IsNull() {
|
||||
req.LBID = uint64(plan.LBID.ValueInt64())
|
||||
} else {
|
||||
parameters := strings.Split(plan.ID.ValueString(), "#")
|
||||
if len(parameters) != 2 {
|
||||
diags.AddError("LBBackendResourceCheckPresence: broken state id",
|
||||
fmt.Sprintf("state id expected: <lb_id>#<backend_name>, got: %v", plan.ID.ValueString()))
|
||||
return nil, diags
|
||||
}
|
||||
lbId, _ := strconv.ParseUint(parameters[0], 10, 64)
|
||||
req.LBID = lbId
|
||||
bName = parameters[1]
|
||||
}
|
||||
|
||||
lb, err := c.CloudBroker().LB().Get(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", req.LBID), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
backends := lb.Backends
|
||||
for _, b := range backends {
|
||||
if b.Name == bName {
|
||||
return &b, diags
|
||||
}
|
||||
}
|
||||
|
||||
diags.AddError(fmt.Sprintf("can not find backend with name: %s for lb: %d", bName, lb.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
func UpdateLBBackend(ctx context.Context, plan, state *models.ResourceLBBackendModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBBackend: Start edit lb backend with ID - %v", state.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
req := lb.BackendUpdateRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
BackendName: plan.Name.ValueString(),
|
||||
}
|
||||
|
||||
if !plan.Algorithm.IsUnknown() {
|
||||
req.Algorithm = plan.Algorithm.ValueString()
|
||||
}
|
||||
|
||||
if !plan.Inter.IsUnknown() {
|
||||
req.Inter = uint64(plan.Inter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.DownInter.IsUnknown() {
|
||||
req.DownInter = uint64(plan.DownInter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Rise.IsUnknown() {
|
||||
req.Rise = uint64(plan.Rise.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Fall.IsUnknown() {
|
||||
req.Fall = uint64(plan.Fall.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.SlowStart.IsUnknown() {
|
||||
req.SlowStart = uint64(plan.SlowStart.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxConn.IsUnknown() {
|
||||
req.MaxConn = uint64(plan.MaxConn.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxQueue.IsUnknown() {
|
||||
req.MaxQueue = uint64(plan.MaxQueue.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Weight.IsUnknown() {
|
||||
req.Weight = uint64(plan.Weight.ValueInt64())
|
||||
}
|
||||
|
||||
_, err := c.CloudBroker().LB().BackendUpdate(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError("UpdateLBBackend: cannot edit lb backend", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBBackend: Finish edit lb backend with name - %v", req.BackendName))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func CreateResourceLBBackendServer(ctx context.Context, plan *models.ResourceLBBackendServerModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("Start create ResourceLBBackendServer: name %s", plan.Name.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
createReq := lb.BackendServerAddRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
BackendName: plan.Backend.ValueString(),
|
||||
ServerName: plan.Name.ValueString(),
|
||||
Address: plan.Address.ValueString(),
|
||||
Port: uint64(plan.Port.ValueInt64()),
|
||||
}
|
||||
|
||||
if !plan.Check.IsUnknown() { // Check is optional & computed
|
||||
createReq.Check = plan.Check.ValueString()
|
||||
}
|
||||
|
||||
if !plan.Inter.IsUnknown() { // Inter is optional & computed
|
||||
createReq.Inter = uint64(plan.Inter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.DownInter.IsUnknown() { // DownInter is optional & computed
|
||||
createReq.DownInter = uint64(plan.DownInter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Rise.IsUnknown() { // Rise is optional & computed
|
||||
createReq.Rise = uint64(plan.Rise.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Fall.IsUnknown() { // Fall is optional & computed
|
||||
createReq.Fall = uint64(plan.Fall.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.SlowStart.IsUnknown() { // SlowStart is optional & computed
|
||||
createReq.SlowStart = uint64(plan.SlowStart.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxConn.IsUnknown() { // MaxConn is optional & computed
|
||||
createReq.MaxConn = uint64(plan.MaxConn.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxQueue.IsUnknown() { // MaxQueue is optional & computed
|
||||
createReq.MaxQueue = uint64(plan.MaxQueue.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Weight.IsUnknown() { // Weight is optional & computed
|
||||
createReq.Weight = uint64(plan.Weight.ValueInt64())
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "ResourceLBBackendServer: before call CloudBroker().LB().BackendServerAdd", map[string]any{"req": createReq})
|
||||
|
||||
resp, err := c.CloudBroker().LB().BackendServerAdd(ctx, createReq)
|
||||
if err != nil {
|
||||
diags.AddError("ResourceLBBackendServer: unable to create LB Backend Server", err.Error())
|
||||
return diags
|
||||
}
|
||||
tflog.Info(ctx, "ResourceLBBackendServer: LB Backend Server created", map[string]any{"responce": resp, "name": plan.Name.ValueString()})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LBBackendServerResourceCheckPresence(ctx context.Context, plan *models.ResourceLBBackendServerModel, c *decort.DecortClient) (*lb.ItemServer, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBBackendServerCheckPresence: Get info about LB Backend Server with ID - %v", plan.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
req := lb.GetRequest{}
|
||||
bName := plan.Backend.ValueString()
|
||||
sName := plan.Name.ValueString()
|
||||
|
||||
if !plan.LBID.IsNull() {
|
||||
req.LBID = uint64(plan.LBID.ValueInt64())
|
||||
} else {
|
||||
parameters := strings.Split(plan.ID.ValueString(), "#")
|
||||
if len(parameters) != 3 {
|
||||
diags.AddError("LBBackendServerResourceCheckPresence: broken state id",
|
||||
fmt.Sprintf("state id expected: <lb_id>#<backend_name>#<server_name>, got: %v", plan.ID.ValueString()))
|
||||
return nil, diags
|
||||
}
|
||||
lbId, _ := strconv.ParseUint(parameters[0], 10, 64)
|
||||
req.LBID = lbId
|
||||
bName = parameters[1]
|
||||
sName = parameters[2]
|
||||
}
|
||||
|
||||
lbItem, err := c.CloudBroker().LB().Get(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", req.LBID), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
backend := &lb.ItemBackend{}
|
||||
backends := lbItem.Backends
|
||||
for i, b := range backends {
|
||||
if b.Name == bName {
|
||||
backend = &backends[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if backend.Name == "" {
|
||||
diags.AddError(fmt.Sprintf("can not find backend with name: %s for lb: %d", bName, lbItem.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
for _, s := range backend.Servers {
|
||||
if s.Name == sName {
|
||||
return &s, nil
|
||||
}
|
||||
}
|
||||
|
||||
diags.AddError(fmt.Sprintf("can not find server with name: %s for backend: %s for lb: %d", sName, bName, lbItem.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
func UpdateLBBackendServer(ctx context.Context, plan, state *models.ResourceLBBackendServerModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBBackendServer: Start edit lb backend server with ID - %v", state.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
req := lb.BackendServerUpdateRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
BackendName: plan.Backend.ValueString(),
|
||||
ServerName: plan.Name.ValueString(),
|
||||
Address: plan.Address.ValueString(),
|
||||
Port: uint64(plan.Port.ValueInt64()),
|
||||
}
|
||||
|
||||
if !plan.Check.IsUnknown() {
|
||||
req.Check = plan.Check.ValueString()
|
||||
}
|
||||
|
||||
if !plan.Inter.IsUnknown() {
|
||||
req.Inter = uint64(plan.Inter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.DownInter.IsUnknown() {
|
||||
req.DownInter = uint64(plan.DownInter.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Rise.IsUnknown() {
|
||||
req.Rise = uint64(plan.Rise.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Fall.IsUnknown() {
|
||||
req.Fall = uint64(plan.Fall.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.SlowStart.IsUnknown() {
|
||||
req.SlowStart = uint64(plan.SlowStart.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxConn.IsUnknown() {
|
||||
req.MaxConn = uint64(plan.MaxConn.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.MaxQueue.IsUnknown() {
|
||||
req.MaxQueue = uint64(plan.MaxQueue.ValueInt64())
|
||||
}
|
||||
|
||||
if !plan.Weight.IsUnknown() {
|
||||
req.Weight = uint64(plan.Weight.ValueInt64())
|
||||
}
|
||||
|
||||
_, err := c.CloudBroker().LB().BackendServerUpdate(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError("UpdateLBBackendServer: cannot edit lb backend server", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBBackendServer: Finish edit lb backend server with name - %v", req.BackendName))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func CreateResourceLBFrontend(ctx context.Context, plan *models.ResourceLBFrontendModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("Start create ResourceLBFrontend: frontend_name %s", plan.Name.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
createReq := lb.FrontendCreateRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
BackendName: plan.Backend.ValueString(),
|
||||
FrontendName: plan.Name.ValueString(),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "ResourceLBFrontend: before call CloudBroker().LB().FrontendCreate", map[string]any{"req": createReq})
|
||||
|
||||
resp, err := c.CloudBroker().LB().FrontendCreate(ctx, createReq)
|
||||
if err != nil {
|
||||
diags.AddError("ResourceLBFrontend: unable to create LB Frontend", err.Error())
|
||||
return diags
|
||||
}
|
||||
tflog.Info(ctx, "ResourceLBFrontend: LB Frontend created", map[string]any{"responce": resp, "name": plan.Name.ValueString()})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LBFrontendResourceCheckPresence(ctx context.Context, plan *models.ResourceLBFrontendModel, c *decort.DecortClient) (*lb.ItemFrontend, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBFrontendCheckPresence: Get info about LB Frontend with ID - %v", plan.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
req := lb.GetRequest{}
|
||||
fName := plan.Name.ValueString()
|
||||
|
||||
if !plan.LBID.IsNull() {
|
||||
req.LBID = uint64(plan.LBID.ValueInt64())
|
||||
} else {
|
||||
parameters := strings.Split(plan.ID.ValueString(), "#")
|
||||
if len(parameters) != 2 {
|
||||
diags.AddError("LBFrontendResourceCheckPresence: broken state id",
|
||||
fmt.Sprintf("state id expected: <lb_id>#<frontend_name>, got: %v", plan.ID.ValueString()))
|
||||
return nil, diags
|
||||
}
|
||||
lbId, _ := strconv.ParseUint(parameters[0], 10, 64)
|
||||
req.LBID = lbId
|
||||
fName = parameters[1]
|
||||
}
|
||||
|
||||
lb, err := c.CloudBroker().LB().Get(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", req.LBID), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
frontends := lb.Frontends
|
||||
for _, f := range frontends {
|
||||
if f.Name == fName {
|
||||
return &f, diags
|
||||
}
|
||||
}
|
||||
|
||||
diags.AddError(fmt.Sprintf("can not find frontend with name: %s for lb: %d", fName, lb.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
decort "repository.basistech.ru/BASIS/decort-golang-sdk"
|
||||
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker/lb"
|
||||
"repository.basistech.ru/BASIS/terraform-provider-dynamix/internal/service/cloudbroker/lb/models"
|
||||
)
|
||||
|
||||
func CreateResourceLBFrontendBind(ctx context.Context, plan *models.ResourceLBFrontendBindModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("Start create ResourceLBFrontendBind: frontend_name %s", plan.Name.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
createReq := lb.FrontendBindRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
FrontendName: plan.Frontend.ValueString(),
|
||||
BindingName: plan.Name.ValueString(),
|
||||
BindingAddress: plan.Address.ValueString(),
|
||||
BindingPort: uint64(plan.Port.ValueInt64()),
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "ResourceLBFrontendBind: before call CloudBroker().LB().FrontendBind", map[string]any{"req": createReq})
|
||||
|
||||
resp, err := c.CloudBroker().LB().FrontendBind(ctx, createReq)
|
||||
if err != nil {
|
||||
diags.AddError("ResourceLBFrontendBind: unable to create LB Frontend Bind", err.Error())
|
||||
return diags
|
||||
}
|
||||
tflog.Info(ctx, "ResourceLBFrontendBind: LB Frontend Bind created", map[string]any{"responce": resp, "name": plan.Name.ValueString()})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LBFrontendBindResourceCheckPresence(ctx context.Context, plan *models.ResourceLBFrontendBindModel, c *decort.DecortClient) (*lb.ItemBinding, diag.Diagnostics) {
|
||||
tflog.Info(ctx, fmt.Sprintf("LBFrontendBindCheckPresence: Get info about LB Frontend Bind with ID - %v", plan.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
req := lb.GetRequest{}
|
||||
fName := plan.Frontend.ValueString()
|
||||
bName := plan.Name.ValueString()
|
||||
|
||||
if !plan.LBID.IsNull() {
|
||||
req.LBID = uint64(plan.LBID.ValueInt64())
|
||||
} else {
|
||||
parameters := strings.Split(plan.ID.ValueString(), "#")
|
||||
if len(parameters) != 3 {
|
||||
diags.AddError("LBFrontendResourceCheckPresence: broken state id",
|
||||
fmt.Sprintf("state id expected: <lb_id>#<frontend_name>#<backend_name>, got: %v", plan.ID.ValueString()))
|
||||
return nil, diags
|
||||
}
|
||||
lbId, _ := strconv.ParseUint(parameters[0], 10, 64)
|
||||
req.LBID = lbId
|
||||
fName = parameters[1]
|
||||
bName = parameters[2]
|
||||
}
|
||||
|
||||
lbItem, err := c.CloudBroker().LB().Get(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError(fmt.Sprintf("Cannot get info about lb with ID %v", req.LBID), err.Error())
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
frontend := &lb.ItemFrontend{}
|
||||
frontends := lbItem.Frontends
|
||||
for i, f := range frontends {
|
||||
if f.Name == fName {
|
||||
frontend = &frontends[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if frontend.Name == "" {
|
||||
diags.AddError(fmt.Sprintf("can not find frontend with name: %s for lb: %d", fName, lbItem.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
for _, b := range frontend.Bindings {
|
||||
if b.Name == bName {
|
||||
return &b, nil
|
||||
}
|
||||
}
|
||||
|
||||
diags.AddError(fmt.Sprintf("can not find bind with name: %s for frontend: %s for lb: %d", bName, fName, lbItem.ID), "")
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
func UpdateLBFrontendBind(ctx context.Context, plan, state *models.ResourceLBFrontendBindModel, c *decort.DecortClient) diag.Diagnostics {
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBFrontendBind: Start edit lb frontend bind with ID - %v", state.ID.ValueString()))
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
req := lb.FrontendBindUpdateRequest{
|
||||
LBID: uint64(plan.LBID.ValueInt64()),
|
||||
FrontendName: plan.Frontend.ValueString(),
|
||||
BindingName: plan.Name.ValueString(),
|
||||
BindingAddress: plan.Address.ValueString(),
|
||||
BindingPort: uint64(plan.Port.ValueInt64()),
|
||||
}
|
||||
|
||||
_, err := c.CloudBroker().LB().FrontendBindUpdate(ctx, req)
|
||||
if err != nil {
|
||||
diags.AddError("UpdateLBFrontendBind: cannot edit lb frontend bind", err.Error())
|
||||
return diags
|
||||
}
|
||||
|
||||
tflog.Info(ctx, fmt.Sprintf("UpdateLBFrontendBind: Finish edit lb frontend bind with name - %v", req.BindingName))
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user