@@ -10,6 +10,7 @@ import (
1010 skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
1111
1212 "github.com/google/uuid"
13+ "github.com/hashicorp/terraform-plugin-framework/path"
1314 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1415 "github.com/hashicorp/terraform-plugin-log/tflog"
1516 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
@@ -274,7 +275,13 @@ func (r *kubeconfigResource) Read(ctx context.Context, req resource.ReadRequest,
274275 projectId := model .ProjectId .ValueString ()
275276 clusterName := model .ClusterName .ValueString ()
276277 kubeconfigUUID := model .KubeconfigId .ValueString ()
277- region := model .Region .ValueString ()
278+ region := r .providerData .GetRegionWithOverride (model .Region )
279+ // Prevent recreation of kubeconfig when updating to v2 api version
280+ diags = resp .State .SetAttribute (ctx , path .Root ("region" ), region )
281+ resp .Diagnostics .Append (diags ... )
282+ if resp .Diagnostics .HasError () {
283+ return
284+ }
278285 ctx = tflog .SetField (ctx , "project_id" , projectId )
279286 ctx = tflog .SetField (ctx , "cluster_name" , clusterName )
280287 ctx = tflog .SetField (ctx , "kube_config_id" , kubeconfigUUID )
@@ -321,7 +328,6 @@ func (r *kubeconfigResource) Read(ctx context.Context, req resource.ReadRequest,
321328 core .LogAndAddError (ctx , & resp .Diagnostics , "Error reading kubeconfig" , fmt .Sprintf ("The existing kubeconfig is invalid, creating a new one: %v" , err ))
322329 return
323330 }
324-
325331 // Set state to fully populated data
326332 diags = resp .State .Set (ctx , model )
327333 resp .Diagnostics .Append (diags ... )
@@ -346,7 +352,7 @@ func (r *kubeconfigResource) createKubeconfig(ctx context.Context, model *Model)
346352 }
347353
348354 // Map response body to schema
349- err = mapFields (kubeconfigResp , model , time .Now ())
355+ err = mapFields (kubeconfigResp , model , time .Now (), model . Region . ValueString () )
350356 if err != nil {
351357 return fmt .Errorf ("processing API payload: %w" , err )
352358 }
@@ -383,7 +389,7 @@ func (r *kubeconfigResource) Delete(ctx context.Context, req resource.DeleteRequ
383389 tflog .Info (ctx , "SKE kubeconfig deleted" )
384390}
385391
386- func mapFields (kubeconfigResp * ske.Kubeconfig , model * Model , creationTime time.Time ) error {
392+ func mapFields (kubeconfigResp * ske.Kubeconfig , model * Model , creationTime time.Time , region string ) error {
387393 if kubeconfigResp == nil {
388394 return fmt .Errorf ("response is nil" )
389395 }
@@ -403,6 +409,7 @@ func mapFields(kubeconfigResp *ske.Kubeconfig, model *Model, creationTime time.T
403409 model .ExpiresAt = types .StringValue (kubeconfigResp .ExpirationTimestamp .Format (time .RFC3339 ))
404410 // set creation time
405411 model .CreationTime = types .StringValue (creationTime .Format (time .RFC3339 ))
412+ model .Region = types .StringValue (region )
406413 return nil
407414}
408415
0 commit comments