Add policy to aws_sqs_queue_policy enumerator

main
Elie 2021-07-16 17:34:07 +02:00
parent 373bb0918b
commit 34de289bd4
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
5 changed files with 134 additions and 4 deletions

View File

@ -1,14 +1,40 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import "github.com/stretchr/testify/mock"
import (
sqs "github.com/aws/aws-sdk-go/service/sqs"
mock "github.com/stretchr/testify/mock"
)
// MockSQSRepository is an autogenerated mock type for the MockSQSRepository type
// MockSQSRepository is an autogenerated mock type for the SQSRepository type
type MockSQSRepository struct {
mock.Mock
}
// GetQueueAttributes provides a mock function with given fields: url
func (_m *MockSQSRepository) GetQueueAttributes(url string) (*sqs.GetQueueAttributesOutput, error) {
ret := _m.Called(url)
var r0 *sqs.GetQueueAttributesOutput
if rf, ok := ret.Get(0).(func(string) *sqs.GetQueueAttributesOutput); ok {
r0 = rf(url)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*sqs.GetQueueAttributesOutput)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(url)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllQueues provides a mock function with given fields:
func (_m *MockSQSRepository) ListAllQueues() ([]*string, error) {
ret := _m.Called()

View File

@ -1,6 +1,9 @@
package repository
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
@ -9,6 +12,7 @@ import (
type SQSRepository interface {
ListAllQueues() ([]*string, error)
GetQueueAttributes(url string) (*sqs.GetQueueAttributesOutput, error)
}
type sqsRepository struct {
@ -23,6 +27,25 @@ func NewSQSRepository(session *session.Session, c cache.Cache) *sqsRepository {
}
}
func (r *sqsRepository) GetQueueAttributes(url string) (*sqs.GetQueueAttributesOutput, error) {
cacheKey := fmt.Sprintf("sqsGetQueueAttributes_%s", url)
if v := r.cache.Get(cacheKey); v != nil {
return v.(*sqs.GetQueueAttributesOutput), nil
}
attributes, err := r.client.GetQueueAttributes(&sqs.GetQueueAttributesInput{
AttributeNames: aws.StringSlice([]string{sqs.QueueAttributeNamePolicy}),
QueueUrl: &url,
})
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, attributes)
return attributes, nil
}
func (r *sqsRepository) ListAllQueues() ([]*string, error) {
if v := r.cache.Get("sqsListAllQueues"); v != nil {
return v.([]*string), nil

View File

@ -79,3 +79,67 @@ func Test_sqsRepository_ListAllQueues(t *testing.T) {
})
}
}
func Test_sqsRepository_GetQueueAttributes(t *testing.T) {
tests := []struct {
name string
mocks func(client *awstest.MockFakeSQS)
want *sqs.GetQueueAttributesOutput
wantErr error
}{
{
name: "get attributes",
mocks: func(client *awstest.MockFakeSQS) {
client.On(
"GetQueueAttributes",
&sqs.GetQueueAttributesInput{
AttributeNames: awssdk.StringSlice([]string{sqs.QueueAttributeNamePolicy}),
QueueUrl: awssdk.String("http://example.com"),
},
).Return(
&sqs.GetQueueAttributesOutput{
Attributes: map[string]*string{
sqs.QueueAttributeNamePolicy: awssdk.String("foobar"),
},
},
nil,
).Once()
},
want: &sqs.GetQueueAttributesOutput{
Attributes: map[string]*string{
sqs.QueueAttributeNamePolicy: awssdk.String("foobar"),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
client := &awstest.MockFakeSQS{}
tt.mocks(client)
r := &sqsRepository{
client: client,
cache: store,
}
got, err := r.GetQueueAttributes("http://example.com")
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.GetQueueAttributes("http://example.com")
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, &sqs.GetQueueAttributesOutput{}, store.Get("sqsGetQueueAttributes_http://example.com"))
}
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}

View File

@ -1,6 +1,7 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
@ -34,12 +35,18 @@ func (e *SQSQueuePolicyEnumerator) Enumerate() ([]resource.Resource, error) {
results := make([]resource.Resource, len(queues))
for _, queue := range queues {
attributes, err := e.repository.GetQueueAttributes(*queue)
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
awssdk.StringValue(queue),
map[string]interface{}{},
map[string]interface{}{
"policy": *attributes.Attributes[sqs.QueueAttributeNamePolicy],
},
),
)
}

View File

@ -6,6 +6,7 @@ import (
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
@ -139,6 +140,15 @@ func TestSQSQueuePolicy(t *testing.T) {
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/foo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/baz"),
}, nil)
client.On("GetQueueAttributes", mock.Anything).Return(
&sqs.GetQueueAttributesOutput{
Attributes: map[string]*string{
sqs.QueueAttributeNamePolicy: awssdk.String(""),
},
},
nil,
)
},
wantErr: nil,
},