Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions stackit/services/logme/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
35 changes: 31 additions & 4 deletions stackit/services/logme/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
}
5 changes: 2 additions & 3 deletions stackit/services/logme/logme_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions stackit/services/mariadb/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
35 changes: 31 additions & 4 deletions stackit/services/mariadb/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
}
5 changes: 2 additions & 3 deletions stackit/services/mariadb/mariadb_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions stackit/services/opensearch/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
35 changes: 31 additions & 4 deletions stackit/services/opensearch/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
}
5 changes: 2 additions & 3 deletions stackit/services/opensearch/opensearch_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions stackit/services/postgresql/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
35 changes: 31 additions & 4 deletions stackit/services/postgresql/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}

Expand Down Expand Up @@ -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))
}
5 changes: 2 additions & 3 deletions stackit/services/postgresql/postgresql_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions stackit/services/rabbitmq/instance/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
Loading