From 765574ec7b67912dbb3e33bb13489855f6978e84 Mon Sep 17 00:00:00 2001 From: Guillaume BEAUMONT <7782511+gbeaumont93@users.noreply.github.com> Date: Mon, 2 Sep 2024 09:54:36 +0200 Subject: [PATCH] delete element when creation error (#16) --- attachment.go | 6 ++++++ elements.go | 6 +++--- node.go | 6 ++++++ transport.go | 6 ++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/attachment.go b/attachment.go index fa4da4a..382a3d4 100644 --- a/attachment.go +++ b/attachment.go @@ -69,6 +69,12 @@ func (c *Client) CreateAttachment(ctx context.Context, payload models.CreateAtta var success bool attachmentPolled, success = WaitUntilFinishedTask(ctx, c, workspaceID, attachment.Data.ID.String(), models.AdministrativeStateDeployed, checkAttachmentFinishedTask) if !success { + // if attachment creation operation failed then we try to delete it + if attachmentPolled != nil { + if _, err := c.DeleteAttachment(ctx, workspaceID, attachmentPolled.ID.String()); err != nil { + return nil, fmt.Errorf("Attachment did not reach '%s' state in time and cannot be reverted. attachment_id is '%s'", models.AdministrativeStateDeployed, attachmentPolled.ID) + } + } return nil, fmt.Errorf("Attachment did not reach '%s' state in time.", models.AdministrativeStateDeployed) } } diff --git a/elements.go b/elements.go index 9465392..fc618a2 100644 --- a/elements.go +++ b/elements.go @@ -39,11 +39,11 @@ type Element interface { GetState() models.AdministrativeState } -func WaitUntilFinishedTask[T Element](ctx context.Context, client *Client, workspaceID, elementID string, waiterOptions models.AdministrativeState, funcToCall func(context.Context, *Client, string, string, models.AdministrativeState) (T, bool)) (T, bool) { +func WaitUntilFinishedTask[T Element](ctx context.Context, client *Client, workspaceID, elementID string, waiterOptions models.AdministrativeState, getElement func(context.Context, *Client, string, string, models.AdministrativeState) (T, bool)) (T, bool) { var lastElement T for i := 0; i < client.poll.maxRetry; i++ { - // Retrieve the element and check if it is in the required administrative state - element, finishedTask := funcToCall(ctx, client, workspaceID, elementID, waiterOptions) + // retrieve the element and check if it is in the required administrative state + element, finishedTask := getElement(ctx, client, workspaceID, elementID, waiterOptions) lastElement = element if finishedTask { return lastElement, true diff --git a/node.go b/node.go index a5073d3..5309098 100644 --- a/node.go +++ b/node.go @@ -69,6 +69,12 @@ func (c *Client) CreateNode(ctx context.Context, payload models.CreateNode, work var success bool nodePolled, success = WaitUntilFinishedTask(ctx, c, workspaceID, node.Data.ID.String(), models.AdministrativeStateDeployed, checkNodeFinishedTask) if !success { + // if node creation operation failed then we try to delete it + if nodePolled != nil { + if _, err := c.DeleteNode(ctx, workspaceID, nodePolled.ID.String()); err != nil { + return nil, fmt.Errorf("Node did not reach '%s' state in time and cannot be reverted. node_id is '%s'", models.AdministrativeStateDeployed, nodePolled.ID) + } + } return nil, fmt.Errorf("Node did not reach '%s' state in time.", models.AdministrativeStateDeployed) } } diff --git a/transport.go b/transport.go index 5e40eed..e55cc67 100644 --- a/transport.go +++ b/transport.go @@ -69,6 +69,12 @@ func (c *Client) CreateTransport(ctx context.Context, payload models.CreateTrans var success bool transportPolled, success = WaitUntilFinishedTask(ctx, c, workspaceID, transport.Data.ID.String(), models.AdministrativeStateDeployed, checkTransportFinishedTask) if !success { + // if transport creation operation failed then we try to delete it + if transportPolled != nil { + if _, err := c.DeleteTransport(ctx, workspaceID, transportPolled.ID.String()); err != nil { + return nil, fmt.Errorf("Transport did not reach '%s' state in time and cannot be reverted. transport_id is '%s'", models.AdministrativeStateDeployed, transportPolled.ID) + } + } return nil, fmt.Errorf("Transport did not reach '%s' state in time.", models.AdministrativeStateDeployed) } }