Skip to content

Commit d628f10

Browse files
author
Christopher Brandt
authored
Merge pull request #6 from cloudgraphdev/feature/CG-1130
feat(networkAcl): add networkAcl service
2 parents 10311ff + 3665d1e commit d628f10

File tree

14 files changed

+234
-2
lines changed

14 files changed

+234
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ CloudGraph Tencent Provider will ask you what regions you would like to crawl an
6262
| securityGroupRule | |
6363
| ccn | ccnAttachment |
6464
| ccnAttachment | ccn |
65-
| subnet | vpc, routeTable |
66-
| vpc | subnet, vpnGateway, routeTable |
65+
| networkAcl | subnet, vpc |
66+
| subnet | networkAcl, vpc, routeTable |
67+
| vpc | networkAcl, subnet, vpnGateway, routeTable |
6768
| vpnGateway | vpc, vpnGatewayRoute |
6869
| vpnGatewayRoute | vpnGateway |
70+
| securityGroup | |

src/enums/schemasMap.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default {
1010
[services.securityGroupRule]: 'tencentSecurityGroupRule',
1111
[services.ccn]: 'tencentCcn',
1212
[services.ccnAttachment]: 'tencentCcnAttachment',
13+
[services.networkAcl]: 'tencentNetworkAcl',
14+
[services.securityGroup]: 'tencentSecurityGroup',
15+
[services.securityGroupRule]: 'tencentSecurityGroupRule',
1316
[services.subnet]: 'tencentSubnet',
1417
[services.vpc]: 'tencentVpc',
1518
[services.vpnGateway]: 'tencentVpnGateway',

src/enums/serviceMap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import services from './services'
2+
import TencentNetworkAcl from '../services/networkAcl'
23
import TencentSecurityGroup from '../services/securityGroup'
34
import TencentSecurityGroupRule from '../services/securityGroupRule'
45
import TencentCcn from '../services/ccn'
@@ -22,6 +23,9 @@ export default {
2223
[services.securityGroupRule]: TencentSecurityGroupRule,
2324
[services.ccn]: TencentCcn,
2425
[services.ccnAttachment]: TencentCcnAttachment,
26+
[services.networkAcl]: TencentNetworkAcl,
27+
[services.securityGroup]: TencentSecurityGroup,
28+
[services.securityGroupRule]: TencentSecurityGroupRule,
2529
[services.subnet]: TencentSubnet,
2630
[services.vpc]: TencentVpc,
2731
[services.vpnGateway]: TencentVpnGateway,

src/enums/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default {
22
customerGateway: 'customerGateway',
33
routeTable: 'routeTable',
4+
networkAcl: 'networkAcl',
45
securityGroup: 'securityGroup',
56
securityGroupRule: 'securityGroupRule',
67
ccn: 'ccn',

src/services/networkAcl/data.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as tencentcloud from 'tencentcloud-sdk-nodejs'
2+
import { NetworkAcl } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models'
3+
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface'
4+
import CloudGraph from '@cloudgraph/sdk'
5+
import groupBy from 'lodash/groupBy'
6+
import isEmpty from 'lodash/isEmpty'
7+
import loggerText from '../../properties/logger'
8+
import { TencentServiceInput } from '../../types'
9+
import { initTestEndpoint, generateTencentErrorLog } from '../../utils'
10+
11+
const lt = { ...loggerText }
12+
const { logger } = CloudGraph
13+
export const serviceName = 'NetworkAcl'
14+
const apiEndpoint = initTestEndpoint(serviceName)
15+
16+
export interface RawTencentNetworkAcl extends NetworkAcl {
17+
id: string
18+
region: string
19+
subnets: string[]
20+
}
21+
22+
export default async ({
23+
regions,
24+
config,
25+
}: TencentServiceInput): Promise<{
26+
[region: string]: RawTencentNetworkAcl[]
27+
}> =>
28+
new Promise(async resolve => {
29+
const naclList: RawTencentNetworkAcl[] = []
30+
31+
for (const region of regions.split(',')) {
32+
/**
33+
* Get all NetworkACLs
34+
*/
35+
try {
36+
const VpcClient = tencentcloud.vpc.v20170312.Client
37+
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } }
38+
const vpc = new VpcClient(clientConfig)
39+
const response = await vpc.DescribeNetworkAcls(null)
40+
41+
if (response && !isEmpty(response.NetworkAclSet)) {
42+
for (const instance of response.NetworkAclSet) {
43+
naclList.push({
44+
id: instance.NetworkAclId,
45+
...instance,
46+
subnets: instance?.SubnetSet?.map(subnet => subnet.SubnetId),
47+
region,
48+
})
49+
}
50+
}
51+
} catch (error) {
52+
generateTencentErrorLog(serviceName, 'vpc:DescribeNetworkAcls', error)
53+
}
54+
}
55+
56+
logger.debug(lt.foundResources(serviceName, naclList.length))
57+
resolve(groupBy(naclList, 'region'))
58+
})

src/services/networkAcl/format.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cuid from 'cuid'
2+
import { TencentNetworkAcl } from '../../types/generated'
3+
import { RawTencentNetworkAcl } from './data'
4+
5+
export default ({
6+
service,
7+
account,
8+
region,
9+
}: {
10+
service: RawTencentNetworkAcl
11+
account: string
12+
region: string
13+
}): TencentNetworkAcl => {
14+
const {
15+
id,
16+
NetworkAclName: name,
17+
VpcId: vpcId,
18+
CreatedTime: createdTime,
19+
IngressEntries,
20+
EgressEntries,
21+
} = service
22+
23+
return {
24+
id,
25+
region,
26+
name,
27+
vpcId,
28+
createdTime,
29+
ingressEntries: IngressEntries?.map(naclEntry => ({
30+
id: cuid(),
31+
...naclEntry,
32+
})),
33+
egressEntries: EgressEntries?.map(naclEntry => ({
34+
id: cuid(),
35+
...naclEntry,
36+
})),
37+
}
38+
}

src/services/networkAcl/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 getData, { serviceName } from './data'
5+
import { getMutation } from '../../utils'
6+
7+
export default class TencentNetworkAcl extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = getMutation(serviceName)
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type tencentNetworkAcl implements tencentBaseService @key(fields: "id") {
2+
vpcId: String @search(by: [hash, regexp])
3+
name: String @search(by: [hash, regexp])
4+
createdTime: String @search(by: [hash, regexp])
5+
ingressEntries: [tencentNetworkAclEntry]
6+
egressEntries: [tencentNetworkAclEntry]
7+
subnets: [tencentSubnet] @hasInverse(field: networkAcl)
8+
vpcInstance: [tencentVpc] @hasInverse(field: networkAcl)
9+
}
10+
11+
type tencentNetworkAclEntry
12+
@generate(
13+
query: { get: false, query: true, aggregate: false }
14+
mutation: { add: false, delete: false }
15+
subscription: false
16+
)
17+
@key(fields: "id") {
18+
id: String! @id @search(by: [hash, regexp])
19+
modifyTime: String @search(by: [hash, regexp])
20+
protocol: String @search(by: [hash, regexp])
21+
port: String @search(by: [hash, regexp])
22+
cidrBlock: String @search(by: [hash, regexp])
23+
ipv6CidrBlock: String @search(by: [hash, regexp])
24+
action: String @search(by: [hash, regexp])
25+
description: String @search(by: [hash, regexp])
26+
}

src/services/subnet/connections.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ServiceConnection } from '@cloudgraph/sdk'
2+
import { rawDataInterface } from '../../types'
3+
import { RawTencentSubnet } from './data'
4+
import { GLOBAL_REGION } from '../../config/constants'
5+
import services from '../../enums/services'
6+
import aliases from '../../enums/serviceAliases'
7+
8+
export default ({
9+
account,
10+
service,
11+
data,
12+
region,
13+
}: {
14+
account: string
15+
service: RawTencentSubnet
16+
data: { name: string; data: { [property: string]: any[] } }[]
17+
region: string
18+
}): {
19+
[property: string]: ServiceConnection[]
20+
} => {
21+
const { id } = service
22+
const connections: ServiceConnection[] = []
23+
const connectTo = Object.values(services).filter(service => service !== services.subnet)
24+
25+
for (const serviceName of connectTo) {
26+
const instances: {
27+
name: string
28+
data: { [property: string]: any[] }
29+
} = data.find(({ name }) => name === serviceName)
30+
31+
const regions = [region, GLOBAL_REGION]
32+
for (const region of regions) {
33+
if (instances?.data?.[region]) {
34+
const filtered = instances.data[region].filter(
35+
({ subnets }: rawDataInterface) => subnets
36+
)
37+
38+
for (const { id, subnet } of filtered) {
39+
for (const name of subnet) {
40+
if (name === service.SubnetId) {
41+
connections.push({
42+
id,
43+
resourceType: serviceName,
44+
relation: 'child',
45+
field: aliases[serviceName] ? aliases[serviceName] : serviceName,
46+
})
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
const result = {
55+
[id]: connections,
56+
}
57+
return result
58+
}

src/services/subnet/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import {Service} from '@cloudgraph/sdk'
22
import BaseService from '../base'
33
import format from './format'
44
import getData, { serviceName } from './data'
5+
import getConnections from './connections'
56
import { getMutation } from '../../utils'
67

78
export default class TencentSubnet extends BaseService implements Service {
89
format = format.bind(this)
910

1011
getData = getData.bind(this)
1112

13+
getConnections = getConnections.bind(this)
14+
1215
mutation = getMutation(serviceName)
1316
}

0 commit comments

Comments
 (0)