|
| 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 | +} |
0 commit comments