feat: region adjustment for load balancer (#721)

* remove deprecated "credential" resource of loadbalancer

* region adjustment load balancer
- adapted load balancer example
This commit is contained in:
Marcel Jacek 2025-03-25 11:36:26 +01:00 committed by GitHub
parent 1444376f35
commit 176fb8408f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 250 additions and 624 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@ -33,7 +34,8 @@ func NewLoadBalancerDataSource() datasource.DataSource {
// loadBalancerDataSource is the data source implementation.
type loadBalancerDataSource struct {
client *loadbalancer.APIClient
client *loadbalancer.APIClient
providerData core.ProviderData
}
// Metadata returns the data source type name.
@ -48,7 +50,8 @@ func (r *loadBalancerDataSource) Configure(ctx context.Context, req datasource.C
return
}
providerData, ok := req.ProviderData.(core.ProviderData)
var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return
@ -56,15 +59,14 @@ func (r *loadBalancerDataSource) Configure(ctx context.Context, req datasource.C
var apiClient *loadbalancer.APIClient
var err error
if providerData.LoadBalancerCustomEndpoint != "" {
if r.providerData.LoadBalancerCustomEndpoint != "" {
apiClient, err = loadbalancer.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper),
config.WithEndpoint(providerData.LoadBalancerCustomEndpoint),
config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(r.providerData.LoadBalancerCustomEndpoint),
)
} else {
apiClient, err = loadbalancer.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper),
config.WithRegion(providerData.GetRegion()),
config.WithCustomAuth(r.providerData.RoundTripper),
)
}
@ -81,7 +83,7 @@ func (r *loadBalancerDataSource) Configure(ctx context.Context, req datasource.C
func (r *loadBalancerDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{
"main": "Load Balancer data source schema. Must have a `region` specified in the provider configuration.",
"id": "Terraform's internal resource ID. It is structured as \"`project_id`\",\"`name`\".",
"id": "Terraform's internal resource ID. It is structured as \"`project_id`\",\"region\",\"`name`\".",
"project_id": "STACKIT project ID to which the Load Balancer is associated.",
"external_address": "External Load Balancer IP address where this Load Balancer is exposed.",
"listeners": "List of all listeners which will accept traffic. Limited to 20.",
@ -111,6 +113,7 @@ func (r *loadBalancerDataSource) Schema(_ context.Context, _ datasource.SchemaRe
"targets": "List of all targets which will be used in the pool. Limited to 1000.",
"targets.display_name": "Target display name",
"ip": "Target IP",
"region": "The resource region. If not defined, the provider region is used.",
}
resp.Schema = schema.Schema{
@ -302,6 +305,11 @@ func (r *loadBalancerDataSource) Schema(_ context.Context, _ datasource.SchemaRe
},
},
},
"region": schema.StringAttribute{
// the region cannot be found, so it has to be passed
Optional: true,
Description: descriptions["region"],
},
},
}
}
@ -316,10 +324,17 @@ func (r *loadBalancerDataSource) Read(ctx context.Context, req datasource.ReadRe
}
projectId := model.ProjectId.ValueString()
name := model.Name.ValueString()
var region string
if utils.IsUndefined(model.Region) {
region = r.providerData.GetRegion()
} else {
region = model.Region.ValueString()
}
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "name", name)
ctx = tflog.SetField(ctx, "region", region)
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, region, name).Execute()
if err != nil {
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -330,7 +345,7 @@ func (r *loadBalancerDataSource) Read(ctx context.Context, req datasource.ReadRe
}
// Map response body to schema
err = mapFields(ctx, lbResp, &model)
err = mapFields(ctx, lbResp, &model, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Processing API payload: %v", err))
return

View file

@ -42,6 +42,7 @@ var (
_ resource.Resource = &loadBalancerResource{}
_ resource.ResourceWithConfigure = &loadBalancerResource{}
_ resource.ResourceWithImportState = &loadBalancerResource{}
_ resource.ResourceWithModifyPlan = &loadBalancerResource{}
)
type Model struct {
@ -54,6 +55,7 @@ type Model struct {
Options types.Object `tfsdk:"options"`
PrivateAddress types.String `tfsdk:"private_address"`
TargetPools types.List `tfsdk:"target_pools"`
Region types.String `tfsdk:"region"`
}
// Struct corresponding to Model.Listeners[i]
@ -173,7 +175,8 @@ func NewLoadBalancerResource() resource.Resource {
// loadBalancerResource is the resource implementation.
type loadBalancerResource struct {
client *loadbalancer.APIClient
client *loadbalancer.APIClient
providerData core.ProviderData
}
// Metadata returns the resource type name.
@ -181,6 +184,36 @@ func (r *loadBalancerResource) Metadata(_ context.Context, req resource.Metadata
resp.TypeName = req.ProviderTypeName + "_loadbalancer"
}
// ModifyPlan implements resource.ResourceWithModifyPlan.
// Use the modifier to set the effective region in the current plan.
func (r *loadBalancerResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
var configModel Model
// skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() {
return
}
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
if resp.Diagnostics.HasError() {
return
}
var planModel Model
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
}
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() {
return
}
}
// Configure adds the provider configured client to the resource.
func (r *loadBalancerResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
@ -188,7 +221,8 @@ func (r *loadBalancerResource) Configure(ctx context.Context, req resource.Confi
return
}
providerData, ok := req.ProviderData.(core.ProviderData)
var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return
@ -196,16 +230,15 @@ func (r *loadBalancerResource) Configure(ctx context.Context, req resource.Confi
var apiClient *loadbalancer.APIClient
var err error
if providerData.LoadBalancerCustomEndpoint != "" {
ctx = tflog.SetField(ctx, "loadbalancer_custom_endpoint", providerData.LoadBalancerCustomEndpoint)
if r.providerData.LoadBalancerCustomEndpoint != "" {
ctx = tflog.SetField(ctx, "loadbalancer_custom_endpoint", r.providerData.LoadBalancerCustomEndpoint)
apiClient, err = loadbalancer.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper),
config.WithEndpoint(providerData.LoadBalancerCustomEndpoint),
config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(r.providerData.LoadBalancerCustomEndpoint),
)
} else {
apiClient, err = loadbalancer.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper),
config.WithRegion(providerData.GetRegion()),
config.WithCustomAuth(r.providerData.RoundTripper),
)
}
@ -225,7 +258,7 @@ func (r *loadBalancerResource) Schema(_ context.Context, _ resource.SchemaReques
descriptions := map[string]string{
"main": "Load Balancer resource schema.",
"id": "Terraform's internal resource ID. It is structured as \"`project_id`\",\"`name`\".",
"id": "Terraform's internal resource ID. It is structured as \"`project_id`\",\"region\",\"`name`\".",
"project_id": "STACKIT project ID to which the Load Balancer is associated.",
"external_address": "External Load Balancer IP address where this Load Balancer is exposed.",
"listeners": "List of all listeners which will accept traffic. Limited to 20.",
@ -255,6 +288,7 @@ func (r *loadBalancerResource) Schema(_ context.Context, _ resource.SchemaReques
"targets": "List of all targets which will be used in the pool. Limited to 1000.",
"targets.display_name": "Target display name",
"ip": "Target IP",
"region": "The resource region. If not defined, the provider region is used.",
}
resp.Schema = schema.Schema{
@ -527,6 +561,15 @@ The example below creates the supporting infrastructure using the STACKIT Terraf
},
},
},
"region": schema.StringAttribute{
Optional: true,
// must be computed to allow for storing the override value from the provider
Computed: true,
Description: descriptions["region"],
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
}
}
@ -541,7 +584,9 @@ func (r *loadBalancerResource) Create(ctx context.Context, req resource.CreateRe
return
}
projectId := model.ProjectId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
// Generate API request body from model
payload, err := toCreatePayload(ctx, &model)
@ -551,20 +596,20 @@ func (r *loadBalancerResource) Create(ctx context.Context, req resource.CreateRe
}
// Create a new load balancer
createResp, err := r.client.CreateLoadBalancer(ctx, projectId).CreateLoadBalancerPayload(*payload).XRequestID(uuid.NewString()).Execute()
createResp, err := r.client.CreateLoadBalancer(ctx, projectId, region).CreateLoadBalancerPayload(*payload).XRequestID(uuid.NewString()).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", fmt.Sprintf("Calling API: %v", err))
return
}
waitResp, err := wait.CreateLoadBalancerWaitHandler(ctx, r.client, projectId, *createResp.Name).SetTimeout(90 * time.Minute).WaitWithContext(ctx)
waitResp, err := wait.CreateLoadBalancerWaitHandler(ctx, r.client, projectId, region, *createResp.Name).SetTimeout(90 * time.Minute).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", fmt.Sprintf("Load balancer creation waiting: %v", err))
return
}
// Map response body to schema
err = mapFields(ctx, waitResp, &model)
err = mapFields(ctx, waitResp, &model, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", fmt.Sprintf("Processing API payload: %v", err))
return
@ -590,10 +635,16 @@ func (r *loadBalancerResource) Read(ctx context.Context, req resource.ReadReques
}
projectId := model.ProjectId.ValueString()
name := model.Name.ValueString()
region := model.Region.ValueString()
if region == "" {
region = r.providerData.GetRegion()
}
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "name", name)
ctx = tflog.SetField(ctx, "region", region)
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, region, name).Execute()
if err != nil {
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -605,7 +656,7 @@ func (r *loadBalancerResource) Read(ctx context.Context, req resource.ReadReques
}
// Map response body to schema
err = mapFields(ctx, lbResp, &model)
err = mapFields(ctx, lbResp, &model, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Processing API payload: %v", err))
return
@ -631,8 +682,10 @@ func (r *loadBalancerResource) Update(ctx context.Context, req resource.UpdateRe
}
projectId := model.ProjectId.ValueString()
name := model.Name.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "name", name)
ctx = tflog.SetField(ctx, "region", region)
targetPoolsModel := []targetPool{}
diags = model.TargetPools.ElementsAs(ctx, &targetPoolsModel, false)
@ -653,7 +706,7 @@ func (r *loadBalancerResource) Update(ctx context.Context, req resource.UpdateRe
}
// Update target pool
_, err = r.client.UpdateTargetPool(ctx, projectId, name, targetPoolName).UpdateTargetPoolPayload(*payload).Execute()
_, err = r.client.UpdateTargetPool(ctx, projectId, region, name, targetPoolName).UpdateTargetPoolPayload(*payload).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating load balancer", fmt.Sprintf("Calling API for target pool: %v", err))
return
@ -662,14 +715,14 @@ func (r *loadBalancerResource) Update(ctx context.Context, req resource.UpdateRe
ctx = tflog.SetField(ctx, "target_pool_name", nil)
// Get updated load balancer
getResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
getResp, err := r.client.GetLoadBalancer(ctx, projectId, region, name).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating load balancer", fmt.Sprintf("Calling API after update: %v", err))
return
}
// Map response body to schema
err = mapFields(ctx, getResp, &model)
err = mapFields(ctx, getResp, &model, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating load balancer", fmt.Sprintf("Processing API payload: %v", err))
return
@ -695,17 +748,19 @@ func (r *loadBalancerResource) Delete(ctx context.Context, req resource.DeleteRe
}
projectId := model.ProjectId.ValueString()
name := model.Name.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "name", name)
ctx = tflog.SetField(ctx, "region", region)
// Delete load balancer
_, err := r.client.DeleteLoadBalancer(ctx, projectId, name).Execute()
_, err := r.client.DeleteLoadBalancer(ctx, projectId, region, name).Execute()
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting load balancer", fmt.Sprintf("Calling API: %v", err))
return
}
_, err = wait.DeleteLoadBalancerWaitHandler(ctx, r.client, projectId, name).WaitWithContext(ctx)
_, err = wait.DeleteLoadBalancerWaitHandler(ctx, r.client, projectId, region, name).WaitWithContext(ctx)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting load balancer", fmt.Sprintf("Load balancer deleting waiting: %v", err))
return
@ -719,16 +774,17 @@ func (r *loadBalancerResource) Delete(ctx context.Context, req resource.DeleteRe
func (r *loadBalancerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing load balancer",
fmt.Sprintf("Expected import identifier with format: [project_id],[name] Got: %q", req.ID),
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[name] Got: %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("name"), idParts[2])...)
tflog.Info(ctx, "Load balancer state imported")
}
@ -1012,7 +1068,7 @@ func toTargetsPayload(ctx context.Context, tp *targetPool) (*[]loadbalancer.Targ
return &payload, nil
}
func mapFields(ctx context.Context, lb *loadbalancer.LoadBalancer, m *Model) error {
func mapFields(ctx context.Context, lb *loadbalancer.LoadBalancer, m *Model, region string) error {
if lb == nil {
return fmt.Errorf("response input is nil")
}
@ -1028,9 +1084,11 @@ func mapFields(ctx context.Context, lb *loadbalancer.LoadBalancer, m *Model) err
} else {
return fmt.Errorf("name not present")
}
m.Region = types.StringValue(region)
m.Name = types.StringValue(name)
idParts := []string{
m.ProjectId.ValueString(),
m.Region.ValueString(),
name,
}
m.Id = types.StringValue(

View file

@ -2,6 +2,7 @@ package loadbalancer
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
@ -267,10 +268,13 @@ func TestToTargetPoolUpdatePayload(t *testing.T) {
}
func TestMapFields(t *testing.T) {
const testRegion = "eu01"
id := fmt.Sprintf("%s,%s,%s", "pid", testRegion, "name")
tests := []struct {
description string
input *loadbalancer.LoadBalancer
modelPrivateNetworkOnly *bool
region string
expected *Model
isValid bool
}{
@ -290,8 +294,9 @@ func TestMapFields(t *testing.T) {
TargetPools: nil,
},
nil,
testRegion,
&Model{
Id: types.StringValue("pid,name"),
Id: types.StringValue(id),
ProjectId: types.StringValue("pid"),
ExternalAddress: types.StringNull(),
Listeners: types.ListNull(types.ObjectType{AttrTypes: listenerTypes}),
@ -303,6 +308,7 @@ func TestMapFields(t *testing.T) {
}),
PrivateAddress: types.StringNull(),
TargetPools: types.ListNull(types.ObjectType{AttrTypes: targetPoolTypes}),
Region: types.StringValue(testRegion),
},
true,
},
@ -361,8 +367,9 @@ func TestMapFields(t *testing.T) {
}),
},
nil,
testRegion,
&Model{
Id: types.StringValue("pid,name"),
Id: types.StringValue(id),
ProjectId: types.StringValue("pid"),
ExternalAddress: types.StringValue("external_address"),
Listeners: types.ListValueMust(types.ObjectType{AttrTypes: listenerTypes}, []attr.Value{
@ -422,6 +429,7 @@ func TestMapFields(t *testing.T) {
}),
}),
}),
Region: types.StringValue(testRegion),
},
true,
},
@ -483,8 +491,9 @@ func TestMapFields(t *testing.T) {
}),
},
utils.Ptr(false),
testRegion,
&Model{
Id: types.StringValue("pid,name"),
Id: types.StringValue(id),
ProjectId: types.StringValue("pid"),
ExternalAddress: types.StringValue("external_address"),
Listeners: types.ListValueMust(types.ObjectType{AttrTypes: listenerTypes}, []attr.Value{
@ -546,6 +555,7 @@ func TestMapFields(t *testing.T) {
}),
}),
}),
Region: types.StringValue(testRegion),
},
true,
},
@ -553,6 +563,7 @@ func TestMapFields(t *testing.T) {
"nil_response",
nil,
nil,
testRegion,
&Model{},
false,
},
@ -560,6 +571,7 @@ func TestMapFields(t *testing.T) {
"no_name",
&loadbalancer.LoadBalancer{},
nil,
testRegion,
&Model{},
false,
},
@ -575,7 +587,7 @@ func TestMapFields(t *testing.T) {
"acl": types.SetNull(types.StringType),
})
}
err := mapFields(context.Background(), tt.input, model)
err := mapFields(context.Background(), tt.input, model, tt.region)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}