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

New resource for AppStream ImageBuilder #21036

Merged
merged 20 commits into from
Sep 29, 2021
3 changes: 3 additions & 0 deletions .changelog/21036.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_appstream_image_builder
```
38 changes: 38 additions & 0 deletions aws/internal/service/appstream/finder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appstream"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/appstream/lister"
)

// StackByName Retrieve a appstream stack by name
Expand Down Expand Up @@ -55,3 +56,40 @@ func FleetByName(ctx context.Context, conn *appstream.AppStream, name string) (*

return fleet, nil
}

// ImageBuilderByName Retrieve a appstream ImageBuilder by name
func ImageBuilderByName(ctx context.Context, conn *appstream.AppStream, name string) (*appstream.ImageBuilder, error) {
input := &appstream.DescribeImageBuildersInput{
Names: []*string{aws.String(name)},
}

var result *appstream.ImageBuilder

err := lister.DescribeImageBuildersPagesWithContext(ctx, conn, input, func(page *appstream.DescribeImageBuildersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, imageBuilder := range page.ImageBuilders {
if imageBuilder == nil {
continue
}
if aws.StringValue(imageBuilder.Name) == name {
result = imageBuilder
return false
}
}

return !lastPage
})

if err != nil {
return nil, err
}

if result == nil {
return nil, nil
}

return result, nil
}
3 changes: 3 additions & 0 deletions aws/internal/service/appstream/lister/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate go run ../../../generators/listpages/main.go -function=DescribeImageBuilders github.com/aws/aws-sdk-go/service/appstream

package lister
31 changes: 31 additions & 0 deletions aws/internal/service/appstream/lister/list_pages_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions aws/internal/service/appstream/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,20 @@ func FleetState(ctx context.Context, conn *appstream.AppStream, name string) res
return fleet, aws.StringValue(fleet.State), nil
}
}

//ImageBuilderState fetches the ImageBuilder and its state
func ImageBuilderState(ctx context.Context, conn *appstream.AppStream, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
imageBuilder, err := finder.ImageBuilderByName(ctx, conn, name)

if err != nil {
return nil, "", err
}

if imageBuilder == nil {
return nil, "", nil
}

return imageBuilder, aws.StringValue(imageBuilder.State), nil
}
}
63 changes: 63 additions & 0 deletions aws/internal/service/appstream/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package waiter

import (
"context"
"fmt"
"time"

"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/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

const (
Expand All @@ -16,6 +20,9 @@ const (
FleetStateTimeout = 180 * time.Minute
// FleetOperationTimeout Maximum amount of time to wait for Fleet operation eventual consistency
FleetOperationTimeout = 15 * time.Minute
// ImageBuilderStateTimeout Maximum amount of time to wait for the ImageBuilderState to be RUNNING
// or for the ImageBuilder to be deleted
ImageBuilderStateTimeout = 60 * time.Minute
)

// StackStateDeleted waits for a deleted stack
Expand Down Expand Up @@ -70,3 +77,59 @@ func FleetStateStopped(ctx context.Context, conn *appstream.AppStream, name stri

return nil, err
}

// ImageBuilderStateRunning waits for a ImageBuilder running
func ImageBuilderStateRunning(ctx context.Context, conn *appstream.AppStream, name string) (*appstream.ImageBuilder, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{appstream.ImageBuilderStatePending},
Target: []string{appstream.ImageBuilderStateRunning},
Refresh: ImageBuilderState(ctx, conn, name),
Timeout: ImageBuilderStateTimeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*appstream.ImageBuilder); ok {
if state, errors := aws.StringValue(output.State), output.ImageBuilderErrors; state == appstream.ImageBuilderStateFailed && len(errors) > 0 {
var errs *multierror.Error

for _, err := range errors {
errs = multierror.Append(errs, fmt.Errorf("%s: %s", aws.StringValue(err.ErrorCode), aws.StringValue(err.ErrorMessage)))
}

tfresource.SetLastError(err, errs.ErrorOrNil())
}

return output, err
}

return nil, err
}

// ImageBuilderStateDeleted waits for a ImageBuilder deleted
func ImageBuilderStateDeleted(ctx context.Context, conn *appstream.AppStream, name string) (*appstream.ImageBuilder, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{appstream.ImageBuilderStatePending, appstream.ImageBuilderStateDeleting},
Target: []string{},
Refresh: ImageBuilderState(ctx, conn, name),
Timeout: ImageBuilderStateTimeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*appstream.ImageBuilder); ok {
if state, errors := aws.StringValue(output.State), output.ImageBuilderErrors; state == appstream.ImageBuilderStateFailed && len(errors) > 0 {
var errs *multierror.Error

for _, err := range errors {
errs = multierror.Append(errs, fmt.Errorf("%s: %s", aws.StringValue(err.ErrorCode), aws.StringValue(err.ErrorMessage)))
}

tfresource.SetLastError(err, errs.ErrorOrNil())
}

return output, err
}

return nil, err
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ func Provider() *schema.Provider {
"aws_apprunner_service": resourceAwsAppRunnerService(),
"aws_appstream_stack": resourceAwsAppStreamStack(),
"aws_appstream_fleet": resourceAwsAppStreamFleet(),
"aws_appstream_image_builder": resourceAwsAppStreamImageBuilder(),
"aws_appsync_api_key": resourceAwsAppsyncApiKey(),
"aws_appsync_datasource": resourceAwsAppsyncDatasource(),
"aws_appsync_function": resourceAwsAppsyncFunction(),
Expand Down
Loading