refactor(aws): launch template enumerator

Remove unused credit_specification attribute
main
sundowndev 2021-12-08 15:31:03 +01:00
parent 83d279c70f
commit a840413568
No known key found for this signature in database
GPG Key ID: 100CE2799D978462
4 changed files with 1 additions and 172 deletions

View File

@ -1,7 +1,6 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/snyk/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/pkg/remote/error"
"github.com/snyk/driftctl/pkg/resource"
@ -33,33 +32,15 @@ func (e *LaunchTemplateEnumerator) Enumerate() ([]*resource.Resource, error) {
results := make([]*resource.Resource, 0, len(templates))
for _, tmpl := range templates {
launchTemplateVersions, err := e.repository.DescribeLaunchTemplateVersions(tmpl)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
ltData := launchTemplateVersions[0].LaunchTemplateData
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*tmpl.LaunchTemplateId,
map[string]interface{}{
"credit_specification": getCreditSpecification(ltData.CreditSpecification),
},
map[string]interface{}{},
),
)
}
return results, nil
}
func getCreditSpecification(cs *ec2.CreditSpecification) []interface{} {
spec := make([]interface{}, 0)
if cs != nil {
spec = append(spec, map[string]interface{}{
"cpu_credits": *cs.CpuCredits,
})
}
return spec
}

View File

@ -1,9 +1,6 @@
package repository
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
@ -27,7 +24,6 @@ type EC2Repository interface {
ListAllSecurityGroups() ([]*ec2.SecurityGroup, []*ec2.SecurityGroup, error)
ListAllNetworkACLs() ([]*ec2.NetworkAcl, error)
DescribeLaunchTemplates() ([]*ec2.LaunchTemplate, error)
DescribeLaunchTemplateVersions(*ec2.LaunchTemplate) ([]*ec2.LaunchTemplateVersion, error)
}
type ec2Repository struct {
@ -395,24 +391,3 @@ func (r *ec2Repository) DescribeLaunchTemplates() ([]*ec2.LaunchTemplate, error)
r.cache.Put(cacheKey, resp.LaunchTemplates)
return resp.LaunchTemplates, nil
}
func (r *ec2Repository) DescribeLaunchTemplateVersions(tmpl *ec2.LaunchTemplate) ([]*ec2.LaunchTemplateVersion, error) {
cacheKey := fmt.Sprintf("DescribeLaunchTemplateVersions_%s", *tmpl.LaunchTemplateId)
if v := r.cache.Get(cacheKey); v != nil {
return v.([]*ec2.LaunchTemplateVersion), nil
}
version := strconv.Itoa(int(*tmpl.LatestVersionNumber))
input := ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: tmpl.LaunchTemplateId,
Versions: []*string{&version},
}
resp, err := r.client.DescribeLaunchTemplateVersions(&input)
if err != nil {
return nil, err
}
r.cache.Put(cacheKey, resp.LaunchTemplateVersions)
return resp.LaunchTemplateVersions, nil
}

View File

@ -1355,96 +1355,3 @@ func Test_ec2Repository_DescribeLaunchTemplates(t *testing.T) {
})
}
}
func Test_ec2Repository_DescribeLaunchTemplateVersions(t *testing.T) {
testErr := errors.New("test")
tests := []struct {
name string
tmpl *ec2.LaunchTemplate
mocks func(client *awstest.MockFakeEC2)
want []*ec2.LaunchTemplateVersion
wantErr error
}{
{
name: "List with 1 pages",
tmpl: &ec2.LaunchTemplate{
LaunchTemplateId: aws.String("id1"),
LatestVersionNumber: aws.Int64(1),
},
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeLaunchTemplateVersions",
&ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: aws.String("id1"),
Versions: []*string{aws.String("1")},
},
).Return(&ec2.DescribeLaunchTemplateVersionsOutput{
LaunchTemplateVersions: []*ec2.LaunchTemplateVersion{
{
LaunchTemplateId: aws.String("id1"),
},
{
LaunchTemplateId: aws.String("id2"),
},
},
}, nil).Once()
},
want: []*ec2.LaunchTemplateVersion{
{
LaunchTemplateId: aws.String("id1"),
},
{
LaunchTemplateId: aws.String("id2"),
},
},
},
{
name: "List return error",
tmpl: &ec2.LaunchTemplate{
LaunchTemplateId: aws.String("id1"),
LatestVersionNumber: aws.Int64(1),
},
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeLaunchTemplateVersions",
&ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateId: aws.String("id1"),
Versions: []*string{aws.String("1")},
},
).Return(nil, testErr)
},
wantErr: testErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
client := &awstest.MockFakeEC2{}
tt.mocks(client)
r := &ec2Repository{
client: client,
cache: store,
}
got, err := r.DescribeLaunchTemplateVersions(tt.tmpl)
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.DescribeLaunchTemplateVersions(tt.tmpl)
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*ec2.LaunchTemplateVersion{}, store.Get("DescribeLaunchTemplateVersions_id1"))
}
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()
}
client.AssertExpectations(t)
})
}
}

View File

@ -2739,22 +2739,6 @@ func TestEC2LaunchTemplate(t *testing.T) {
}
repository.On("DescribeLaunchTemplates").Return(launchTemplates, nil)
repository.On("DescribeLaunchTemplateVersions", launchTemplates[0]).Return([]*ec2.LaunchTemplateVersion{
{
LaunchTemplateData: &ec2.ResponseLaunchTemplateData{
CreditSpecification: &ec2.CreditSpecification{CpuCredits: awssdk.String("standard")},
},
},
}, nil).Once()
repository.On("DescribeLaunchTemplateVersions", launchTemplates[1]).Return([]*ec2.LaunchTemplateVersion{
{
LaunchTemplateData: &ec2.ResponseLaunchTemplateData{
CreditSpecification: &ec2.CreditSpecification{CpuCredits: awssdk.String("standard")},
},
},
}, nil).Once()
},
},
{
@ -2768,24 +2752,6 @@ func TestEC2LaunchTemplate(t *testing.T) {
},
wantErr: nil,
},
{
test: "cannot list launch template versions",
dirName: "aws_launch_template",
mocks: func(repository *repository.MockEC2Repository, alerter *mocks.AlerterInterface) {
awsError := awserr.NewRequestFailure(awserr.New("AccessDeniedException", "", errors.New("")), 403, "")
launchTemplates := []*ec2.LaunchTemplate{
{LaunchTemplateId: awssdk.String("lt-0ed993d09ce6afc67"), LatestVersionNumber: awssdk.Int64(1)},
{LaunchTemplateId: awssdk.String("lt-00b2d18c6cee7fe23"), LatestVersionNumber: awssdk.Int64(1)},
}
repository.On("DescribeLaunchTemplates").Return(launchTemplates, nil)
repository.On("DescribeLaunchTemplateVersions", launchTemplates[0]).Return(nil, awsError).Once()
alerter.On("SendAlert", resourceaws.AwsLaunchTemplateResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(awsError, resourceaws.AwsLaunchTemplateResourceType, resourceaws.AwsLaunchTemplateResourceType), alerts.EnumerationPhase)).Return()
},
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")