Skip to content

Commit

Permalink
delete element when creation error (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbeaumont93 authored Sep 2, 2024
1 parent 3957fc8 commit 765574e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 6 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 765574e

Please sign in to comment.