Skip to content

Commit 7f0f3c7

Browse files
Pauline EspalieuPauline ESPALIEU
andauthored
change elementOptions to only take into consideration deployed, delet… (#14)
Co-authored-by: Pauline ESPALIEU <paulineespalieu@mbppaulespalieu.home>
1 parent b6fb4fe commit 7f0f3c7

File tree

10 files changed

+219
-384
lines changed

10 files changed

+219
-384
lines changed

attachment.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ import (
1212
"github.com/intercloud/autonomi-sdk/models"
1313
)
1414

15-
func checkAttachmentAdministrativeState(ctx context.Context, c *Client, workspaceID, attachmentID string, state models.AdministrativeState) (*models.Attachment, bool) {
15+
func checkAttachmentFinishedTask(ctx context.Context, c *Client, workspaceID, attachmentID string, waiterOptionState models.AdministrativeState) (*models.Attachment, bool) {
1616
attachment, err := c.GetAttachment(ctx, workspaceID, attachmentID)
1717
if err != nil {
1818
// if wanted state is deleted and the attachment is in this state, api has returned 404
19-
if state == models.AdministrativeStateDeleted && strings.Contains(err.Error(), "status: 404") {
20-
return nil, true
19+
if waiterOptionState == models.AdministrativeStateDeleted {
20+
if strings.Contains(err.Error(), "status: 404") {
21+
return nil, true
22+
}
2123
}
2224
log.Printf("an error occurs when getting attachment, err: %s" + err.Error())
2325
return nil, false
2426
}
2527

26-
return attachment, attachment.State == state
28+
return attachment, waiterOptionState == attachment.State
2729
}
2830

29-
// CreateAttachment creates asynchronously an attachment. The attachment returned will depend of the administrative state passed in options.
30-
// If none is passed models.AdministrativeStateDeployed will be set by default.
31-
// The valid administrative states options for a attachment creation are [models.AdministrativeStateCreationPending, models.AdministrativeStateCreationProceed, models.AdministrativeStateCreationError, models.AdministrativeStateDeployed]
31+
// CreateAttachment creates asynchronously an attachment. The attachment returned will depend of the passed option.
32+
// If none is passed the attachment will be returned once created in database with administrative state creation_pending.
33+
// If the option WithWaitUntilElementDeployed() is passed, the attachment will be returned when its state reach deployed or creation_error.
34+
// If the option WithWaitUntilElementUndeployed() is passed, it will not be considered hence the attachment returned will be in state creation_pending
3235
func (c *Client) CreateAttachment(ctx context.Context, payload models.CreateAttachment, workspaceID string, options ...OptionElement) (*models.Attachment, error) {
3336
body := new(bytes.Buffer)
3437
err := json.NewEncoder(body).Encode(&payload)
@@ -45,14 +48,6 @@ func (c *Client) CreateAttachment(ctx context.Context, payload models.CreateAtta
4548
o(attachmentOptions)
4649
}
4750

48-
if attachmentOptions.administrativeState == "" {
49-
attachmentOptions.administrativeState = models.AdministrativeStateDeployed
50-
}
51-
52-
if _, ok := validCreationAdministrativeStates[attachmentOptions.administrativeState]; !ok {
53-
return nil, ErrCreationAdministrativeState
54-
}
55-
5651
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/accounts/%s/workspaces/%s/attachments", c.hostURL, c.accountID, workspaceID), body)
5752
if err != nil {
5853
return nil, err
@@ -69,9 +64,13 @@ func (c *Client) CreateAttachment(ctx context.Context, payload models.CreateAtta
6964
return nil, err
7065
}
7166

72-
attachmentPolled, success := WaitForAdministrativeState(ctx, c, workspaceID, attachment.Data.ID.String(), attachmentOptions.administrativeState, checkAttachmentAdministrativeState)
73-
if !success {
74-
return nil, fmt.Errorf("Attachment did not reach '%s' state in time.", attachmentOptions.administrativeState)
67+
var attachmentPolled = &attachment.Data
68+
if attachmentOptions.waitUntilElementDeployed {
69+
var success bool
70+
attachmentPolled, success = WaitUntilFinishedTask(ctx, c, workspaceID, attachment.Data.ID.String(), models.AdministrativeStateDeployed, checkAttachmentFinishedTask)
71+
if !success {
72+
return nil, fmt.Errorf("Attachment did not reach '%s' state in time.", models.AdministrativeStateDeployed)
73+
}
7574
}
7675

7776
return attachmentPolled, nil
@@ -97,23 +96,16 @@ func (c *Client) GetAttachment(ctx context.Context, workspaceID, attachmentID st
9796
return &attachment.Data, err
9897
}
9998

100-
// DeleteAttachment deletes asynchronously a cloud attachment. The attachment returned will depend of the administrative state passed in options.
101-
// If none is passed models.AdministrativeStateDeleted will be set by default.
102-
// The valid administrative states options for a attachment creation are [ models.AdministrativeStateDeletePending, models.AdministrativeStateDeleteProceed, models.AdministrativeStateDeleteError, models.AdministrativeStateDeleted]
99+
// DeleteAttachment deletes asynchronously an attachment. The attachment returned will depend of the option passed.
100+
// If none is passed the attachment will be returned once the request accepted, its state will be delete_pending
101+
// If the option WithWaitUntilElementUndeployed() is passed, the attachment won't be returned as it would have been deleted. However, if an error is triggered, an object could be returned with a delete_error state.
102+
// If the option WithWaitUntilElementDeployed() is passed, it will not be considered hence the attachment returned will be in state delete_pending
103103
func (c *Client) DeleteAttachment(ctx context.Context, workspaceID, attachmentID string, options ...OptionElement) (*models.Attachment, error) {
104104
attachmentOptions := &elementOptions{}
105105
for _, o := range options {
106106
o(attachmentOptions)
107107
}
108108

109-
if attachmentOptions.administrativeState == "" {
110-
attachmentOptions.administrativeState = models.AdministrativeStateDeleted
111-
}
112-
113-
if _, ok := validDeletionAdministrativeStates[attachmentOptions.administrativeState]; !ok {
114-
return nil, ErrDeletionAdministrativeState
115-
}
116-
117109
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/accounts/%s/workspaces/%s/attachments/%s", c.hostURL, c.accountID, workspaceID, attachmentID), nil)
118110
if err != nil {
119111
return nil, err
@@ -130,9 +122,13 @@ func (c *Client) DeleteAttachment(ctx context.Context, workspaceID, attachmentID
130122
return nil, err
131123
}
132124

133-
attachmentPolled, success := WaitForAdministrativeState(ctx, c, workspaceID, attachment.Data.ID.String(), attachmentOptions.administrativeState, checkAttachmentAdministrativeState)
134-
if !success {
135-
return nil, fmt.Errorf("Attachment did not reach '%s' state in time.", attachmentOptions.administrativeState)
125+
var attachmentPolled = &attachment.Data
126+
if attachmentOptions.waitUntilElementUndeployed {
127+
var success bool
128+
attachmentPolled, success = WaitUntilFinishedTask(ctx, c, workspaceID, attachment.Data.ID.String(), models.AdministrativeStateDeleted, checkAttachmentFinishedTask)
129+
if !success {
130+
return nil, fmt.Errorf("Attachment did not reach '%s' state in time.", models.AdministrativeStateDeleted)
131+
}
136132
}
137133

138134
// If the attachment was deleted and we were waiting for the "deleted" state,

0 commit comments

Comments
 (0)