Add aws_cloudformation_stack
parent
c5e00a722e
commit
e3e431cc0d
|
@ -145,6 +145,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
|
|||
{name: "VPC", dirName: "vpc", wantErr: false},
|
||||
{name: "Subnet", dirName: "subnet", wantErr: false},
|
||||
{name: "RDS cluster", dirName: "rds_cluster", wantErr: false},
|
||||
{name: "Cloudformation stack", dirName: "cloudformation_stack", wantErr: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
[
|
||||
{
|
||||
"Id": "arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab",
|
||||
"Type": "aws_cloudformation_stack",
|
||||
"Attrs": {
|
||||
"capabilities": [
|
||||
"CAPABILITY_NAMED_IAM"
|
||||
],
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab",
|
||||
"name": "bar-stack",
|
||||
"template_body": "Resources:\n myUser:\n Type: AWS::IAM::User\n Properties:\n UserName: \"bar_cfn\"\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Id": "arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757",
|
||||
"Type": "aws_cloudformation_stack",
|
||||
"Attrs": {
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757",
|
||||
"name": "foo-stack",
|
||||
"parameters": {
|
||||
"VPCCidr": "10.0.0.0/16"
|
||||
},
|
||||
"template_body": "{\"Parameters\":{\"VPCCidr\":{\"Default\":\"10.0.0.0/16\",\"Description\":\"Enter the CIDR block for the VPC. Default is 10.0.0.0/16.\",\"Type\":\"String\"}},\"Resources\":{\"myVpc\":{\"Properties\":{\"CidrBlock\":{\"Ref\":\"VPCCidr\"},\"Tags\":[{\"Key\":\"Name\",\"Value\":\"Primary_CF_VPC\"}]},\"Type\":\"AWS::EC2::VPC\"}}}"
|
||||
}
|
||||
}
|
||||
]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,77 @@
|
|||
{
|
||||
"version": 4,
|
||||
"terraform_version": "1.0.5",
|
||||
"serial": 9,
|
||||
"lineage": "4b33dcbc-f194-5350-9824-9ef93b014e56",
|
||||
"outputs": {},
|
||||
"resources": [
|
||||
{
|
||||
"mode": "managed",
|
||||
"type": "aws_cloudformation_stack",
|
||||
"name": "bar",
|
||||
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
|
||||
"instances": [
|
||||
{
|
||||
"schema_version": 0,
|
||||
"attributes": {
|
||||
"capabilities": [
|
||||
"CAPABILITY_NAMED_IAM"
|
||||
],
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab",
|
||||
"name": "bar-stack",
|
||||
"notification_arns": null,
|
||||
"on_failure": null,
|
||||
"outputs": {},
|
||||
"parameters": {},
|
||||
"policy_body": null,
|
||||
"policy_url": null,
|
||||
"tags": null,
|
||||
"tags_all": {},
|
||||
"template_body": "Resources:\n myUser:\n Type: AWS::IAM::User\n Properties:\n UserName: \"bar_cfn\"\n",
|
||||
"template_url": null,
|
||||
"timeout_in_minutes": null,
|
||||
"timeouts": null
|
||||
},
|
||||
"sensitive_attributes": [],
|
||||
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxODAwMDAwMDAwMDAwLCJkZWxldGUiOjE4MDAwMDAwMDAwMDAsInVwZGF0ZSI6MTgwMDAwMDAwMDAwMH19"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"mode": "managed",
|
||||
"type": "aws_cloudformation_stack",
|
||||
"name": "foo",
|
||||
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
|
||||
"instances": [
|
||||
{
|
||||
"schema_version": 0,
|
||||
"attributes": {
|
||||
"capabilities": null,
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757",
|
||||
"name": "foo-stack",
|
||||
"notification_arns": null,
|
||||
"on_failure": null,
|
||||
"outputs": {},
|
||||
"parameters": {
|
||||
"VPCCidr": "10.0.0.0/16"
|
||||
},
|
||||
"policy_body": null,
|
||||
"policy_url": null,
|
||||
"tags": {},
|
||||
"tags_all": {},
|
||||
"template_body": "{\"Parameters\":{\"VPCCidr\":{\"Default\":\"10.0.0.0/16\",\"Description\":\"Enter the CIDR block for the VPC. Default is 10.0.0.0/16.\",\"Type\":\"String\"}},\"Resources\":{\"myVpc\":{\"Properties\":{\"CidrBlock\":{\"Ref\":\"VPCCidr\"},\"Tags\":[{\"Key\":\"Name\",\"Value\":\"Primary_CF_VPC\"}]},\"Type\":\"AWS::EC2::VPC\"}}}",
|
||||
"template_url": null,
|
||||
"timeout_in_minutes": null,
|
||||
"timeouts": null
|
||||
},
|
||||
"sensitive_attributes": [],
|
||||
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoxODAwMDAwMDAwMDAwLCJkZWxldGUiOjE4MDAwMDAwMDAwMDAsInVwZGF0ZSI6MTgwMDAwMDAwMDAwMH19"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
)
|
||||
|
||||
type CloudformationStackEnumerator struct {
|
||||
repository repository.CloudformationRepository
|
||||
factory resource.ResourceFactory
|
||||
}
|
||||
|
||||
func NewCloudformationStackEnumerator(repo repository.CloudformationRepository, factory resource.ResourceFactory) *CloudformationStackEnumerator {
|
||||
return &CloudformationStackEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *CloudformationStackEnumerator) SupportedType() resource.ResourceType {
|
||||
return aws.AwsCloudformationStackResourceType
|
||||
}
|
||||
|
||||
func (e *CloudformationStackEnumerator) Enumerate() ([]*resource.Resource, error) {
|
||||
stacks, err := e.repository.ListAllStacks()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
results := make([]*resource.Resource, len(stacks))
|
||||
|
||||
for _, stack := range stacks {
|
||||
attrs := map[string]interface{}{}
|
||||
if stack.Parameters != nil && len(stack.Parameters) > 0 {
|
||||
attrs["parameters"] = flattenParameters(stack.Parameters)
|
||||
}
|
||||
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
*stack.StackId,
|
||||
attrs,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
func flattenParameters(parameters []*cloudformation.Parameter) interface{} {
|
||||
params := make(map[string]interface{}, len(parameters))
|
||||
for _, p := range parameters {
|
||||
params[*p.ParameterKey] = *p.ParameterValue
|
||||
}
|
||||
return params
|
||||
}
|
|
@ -51,6 +51,7 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
ecrRepository := repository.NewECRRepository(provider.session, repositoryCache)
|
||||
kmsRepository := repository.NewKMSRepository(provider.session, repositoryCache)
|
||||
iamRepository := repository.NewIAMRepository(provider.session, repositoryCache)
|
||||
cloudformationRepository := repository.NewCloudformationRepository(provider.session, repositoryCache)
|
||||
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
providerLibrary.AddProvider(terraform.AWS, provider)
|
||||
|
@ -173,6 +174,9 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
remoteLibrary.AddEnumerator(NewRDSClusterEnumerator(rdsRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsRDSClusterResourceType, common.NewGenericDetailsFetcher(aws.AwsRDSClusterResourceType, provider, deserializer))
|
||||
|
||||
remoteLibrary.AddEnumerator(NewCloudformationStackEnumerator(cloudformationRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsCloudformationStackResourceType, common.NewGenericDetailsFetcher(aws.AwsCloudformationStackResourceType, provider, deserializer))
|
||||
|
||||
err = resourceSchemaRepository.Init(terraform.AWS, version, provider.Schema())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type CloudformationRepository interface {
|
||||
ListAllStacks() ([]*cloudformation.Stack, error)
|
||||
}
|
||||
|
||||
type cloudformationRepository struct {
|
||||
client cloudformationiface.CloudFormationAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewCloudformationRepository(session *session.Session, c cache.Cache) *cloudformationRepository {
|
||||
return &cloudformationRepository{
|
||||
cloudformation.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *cloudformationRepository) ListAllStacks() ([]*cloudformation.Stack, error) {
|
||||
if v := r.cache.Get("cloudformationListAllStacks"); v != nil {
|
||||
return v.([]*cloudformation.Stack), nil
|
||||
}
|
||||
|
||||
var stacks []*cloudformation.Stack
|
||||
input := cloudformation.DescribeStacksInput{}
|
||||
err := r.client.DescribeStacksPages(&input,
|
||||
func(resp *cloudformation.DescribeStacksOutput, lastPage bool) bool {
|
||||
if resp.Stacks != nil {
|
||||
stacks = append(stacks, resp.Stacks...)
|
||||
}
|
||||
return !lastPage
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("cloudformationListAllStacks", stacks)
|
||||
return stacks, nil
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudformation"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/r3labs/diff/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_cloudformationRepository_ListAllStacks(t *testing.T) {
|
||||
stacks := []*cloudformation.Stack{
|
||||
{StackId: aws.String("stack1")},
|
||||
{StackId: aws.String("stack2")},
|
||||
{StackId: aws.String("stack3")},
|
||||
{StackId: aws.String("stack4")},
|
||||
{StackId: aws.String("stack5")},
|
||||
{StackId: aws.String("stack6")},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mocks func(client *awstest.MockFakeCloudformation, store *cache.MockCache)
|
||||
want []*cloudformation.Stack
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "list multiple stacks",
|
||||
mocks: func(client *awstest.MockFakeCloudformation, store *cache.MockCache) {
|
||||
client.On("DescribeStacksPages",
|
||||
&cloudformation.DescribeStacksInput{},
|
||||
mock.MatchedBy(func(callback func(res *cloudformation.DescribeStacksOutput, lastPage bool) bool) bool {
|
||||
callback(&cloudformation.DescribeStacksOutput{
|
||||
Stacks: stacks[:3],
|
||||
}, false)
|
||||
callback(&cloudformation.DescribeStacksOutput{
|
||||
Stacks: stacks[3:],
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil).Once()
|
||||
|
||||
store.On("Get", "cloudformationListAllStacks").Return(nil).Times(1)
|
||||
store.On("Put", "cloudformationListAllStacks", stacks).Return(false).Times(1)
|
||||
},
|
||||
want: stacks,
|
||||
},
|
||||
{
|
||||
name: "should hit cache",
|
||||
mocks: func(client *awstest.MockFakeCloudformation, store *cache.MockCache) {
|
||||
store.On("Get", "cloudformationListAllStacks").Return(stacks).Times(1)
|
||||
},
|
||||
want: stacks,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := &cache.MockCache{}
|
||||
client := &awstest.MockFakeCloudformation{}
|
||||
tt.mocks(client, store)
|
||||
r := &cloudformationRepository{
|
||||
client: client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllStacks()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
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()
|
||||
}
|
||||
store.AssertExpectations(t)
|
||||
client.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
// Code generated by mockery v1.0.0. DO NOT EDIT.
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
cloudformation "github.com/aws/aws-sdk-go/service/cloudformation"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockCloudformationRepository is an autogenerated mock type for the CloudformationRepository type
|
||||
type MockCloudformationRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// ListAllStacks provides a mock function with given fields:
|
||||
func (_m *MockCloudformationRepository) ListAllStacks() ([]*cloudformation.Stack, error) {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 []*cloudformation.Stack
|
||||
if rf, ok := ret.Get(0).(func() []*cloudformation.Stack); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*cloudformation.Stack)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func() error); ok {
|
||||
r1 = rf()
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
|
@ -0,0 +1,126 @@
|
|||
package remote
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
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/cloudformation"
|
||||
"github.com/cloudskiff/driftctl/mocks"
|
||||
"github.com/cloudskiff/driftctl/pkg/filter"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/alerts"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
||||
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestCloudformationStack(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(*repository.MockCloudformationRepository, *mocks.AlerterInterface)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no cloudformation stacks",
|
||||
dirName: "aws_cloudformation_stack_empty",
|
||||
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllStacks").Return([]*cloudformation.Stack{}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "multiple cloudformation stacks",
|
||||
dirName: "aws_cloudformation_stack_multiple",
|
||||
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
|
||||
repository.On("ListAllStacks").Return([]*cloudformation.Stack{
|
||||
{StackId: awssdk.String("arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab")},
|
||||
{StackId: awssdk.String("arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757")},
|
||||
}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "cannot list cloudformation stacks",
|
||||
dirName: "aws_cloudformation_stack_list",
|
||||
mocks: func(repository *repository.MockCloudformationRepository, alerter *mocks.AlerterInterface) {
|
||||
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 400, "")
|
||||
repository.On("ListAllStacks").Return(nil, awsError)
|
||||
|
||||
alerter.On("SendAlert", resourceaws.AwsCloudformationStackResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsCloudformationStackResourceType, resourceaws.AwsCloudformationStackResourceType), alerts.EnumerationPhase)).Return()
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||||
resourceaws.InitResourcesMetadata(schemaRepository)
|
||||
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
|
||||
for _, c := range tests {
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
shouldUpdate := c.dirName == *goldenfile.Update
|
||||
|
||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
}))
|
||||
|
||||
scanOptions := ScannerOptions{Deep: true}
|
||||
providerLibrary := terraform.NewProviderLibrary()
|
||||
remoteLibrary := common.NewRemoteLibrary()
|
||||
|
||||
// Initialize mocks
|
||||
alerter := &mocks.AlerterInterface{}
|
||||
fakeRepo := &repository.MockCloudformationRepository{}
|
||||
c.mocks(fakeRepo, alerter)
|
||||
|
||||
var repo repository.CloudformationRepository = fakeRepo
|
||||
providerVersion := "3.19.0"
|
||||
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := terraform2.NewFakeTerraformProvider(realProvider)
|
||||
provider.WithResponse(c.dirName)
|
||||
|
||||
// Replace mock by real resources if we are in update mode
|
||||
if shouldUpdate {
|
||||
err := realProvider.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider.ShouldUpdate()
|
||||
repo = repository.NewCloudformationRepository(sess, cache.New(0))
|
||||
}
|
||||
|
||||
remoteLibrary.AddEnumerator(aws.NewCloudformationStackEnumerator(repo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(resourceaws.AwsCloudformationStackResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsCloudformationStackResourceType, provider, deserializer))
|
||||
|
||||
testFilter := &filter.MockFilter{}
|
||||
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
|
||||
|
||||
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
|
||||
got, err := s.Resources()
|
||||
assert.Equal(tt, err, c.wantErr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
test.TestAgainstGoldenFile(got, resourceaws.AwsCloudformationStackResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
alerter.AssertExpectations(tt)
|
||||
fakeRepo.AssertExpectations(tt)
|
||||
testFilter.AssertExpectations(tt)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"Typ": "WyJvYmplY3QiLHsiY2FwYWJpbGl0aWVzIjpbInNldCIsInN0cmluZyJdLCJkaXNhYmxlX3JvbGxiYWNrIjoiYm9vbCIsImlhbV9yb2xlX2FybiI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsIm5vdGlmaWNhdGlvbl9hcm5zIjpbInNldCIsInN0cmluZyJdLCJvbl9mYWlsdXJlIjoic3RyaW5nIiwib3V0cHV0cyI6WyJtYXAiLCJzdHJpbmciXSwicGFyYW1ldGVycyI6WyJtYXAiLCJzdHJpbmciXSwicG9saWN5X2JvZHkiOiJzdHJpbmciLCJwb2xpY3lfdXJsIjoic3RyaW5nIiwidGFncyI6WyJtYXAiLCJzdHJpbmciXSwidGVtcGxhdGVfYm9keSI6InN0cmluZyIsInRlbXBsYXRlX3VybCI6InN0cmluZyIsInRpbWVvdXRfaW5fbWludXRlcyI6Im51bWJlciIsInRpbWVvdXRzIjpbIm9iamVjdCIseyJjcmVhdGUiOiJzdHJpbmciLCJkZWxldGUiOiJzdHJpbmciLCJ1cGRhdGUiOiJzdHJpbmcifV19XQ==",
|
||||
"Val": "eyJjYXBhYmlsaXRpZXMiOlsiQ0FQQUJJTElUWV9OQU1FRF9JQU0iXSwiZGlzYWJsZV9yb2xsYmFjayI6ZmFsc2UsImlhbV9yb2xlX2FybiI6IiIsImlkIjoiYXJuOmF3czpjbG91ZGZvcm1hdGlvbjp1cy1lYXN0LTE6MDQ3MDgxMDE0MzE1OnN0YWNrL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pbl9taW51dGVzIjpudWxsLCJ0aW1lb3V0cyI6eyJjcmVhdGUiOm51bGwsImRlbGV0ZSI6bnVsbCwidXBkYXRlIjpudWxsfX0=",
|
||||
"Err": null
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
Resources:
|
||||
myUser:
|
||||
Type: AWS::IAM::User
|
||||
Properties:
|
||||
UserName: "bar_cfn"
|
|
@ -0,0 +1,42 @@
|
|||
[
|
||||
{
|
||||
"capabilities": null,
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/foo-stack/c7aa0ab0-0f21-11ec-ba25-129d8c0b3757",
|
||||
"name": "foo-stack",
|
||||
"notification_arns": null,
|
||||
"on_failure": null,
|
||||
"outputs": null,
|
||||
"parameters": {
|
||||
"VPCCidr": "10.0.0.0/16"
|
||||
},
|
||||
"policy_body": null,
|
||||
"policy_url": null,
|
||||
"tags": null,
|
||||
"template_body": "{\"Parameters\":{\"VPCCidr\":{\"Default\":\"10.0.0.0/16\",\"Description\":\"Enter the CIDR block for the VPC. Default is 10.0.0.0/16.\",\"Type\":\"String\"}},\"Resources\":{\"myVpc\":{\"Properties\":{\"CidrBlock\":{\"Ref\":\"VPCCidr\"},\"Tags\":[{\"Key\":\"Name\",\"Value\":\"Primary_CF_VPC\"}]},\"Type\":\"AWS::EC2::VPC\"}}}",
|
||||
"template_url": null,
|
||||
"timeout_in_minutes": null,
|
||||
"timeouts": null
|
||||
},
|
||||
{
|
||||
"capabilities": [
|
||||
"CAPABILITY_NAMED_IAM"
|
||||
],
|
||||
"disable_rollback": false,
|
||||
"iam_role_arn": "",
|
||||
"id": "arn:aws:cloudformation:us-east-1:047081014315:stack/bar-stack/c7a96e70-0f21-11ec-bd2a-0a2d95c2b2ab",
|
||||
"name": "bar-stack",
|
||||
"notification_arns": null,
|
||||
"on_failure": null,
|
||||
"outputs": null,
|
||||
"parameters": null,
|
||||
"policy_body": null,
|
||||
"policy_url": null,
|
||||
"tags": null,
|
||||
"template_body": "Resources:\n myUser:\n Type: AWS::IAM::User\n Properties:\n UserName: \"bar_cfn\"\n",
|
||||
"template_url": null,
|
||||
"timeout_in_minutes": null,
|
||||
"timeouts": null
|
||||
}
|
||||
]
|
|
@ -0,0 +1,43 @@
|
|||
provider "aws" {
|
||||
region = "us-east-1"
|
||||
profile = "cloudskiff"
|
||||
}
|
||||
|
||||
resource "aws_cloudformation_stack" "foo" {
|
||||
name = "foo-stack"
|
||||
|
||||
parameters = {
|
||||
VPCCidr = "10.0.0.0/16"
|
||||
}
|
||||
|
||||
template_body = <<STACK
|
||||
{
|
||||
"Parameters" : {
|
||||
"VPCCidr" : {
|
||||
"Type" : "String",
|
||||
"Default" : "10.0.0.0/16",
|
||||
"Description" : "Enter the CIDR block for the VPC. Default is 10.0.0.0/16."
|
||||
}
|
||||
},
|
||||
"Resources" : {
|
||||
"myVpc": {
|
||||
"Type" : "AWS::EC2::VPC",
|
||||
"Properties" : {
|
||||
"CidrBlock" : { "Ref" : "VPCCidr" },
|
||||
"Tags" : [
|
||||
{"Key": "Name", "Value": "Primary_CF_VPC"}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
STACK
|
||||
}
|
||||
|
||||
resource "aws_cloudformation_stack" "bar" {
|
||||
name = "bar-stack"
|
||||
|
||||
capabilities = [ "CAPABILITY_NAMED_IAM" ]
|
||||
|
||||
template_body = file("./iam.yml")
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/hashicorp/terraform/flatmap"
|
||||
)
|
||||
|
||||
const AwsCloudformationStackResourceType = "aws_cloudformation_stack"
|
||||
|
||||
func initAwsCloudformationStackMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
|
||||
resourceSchemaRepository.SetResolveReadAttributesFunc(AwsCloudformationStackResourceType, func(res *resource.Resource) map[string]string {
|
||||
attrs := make(map[string]interface{})
|
||||
if v := res.Attributes().GetMap("parameters"); v != nil {
|
||||
attrs["parameters.%"] = strconv.FormatInt(int64(len(v)), 10)
|
||||
attrs["parameters"] = v
|
||||
}
|
||||
return flatmap.Flatten(attrs)
|
||||
})
|
||||
resourceSchemaRepository.SetNormalizeFunc(AwsCloudformationStackResourceType, func(res *resource.Resource) {
|
||||
val := res.Attrs
|
||||
val.SafeDelete([]string{"timeouts"})
|
||||
})
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package aws_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/acceptance"
|
||||
)
|
||||
|
||||
func TestAcc_Aws_CloudformationStack(t *testing.T) {
|
||||
acceptance.Run(t, acceptance.AccTestCase{
|
||||
TerraformVersion: "0.15.5",
|
||||
Paths: []string{"./testdata/acc/aws_cloudformation_stack"},
|
||||
Args: []string{"scan", "--filter", "Type=='aws_cloudformation_stack'", "--deep"},
|
||||
Checks: []acceptance.AccCheck{
|
||||
{
|
||||
Env: map[string]string{
|
||||
"AWS_REGION": "us-east-1",
|
||||
},
|
||||
Check: func(result *test.ScanResult, stdout string, err error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result.AssertInfrastructureIsInSync()
|
||||
result.AssertManagedCount(2)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
|
@ -49,4 +49,5 @@ func InitResourcesMetadata(resourceSchemaRepository resource.SchemaRepositoryInt
|
|||
initAwsSecurityGroupRuleMetaData(resourceSchemaRepository)
|
||||
initAwsSecurityGroupMetaData(resourceSchemaRepository)
|
||||
initAwsRDSClusterMetaData(resourceSchemaRepository)
|
||||
initAwsCloudformationStackMetaData(resourceSchemaRepository)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# This file is maintained automatically by "terraform init".
|
||||
# Manual edits may be lost in future updates.
|
||||
|
||||
provider "registry.terraform.io/hashicorp/aws" {
|
||||
version = "3.19.0"
|
||||
constraints = "3.19.0"
|
||||
hashes = [
|
||||
"h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=",
|
||||
"zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b",
|
||||
"zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c",
|
||||
"zh:703f6da36b1b5f3497baa38fccaa7765fb8a2b6440344e4c97172516b49437dd",
|
||||
"zh:770855565462abadbbddd98cb357d2f1a8f30f68a358cb37cbd5c072cb15b377",
|
||||
"zh:8008db43149fe4345301f81e15e6d9ddb47aa5e7a31648f9b290af96ad86e92a",
|
||||
"zh:8cdd27d375da6dcb7687f1fed126b7c04efce1671066802ee876dbbc9c66ec79",
|
||||
"zh:be22ae185005690d1a017c1b909e0d80ab567e239b4f06ecacdba85080667c1c",
|
||||
"zh:d2d02e72dbd80f607636cd6237a6c862897caabc635c7b50c0cb243d11246723",
|
||||
"zh:d8f125b66a1eda2555c0f9bbdf12036a5f8d073499a22ca9e4812b68067fea31",
|
||||
"zh:f5a98024c64d5d2973ff15b093725a074c0cb4afde07ef32c542e69f17ac90bc",
|
||||
]
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
Resources:
|
||||
myUser:
|
||||
Type: AWS::IAM::User
|
||||
Properties:
|
||||
UserName: "bar_cfn"
|
|
@ -0,0 +1,48 @@
|
|||
provider "aws" {
|
||||
region = "us-east-1"
|
||||
}
|
||||
|
||||
terraform {
|
||||
required_providers {
|
||||
aws = "3.19.0"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_cloudformation_stack" "foo" {
|
||||
name = "foo-stack"
|
||||
|
||||
parameters = {
|
||||
VPCCidr = "10.0.0.0/16"
|
||||
}
|
||||
|
||||
template_body = <<STACK
|
||||
{
|
||||
"Parameters" : {
|
||||
"VPCCidr" : {
|
||||
"Type" : "String",
|
||||
"Default" : "10.0.0.0/16",
|
||||
"Description" : "Enter the CIDR block for the VPC. Default is 10.0.0.0/16."
|
||||
}
|
||||
},
|
||||
"Resources" : {
|
||||
"myVpc": {
|
||||
"Type" : "AWS::EC2::VPC",
|
||||
"Properties" : {
|
||||
"CidrBlock" : { "Ref" : "VPCCidr" },
|
||||
"Tags" : [
|
||||
{"Key": "Name", "Value": "Primary_CF_VPC"}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
STACK
|
||||
}
|
||||
|
||||
resource "aws_cloudformation_stack" "bar" {
|
||||
name = "bar-stack"
|
||||
|
||||
capabilities = [ "CAPABILITY_NAMED_IAM" ]
|
||||
|
||||
template_body = file("./iam.yml")
|
||||
}
|
|
@ -56,6 +56,7 @@ var supportedTypes = map[string]struct{}{
|
|||
"aws_subnet": {},
|
||||
"aws_vpc": {},
|
||||
"aws_rds_cluster": {},
|
||||
"aws_cloudformation_stack": {},
|
||||
|
||||
"github_branch_protection": {},
|
||||
"github_membership": {},
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
|
||||
)
|
||||
|
||||
type FakeCloudformation interface {
|
||||
cloudformationiface.CloudFormationAPI
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue