Skip to content

Commit d6ead16

Browse files
authored
add poll timeout for update (#137)
* add poll timeout for update * add time start
1 parent aac6550 commit d6ead16

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

internal/provider/resources/space_gitlab_enterprise_repository_resource.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Read(ctx context.Context
214214

215215
func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
216216
var data TorqueSpaceGitlabEnterpriseRepositoryResourceModel
217-
217+
const (
218+
StatusSyncing = "Syncing"
219+
StatusConnected = "Connected"
220+
Interval = 4 * time.Second
221+
)
218222
// Read Terraform plan data into the model
219223
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
220224

@@ -227,9 +231,28 @@ func (r *TorqueSpaceGitlabEnterpriseRepositoryResource) Update(ctx context.Conte
227231
agents = append(agents, strings.Trim(agent.String(), "\""))
228232
}
229233
}
234+
start := time.Now()
230235
err := r.client.UpdateRepoConfiguration(data.SpaceName.ValueString(), data.RepositoryName.ValueString(),
231236
data.CredentialName.ValueString(), agents, data.UseAllAgents.ValueBool())
232237
if err != nil {
238+
repo, err := r.client.GetRepoDetails(data.SpaceName.ValueString(), data.RepositoryName.ValueString())
239+
if repo.Status == StatusSyncing {
240+
timeout := time.Duration(data.TimeOut.ValueInt32()) * time.Minute
241+
for time.Since(start) < timeout {
242+
repo, err := r.client.GetRepoDetails(data.SpaceName.ValueString(), data.RepositoryName.ValueString())
243+
if err != nil {
244+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error while polling repository status: %s", err))
245+
return
246+
}
247+
if repo.Status == StatusConnected {
248+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
249+
return
250+
}
251+
time.Sleep(Interval)
252+
}
253+
resp.Diagnostics.AddError("Sync Timeout", "Timed out while syncing repository")
254+
return
255+
}
233256
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update repository configuration, got error: %s", err))
234257
return
235258
}

0 commit comments

Comments
 (0)