From b2014cafe9ec1ad944aea4ff8a0cf11ecefb1b25 Mon Sep 17 00:00:00 2001 From: Tom Watt Date: Sat, 14 Dec 2024 08:09:24 +0000 Subject: [PATCH] refactor: move dispatch logic into Retry to cover any issues during overall update Signed-off-by: Tom Watt --- buildkite/resource_cluster_queue.go | 103 ++++++++++++++-------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/buildkite/resource_cluster_queue.go b/buildkite/resource_cluster_queue.go index 65e09144..0e8eec51 100644 --- a/buildkite/resource_cluster_queue.go +++ b/buildkite/resource_cluster_queue.go @@ -244,62 +244,60 @@ func (cq *clusterQueueResource) Update(ctx context.Context, req resource.UpdateR var r *updateClusterQueueResponse - // Extract the planned and state values for DispatchPaused attribute - // to compare if the Queue should be paused or resumed - // if neither do nothing - planDispatchPaused = plan.DispatchPaused.ValueBool() - stateDispatchPaused = state.DispatchPaused.ValueBool() - - // Check the planned value against the current state value - // Planned to be true (changing from false to true) - if planDispatchPaused && !stateDispatchPaused { - err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { - log.Printf("Pausing dispatch for cluster queue %s", state.Key) - _, err := pauseDispatchClusterQueue( - ctx, - cq.client.genqlient, - state.Id.ValueString(), - ) - - return retryContextError(err) - }) - if err != nil { - resp.Diagnostics.AddError( - "Unable to pause cluster queue dispatch", - fmt.Sprintf("Unable to pause dispatch for cluster queue: %s", err.Error()), - ) - return - } - state.DispatchPaused = types.BoolValue(plan.DispatchPaused.ValueBool()) - - } - - // Planned to be false (changing from true to false) - if !planDispatchPaused && stateDispatchPaused { - err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { - log.Printf("Resuming dispatch for cluster queue %s", state.Key) - _, err := resumeDispatchClusterQueue( - ctx, - cq.client.genqlient, - state.Id.ValueString(), - ) - - return retryContextError(err) - }) - if err != nil { - resp.Diagnostics.AddError( - "Unable to resume cluster queue dispatch", - fmt.Sprintf("Unable to resume dispatch for cluster queue: %s", err.Error()), - ) - return - } - state.DispatchPaused = types.BoolValue(plan.DispatchPaused.ValueBool()) - } - err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { org, err := cq.client.GetOrganizationID() if err == nil { log.Printf("Updating cluster queue %s ...", state.Id.ValueString()) + + // Extract the planned and state values for DispatchPaused attribute + // to compare if the Queue should be paused or resumed + // if neither do nothing + planDispatchPaused = plan.DispatchPaused.ValueBool() + stateDispatchPaused = state.DispatchPaused.ValueBool() + + // Check the planned value against the current state value + // Planned to be true (changing from false to true) + if planDispatchPaused && !stateDispatchPaused { + err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { + log.Printf("Pausing dispatch for cluster queue %s", state.Key) + _, err := pauseDispatchClusterQueue( + ctx, + cq.client.genqlient, + state.Id.ValueString(), + ) + + return retryContextError(err) + }) + if err != nil { + resp.Diagnostics.AddError( + "Unable to pause cluster queue dispatch", + fmt.Sprintf("Unable to pause dispatch for cluster queue: %s", err.Error()), + ) + return retryContextError(err) + } + } + + // Planned to be false (changing from true to false) + if !planDispatchPaused && stateDispatchPaused { + err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { + log.Printf("Resuming dispatch for cluster queue %s", state.Key) + _, err := resumeDispatchClusterQueue( + ctx, + cq.client.genqlient, + state.Id.ValueString(), + ) + + return retryContextError(err) + }) + if err != nil { + resp.Diagnostics.AddError( + "Unable to resume cluster queue dispatch", + fmt.Sprintf("Unable to resume dispatch for cluster queue: %s", err.Error()), + ) + return retryContextError(err) + } + } + r, err = updateClusterQueue(ctx, cq.client.genqlient, *org, @@ -319,6 +317,7 @@ func (cq *clusterQueueResource) Update(ctx context.Context, req resource.UpdateR } state.Description = types.StringPointerValue(r.ClusterQueueUpdate.ClusterQueue.Description) + state.DispatchPaused = types.BoolValue(r.ClusterQueueUpdate.ClusterQueue.DispatchPaused) resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) }