fix(kubeconfig): Region is not set correctly for Read() (#925)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
This commit is contained in:
parent
189fa1ece7
commit
229930ee0c
2 changed files with 14 additions and 5 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
|
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||||
|
|
@ -274,7 +275,13 @@ func (r *kubeconfigResource) Read(ctx context.Context, req resource.ReadRequest,
|
||||||
projectId := model.ProjectId.ValueString()
|
projectId := model.ProjectId.ValueString()
|
||||||
clusterName := model.ClusterName.ValueString()
|
clusterName := model.ClusterName.ValueString()
|
||||||
kubeconfigUUID := model.KubeconfigId.ValueString()
|
kubeconfigUUID := model.KubeconfigId.ValueString()
|
||||||
region := model.Region.ValueString()
|
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||||
|
// Prevent recreation of kubeconfig when updating to v2 api version
|
||||||
|
diags = resp.State.SetAttribute(ctx, path.Root("region"), region)
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||||
ctx = tflog.SetField(ctx, "cluster_name", clusterName)
|
ctx = tflog.SetField(ctx, "cluster_name", clusterName)
|
||||||
ctx = tflog.SetField(ctx, "kube_config_id", kubeconfigUUID)
|
ctx = tflog.SetField(ctx, "kube_config_id", kubeconfigUUID)
|
||||||
|
|
@ -321,7 +328,6 @@ func (r *kubeconfigResource) Read(ctx context.Context, req resource.ReadRequest,
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading kubeconfig", fmt.Sprintf("The existing kubeconfig is invalid, creating a new one: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading kubeconfig", fmt.Sprintf("The existing kubeconfig is invalid, creating a new one: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set state to fully populated data
|
// Set state to fully populated data
|
||||||
diags = resp.State.Set(ctx, model)
|
diags = resp.State.Set(ctx, model)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
|
|
@ -346,7 +352,7 @@ func (r *kubeconfigResource) createKubeconfig(ctx context.Context, model *Model)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map response body to schema
|
// Map response body to schema
|
||||||
err = mapFields(kubeconfigResp, model, time.Now())
|
err = mapFields(kubeconfigResp, model, time.Now(), model.Region.ValueString())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("processing API payload: %w", err)
|
return fmt.Errorf("processing API payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
@ -383,7 +389,7 @@ func (r *kubeconfigResource) Delete(ctx context.Context, req resource.DeleteRequ
|
||||||
tflog.Info(ctx, "SKE kubeconfig deleted")
|
tflog.Info(ctx, "SKE kubeconfig deleted")
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapFields(kubeconfigResp *ske.Kubeconfig, model *Model, creationTime time.Time) error {
|
func mapFields(kubeconfigResp *ske.Kubeconfig, model *Model, creationTime time.Time, region string) error {
|
||||||
if kubeconfigResp == nil {
|
if kubeconfigResp == nil {
|
||||||
return fmt.Errorf("response is nil")
|
return fmt.Errorf("response is nil")
|
||||||
}
|
}
|
||||||
|
|
@ -403,6 +409,7 @@ func mapFields(kubeconfigResp *ske.Kubeconfig, model *Model, creationTime time.T
|
||||||
model.ExpiresAt = types.StringValue(kubeconfigResp.ExpirationTimestamp.Format(time.RFC3339))
|
model.ExpiresAt = types.StringValue(kubeconfigResp.ExpirationTimestamp.Format(time.RFC3339))
|
||||||
// set creation time
|
// set creation time
|
||||||
model.CreationTime = types.StringValue(creationTime.Format(time.RFC3339))
|
model.CreationTime = types.StringValue(creationTime.Format(time.RFC3339))
|
||||||
|
model.Region = types.StringValue(region)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMapFields(t *testing.T) {
|
func TestMapFields(t *testing.T) {
|
||||||
|
const testRegion = "eu01"
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
description string
|
description string
|
||||||
input *ske.Kubeconfig
|
input *ske.Kubeconfig
|
||||||
|
|
@ -32,6 +33,7 @@ func TestMapFields(t *testing.T) {
|
||||||
Refresh: types.BoolNull(),
|
Refresh: types.BoolNull(),
|
||||||
ExpiresAt: types.StringValue("2024-02-07T16:42:12Z"),
|
ExpiresAt: types.StringValue("2024-02-07T16:42:12Z"),
|
||||||
CreationTime: types.StringValue("2024-02-05T14:40:12Z"),
|
CreationTime: types.StringValue("2024-02-05T14:40:12Z"),
|
||||||
|
Region: types.StringValue(testRegion),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
@ -63,7 +65,7 @@ func TestMapFields(t *testing.T) {
|
||||||
ClusterName: tt.expected.ClusterName,
|
ClusterName: tt.expected.ClusterName,
|
||||||
}
|
}
|
||||||
creationTime, _ := time.Parse(time.RFC3339, tt.expected.CreationTime.ValueString())
|
creationTime, _ := time.Parse(time.RFC3339, tt.expected.CreationTime.ValueString())
|
||||||
err := mapFields(tt.input, state, creationTime)
|
err := mapFields(tt.input, state, creationTime, testRegion)
|
||||||
if !tt.isValid && err == nil {
|
if !tt.isValid && err == nil {
|
||||||
t.Fatalf("Should have failed")
|
t.Fatalf("Should have failed")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue