Skip to content

Commit 7b27c57

Browse files
tasdomas0x2b3bfa0
andauthored
Restructure how resource allocation/deallocation/read steps are defined and executed (#645)
* Restructure how resource allocation/deallocation/read steps are defined and executed. Defining steps as a slice of structures at least removes the necessity to renumber the progress indicator when inserting new steps (or removin them). * Fix aws allocation steps. * Fix typo. * Handle deletion of non-existant buckets in aws. * Apparently we're using os.Env to pass parameters between resources the in the k8s implementation of the task resource. This is just a temporary fix - the k8s provider needs a serious refactoring. * Revert change. Co-authored-by: Helio Machado <0x2b3bfa0+git@googlemail.com>
1 parent 1e888fb commit 7b27c57

File tree

7 files changed

+542
-456
lines changed

7 files changed

+542
-456
lines changed

task/aws/resources/resource_bucket.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import (
1414
"terraform-provider-iterative/task/common"
1515
)
1616

17+
const (
18+
errNoSuchBucket = "NoSuchBucket"
19+
errNotFound = "NotFound"
20+
errBucketAlreadyOwnedByYou = "BucketAlreadyOwnedByYou"
21+
)
22+
1723
func ListBuckets(ctx context.Context, client *client.Client) ([]common.Identifier, error) {
1824
output, err := client.Services.S3.ListBuckets(ctx, &s3.ListBucketsInput{})
1925
if err != nil {
@@ -55,8 +61,7 @@ func (b *Bucket) Create(ctx context.Context) error {
5561
}
5662

5763
if _, err := b.Client.Services.S3.CreateBucket(ctx, &createInput); err != nil {
58-
var e smithy.APIError
59-
if errors.As(err, &e) && e.ErrorCode() == "BucketAlreadyOwnedByYou" {
64+
if errorCodeIs(err, errBucketAlreadyOwnedByYou) {
6065
return b.Read(ctx)
6166
}
6267
return err
@@ -79,8 +84,7 @@ func (b *Bucket) Read(ctx context.Context) error {
7984
}
8085

8186
if _, err := b.Client.Services.S3.HeadBucket(ctx, &input); err != nil {
82-
var e smithy.APIError
83-
if errors.As(err, &e) && e.ErrorCode() == "NotFound" {
87+
if errorCodeIs(err, errNotFound) {
8488
return common.NotFoundError
8589
}
8690
return err
@@ -101,13 +105,11 @@ func (b *Bucket) Delete(ctx context.Context) error {
101105

102106
for paginator := s3.NewListObjectsV2Paginator(b.Client.Services.S3, &listInput); paginator.HasMorePages(); {
103107
page, err := paginator.NextPage(ctx)
104-
108+
if errorCodeIs(err, errNoSuchBucket) {
109+
b.Resource = nil
110+
return nil
111+
}
105112
if err != nil {
106-
var e smithy.APIError
107-
if errors.As(err, &e) && e.ErrorCode() == "NoSuchBucket" {
108-
b.Resource = nil
109-
return nil
110-
}
111113
return err
112114
}
113115

@@ -139,10 +141,24 @@ func (b *Bucket) Delete(ctx context.Context) error {
139141
}
140142

141143
_, err := b.Client.Services.S3.DeleteBucket(ctx, &deleteInput)
144+
if errorCodeIs(err, errNoSuchBucket) {
145+
b.Resource = nil
146+
return nil
147+
}
142148
if err != nil {
143149
return err
144150
}
145151

146152
b.Resource = nil
147153
return nil
148154
}
155+
156+
// errorCodeIs checks if the provided error is an AWS API error
157+
// and its error code matches the supplied value.
158+
func errorCodeIs(err error, code string) bool {
159+
var e smithy.APIError
160+
if errors.As(err, &e) {
161+
return e.ErrorCode() == code
162+
}
163+
return false
164+
}

task/aws/task.go

Lines changed: 113 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -109,54 +109,51 @@ type Task struct {
109109

110110
func (t *Task) Create(ctx context.Context) error {
111111
logrus.Info("Creating resources...")
112-
logrus.Info("[1/12] Parsing PermissionSet...")
113-
if err := t.DataSources.PermissionSet.Read(ctx); err != nil {
114-
return err
115-
}
116-
logrus.Info("[2/12] Importing DefaultVPC...")
117-
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
118-
return err
119-
}
120-
logrus.Info("[3/12] Importing DefaultVPCSubnets...")
121-
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
122-
return err
123-
}
124-
logrus.Info("[4/12] Reading Image...")
125-
if err := t.DataSources.Image.Read(ctx); err != nil {
126-
return err
127-
}
128-
logrus.Info("[5/12] Creating Bucket...")
129-
if err := t.Resources.Bucket.Create(ctx); err != nil {
130-
return err
131-
}
132-
logrus.Info("[6/12] Creating SecurityGroup...")
133-
if err := t.Resources.SecurityGroup.Create(ctx); err != nil {
134-
return err
135-
}
136-
logrus.Info("[7/12] Creating KeyPair...")
137-
if err := t.Resources.KeyPair.Create(ctx); err != nil {
138-
return err
139-
}
140-
logrus.Info("[8/12] Reading Credentials...")
141-
if err := t.DataSources.Credentials.Read(ctx); err != nil {
142-
return err
143-
}
144-
logrus.Info("[9/12] Creating LaunchTemplate...")
145-
if err := t.Resources.LaunchTemplate.Create(ctx); err != nil {
146-
return err
147-
}
148-
logrus.Info("[10/12] Creating AutoScalingGroup...")
149-
if err := t.Resources.AutoScalingGroup.Create(ctx); err != nil {
150-
return err
151-
}
152-
logrus.Info("[11/12] Uploading Directory...")
112+
steps := []common.Step{{
113+
Description: "Parsing PermissionSet...",
114+
Action: t.DataSources.PermissionSet.Read,
115+
}, {
116+
Description: "Importing DefaultVPC...",
117+
Action: t.DataSources.DefaultVPC.Read,
118+
}, {
119+
Description: "Importing DefaultVPCSubnets...",
120+
Action: t.DataSources.DefaultVPCSubnets.Read,
121+
}, {
122+
Description: "Reading Image...",
123+
Action: t.DataSources.Image.Read,
124+
}, {
125+
Description: "Creating Bucket...",
126+
Action: t.Resources.Bucket.Create,
127+
}, {
128+
Description: "Creating SecurityGroup...",
129+
Action: t.Resources.SecurityGroup.Create,
130+
}, {
131+
Description: "Creating KeyPair...",
132+
Action: t.Resources.KeyPair.Create,
133+
}, {
134+
Description: "Reading Credentials...",
135+
Action: t.DataSources.Credentials.Read,
136+
}, {
137+
Description: "Creating LaunchTemplate...",
138+
Action: t.Resources.LaunchTemplate.Create,
139+
}, {
140+
Description: "Creating AutoScalingGroup...",
141+
Action: t.Resources.AutoScalingGroup.Create,
142+
}}
143+
153144
if t.Attributes.Environment.Directory != "" {
154-
if err := t.Push(ctx, t.Attributes.Environment.Directory); err != nil {
155-
return err
156-
}
157-
}
158-
logrus.Info("[12/12] Starting task...")
159-
if err := t.Start(ctx); err != nil {
145+
steps = append(steps, common.Step{
146+
Description: "Uploading Directory...",
147+
Action: func(ctx context.Context) error {
148+
return t.Push(ctx, t.Attributes.Environment.Directory)
149+
},
150+
})
151+
}
152+
steps = append(steps, common.Step{
153+
Description: "Starting task...",
154+
Action: t.Start,
155+
})
156+
if err := common.RunSteps(ctx, steps); err != nil {
160157
return err
161158
}
162159
logrus.Info("Creation completed")
@@ -168,40 +165,35 @@ func (t *Task) Create(ctx context.Context) error {
168165

169166
func (t *Task) Read(ctx context.Context) error {
170167
logrus.Info("Reading resources... (this may happen several times)")
171-
logrus.Info("[1/9] Reading DefaultVPC...")
172-
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
173-
return err
174-
}
175-
logrus.Info("[2/9] Reading DefaultVPCSubnets...")
176-
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
177-
return err
178-
}
179-
logrus.Info("[3/9] Reading Image...")
180-
if err := t.DataSources.Image.Read(ctx); err != nil {
181-
return err
182-
}
183-
logrus.Info("[4/9] Reading Bucket...")
184-
if err := t.Resources.Bucket.Read(ctx); err != nil {
185-
return err
186-
}
187-
logrus.Info("[5/9] Reading SecurityGroup...")
188-
if err := t.Resources.SecurityGroup.Read(ctx); err != nil {
189-
return err
190-
}
191-
logrus.Info("[6/9] Reading KeyPair...")
192-
if err := t.Resources.KeyPair.Read(ctx); err != nil {
193-
return err
194-
}
195-
logrus.Info("[7/9] Reading Credentials...")
196-
if err := t.DataSources.Credentials.Read(ctx); err != nil {
197-
return err
198-
}
199-
logrus.Info("[8/9] Reading LaunchTemplate...")
200-
if err := t.Resources.LaunchTemplate.Read(ctx); err != nil {
201-
return err
202-
}
203-
logrus.Info("[9/9] Reading AutoScalingGroup...")
204-
if err := t.Resources.AutoScalingGroup.Read(ctx); err != nil {
168+
steps := []common.Step{{
169+
Description: "Reading DefaultVPC...",
170+
Action: t.DataSources.DefaultVPC.Read,
171+
}, {
172+
Description: "Reading DefaultVPCSubnets...",
173+
Action: t.DataSources.DefaultVPCSubnets.Read,
174+
}, {
175+
Description: "Reading Image...",
176+
Action: t.DataSources.Image.Read,
177+
}, {
178+
Description: "Reading Bucket...",
179+
Action: t.Resources.Bucket.Read,
180+
}, {
181+
Description: "Reading SecurityGroup...",
182+
Action: t.Resources.SecurityGroup.Read,
183+
}, {
184+
Description: "Reading KeyPair...",
185+
Action: t.Resources.KeyPair.Read,
186+
}, {
187+
Description: "Reading Credentials...",
188+
Action: t.DataSources.Credentials.Read,
189+
}, {
190+
Description: "Reading LaunchTemplate...",
191+
Action: t.Resources.LaunchTemplate.Read,
192+
}, {
193+
Description: "Reading AutoScalingGroup...",
194+
Action: t.Resources.AutoScalingGroup.Read,
195+
}}
196+
if err := common.RunSteps(ctx, steps); err != nil {
205197
return err
206198
}
207199
logrus.Info("Read completed")
@@ -213,41 +205,49 @@ func (t *Task) Read(ctx context.Context) error {
213205

214206
func (t *Task) Delete(ctx context.Context) error {
215207
logrus.Info("Deleting resources...")
216-
logrus.Info("[1/8] Downloading Directory...")
208+
steps := []common.Step{}
217209
if t.Read(ctx) == nil {
218210
if t.Attributes.Environment.DirectoryOut != "" {
219-
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
220-
return err
221-
}
211+
steps = []common.Step{{
212+
Description: "Downloading Directory...",
213+
Action: func(ctx context.Context) error {
214+
err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut)
215+
if err != nil && err != common.NotFoundError {
216+
return err
217+
}
218+
return nil
219+
}}}
222220
}
223-
logrus.Info("[2/8] Emptying Bucket...")
224-
225-
if err := machine.Delete(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"]); err != nil && err != common.NotFoundError {
226-
return err
227-
}
228-
}
229-
logrus.Info("[3/8] Deleting AutoScalingGroup...")
230-
if err := t.Resources.AutoScalingGroup.Delete(ctx); err != nil {
231-
return err
232-
}
233-
logrus.Info("[4/8] Deleting LaunchTemplate...")
234-
if err := t.Resources.LaunchTemplate.Delete(ctx); err != nil {
235-
return err
236-
}
237-
logrus.Info("[5/8] Deleting KeyPair...")
238-
if err := t.Resources.KeyPair.Delete(ctx); err != nil {
239-
return err
240-
}
241-
logrus.Info("[6/8] Deleting SecurityGroup...")
242-
if err := t.Resources.SecurityGroup.Delete(ctx); err != nil {
243-
return err
244-
}
245-
logrus.Info("[7/8] Reading Credentials...")
246-
if err := t.DataSources.Credentials.Read(ctx); err != nil {
247-
return err
248-
}
249-
logrus.Info("[8/8] Deleting Bucket...")
250-
if err := t.Resources.Bucket.Delete(ctx); err != nil {
221+
steps = append(steps, common.Step{
222+
Description: "Emptying Bucket...",
223+
Action: func(ctx context.Context) error {
224+
err := machine.Delete(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"])
225+
if err != nil && err != common.NotFoundError {
226+
return err
227+
}
228+
return nil
229+
}})
230+
}
231+
steps = append(steps, []common.Step{{
232+
Description: "Deleting AutoScalingGroup...",
233+
Action: t.Resources.AutoScalingGroup.Delete,
234+
}, {
235+
Description: "Deleting LaunchTemplate...",
236+
Action: t.Resources.LaunchTemplate.Delete,
237+
}, {
238+
Description: "Deleting KeyPair...",
239+
Action: t.Resources.KeyPair.Delete,
240+
}, {
241+
Description: "Deleting SecurityGroup...",
242+
Action: t.Resources.SecurityGroup.Delete,
243+
}, {
244+
Description: "Reading Credentials...",
245+
Action: t.DataSources.Credentials.Read,
246+
}, {
247+
Description: "Deleting Bucket...",
248+
Action: t.Resources.Bucket.Delete,
249+
}}...)
250+
if err := common.RunSteps(ctx, steps); err != nil {
251251
return err
252252
}
253253
logrus.Info("Deletion completed")

0 commit comments

Comments
 (0)