From 386ccac6b4d7a5e441e66ea1257a9a635d1d5439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Wed, 20 Sep 2023 11:00:08 +0200 Subject: [PATCH] Extend DSA `Read`s to compute plan name and version from plan ID (#20) * Extend Read to compute plan name and version from plan ID * Lint * Fix error handling; Pass client to loadPlanNameAndVersion * Change error to warning * Replicate to other DSA * Update acc tests to test import of plan name and version --- stackit/services/logme/instance/datasource.go | 7 ++++ stackit/services/logme/instance/resource.go | 35 ++++++++++++++++--- stackit/services/logme/logme_acc_test.go | 5 ++- .../services/mariadb/instance/datasource.go | 7 ++++ stackit/services/mariadb/instance/resource.go | 35 ++++++++++++++++--- stackit/services/mariadb/mariadb_acc_test.go | 5 ++- .../opensearch/instance/datasource.go | 7 ++++ .../services/opensearch/instance/resource.go | 35 ++++++++++++++++--- .../opensearch/opensearch_acc_test.go | 5 ++- .../postgresql/instance/datasource.go | 7 ++++ .../services/postgresql/instance/resource.go | 35 ++++++++++++++++--- .../postgresql/postgresql_acc_test.go | 5 ++- .../services/rabbitmq/instance/datasource.go | 7 ++++ .../services/rabbitmq/instance/resource.go | 35 ++++++++++++++++--- .../services/rabbitmq/rabbitmq_acc_test.go | 5 ++- stackit/services/redis/instance/datasource.go | 7 ++++ stackit/services/redis/instance/resource.go | 35 ++++++++++++++++--- stackit/services/redis/redis_acc_test.go | 5 ++- 18 files changed, 240 insertions(+), 42 deletions(-) diff --git a/stackit/services/logme/instance/datasource.go b/stackit/services/logme/instance/datasource.go index d6e15ff4..8f73c0a6 100644 --- a/stackit/services/logme/instance/datasource.go +++ b/stackit/services/logme/instance/datasource.go @@ -174,6 +174,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/logme/instance/resource.go b/stackit/services/logme/instance/resource.go index d0811116..3a51dc40 100644 --- a/stackit/services/logme/instance/resource.go +++ b/stackit/services/logme/instance/resource.go @@ -234,8 +234,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load LogMe service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -310,6 +309,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -330,8 +336,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load LogMe service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -641,3 +646,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *logme.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list LogMe offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/logme/logme_acc_test.go b/stackit/services/logme/logme_acc_test.go index e3169886..543b215d 100644 --- a/stackit/services/logme/logme_acc_test.go +++ b/stackit/services/logme/logme_acc_test.go @@ -136,9 +136,8 @@ func TestAccLogMeResource(t *testing.T) { } return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_logme_credentials.credentials", diff --git a/stackit/services/mariadb/instance/datasource.go b/stackit/services/mariadb/instance/datasource.go index b10e35e0..9d24b20b 100644 --- a/stackit/services/mariadb/instance/datasource.go +++ b/stackit/services/mariadb/instance/datasource.go @@ -174,6 +174,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/mariadb/instance/resource.go b/stackit/services/mariadb/instance/resource.go index 2532868a..d2d9d63b 100644 --- a/stackit/services/mariadb/instance/resource.go +++ b/stackit/services/mariadb/instance/resource.go @@ -216,8 +216,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load MariaDB service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -292,6 +291,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -312,8 +318,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load MariaDB service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -622,3 +627,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *mariadb.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list MariaDB offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/mariadb/mariadb_acc_test.go b/stackit/services/mariadb/mariadb_acc_test.go index df847788..1e737d59 100644 --- a/stackit/services/mariadb/mariadb_acc_test.go +++ b/stackit/services/mariadb/mariadb_acc_test.go @@ -135,9 +135,8 @@ func TestAccMariaDBResource(t *testing.T) { } return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_mariadb_credentials.credentials", diff --git a/stackit/services/opensearch/instance/datasource.go b/stackit/services/opensearch/instance/datasource.go index 3cce5339..6888cd8e 100644 --- a/stackit/services/opensearch/instance/datasource.go +++ b/stackit/services/opensearch/instance/datasource.go @@ -174,6 +174,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/opensearch/instance/resource.go b/stackit/services/opensearch/instance/resource.go index 3e8932f3..ee0a8470 100644 --- a/stackit/services/opensearch/instance/resource.go +++ b/stackit/services/opensearch/instance/resource.go @@ -216,8 +216,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load OpenSearch service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -292,6 +291,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -312,8 +318,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load OpenSearch service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -621,3 +626,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *opensearch.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list OpenSearch offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/opensearch/opensearch_acc_test.go b/stackit/services/opensearch/opensearch_acc_test.go index 57ce148d..071cb71f 100644 --- a/stackit/services/opensearch/opensearch_acc_test.go +++ b/stackit/services/opensearch/opensearch_acc_test.go @@ -160,9 +160,8 @@ func TestAccOpenSearchResource(t *testing.T) { return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_opensearch_credentials.credentials", diff --git a/stackit/services/postgresql/instance/datasource.go b/stackit/services/postgresql/instance/datasource.go index af84630f..181813d9 100644 --- a/stackit/services/postgresql/instance/datasource.go +++ b/stackit/services/postgresql/instance/datasource.go @@ -191,6 +191,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/postgresql/instance/resource.go b/stackit/services/postgresql/instance/resource.go index d81c0ceb..e3b0d0c0 100644 --- a/stackit/services/postgresql/instance/resource.go +++ b/stackit/services/postgresql/instance/resource.go @@ -261,8 +261,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load PostgreSQL service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -376,6 +375,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -396,8 +402,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load PostgreSQL service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -702,3 +707,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *postgresql.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list PostgreSQL offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/postgresql/postgresql_acc_test.go b/stackit/services/postgresql/postgresql_acc_test.go index 195dddd9..ef048a77 100644 --- a/stackit/services/postgresql/postgresql_acc_test.go +++ b/stackit/services/postgresql/postgresql_acc_test.go @@ -144,9 +144,8 @@ func TestAccPostgreSQLResource(t *testing.T) { } return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_postgresql_credentials.credentials", diff --git a/stackit/services/rabbitmq/instance/datasource.go b/stackit/services/rabbitmq/instance/datasource.go index eaff6473..dad85ee2 100644 --- a/stackit/services/rabbitmq/instance/datasource.go +++ b/stackit/services/rabbitmq/instance/datasource.go @@ -174,6 +174,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/rabbitmq/instance/resource.go b/stackit/services/rabbitmq/instance/resource.go index 512c7109..0cd3b17d 100644 --- a/stackit/services/rabbitmq/instance/resource.go +++ b/stackit/services/rabbitmq/instance/resource.go @@ -229,8 +229,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load RabbitMQ service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -326,6 +325,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -346,8 +352,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load RabbitMQ service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -635,3 +640,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *rabbitmq.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list RabbitMQ offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/rabbitmq/rabbitmq_acc_test.go b/stackit/services/rabbitmq/rabbitmq_acc_test.go index 5065e73f..a804a646 100644 --- a/stackit/services/rabbitmq/rabbitmq_acc_test.go +++ b/stackit/services/rabbitmq/rabbitmq_acc_test.go @@ -181,9 +181,8 @@ func TestAccRabbitMQResource(t *testing.T) { } return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_rabbitmq_credentials.credentials", diff --git a/stackit/services/redis/instance/datasource.go b/stackit/services/redis/instance/datasource.go index ff90fe7e..662c5f00 100644 --- a/stackit/services/redis/instance/datasource.go +++ b/stackit/services/redis/instance/datasource.go @@ -174,6 +174,13 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) diff --git a/stackit/services/redis/instance/resource.go b/stackit/services/redis/instance/resource.go index c779d564..77496650 100644 --- a/stackit/services/redis/instance/resource.go +++ b/stackit/services/redis/instance/resource.go @@ -226,8 +226,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = tflog.SetField(ctx, "project_id", projectId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load Redis service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -323,6 +322,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error()) return } + + // Compute and store values not present in the API response + loadPlanNameAndVersion(ctx, r.client, &resp.Diagnostics, &state) + if resp.Diagnostics.HasError() { + return + } + // Set refreshed state diags = resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) @@ -343,8 +349,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = tflog.SetField(ctx, "instance_id", instanceId) r.loadPlanId(ctx, &resp.Diagnostics, &model) - if diags.HasError() { - core.LogAndAddError(ctx, &diags, "Failed to load Redis service plan", "plan "+model.PlanName.ValueString()) + if resp.Diagnostics.HasError() { return } @@ -632,3 +637,25 @@ func (r *instanceResource) loadPlanId(ctx context.Context, diags *diag.Diagnosti } diags.AddError("Invalid plan_name", fmt.Sprintf("Couldn't find plan_name '%s' for version %s, available names are:%s", planName, version, availablePlanNames)) } + +func loadPlanNameAndVersion(ctx context.Context, client *redis.APIClient, diags *diag.Diagnostics, model *Model) { + projectId := model.ProjectId.ValueString() + planId := model.PlanId.ValueString() + res, err := client.GetOfferings(ctx, projectId).Execute() + if err != nil { + diags.AddError("Failed to list Redis offerings", err.Error()) + return + } + + for _, offer := range *res.Offerings { + for _, plan := range *offer.Plans { + if strings.EqualFold(*plan.Id, planId) && plan.Id != nil { + model.PlanName = types.StringPointerValue(plan.Name) + model.Version = types.StringPointerValue(offer.Version) + return + } + } + } + + diags.AddWarning("Failed to get plan_name and version", fmt.Sprintf("Couldn't find plan_name and version for plan_id = %s", planId)) +} diff --git a/stackit/services/redis/redis_acc_test.go b/stackit/services/redis/redis_acc_test.go index 5ad39ca6..81ecfe62 100644 --- a/stackit/services/redis/redis_acc_test.go +++ b/stackit/services/redis/redis_acc_test.go @@ -181,9 +181,8 @@ func TestAccRedisResource(t *testing.T) { } return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil }, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plan_name", "version"}, + ImportState: true, + ImportStateVerify: true, }, { ResourceName: "stackit_redis_credentials.credentials",