Skip to content

Commit 52b277a

Browse files
committed
feat: Add Additional data to transit gateway attachment
1 parent 76df6f6 commit 52b277a

7 files changed

Lines changed: 113 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ CloudGraph AWS Provider will ask you what regions you would like to crawl and wi
153153
| ses | |
154154
| sns | kms, cloudtrail, cloudwatch, s3 |
155155
| sqs | elasticBeanstalkEnv, s3 |
156-
| subnet | alb, asg, codebuild, dmsReplicationInstance, ec2, ecsService, efsMountTarget, elastiCacheCluster, elasticSearchDomain, elb, lambda, managedAirflow, natGateway, networkInterface, rdsCluster, sageMakerNotebookInstance, routeTable, vpc, eksCluster, emrCluster, flowLog |
156+
| subnet | alb, asg, codebuild, dmsReplicationInstance, ec2, ecsService, efsMountTarget, elastiCacheCluster, elasticSearchDomain, elb, lambda, managedAirflow, natGateway, networkInterface, rdsCluster, sageMakerNotebookInstance, routeTable, transitGatewayAttachment, vpc, eksCluster, emrCluster, flowLog |
157157
| systemsManagerInstance | ec2, iamRole |
158158
| systemsManagerDocument | |
159159
| transitGateway | routeTable, transitGatewayAttachment, vpnConnection |
160-
| transitGatewayAttachment | routeTable, transitGateway, vpc, vpnConnection |
160+
| transitGatewayAttachment | routeTable, subnet, transitGateway, vpc, vpnConnection |
161161
| vpc | alb, codebuild, dmsReplicationInstance, ec2, eip, elb, ecsService, efsMountTarget, eksCluster igw, elastiCacheCluster, elasticSearchDomain, lambda, nacl, natGateway, networkInterface, rdsClusterSnapshot, rdsDbInstance, redshiftCluster, route53HostedZone, routeTable, subnet, flowLog, vpnGateway, transitGatewayAttachment |
162162
| vpnConnection | customerGateway, transitGateway, transitGatewayAttachment, vpnGateway |
163163
| vpnGateway | vpc, vpnConnection |

