Skip to content

Commit

Permalink
Restructure how resource allocation/deallocation/read steps are defin…
Browse files Browse the repository at this point in the history
…ed 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>
  • Loading branch information
tasdomas and 0x2b3bfa0 authored Aug 29, 2022
1 parent 1e888fb commit 7b27c57
Show file tree
Hide file tree
Showing 7 changed files with 542 additions and 456 deletions.
36 changes: 26 additions & 10 deletions task/aws/resources/resource_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
"terraform-provider-iterative/task/common"
)

const (
errNoSuchBucket = "NoSuchBucket"
errNotFound = "NotFound"
errBucketAlreadyOwnedByYou = "BucketAlreadyOwnedByYou"
)

func ListBuckets(ctx context.Context, client *client.Client) ([]common.Identifier, error) {
output, err := client.Services.S3.ListBuckets(ctx, &s3.ListBucketsInput{})
if err != nil {
Expand Down Expand Up @@ -55,8 +61,7 @@ func (b *Bucket) Create(ctx context.Context) error {
}

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

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

for paginator := s3.NewListObjectsV2Paginator(b.Client.Services.S3, &listInput); paginator.HasMorePages(); {
page, err := paginator.NextPage(ctx)

if errorCodeIs(err, errNoSuchBucket) {
b.Resource = nil
return nil
}
if err != nil {
var e smithy.APIError
if errors.As(err, &e) && e.ErrorCode() == "NoSuchBucket" {
b.Resource = nil
return nil
}
return err
}

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

_, err := b.Client.Services.S3.DeleteBucket(ctx, &deleteInput)
if errorCodeIs(err, errNoSuchBucket) {
b.Resource = nil
return nil
}
if err != nil {
return err
}

b.Resource = nil
return nil
}

// errorCodeIs checks if the provided error is an AWS API error
// and its error code matches the supplied value.
func errorCodeIs(err error, code string) bool {
var e smithy.APIError
if errors.As(err, &e) {
return e.ErrorCode() == code
}
return false
}
226 changes: 113 additions & 113 deletions task/aws/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,54 +109,51 @@ type Task struct {

func (t *Task) Create(ctx context.Context) error {
logrus.Info("Creating resources...")
logrus.Info("[1/12] Parsing PermissionSet...")
if err := t.DataSources.PermissionSet.Read(ctx); err != nil {
return err
}
logrus.Info("[2/12] Importing DefaultVPC...")
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
return err
}
logrus.Info("[3/12] Importing DefaultVPCSubnets...")
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
return err
}
logrus.Info("[4/12] Reading Image...")
if err := t.DataSources.Image.Read(ctx); err != nil {
return err
}
logrus.Info("[5/12] Creating Bucket...")
if err := t.Resources.Bucket.Create(ctx); err != nil {
return err
}
logrus.Info("[6/12] Creating SecurityGroup...")
if err := t.Resources.SecurityGroup.Create(ctx); err != nil {
return err
}
logrus.Info("[7/12] Creating KeyPair...")
if err := t.Resources.KeyPair.Create(ctx); err != nil {
return err
}
logrus.Info("[8/12] Reading Credentials...")
if err := t.DataSources.Credentials.Read(ctx); err != nil {
return err
}
logrus.Info("[9/12] Creating LaunchTemplate...")
if err := t.Resources.LaunchTemplate.Create(ctx); err != nil {
return err
}
logrus.Info("[10/12] Creating AutoScalingGroup...")
if err := t.Resources.AutoScalingGroup.Create(ctx); err != nil {
return err
}
logrus.Info("[11/12] Uploading Directory...")
steps := []common.Step{{
Description: "Parsing PermissionSet...",
Action: t.DataSources.PermissionSet.Read,
}, {
Description: "Importing DefaultVPC...",
Action: t.DataSources.DefaultVPC.Read,
}, {
Description: "Importing DefaultVPCSubnets...",
Action: t.DataSources.DefaultVPCSubnets.Read,
}, {
Description: "Reading Image...",
Action: t.DataSources.Image.Read,
}, {
Description: "Creating Bucket...",
Action: t.Resources.Bucket.Create,
}, {
Description: "Creating SecurityGroup...",
Action: t.Resources.SecurityGroup.Create,
}, {
Description: "Creating KeyPair...",
Action: t.Resources.KeyPair.Create,
}, {
Description: "Reading Credentials...",
Action: t.DataSources.Credentials.Read,
}, {
Description: "Creating LaunchTemplate...",
Action: t.Resources.LaunchTemplate.Create,
}, {
Description: "Creating AutoScalingGroup...",
Action: t.Resources.AutoScalingGroup.Create,
}}

if t.Attributes.Environment.Directory != "" {
if err := t.Push(ctx, t.Attributes.Environment.Directory); err != nil {
return err
}
}
logrus.Info("[12/12] Starting task...")
if err := t.Start(ctx); err != nil {
steps = append(steps, common.Step{
Description: "Uploading Directory...",
Action: func(ctx context.Context) error {
return t.Push(ctx, t.Attributes.Environment.Directory)
},
})
}
steps = append(steps, common.Step{
Description: "Starting task...",
Action: t.Start,
})
if err := common.RunSteps(ctx, steps); err != nil {
return err
}
logrus.Info("Creation completed")
Expand All @@ -168,40 +165,35 @@ func (t *Task) Create(ctx context.Context) error {

func (t *Task) Read(ctx context.Context) error {
logrus.Info("Reading resources... (this may happen several times)")
logrus.Info("[1/9] Reading DefaultVPC...")
if err := t.DataSources.DefaultVPC.Read(ctx); err != nil {
return err
}
logrus.Info("[2/9] Reading DefaultVPCSubnets...")
if err := t.DataSources.DefaultVPCSubnets.Read(ctx); err != nil {
return err
}
logrus.Info("[3/9] Reading Image...")
if err := t.DataSources.Image.Read(ctx); err != nil {
return err
}
logrus.Info("[4/9] Reading Bucket...")
if err := t.Resources.Bucket.Read(ctx); err != nil {
return err
}
logrus.Info("[5/9] Reading SecurityGroup...")
if err := t.Resources.SecurityGroup.Read(ctx); err != nil {
return err
}
logrus.Info("[6/9] Reading KeyPair...")
if err := t.Resources.KeyPair.Read(ctx); err != nil {
return err
}
logrus.Info("[7/9] Reading Credentials...")
if err := t.DataSources.Credentials.Read(ctx); err != nil {
return err
}
logrus.Info("[8/9] Reading LaunchTemplate...")
if err := t.Resources.LaunchTemplate.Read(ctx); err != nil {
return err
}
logrus.Info("[9/9] Reading AutoScalingGroup...")
if err := t.Resources.AutoScalingGroup.Read(ctx); err != nil {
steps := []common.Step{{
Description: "Reading DefaultVPC...",
Action: t.DataSources.DefaultVPC.Read,
}, {
Description: "Reading DefaultVPCSubnets...",
Action: t.DataSources.DefaultVPCSubnets.Read,
}, {
Description: "Reading Image...",
Action: t.DataSources.Image.Read,
}, {
Description: "Reading Bucket...",
Action: t.Resources.Bucket.Read,
}, {
Description: "Reading SecurityGroup...",
Action: t.Resources.SecurityGroup.Read,
}, {
Description: "Reading KeyPair...",
Action: t.Resources.KeyPair.Read,
}, {
Description: "Reading Credentials...",
Action: t.DataSources.Credentials.Read,
}, {
Description: "Reading LaunchTemplate...",
Action: t.Resources.LaunchTemplate.Read,
}, {
Description: "Reading AutoScalingGroup...",
Action: t.Resources.AutoScalingGroup.Read,
}}
if err := common.RunSteps(ctx, steps); err != nil {
return err
}
logrus.Info("Read completed")
Expand All @@ -213,41 +205,49 @@ func (t *Task) Read(ctx context.Context) error {

func (t *Task) Delete(ctx context.Context) error {
logrus.Info("Deleting resources...")
logrus.Info("[1/8] Downloading Directory...")
steps := []common.Step{}
if t.Read(ctx) == nil {
if t.Attributes.Environment.DirectoryOut != "" {
if err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut); err != nil && err != common.NotFoundError {
return err
}
steps = []common.Step{{
Description: "Downloading Directory...",
Action: func(ctx context.Context) error {
err := t.Pull(ctx, t.Attributes.Environment.Directory, t.Attributes.Environment.DirectoryOut)
if err != nil && err != common.NotFoundError {
return err
}
return nil
}}}
}
logrus.Info("[2/8] Emptying Bucket...")

if err := machine.Delete(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"]); err != nil && err != common.NotFoundError {
return err
}
}
logrus.Info("[3/8] Deleting AutoScalingGroup...")
if err := t.Resources.AutoScalingGroup.Delete(ctx); err != nil {
return err
}
logrus.Info("[4/8] Deleting LaunchTemplate...")
if err := t.Resources.LaunchTemplate.Delete(ctx); err != nil {
return err
}
logrus.Info("[5/8] Deleting KeyPair...")
if err := t.Resources.KeyPair.Delete(ctx); err != nil {
return err
}
logrus.Info("[6/8] Deleting SecurityGroup...")
if err := t.Resources.SecurityGroup.Delete(ctx); err != nil {
return err
}
logrus.Info("[7/8] Reading Credentials...")
if err := t.DataSources.Credentials.Read(ctx); err != nil {
return err
}
logrus.Info("[8/8] Deleting Bucket...")
if err := t.Resources.Bucket.Delete(ctx); err != nil {
steps = append(steps, common.Step{
Description: "Emptying Bucket...",
Action: func(ctx context.Context) error {
err := machine.Delete(ctx, t.DataSources.Credentials.Resource["RCLONE_REMOTE"])
if err != nil && err != common.NotFoundError {
return err
}
return nil
}})
}
steps = append(steps, []common.Step{{
Description: "Deleting AutoScalingGroup...",
Action: t.Resources.AutoScalingGroup.Delete,
}, {
Description: "Deleting LaunchTemplate...",
Action: t.Resources.LaunchTemplate.Delete,
}, {
Description: "Deleting KeyPair...",
Action: t.Resources.KeyPair.Delete,
}, {
Description: "Deleting SecurityGroup...",
Action: t.Resources.SecurityGroup.Delete,
}, {
Description: "Reading Credentials...",
Action: t.DataSources.Credentials.Read,
}, {
Description: "Deleting Bucket...",
Action: t.Resources.Bucket.Delete,
}}...)
if err := common.RunSteps(ctx, steps); err != nil {
return err
}
logrus.Info("Deletion completed")
Expand Down
Loading

0 comments on commit 7b27c57

Please sign in to comment.