|
| 1 | +import { ProviderData } from '@cloudgraph/sdk' |
| 2 | +import get from 'lodash/get' |
| 3 | +import isEmpty from 'lodash/isEmpty' |
| 4 | +import { rawDataInterface } from '.' |
| 5 | +import { regionMap } from '../../enums/regions' |
| 6 | +import services from '../../enums/services' |
| 7 | +import addAccountConnections from '../account/connections' |
| 8 | + |
| 9 | +/** |
| 10 | + * Data Enhancers |
| 11 | + */ |
| 12 | +export interface EnhancerConfig { |
| 13 | + rawData: rawDataInterface[] |
| 14 | + accounts: { id: string; regions: string[] }[] |
| 15 | + configuredRegions: string |
| 16 | + data: ProviderData |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Generates AWS services connections to Scanned accounts |
| 21 | + * @param {EnhancerConfig} accounts Scanned accounts |
| 22 | + * @param {EnhancerConfig} data AWS Services fetched data |
| 23 | + * @returns {ProviderData} |
| 24 | + */ |
| 25 | +export const connectAWSServicesToAccount = ({ |
| 26 | + accounts, |
| 27 | + data, |
| 28 | +}: EnhancerConfig): ProviderData => { |
| 29 | + let accountsConnections = {} |
| 30 | + for (const account of accounts) { |
| 31 | + const connections = addAccountConnections({ |
| 32 | + service: account, |
| 33 | + data: data.entities, |
| 34 | + }) |
| 35 | + accountsConnections = { |
| 36 | + ...accountsConnections, |
| 37 | + ...connections, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return { |
| 42 | + entities: data.entities, |
| 43 | + connections: { |
| 44 | + ...data.connections, |
| 45 | + ...accountsConnections, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Adds Billing data to EC2 instances |
| 52 | + * @param {EnhancerConfig} |
| 53 | + * @returns {ProviderData} |
| 54 | + */ |
| 55 | +export const enrichInstanceWithBillingData = ({ |
| 56 | + rawData, |
| 57 | + configuredRegions, |
| 58 | + data: { entities, connections }, |
| 59 | +}: EnhancerConfig): ProviderData => { |
| 60 | + const billingRegion = regionMap.usEast1 |
| 61 | + let result = entities |
| 62 | + if (configuredRegions.includes(billingRegion)) { |
| 63 | + const billingArray = |
| 64 | + rawData.find(({ name }) => name === services.billing)?.data?.[ |
| 65 | + billingRegion |
| 66 | + ] ?? [] |
| 67 | + for (const billing of billingArray) { |
| 68 | + const individualData: { |
| 69 | + [key: string]: { |
| 70 | + cost: number |
| 71 | + currency: string |
| 72 | + formattedCost: string |
| 73 | + } |
| 74 | + } = get(billing, ['individualData'], undefined) |
| 75 | + if (individualData) { |
| 76 | + for (const [key, value] of Object.entries(individualData)) { |
| 77 | + if (key.includes('natgateway') && !isEmpty()) { |
| 78 | + // this billing data is for natgateway, search for the instance |
| 79 | + const { |
| 80 | + name, |
| 81 | + mutation, |
| 82 | + data: nats, |
| 83 | + } = result.find( |
| 84 | + ({ name: instanceName }: { name: string }) => |
| 85 | + instanceName === services.nat |
| 86 | + ) ?? {} |
| 87 | + if (!isEmpty(nats)) { |
| 88 | + const natsWithBilling = nats.map(val => { |
| 89 | + if (key.includes(val.id)) { |
| 90 | + return { |
| 91 | + ...val, |
| 92 | + dailyCost: { |
| 93 | + cost: value?.cost, |
| 94 | + currency: value?.currency, |
| 95 | + formattedCost: value?.formattedCost, |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | + return val |
| 100 | + }) |
| 101 | + result = result.filter( |
| 102 | + ({ name: serviceName }) => serviceName !== services.nat |
| 103 | + ) |
| 104 | + result.push({ |
| 105 | + name, |
| 106 | + mutation, |
| 107 | + data: natsWithBilling, |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + if (key.includes('i-')) { |
| 112 | + // this billing data is for ec2, search for the instance |
| 113 | + const { |
| 114 | + name, |
| 115 | + mutation, |
| 116 | + data: ec2s, |
| 117 | + } = result.find( |
| 118 | + ({ name: instanceName }: { name: string }) => |
| 119 | + instanceName === services.ec2Instance |
| 120 | + ) ?? {} |
| 121 | + if (!isEmpty(ec2s)) { |
| 122 | + const ec2WithBilling = ec2s.map(val => { |
| 123 | + if (key === val.id) { |
| 124 | + return { |
| 125 | + ...val, |
| 126 | + dailyCost: { |
| 127 | + cost: value?.cost, |
| 128 | + currency: value?.currency, |
| 129 | + formattedCost: value?.formattedCost, |
| 130 | + }, |
| 131 | + } |
| 132 | + } |
| 133 | + return val |
| 134 | + }) |
| 135 | + result = result.filter( |
| 136 | + ({ name: serviceName }) => serviceName !== services.ec2Instance |
| 137 | + ) |
| 138 | + result.push({ |
| 139 | + name, |
| 140 | + mutation, |
| 141 | + data: ec2WithBilling, |
| 142 | + }) |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return { |
| 150 | + entities: result, |
| 151 | + connections, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +export default [ |
| 156 | + { name: 'account', enhancer: connectAWSServicesToAccount }, |
| 157 | + { name: 'billing', enhancer: enrichInstanceWithBillingData }, |
| 158 | +] |
0 commit comments