src/properties/logger.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ export default {
479479
* Transit Gateway Attachment
480480
*/
481481
fetchedTransitGatewayAttachments: (num: number): string => `Found ${num} Transit Gateway Attachments`,
482+
fetchedTransitGatewayVpcAttachments: (num: number): string => `Found ${num} Transit Gateway VPC Attachments`,
482483
/**
483484
* VPN Gateway
484485
*/

src/services/subnet/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ type awsSubnet implements awsBaseService @key(fields: "id") {
3333
sageMakerNotebookInstances: [awsSageMakerNotebookInstance]
3434
@hasInverse(field: subnet)
3535
rdsCluster: [awsRdsCluster] @hasInverse(field: subnets)
36+
transitGatewayAttachment: [awsTransitGatewayAttachment] @hasInverse(field: subnets)
3637
}

src/services/transitGatewayAttachment/connections.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import isEmpty from 'lodash/isEmpty'
22

33
import { ServiceConnection } from '@cloudgraph/sdk'
44

5-
import { TransitGatewayAttachment, TagList } from 'aws-sdk/clients/ec2'
6-
75
import services from '../../enums/services'
86
import { RawAwsVpnConnection } from '../vpnConnection/data'
97
import { RawAwsVpc } from '../vpc/data'
108
import { RawAwsTransitGateway } from '../transitGateway/data'
9+
import { RawAwsTransitGatewayAttachment } from '../transitGatewayAttachment/data'
1110
import { RawAwsRouteTable } from '../routeTable/data'
11+
import { RawAwsSubnet } from '../subnet/data'
1212

1313
/**
1414
* Transit Gateway Attachment
@@ -21,9 +21,7 @@ export default ({
2121
}: {
2222
account: string
2323
data: { name: string; data: { [property: string]: any[] } }[]
24-
service: TransitGatewayAttachment & {
25-
Tags?: TagList
26-
}
24+
service: RawAwsTransitGatewayAttachment
2725
region: string
2826
}): { [key: string]: ServiceConnection[] } => {
2927
const connections: ServiceConnection[] = []
@@ -32,6 +30,7 @@ export default ({
3230
TransitGatewayId: transitGatewayId,
3331
Association: association,
3432
ResourceId: resourceId,
33+
SubnetIds: subnetIds,
3534
} = transitGatewayAttachment
3635

3736
/**
@@ -147,6 +146,29 @@ export default ({
147146
}
148147
}
149148

149+
/**
150+
* Find Subnets
151+
* related to this Transit Gateway Attachment
152+
*/
153+
const subnets = data.find(({ name }) => name === services.subnet)
154+
if (subnets?.data?.[region]) {
155+
const subnetsInRegion = subnets.data[region].filter(
156+
({ SubnetId }: RawAwsSubnet) => subnetIds.includes(SubnetId)
157+
)
158+
if (!isEmpty(subnetsInRegion)) {
159+
for (const subnet of subnetsInRegion) {
160+
const { SubnetId }: RawAwsSubnet = subnet
161+
162+
connections.push({
163+
id: SubnetId,
164+
resourceType: services.subnet,
165+
relation: 'child',
166+
field: 'subnets',
167+
})
168+
}
169+
}
170+
}
171+
150172
const transitGatewayAttachmentResult = {
151173
[id]: connections,
152174
}

src/services/transitGatewayAttachment/data.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import EC2, {
77
DescribeTransitGatewayAttachmentsResult,
88
TransitGatewayAttachmentList,
99
TransitGatewayAttachment,
10+
DescribeTransitGatewayVpcAttachmentsResult,
11+
TransitGatewayVpcAttachmentList,
12+
TransitGatewayVpcAttachment,
13+
DescribeTransitGatewayVpcAttachmentsRequest,
1014
} from 'aws-sdk/clients/ec2'
1115

1216
import { Config } from 'aws-sdk/lib/config'
@@ -52,7 +56,6 @@ const listTransitGatewayAttachmentsData = async ({
5256
err,
5357
})
5458
}
55-
5659
if (!isEmpty(data)) {
5760
const {
5861
NextToken: nextToken,
@@ -84,6 +87,67 @@ const listTransitGatewayAttachmentsData = async ({
8487
)
8588
})
8689

90+
const listTransitGatewayVpcAttachmentsData = async ({
91+
ec2,
92+
region,
93+
nextToken: NextToken = '',
94+
}: {
95+
ec2: EC2
96+
region: string
97+
nextToken?: string
98+
}): Promise<(TransitGatewayVpcAttachment & { region: string })[]> =>
99+
new Promise<(TransitGatewayVpcAttachment & { region: string })[]>(resolve => {
100+
let transitGatewayVpcAttachmentData: (TransitGatewayVpcAttachment & {
101+
region: string
102+
})[] = []
103+
const transitGatewayVpcAttachmentList: TransitGatewayVpcAttachmentList = []
104+
let args: DescribeTransitGatewayVpcAttachmentsRequest = {}
105+
106+
if (NextToken) {
107+
args = { ...args, NextToken }
108+
}
109+
110+
ec2.describeTransitGatewayVpcAttachments(
111+
args,
112+
(err: AWSError, data: DescribeTransitGatewayVpcAttachmentsResult) => {
113+
if (err) {
114+
errorLog.generateAwsErrorLog({
115+
functionName: 'ec2:describeTransitGatewayVpcAttachments',
116+
err,
117+
})
118+
}
119+
120+
if (!isEmpty(data)) {
121+
const {
122+
NextToken: nextToken,
123+
TransitGatewayVpcAttachments: transitGatewayVpcAttachments = [],
124+
} = data
125+
126+
transitGatewayVpcAttachmentList.push(...transitGatewayVpcAttachments)
127+
128+
logger.debug(
129+
lt.fetchedTransitGatewayVpcAttachments(
130+
transitGatewayVpcAttachments.length
131+
)
132+
)
133+
134+
if (nextToken) {
135+
listTransitGatewayVpcAttachmentsData({ ec2, region, nextToken })
136+
}
137+
138+
transitGatewayVpcAttachmentData = transitGatewayVpcAttachmentList.map(
139+
vpcAttachment => ({
140+
...vpcAttachment,
141+
region,
142+
})
143+
)
144+
}
145+
146+
resolve(transitGatewayVpcAttachmentData)
147+
}
148+
)
149+
})
150+
87151
/**
88152
* Transit Gateway Attachment
89153
*/
@@ -92,6 +156,7 @@ export interface RawAwsTransitGatewayAttachment
92156
extends Omit<TransitGatewayAttachment, 'Tags'> {
93157
region: string
94158
Tags?: TagMap
159+
SubnetIds?: string[]
95160
}
96161

97162
export default async ({
@@ -118,11 +183,24 @@ export default async ({
118183
})
119184

120185
if (!isEmpty(transitGatewayAttachments)) {
186+
// Get Transit Gateway Vpc Attachment Data
187+
const transitGatewayVpcAttachments =
188+
await listTransitGatewayVpcAttachmentsData({
189+
ec2,
190+
region,
191+
})
192+
121193
for (const attachment of transitGatewayAttachments) {
122194
transitGatewayAttachmentsResult.push({
123195
...attachment,
124196
region,
125197
Tags: convertAwsTagsToTagMap(attachment.Tags as AwsTag[]),
198+
SubnetIds:
199+
transitGatewayVpcAttachments?.find(
200+
a =>
201+
a.TransitGatewayAttachmentId ===
202+
attachment.TransitGatewayAttachmentId
203+
)?.SubnetIds || [],
126204
})
127205
}
128206
}

src/services/transitGatewayAttachment/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ type awsTransitGatewayAttachment @key(fields: "arn") {
1717
routeTable: [awsRouteTable] @hasInverse(field: transitGatewayAttachment)
1818
vpc: [awsVpc] @hasInverse(field: transitGatewayAttachments)
1919
vpnConnection: [awsVpnConnection] @hasInverse(field: transitGatewayAttachment)
20+
subnets: [awsSubnet] @hasInverse(field: transitGatewayAttachment)
2021
}

src/types/generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4054,6 +4054,7 @@ export type AwsSubnet = AwsBaseService & {
40544054
sageMakerNotebookInstances?: Maybe<Array<Maybe<AwsSageMakerNotebookInstance>>>;
40554055
state?: Maybe<Scalars['String']>;
40564056
tags?: Maybe<Array<Maybe<AwsRawTag>>>;
4057+
transitGatewayAttachment?: Maybe<Array<Maybe<AwsTransitGatewayAttachment>>>;
40574058
vpc?: Maybe<Array<Maybe<AwsVpc>>>;
40584059
};
40594060

@@ -4225,6 +4226,7 @@ export type AwsTransitGatewayAttachment = {
42254226
resourceType?: Maybe<Scalars['String']>;
42264227
routeTable?: Maybe<Array<Maybe<AwsRouteTable>>>;
42274228
state?: Maybe<Scalars['String']>;
4229+
subnets?: Maybe<Array<Maybe<AwsSubnet>>>;
42284230
tags?: Maybe<Array<Maybe<AwsRawTag>>>;
42294231
transitGateway?: Maybe<Array<Maybe<AwsTransitGateway>>>;
42304232
transitGatewayId?: Maybe<Scalars['String']>;

0 commit comments

Comments
 (0)