Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sweeper/AppStream: Delete the correct resource types #22442

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/service/appstream/directory_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,19 @@ func resourceDirectoryConfigUpdate(ctx context.Context, d *schema.ResourceData,
func resourceDirectoryConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AppStreamConn

log.Printf("[DEBUG] Deleting AppStream Directory Config: (%s)", d.Id())
_, err := conn.DeleteDirectoryConfigWithContext(ctx, &appstream.DeleteDirectoryConfigInput{
DirectoryName: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}
return diag.FromErr(fmt.Errorf("error deleting AppStream Directory Config (%s): %w", d.Id(), err))
}

return nil
}

Expand Down
18 changes: 11 additions & 7 deletions internal/service/appstream/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,20 @@ func resourceFleetCreate(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(fmt.Errorf("error creating Appstream Fleet (%s): %w", d.Id(), err))
}

d.SetId(aws.StringValue(output.Fleet.Name))

// Start fleet workflow
_, err = conn.StartFleetWithContext(ctx, &appstream.StartFleetInput{
Name: output.Fleet.Name,
Name: aws.String(d.Id()),
})
if err != nil {
return diag.FromErr(fmt.Errorf("error starting Appstream Fleet (%s): %w", d.Id(), err))
}

if _, err = waitFleetStateRunning(ctx, conn, aws.StringValue(output.Fleet.Name)); err != nil {
if _, err = waitFleetStateRunning(ctx, conn, d.Id()); err != nil {
return diag.FromErr(fmt.Errorf("error waiting for Appstream Fleet (%s) to be running: %w", d.Id(), err))
}

d.SetId(aws.StringValue(output.Fleet.Name))

return resourceFleetRead(ctx, d, meta)
}

