/
activity.go
202 lines (174 loc) · 4.72 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package client
import (
"context"
"fmt"
"strings"
"time"
"github.com/sethvargo/go-retry"
)
type ActivityClient struct {
c *Client
}
func (c *Client) Activity() *ActivityClient {
return &ActivityClient{c}
}
type Activity struct {
ID string `terraform:"id"`
TenantId string `terraform:"tenant_id"`
Description string `terraform:"description"`
Type string `terraform:"type"`
Tags []string `terraform:"tags"`
CreationDate time.Time `terraform:"creation_date"`
ConcernedItems []ActivityConcernedItem `terraform:"concerned_items"`
State map[string]ActivityState `terraform:"-"`
}
type ActivityState struct {
StartDate string
StopDate string
Reason string
Result string
Progression float64
}
type ActivityConcernedItem struct {
ID string `terraform:"id"`
Type string `terraform:"type"`
}
func (c *ActivityClient) List(ctx context.Context, filter *struct{}) ([]*Activity, error) {
r := c.c.newRequest("GET", "/activity/v1/activities")
resp, err := c.c.doRequest(ctx, r)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, err
}
var out []*Activity
if err := decodeBody(resp, &out); err != nil {
return nil, err
}
return out, nil
}
func (c *ActivityClient) Read(ctx context.Context, id string) (*Activity, error) {
r := c.c.newRequest("GET", "/activity/v1/activities/%s", id)
resp, err := c.c.doRequest(ctx, r)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
found, err := requireNotFoundOrOK(resp, 404)
if err != nil || !found {
return nil, err
}
var out Activity
if err := decodeBody(resp, &out); err != nil {
return nil, err
}
return &out, nil
}
type ActivityCompletionError struct {
message string
activity *Activity
}
const activityErrorMessage = `
Description: %s
Tenant ID: %q
Created at %s
Type: %s
Tags: %s
Concerned Items:
%s
%s`
func (a *ActivityCompletionError) Error() string {
message := a.message
if message == "" {
message = fmt.Sprintf("an error occured while waiting for completion of activity %q:", a.activity.ID)
}
if a.activity != nil {
var concernedItemMessage []string
for _, concernedItem := range a.activity.ConcernedItems {
concernedItemMessage = append(
concernedItemMessage,
fmt.Sprintf(" - ID: %q\n Type: %s", concernedItem.ID, concernedItem.Type),
)
}
if len(concernedItemMessage) == 0 {
concernedItemMessage = []string{" none"}
}
var stateMessage []string
for name, state := range a.activity.State {
stateMessage = append(
stateMessage,
fmt.Sprintf(
" State: %s\n Result: %s\n Reason: %s\n Started at %s\n Stopped at %s",
name,
state.Result,
state.Reason,
state.StartDate,
state.StopDate,
),
)
}
message += fmt.Sprintf(
activityErrorMessage,
a.activity.Description,
a.activity.TenantId,
a.activity.CreationDate.String(),
a.activity.Type,
strings.Join(a.activity.Tags, ", "),
strings.Join(concernedItemMessage, "\n"),
strings.Join(stateMessage, "\n"),
)
}
return message
}
func (c *ActivityClient) WaitForCompletion(ctx context.Context, id string, options *WaiterOptions) (*Activity, error) {
b := retry.NewFibonacci(1 * time.Second)
b = retry.WithCappedDuration(30*time.Second, b)
var res *Activity
var count int
err := retry.Do(ctx, b, func(ctx context.Context) error {
count++
activity, err := c.Read(ctx, id)
if err != nil {
return options.retryableError(&ActivityCompletionError{
message: fmt.Sprintf("an error occured while getting the status of activity %q: %s", id, err),
activity: activity,
})
}
if activity == nil {
err := &ActivityCompletionError{
message: fmt.Sprintf("the activity %q could not be found", id),
}
if count == 1 {
return options.retryableError(err)
}
return options.error(err)
}
if len(activity.State) != 1 {
return options.retryableError(&ActivityCompletionError{
message: fmt.Sprintf("unexpected state for activity %q: %v", id, activity.State),
})
}
res = activity
for state := range activity.State {
switch state {
case "completed":
options.log(fmt.Sprintf("the activity %q is completed", id))
return nil
case "failed":
return options.error(&ActivityCompletionError{
activity: activity,
})
default:
return options.retryableError(&ActivityCompletionError{
message: fmt.Sprintf("unexpected state for activity %q: %v", id, state),
activity: activity,
})
}
}
options.log(fmt.Sprintf("no state found for activity %q", id))
return nil
})
return res, err
}