Add aws_apigatewayv2_vpc_link resource

main
William Beuil 2021-11-17 17:03:37 +01:00
parent 383ae5d0c9
commit 141c642c98
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
17 changed files with 172150 additions and 0 deletions

View File

@ -176,6 +176,7 @@ func TestTerraformStateReader_AWS_Resources(t *testing.T) {
{name: "default network acl", dirName: "aws_default_network_acl", wantErr: false},
{name: "App autoscaling policy", dirName: "aws_appautoscaling_policy", wantErr: false},
{name: "App autoscaling scheduled action", dirName: "aws_appautoscaling_scheduled_action", wantErr: false},
{name: "App gateway v2 vpc link", dirName: "apigatewayv2_vpc_link", wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,21 @@
[
{
"Id": "b8r351",
"Type": "aws_apigatewayv2_vpc_link",
"Attrs": {
"arn": "arn:aws:apigateway:us-east-2::/vpclinks/b8r351",
"id": "b8r351",
"name": "foo",
"security_group_ids": [
"sg-055d40d3b933c2c4b"
],
"subnet_ids": [
"subnet-06e2e690aa9cfcb4e",
"subnet-0b536f373c65d780a"
],
"tags": {
"Usage": "example"
}
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
{
"version": 4,
"terraform_version": "1.0.5",
"serial": 14,
"lineage": "e6ff1294-2037-a68a-e0b9-73953d4da0e7",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_apigatewayv2_vpc_link",
"name": "foo",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arn": "arn:aws:apigateway:us-east-2::/vpclinks/b8r351",
"id": "b8r351",
"name": "foo",
"security_group_ids": [
"sg-055d40d3b933c2c4b"
],
"subnet_ids": [
"subnet-06e2e690aa9cfcb4e",
"subnet-0b536f373c65d780a"
],
"tags": {
"Usage": "example"
},
"tags_all": {
"Usage": "example"
}
},
"sensitive_attributes": [],
"private": "bnVsbA==",
"dependencies": [
"aws_security_group.foo",
"aws_subnet.subnet1",
"aws_subnet.subnet2"
]
}
]
}
]
}

View File

@ -0,0 +1,46 @@
package aws
import (
"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 ApiGatewayV2VpcLinkEnumerator struct {
repository repository.ApiGatewayV2Repository
factory resource.ResourceFactory
}
func NewApiGatewayV2VpcLinkEnumerator(repo repository.ApiGatewayV2Repository, factory resource.ResourceFactory) *ApiGatewayV2VpcLinkEnumerator {
return &ApiGatewayV2VpcLinkEnumerator{
repository: repo,
factory: factory,
}
}
func (e *ApiGatewayV2VpcLinkEnumerator) SupportedType() resource.ResourceType {
return aws.AwsApiGatewayV2VpcLinkResourceType
}
func (e *ApiGatewayV2VpcLinkEnumerator) Enumerate() ([]*resource.Resource, error) {
vpcLinks, err := e.repository.ListAllVpcLinks()
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0, len(vpcLinks))
for _, vpcLink := range vpcLinks {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*vpcLink.VpcLinkId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -203,6 +203,7 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewApiGatewayIntegrationResponseEnumerator(apigatewayRepository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2ApiEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewApiGatewayV2VpcLinkEnumerator(apigatewayv2Repository, factory))
remoteLibrary.AddEnumerator(NewAppAutoscalingTargetEnumerator(appAutoScalingRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, common.NewGenericDetailsFetcher(aws.AwsAppAutoscalingTargetResourceType, provider, deserializer))

View File

@ -9,6 +9,7 @@ import (
type ApiGatewayV2Repository interface {
ListAllApis() ([]*apigatewayv2.Api, error)
ListAllVpcLinks() ([]*apigatewayv2.VpcLink, error)
}
type apigatewayv2Repository struct {
@ -40,3 +41,18 @@ func (r *apigatewayv2Repository) ListAllApis() ([]*apigatewayv2.Api, error) {
r.cache.Put(cacheKey, resources.Items)
return resources.Items, nil
}
func (r *apigatewayv2Repository) ListAllVpcLinks() ([]*apigatewayv2.VpcLink, error) {
if v := r.cache.Get("apigatewayv2ListAllVpcLinks"); v != nil {
return v.([]*apigatewayv2.VpcLink), nil
}
input := apigatewayv2.GetVpcLinksInput{}
resources, err := r.client.GetVpcLinks(&input)
if err != nil {
return nil, err
}
r.cache.Put("apigatewayv2ListAllVpcLinks", resources.Items)
return resources.Items, nil
}

View File

@ -86,3 +86,76 @@ func Test_apigatewayv2Repository_ListAllApis(t *testing.T) {
})
}
}
func Test_apigatewayv2Repository_ListAllVpcLinks(t *testing.T) {
vpcLinks := []*apigatewayv2.VpcLink{
{VpcLinkId: aws.String("vpcLink1")},
{VpcLinkId: aws.String("vpcLink2")},
{VpcLinkId: aws.String("vpcLink3")},
{VpcLinkId: aws.String("vpcLink4")},
{VpcLinkId: aws.String("vpcLink5")},
{VpcLinkId: aws.String("vpcLink6")},
}
remoteError := errors.New("remote error")
tests := []struct {
name string
mocks func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache)
want []*apigatewayv2.VpcLink
wantErr error
}{
{
name: "list multiple vpc links",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
client.On("GetVpcLinks",
&apigatewayv2.GetVpcLinksInput{}).Return(&apigatewayv2.GetVpcLinksOutput{Items: vpcLinks}, nil).Once()
store.On("Get", "apigatewayv2ListAllVpcLinks").Return(nil).Times(1)
store.On("Put", "apigatewayv2ListAllVpcLinks", vpcLinks).Return(false).Times(1)
},
want: vpcLinks,
},
{
name: "should hit cache",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
store.On("Get", "apigatewayv2ListAllVpcLinks").Return(vpcLinks).Times(1)
},
want: vpcLinks,
},
{
name: "should return remote error",
mocks: func(client *awstest.MockFakeApiGatewayV2, store *cache.MockCache) {
client.On("GetVpcLinks",
&apigatewayv2.GetVpcLinksInput{}).Return(nil, remoteError).Once()
store.On("Get", "apigatewayv2ListAllVpcLinks").Return(nil).Times(1)
},
wantErr: remoteError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cache.MockCache{}
client := &awstest.MockFakeApiGatewayV2{}
tt.mocks(client, store)
r := &apigatewayv2Repository{
client: client,
cache: store,
}
got, err := r.ListAllVpcLinks()
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)
})
}
}

View File

@ -34,3 +34,26 @@ func (_m *MockApiGatewayV2Repository) ListAllApis() ([]*apigatewayv2.Api, error)
return r0, r1
}
// ListAllVpcLinks provides a mock function with given fields:
func (_m *MockApiGatewayV2Repository) ListAllVpcLinks() ([]*apigatewayv2.VpcLink, error) {
ret := _m.Called()
var r0 []*apigatewayv2.VpcLink
if rf, ok := ret.Get(0).(func() []*apigatewayv2.VpcLink); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*apigatewayv2.VpcLink)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -99,3 +99,82 @@ func TestApiGatewayV2Api(t *testing.T) {
})
}
}
func TestApiGatewayV2VpcLink(t *testing.T) {
dummyError := errors.New("this is an error")
tests := []struct {
test string
mocks func(*repository.MockApiGatewayV2Repository, *mocks.AlerterInterface)
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no api gateway v2 vpc links",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVpcLinks").Return([]*apigatewayv2.VpcLink{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "single api gateway v2 vpc link",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVpcLinks").Return([]*apigatewayv2.VpcLink{
{VpcLinkId: awssdk.String("b8r351")},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 1)
assert.Equal(t, got[0].ResourceId(), "b8r351")
assert.Equal(t, got[0].ResourceType(), resourceaws.AwsApiGatewayV2VpcLinkResourceType)
},
},
{
test: "cannot list api gateway v2 vpc links",
mocks: func(repository *repository.MockApiGatewayV2Repository, alerter *mocks.AlerterInterface) {
repository.On("ListAllVpcLinks").Return(nil, dummyError)
alerter.On("SendAlert", resourceaws.AwsApiGatewayV2VpcLinkResourceType, alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(dummyError, resourceaws.AwsApiGatewayV2VpcLinkResourceType, resourceaws.AwsApiGatewayV2VpcLinkResourceType), alerts.EnumerationPhase)).Return()
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceaws.AwsApiGatewayV2VpcLinkResourceType),
},
}
providerVersion := "3.19.0"
schemaRepository := testresource.InitFakeSchemaRepository("aws", providerVersion)
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockApiGatewayV2Repository{}
c.mocks(fakeRepo, alerter)
var repo repository.ApiGatewayV2Repository = fakeRepo
remoteLibrary.AddEnumerator(aws.NewApiGatewayV2VpcLinkEnumerator(repo, factory))
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
}
c.assertExpected(tt, got)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
testFilter.AssertExpectations(tt)
})
}
}

View File

@ -0,0 +1,3 @@
package aws
const AwsApiGatewayV2VpcLinkResourceType = "aws_apigatewayv2_vpc_link"

View File

@ -0,0 +1,30 @@
package aws_test
import (
"testing"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/acceptance"
)
func TestAcc_Aws_ApiGatewayV2VpcLink(t *testing.T) {
acceptance.Run(t, acceptance.AccTestCase{
TerraformVersion: "0.15.5",
Paths: []string{"./testdata/acc/aws_apigatewayv2_vpc_link"},
Args: []string{"scan"},
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(1)
},
},
},
})
}

View File

@ -32,6 +32,7 @@ func TestAWS_Metadata_Flags(t *testing.T) {
AwsApiGatewayStageResourceType: {},
AwsApiGatewayVpcLinkResourceType: {},
AwsApiGatewayV2ApiResourceType: {},
AwsApiGatewayV2VpcLinkResourceType: {},
AwsAppAutoscalingPolicyResourceType: {resource.FlagDeepMode},
AwsAppAutoscalingScheduledActionResourceType: {},
AwsAppAutoscalingTargetResourceType: {resource.FlagDeepMode},

View File

@ -0,0 +1,2 @@
*
!aws_apigatewayv2_vpc_link

View File

@ -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",
]
}

View File

@ -0,0 +1,31 @@
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = "3.19.0"
}
}
resource "aws_vpc" "vpc" {
cidr_block = "10.100.0.0/16"
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.100.0.0/24"
}
resource "aws_security_group" "foo" {
vpc_id = aws_vpc.vpc.id
}
resource "aws_apigatewayv2_vpc_link" "foo" {
name = "foo"
security_group_ids = [aws_security_group.foo.id]
subnet_ids = [aws_subnet.subnet.id]
tags = {
Usage = "example"
}
}

View File

@ -140,6 +140,7 @@ var supportedTypes = map[string]ResourceTypeMeta{
"aws_appautoscaling_policy": {},
"aws_appautoscaling_scheduled_action": {},
"aws_apigatewayv2_api": {},
"aws_apigatewayv2_vpc_link": {},
"github_branch_protection": {},
"github_membership": {},