Skip to content

Commit

Permalink
added new delete check for s3bucketpolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
krishchow committed Aug 10, 2020
1 parent 9e59da3 commit 69a820b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
9 changes: 0 additions & 9 deletions pkg/clients/s3/fake/s3bucketpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fake

import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/service/s3"

"github.com/crossplane/provider-aws/pkg/clients/iam"
Expand Down Expand Up @@ -41,11 +40,3 @@ func NewMockBucketPolicyClient(conf *aws.Config) (clientset.BucketPolicyClient,
iamclient := fake.Client{}
return &s3client, &iamclient, nil
}

// IsErrorNotFound returns true if the error code indicates that the item was not found
func IsErrorNotFound(err error) bool {
if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == "NoSuchBucketPolicy" {
return true
}
return false
}
12 changes: 10 additions & 2 deletions pkg/clients/s3/s3bucketpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ func NewBucketPolicyClient(conf *aws.Config) (BucketPolicyClient, iam.Client, er
return s3client, iamclient, nil
}

// IsErrorNotFound returns true if the error code indicates that the item was not found
func IsErrorNotFound(err error) bool {
// IsErrorPolicyNotFound returns true if the error code indicates that the item was not found
func IsErrorPolicyNotFound(err error) bool {
if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == "NoSuchBucketPolicy" {
return true
}
return false
}

// IsErrorBucketNotFound returns true if the error code indicates that the bucket was not found
func IsErrorBucketNotFound(err error) bool {
if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == s3.ErrCodeNoSuchBucket {
return true
}
return false
}
13 changes: 10 additions & 3 deletions pkg/controller/s3/s3bucketpolicy/s3bucketpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,16 @@ func (e *external) Observe(ctx context.Context, mgd resource.Managed) (managed.E
Bucket: cr.Spec.PolicyBody.BucketName,
}).Send(ctx)
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(s3.IsErrorNotFound, err), errGet)
if s3.IsErrorBucketNotFound(err) {
return managed.ExternalObservation{}, nil
}
return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(s3.IsErrorPolicyNotFound, err), errGet)
}

policyData, err := e.formatBucketPolicy(cr)

if err != nil {
return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(s3.IsErrorNotFound, err), errGet)
return managed.ExternalObservation{}, errors.Wrap(resource.Ignore(s3.IsErrorPolicyNotFound, err), errGet)
}

cr.SetConditions(runtimev1alpha1.Available())
Expand Down Expand Up @@ -202,5 +205,9 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error {
}
cr.SetConditions(runtimev1alpha1.Deleting())
_, err := e.client.DeleteBucketPolicyRequest(&awss3.DeleteBucketPolicyInput{Bucket: cr.Spec.PolicyBody.BucketName}).Send(context.TODO())
return errors.Wrap(resource.Ignore(s3.IsErrorNotFound, err), errDelete)
if s3.IsErrorBucketNotFound(err) {
return nil
}

return errors.Wrap(resource.Ignore(s3.IsErrorPolicyNotFound, err), errDelete)
}

0 comments on commit 69a820b

Please sign in to comment.