Skip to content

Commit

Permalink
fix: lookup rebalancing scheduled job by schedule ID if deleted or re…
Browse files Browse the repository at this point in the history
…created
  • Loading branch information
jansyk13 committed Apr 19, 2024
1 parent ecf6f19 commit a9f33e5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
26 changes: 24 additions & 2 deletions castai/resource_rebalancing_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,31 @@ func resourceRebalancingJobCreate(ctx context.Context, d *schema.ResourceData, m
}

func resourceRebalancingJobRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*ProviderConfig).api
clusterID := d.Get("cluster_id").(string)
job, found, err := getRebalancingJobById(ctx, meta.(*ProviderConfig).api, clusterID, d.Id())
job, found, err := getRebalancingJobById(ctx, client, clusterID, d.Id())
if err != nil {
return diag.FromErr(err)
}
if !d.IsNewResource() && !found {
rebalancingScheduleID := d.Get("rebalancing_schedule_id").(string)
params := &sdk.ScheduledRebalancingAPIListRebalancingJobsParams{
RebalancingScheduleId: lo.ToPtr(rebalancingScheduleID),
}
listResp, err := client.ScheduledRebalancingAPIListRebalancingJobsWithResponse(ctx, clusterID, params)
if checkErr := sdk.CheckOKResponse(listResp, err); checkErr != nil {
return diag.FromErr(checkErr)
}
for _, j := range *listResp.JSON200.Jobs {
if *j.RebalancingScheduleId == rebalancingScheduleID {
if err := rebalancingJobToState(&j, d); err != nil {
return diag.FromErr(err)
}

return nil
}
}

tflog.Warn(ctx, "Rebalancing job not found, removing from state", map[string]any{"id": d.Id()})
d.SetId("")
return nil
Expand Down Expand Up @@ -189,7 +208,10 @@ func getRebalancingJobByScheduleName(ctx context.Context, client *sdk.ClientWith
return nil, fmt.Errorf("getting schedule: %w", err)
}

resp, err := client.ScheduledRebalancingAPIListRebalancingJobsWithResponse(ctx, clusterID)
params := sdk.ScheduledRebalancingAPIListRebalancingJobsParams{
RebalancingScheduleId: schedule.Id,
}
resp, err := client.ScheduledRebalancingAPIListRebalancingJobsWithResponse(ctx, clusterID, &params)
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
return nil, checkErr
}
Expand Down
5 changes: 5 additions & 0 deletions castai/sdk/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 27 additions & 7 deletions castai/sdk/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions castai/sdk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a9f33e5

Please sign in to comment.