Skip to content

Commit aaaf8b7

Browse files
authored
Merge pull request #79 from cloudgraphdev/feature/EP-3226-add-route-table-services
feat(azure): add route table services
2 parents f31a451 + 5573e8e commit aaaf8b7

File tree

17 files changed

+338
-0
lines changed

17 files changed

+338
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ CloudGraph needs read permissions in order to ingest your data. To keep things e
104104
| replicationNetworks | resourceGroup |
105105
| replicationPolicies | resourceGroup |
106106
| resourceGroup | **all services** |
107+
| routeFilter | |
108+
| routeTable | |
107109
| securityAssessments | |
108110
| securityContacts | |
109111
| securityGroup | networkInterface, resourceGroup |

src/enums/schemasMap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export default {
7777
[services.replicationNetworks]: 'azureReplicationNetwork',
7878
[services.replicationPolicies]: 'azureReplicationPolicy',
7979
[services.resourceGroup]: 'azureResourceGroup',
80+
[services.routeFilter]: 'azureRouteFilter',
81+
[services.routeTable]: 'azureRouteTable',
8082
[services.securityAssesments]: 'azureSecurityAssesment',
8183
[services.securityContacts]: 'azureSecurityContact',
8284
[services.securityGroup]: 'azureNetworkSecurityGroup',

src/enums/serviceAliases.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export default {
6565
[services.replicationNetworks]: 'replicationNetworks',
6666
[services.replicationPolicies]: 'replicationPolicies',
6767
[services.resourceGroup]: 'resourceGroups',
68+
[services.routeFilter]: 'routeFilters',
69+
[services.routeTable]: 'routeTables',
6870
[services.securityAssesments]: 'securityAssesments',
6971
[services.securityContacts]: 'securityContacts',
7072
[services.securityGroup]: 'securityGroups',

src/enums/serviceMap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import AzureBilling from '../services/billing'
8989
import AzureLogProfiles from '../services/logProfiles'
9090
import Subscription from '../services/subscription'
9191
import AzureNetworkWatcher from '../services/networkWatcher'
92+
import AzureRouteTable from '../services/routeTable'
93+
import AzureRouteFilter from '../services/routeFilter'
9294

9395
/**
9496
* serviceMap is an object that contains all currently supported services for AZURE
@@ -169,6 +171,8 @@ export default {
169171
[services.replicationNetworks]: AzureReplicationNetwork,
170172
[services.replicationPolicies]: AzureReplicationPolicy,
171173
[services.resourceGroup]: AzureResourceGroup,
174+
[services.routeFilter]: AzureRouteFilter,
175+
[services.routeTable]: AzureRouteTable,
172176
[services.securityAssesments]: AzureSecurityAssesments,
173177
[services.securityContacts]: AzureSecurityContacts,
174178
[services.securityGroup]: AzureNetworkSecurityGroup,

src/enums/services.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export default {
7171
replicationNetworks: 'replicationNetworks',
7272
replicationPolicies: 'replicationPolicies',
7373
resourceGroup: 'resourceGroup',
74+
routeFilter: 'routeFilter',
75+
routeTable: 'routeTable',
7476
securityAssesments: 'securityAssesments',
7577
securityContacts: 'securityContacts',
7678
securityGroup: 'securityGroup',

src/properties/logger.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ export default {
173173
`Found ${num} replication policies`,
174174
// Resource Groups
175175
foundResourceGroups: (num: number): string => `Found ${num} resource groups`,
176+
// Route Filters
177+
foundRouteFilters: (num: number): string => `Found ${num} route filters`,
178+
// Route Tables
179+
foundRouteTables: (num: number): string => `Found ${num} route tables`,
176180
// Security Assesments
177181
foundSecurityAssesments: (num: number): string =>
178182
`Found ${num} security assesments`,

src/services/routeFilter/data.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { NetworkManagementClient, RouteFilter } from '@azure/arm-network'
2+
import { PagedAsyncIterableIterator } from '@azure/core-paging'
3+
import CloudGraph from '@cloudgraph/sdk'
4+
5+
import azureLoggerText from '../../properties/logger'
6+
import { AzureServiceInput, TagMap } from '../../types'
7+
import { tryCatchWrapper } from '../../utils'
8+
import { lowerCaseLocation } from '../../utils/format'
9+
import { getResourceGroupFromEntity } from '../../utils/idParserUtils'
10+
11+
const { logger } = CloudGraph
12+
const lt = { ...azureLoggerText }
13+
const serviceName = 'RouteFilter'
14+
15+
export interface RawAzureRouteFilter
16+
extends Omit<RouteFilter, 'tags' | 'location'> {
17+
region: string
18+
resourceGroupId: string
19+
Tags: TagMap
20+
}
21+
22+
export default async ({
23+
regions,
24+
config,
25+
}: AzureServiceInput): Promise<{
26+
[property: string]: RawAzureRouteFilter[]
27+
}> => {
28+
try {
29+
const { tokenCredentials, subscriptionId } = config
30+
const client = new NetworkManagementClient(tokenCredentials, subscriptionId)
31+
32+
const routeFilterData: RawAzureRouteFilter[] = []
33+
await tryCatchWrapper(
34+
async () => {
35+
const routeFilterIterable: PagedAsyncIterableIterator<RouteFilter> =
36+
client.routeFilters.list()
37+
for await (const routeFilter of routeFilterIterable) {
38+
if (routeFilter) {
39+
const { location, tags, ...rest } = routeFilter
40+
const resourceGroupId = getResourceGroupFromEntity(rest)
41+
const region = lowerCaseLocation(location)
42+
routeFilterData.push({
43+
...rest,
44+
region,
45+
resourceGroupId,
46+
Tags: tags || {},
47+
})
48+
}
49+
}
50+
},
51+
{
52+
service: serviceName,
53+
client,
54+
scope: 'routeFilters',
55+
operation: 'list',
56+
}
57+
)
58+
const result: { [property: string]: RawAzureRouteFilter[] } = {}
59+
let numOfGroups = 0
60+
routeFilterData.forEach(({ region, ...rest }) => {
61+
if (regions.includes(region)) {
62+
if (!result[region]) {
63+
result[region] = []
64+
}
65+
result[region].push({
66+
region,
67+
...rest,
68+
})
69+
numOfGroups += 1
70+
}
71+
})
72+
logger.debug(lt.foundRouteFilters(numOfGroups))
73+
74+
return result
75+
} catch (e) {
76+
logger.error(e)
77+
return {}
78+
}
79+
}

src/services/routeFilter/format.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { RawAzureRouteFilter } from './data'
2+
import { AzureRouteFilter } from '../../types/generated'
3+
import { formatTagsFromMap } from '../../utils/format'
4+
5+
export default ({
6+
service,
7+
account: subscriptionId,
8+
}: {
9+
service: RawAzureRouteFilter
10+
account: string
11+
}): AzureRouteFilter => {
12+
const { id, name, type, region, Tags } = service
13+
14+
return {
15+
id,
16+
name,
17+
type,
18+
region,
19+
subscriptionId,
20+
tags: formatTagsFromMap(Tags),
21+
}
22+
}

src/services/routeFilter/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Service } from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import mutation from './mutation'
5+
import getData from './data'
6+
7+
export default class AzureRouteFilter extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = mutation
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `mutation($input: [AddazureRouteFilterInput!]!) {
2+
addazureRouteFilter(input: $input, upsert: true) {
3+
numUids
4+
}
5+
}`;

0 commit comments

Comments
 (0)