Expand Down Expand Up @@ -492,6 +492,7 @@ func resourceFleetDelete(ctx context.Context, d *schema.ResourceData, meta inter
conn := meta.(*conns.AWSClient).AppStreamConn

// Stop fleet workflow
log.Printf("[DEBUG] Stopping AppStream Fleet: (%s)", d.Id())
_, err := conn.StopFleetWithContext(ctx, &appstream.StopFleetInput{
Name: aws.String(d.Id()),
})
Expand All @@ -503,16 +504,19 @@ func resourceFleetDelete(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(fmt.Errorf("error waiting for Appstream Fleet (%s) to be stopped: %w", d.Id(), err))
}

log.Printf("[DEBUG] Deleting AppStream Fleet: (%s)", d.Id())
_, err = conn.DeleteFleetWithContext(ctx, &appstream.DeleteFleetInput{
Name: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}
return diag.FromErr(fmt.Errorf("error deleting Appstream Fleet (%s): %w", d.Id(), err))
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/service/appstream/image_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func resourceImageBuilderUpdate(ctx context.Context, d *schema.ResourceData, met
func resourceImageBuilderDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AppStreamConn

log.Printf("[DEBUG] Deleting AppStream Image Builder: (%s)", d.Id())
_, err := conn.DeleteImageBuilderWithContext(ctx, &appstream.DeleteImageBuilderInput{
Name: aws.String(d.Id()),
})
Expand Down
9 changes: 6 additions & 3 deletions internal/service/appstream/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,16 @@ func resourceStackUpdate(ctx context.Context, d *schema.ResourceData, meta inter
func resourceStackDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AppStreamConn

log.Printf("[DEBUG] Deleting AppStream Stack: (%s)", d.Id())
_, err := conn.DeleteStackWithContext(ctx, &appstream.DeleteStackInput{
Name: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}

if err != nil {
if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil
}
return diag.FromErr(fmt.Errorf("error deleting Appstream Stack (%s): %w", d.Id(), err))
}

Expand Down
137 changes: 42 additions & 95 deletions internal/service/appstream/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package appstream

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appstream"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
Expand Down Expand Up @@ -40,32 +38,22 @@ func init() {

func sweepDirectoryConfigs(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)

if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.(*conns.AWSClient).AppStreamConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

input := &appstream.DescribeDirectoryConfigsInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = describeDirectoryConfigsPagesWithContext(context.TODO(), conn, input, func(page *appstream.DescribeDirectoryConfigsOutput, lastPage bool) bool {
err = describeDirectoryConfigsPages(conn, input, func(page *appstream.DescribeDirectoryConfigsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, directoryConfig := range page.DirectoryConfigs {
if directoryConfig == nil {
continue
}

id := aws.StringValue(directoryConfig.DirectoryName)

for _, v := range page.DirectoryConfigs {
r := ResourceDirectoryConfig()
d := r.Data(nil)
d.SetId(id)
d.SetId(aws.StringValue(v.DirectoryName))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand All @@ -74,54 +62,41 @@ func sweepDirectoryConfigs(region string) error {
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping Appstream Directory Config sweep for %s: %s", region, err)
log.Printf("[WARN] Skipping AppStream Directory Config sweep for %s: %s", region, err)
return nil
}

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing AppStream Directory Configs: %w", err))
return fmt.Errorf("error listing AppStream Directory Configs (%s): %w", region, err)
}

if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AppStream Directory Configs for %s: %w", region, err))
}
err = sweep.SweepOrchestrator(sweepResources)

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AppStream Directory Config sweep for %s: %s", region, err)
return nil // In case we have completed some pages, but had errors
if err != nil {
return fmt.Errorf("error sweeping AppStream Directory Configs (%s): %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepFleets(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)

if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.(*conns.AWSClient).AppStreamConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

input := &appstream.DescribeFleetsInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = describeFleetsPagesWithContext(context.TODO(), conn, input, func(page *appstream.DescribeFleetsOutput, lastPage bool) bool {
err = describeFleetsPages(conn, input, func(page *appstream.DescribeFleetsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, fleet := range page.Fleets {
if fleet == nil {
continue
}

id := aws.StringValue(fleet.Name)

r := ResourceImageBuilder()
for _, v := range page.Fleets {
r := ResourceFleet()
d := r.Data(nil)
d.SetId(id)
d.SetId(aws.StringValue(v.Name))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand All @@ -130,53 +105,41 @@ func sweepFleets(region string) error {
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping Appstream Fleets sweep for %s: %s", region, err)
log.Printf("[WARN] Skipping AppStream Fleet sweep for %s: %s", region, err)
return nil
}

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing AppStream Fleets: %w", err))
return fmt.Errorf("error listing AppStream Fleets (%s): %w", region, err)
}

if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AppStream Fleets for %s: %w", region, err))
}
err = sweep.SweepOrchestrator(sweepResources)

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AppStream Fleets sweep for %s: %s", region, err)
return nil // In case we have completed some pages, but had errors
if err != nil {
return fmt.Errorf("error sweeping AppStream Fleets (%s): %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepImageBuilders(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.(*conns.AWSClient).AppStreamConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

input := &appstream.DescribeImageBuildersInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = describeImageBuildersPagesWithContext(context.TODO(), conn, input, func(page *appstream.DescribeImageBuildersOutput, lastPage bool) bool {
err = describeImageBuildersPages(conn, input, func(page *appstream.DescribeImageBuildersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, imageBuilder := range page.ImageBuilders {
if imageBuilder == nil {
continue
}

id := aws.StringValue(imageBuilder.Name)

for _, v := range page.ImageBuilders {
r := ResourceImageBuilder()
d := r.Data(nil)
d.SetId(id)
d.SetId(aws.StringValue(v.Name))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand All @@ -185,54 +148,41 @@ func sweepImageBuilders(region string) error {
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping Appstream Image Builders sweep for %s: %s", region, err)
log.Printf("[WARN] Skipping AppStream Image Builder sweep for %s: %s", region, err)
return nil
}

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing AppStream Image Builders: %w", err))
return fmt.Errorf("error listing AppStream Image Builders (%s): %w", region, err)
}

if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AppStream Image Builders for %s: %w", region, err))
}
err = sweep.SweepOrchestrator(sweepResources)

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AppStream Image Builders sweep for %s: %s", region, err)
return nil // In case we have completed some pages, but had errors
if err != nil {
return fmt.Errorf("error sweeping AppStream Image Builders (%s): %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepStacks(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)

if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.(*conns.AWSClient).AppStreamConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

input := &appstream.DescribeStacksInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = describeStacksPagesWithContext(context.TODO(), conn, input, func(page *appstream.DescribeStacksOutput, lastPage bool) bool {
err = describeStacksPages(conn, input, func(page *appstream.DescribeStacksOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, stack := range page.Stacks {
if stack == nil {
continue
}

id := aws.StringValue(stack.Name)

r := ResourceImageBuilder()
for _, v := range page.Stacks {
r := ResourceStack()
d := r.Data(nil)
d.SetId(id)
d.SetId(aws.StringValue(v.Name))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
Expand All @@ -241,22 +191,19 @@ func sweepStacks(region string) error {
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping Appstream Stacks sweep for %s: %s", region, err)
log.Printf("[WARN] Skipping AppStream Stack sweep for %s: %s", region, err)
return nil
}

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing AppStream Stacks: %w", err))
return fmt.Errorf("error listing AppStream Stacks (%s): %w", region, err)
}

if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AppStream Stacks for %s: %w", region, err))
}
err = sweep.SweepOrchestrator(sweepResources)

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AppStream Stacks sweep for %s: %s", region, err)
return nil // In case we have completed some pages, but had errors
if err != nil {
return fmt.Errorf("error sweeping AppStream Stacks (%s): %w", region, err)
}

return errs.ErrorOrNil()
return nil
}