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 User and Stack User Association #21485

Merged
merged 20 commits into from
Nov 23, 2021
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
7 changes: 7 additions & 0 deletions .changelog/21485.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-resource
aws_appstream_user
```

```release-note:new-resource
aws_appstream_stack_user_association
```
10 changes: 6 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,12 @@ func Provider() *schema.Provider {
"aws_apprunner_custom_domain_association": apprunner.ResourceCustomDomainAssociation(),
"aws_apprunner_service": apprunner.ResourceService(),

"aws_appstream_directory_config": appstream.ResourceDirectoryConfig(),
"aws_appstream_fleet": appstream.ResourceFleet(),
"aws_appstream_image_builder": appstream.ResourceImageBuilder(),
"aws_appstream_stack": appstream.ResourceStack(),
"aws_appstream_directory_config": appstream.ResourceDirectoryConfig(),
"aws_appstream_fleet": appstream.ResourceFleet(),
"aws_appstream_image_builder": appstream.ResourceImageBuilder(),
"aws_appstream_stack": appstream.ResourceStack(),
"aws_appstream_user_stack_association": appstream.ResourceUserStackAssociation(),
"aws_appstream_user": appstream.ResourceUser(),

"aws_appsync_api_key": appsync.ResourceAPIKey(),
"aws_appsync_datasource": appsync.ResourceDataSource(),
Expand Down
60 changes: 59 additions & 1 deletion internal/service/appstream/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appstream"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

// FindStackByName Retrieve a appstream stack by name
Expand Down Expand Up @@ -82,12 +84,68 @@ func FindImageBuilderByName(ctx context.Context, conn *appstream.AppStream, name
return !lastPage
})

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if result == nil {
return nil, nil
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return result, nil
}

// FindUserByUserNameAndAuthType Retrieve a appstream fleet by Username and authentication type
func FindUserByUserNameAndAuthType(ctx context.Context, conn *appstream.AppStream, username, authType string) (*appstream.User, error) {
input := &appstream.DescribeUsersInput{
AuthenticationType: aws.String(authType),
}

var result *appstream.User

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

for _, user := range page.Users {
if user == nil {
continue
}
if aws.StringValue(user.UserName) == username {
result = user
return false
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move handling not-found into this function, then calling functions can be simpler. For instance, here, we can check the following before other errors

if tfawserr.ErrCodeEquals(err, appstream.ErrCodeResourceNotFoundException) {
	return nil, &resource.NotFoundError{
		LastError:   err,
		LastRequest: input,
	}
}

And then, after looping over the results, if user is nil

if user == nil {
	return nil, &resource.NotFoundError{
		Message:  "Empty result",
		LastRequest: input,
	}
}

Then, in code that calls this function, it can simply check for a not found error by using the function tfresource.NotFound(err)

See https://github.com/hashicorp/terraform-provider-aws/blob/main/internal/service/ds/find.go for an example

return nil, err
}

if result == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return result, nil
Expand Down
1 change: 1 addition & 0 deletions internal/service/appstream/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
func testAccErrorCheckSkipAppStream(t *testing.T) resource.ErrorCheckFunc {
return acctest.ErrorCheckSkipMessagesContaining(t,
"ResourceNotFoundException: The image",
"InvalidParameterValueException: The AppStream 2.0 user pool feature",
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/service/appstream/generate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate go run ../../generate/listpages/main.go -ListOps=DescribeFleets,DescribeImageBuilders,DescribeStacks
//go:generate go run ../../generate/listpages/main.go -ListOps=DescribeFleets,DescribeImageBuilders,DescribeStacks,DescribeUsers
//go:generate go run ../../generate/tags/main.go -ListTags -ServiceTagsMap -UpdateTags
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
23 changes: 22 additions & 1 deletion internal/service/appstream/list_pages_gen.go

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

18 changes: 18 additions & 0 deletions internal/service/appstream/status.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/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

//statusStackState fetches the fleet and its state
Expand Down Expand Up @@ -57,3 +58,20 @@ func statusImageBuilderState(ctx context.Context, conn *appstream.AppStream, nam
return imageBuilder, aws.StringValue(imageBuilder.State), nil
}
}

//statusUserAvailable fetches the user available
func statusUserAvailable(ctx context.Context, conn *appstream.AppStream, username, authType string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
user, err := FindUserByUserNameAndAuthType(ctx, conn, username, authType)

if tfresource.NotFound(err) {
return nil, "", nil
}

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

return user, userAvailable, nil
}
}
Loading