Skip to content

Commit cbecdc4

Browse files
author
Marco Franceschi
committed
feat: Created enhancers for scanned data
1 parent 3aaa470 commit cbecdc4

7 files changed

Lines changed: 206 additions & 137 deletions

File tree

src/services/account/connections.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { ServiceConnection } from '@cloudgraph/sdk'
1+
import { Entity, ServiceConnection } from '@cloudgraph/sdk'
22
import { flatMap } from 'lodash'
33
import services from '../../enums/services'
4+
import aliases from '../../enums/serviceAliases'
45

56
export default ({
67
service,
78
data,
89
}: {
910
service: any
10-
data: Array<{
11-
name: string
12-
accountId: string
13-
data: { [property: string]: any[] }
14-
}>
11+
data: Entity[]
1512
}): {
1613
[property: string]: ServiceConnection[]
1714
} => {
@@ -33,10 +30,10 @@ export default ({
3330
for (const instance of filtered) {
3431
if (instance) {
3532
connections.push({
36-
id: instance.cgId,
33+
id: instance.id,
3734
resourceType: serviceName,
3835
relation: 'child',
39-
field: serviceName,
36+
field: aliases[serviceName] ? aliases[serviceName] : serviceName,
4037
})
4138
}
4239
}

src/services/account/data.ts

Whitespace-only changes.

src/services/account/format.ts

Whitespace-only changes.

src/services/account/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Service } from '@cloudgraph/sdk'
22
import BaseService from '../base'
3-
import getConnections from './connections'
43
import mutation from './mutation'
54

65
export default class AwsAccount extends BaseService implements Service {
76
format = ({ service }: { service: any }): any => service
87

9-
getConnections = getConnections.bind(this)
10-
118
getData
129

1310
mutation = mutation

src/services/base/enhancers.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
]

src/services/base/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { Logger } from '@cloudgraph/sdk'
22

3+
export interface Account {
4+
profile: string
5+
roleArn: string | undefined
6+
externalId: string | undefined
7+
accessKeyId?: string
8+
secretAccessKey?: string
9+
}
10+
11+
export interface rawDataInterface {
12+
className: string
13+
name: string
14+
accountId?: string
15+
data: any
16+
}
317
export default class BaseService {
418
constructor(config: any) {
519
this.logger = config.logger

0 commit comments

Comments
 (0)