driftctl/enumeration/remote/aws/cloudformation_stack_enumer...

64 lines
1.7 KiB
Go
Raw Normal View History

2021-09-08 09:16:44 +00:00
package aws
import (
"strconv"
2021-09-08 09:16:44 +00:00
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/snyk/driftctl/enumeration/resource/aws"
2021-09-08 09:16:44 +00:00
)
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()))
}
2021-10-27 09:30:47 +00:00
results := make([]*resource.Resource, 0, len(stacks))
2021-09-08 09:16:44 +00:00
for _, stack := range stacks {
attrs := map[string]interface{}{}
if stack.Parameters != nil && len(stack.Parameters) > 0 {
attrs["parameters.%"] = strconv.FormatInt(int64(len(stack.Parameters)), 10)
2021-09-08 09:16:44 +00:00
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
}