Skip to content

Commit

Permalink
Returns resource.NotFoundError from FindUserByUserNameAndAuthType()
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison authored and coderGo93 committed Nov 17, 2021
1 parent 345127f commit 969b46c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
11 changes: 10 additions & 1 deletion internal/service/appstream/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,21 @@ func FindUserByUserNameAndAuthType(ctx context.Context, conn *appstream.AppStrea
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
Expand Down
9 changes: 5 additions & 4 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 @@ -63,12 +64,12 @@ func statusUserAvailable(ctx context.Context, conn *appstream.AppStream, usernam
return func() (interface{}, string, error) {
user, err := FindUserByUserNameAndAuthType(ctx, conn, username, authType)

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

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

return user, "AVAILABLE", nil
Expand Down
13 changes: 6 additions & 7 deletions internal/service/appstream/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func ResourceUser() *schema.Resource {
Expand Down Expand Up @@ -143,16 +144,14 @@ func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interfac
}

user, err := FindUserByUserNameAndAuthType(ctx, conn, userName, authType)

if err != nil {
return diag.FromErr(fmt.Errorf("error reading Appstream User (%s): %w", d.Id(), err))
}

if !d.IsNewResource() && user == nil {
if tfresource.NotFound(err) && !d.IsNewResource() {
log.Printf("[WARN] AppStream User (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return diag.FromErr(fmt.Errorf("error reading AppStream User (%s): %w", d.Id(), err))
}

d.Set("arn", user.Arn)
d.Set("authentication_type", user.AuthenticationType)
Expand All @@ -172,7 +171,7 @@ func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interf

userName, authType, err := DecodeUserID(d.Id())
if err != nil {
return diag.FromErr(fmt.Errorf("error decoding id AppStream User (%s): %w", d.Id(), err))
return diag.FromErr(fmt.Errorf("error decoding AppStream User ID (%s): %w", d.Id(), err))
}

if d.HasChange("enabled") {
Expand Down
1 change: 0 additions & 1 deletion internal/service/appstream/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func waitImageBuilderStateDeleted(ctx context.Context, conn *appstream.AppStream
// waitUserAvailable waits for a user be available
func waitUserAvailable(ctx context.Context, conn *appstream.AppStream, username, authType string) (*appstream.User, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"NotFound"},
Target: []string{"AVAILABLE"},
Refresh: statusUserAvailable(ctx, conn, username, authType),
Timeout: userOperationTimeout,
Expand Down

0 comments on commit 969b46c

Please sign in to comment.