Extend DSA Reads 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
This commit is contained in:
parent
7f35b5a1dc
commit
386ccac6b4
18 changed files with 240 additions and 42 deletions
|
|
@ -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...)
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue