-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(securityGroup): add security group service
- Loading branch information
Christopher Brandt
committed
May 9, 2022
1 parent
762dc79
commit 1f20850
Showing
9 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export default { | ||
securityGroup: 'securityGroup', | ||
subnet: 'subnet', | ||
vpc: 'vpc', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as tencentcloud from 'tencentcloud-sdk-nodejs' | ||
import { SecurityGroup } from 'tencentcloud-sdk-nodejs/tencentcloud/services/vpc/v20170312/vpc_models' | ||
import { ClientConfig } from 'tencentcloud-sdk-nodejs/tencentcloud/common/interface' | ||
import CloudGraph from '@cloudgraph/sdk' | ||
import groupBy from 'lodash/groupBy' | ||
import isEmpty from 'lodash/isEmpty' | ||
import loggerText from '../../properties/logger' | ||
import { TencentServiceInput } from '../../types' | ||
import { initTestEndpoint, generateTencentErrorLog } from '../../utils' | ||
|
||
const lt = { ...loggerText } | ||
const { logger } = CloudGraph | ||
export const serviceName = 'SecurityGroup' | ||
const apiEndpoint = initTestEndpoint(serviceName) | ||
|
||
export interface RawTencentSecurityGroup extends SecurityGroup { | ||
id: string | ||
region: string | ||
} | ||
|
||
export default async ({ | ||
regions, | ||
config, | ||
}: TencentServiceInput): Promise<{ | ||
[region: string]: RawTencentSecurityGroup[] | ||
}> => | ||
new Promise(async resolve => { | ||
const vpcList: RawTencentSecurityGroup[] = [] | ||
|
||
for (const region of regions.split(',')) { | ||
/** | ||
* Get all security groups | ||
*/ | ||
try { | ||
const VpcClient = tencentcloud.vpc.v20170312.Client | ||
const clientConfig: ClientConfig = { credential: config, region, profile: { httpProfile: { endpoint: apiEndpoint } } } | ||
const vpc = new VpcClient(clientConfig) | ||
const response = await vpc.DescribeSecurityGroups(null) | ||
|
||
if (response && !isEmpty(response.SecurityGroupSet)) { | ||
for (const instance of response.SecurityGroupSet) { | ||
vpcList.push({ | ||
id: instance.SecurityGroupId, | ||
...instance, | ||
region, | ||
}) | ||
} | ||
} | ||
|
||
} catch (error) { | ||
generateTencentErrorLog(serviceName, 'vpc:DescribeSecurityGroups', error) | ||
} | ||
} | ||
|
||
logger.debug(lt.foundResources(serviceName, vpcList.length)) | ||
resolve(groupBy(vpcList, 'region')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import cuid from 'cuid' | ||
import { TencentSecurityGroup } from '../../types/generated' | ||
import { RawTencentSecurityGroup } from './data' | ||
|
||
export default ({ | ||
service, | ||
account, | ||
region, | ||
}: { | ||
service: RawTencentSecurityGroup | ||
account: string | ||
region: string | ||
}): TencentSecurityGroup => { | ||
const { | ||
id, | ||
SecurityGroupName: name, | ||
SecurityGroupDesc: securityGroupDesc, | ||
ProjectId: projectId, | ||
IsDefault: isDefault, | ||
CreatedTime: createdTime, | ||
TagSet, | ||
UpdateTime: updateTime, | ||
} = service | ||
|
||
return { | ||
id, | ||
region, | ||
name, | ||
securityGroupDesc, | ||
projectId, | ||
isDefault, | ||
createdTime, | ||
tags: TagSet?.map(tagSet => ({ | ||
id: cuid(), | ||
key: tagSet.Key, | ||
value: tagSet.Value, | ||
})), | ||
updateTime, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Service} from '@cloudgraph/sdk' | ||
import BaseService from '../base' | ||
import format from './format' | ||
import getData, { serviceName } from './data' | ||
import { getMutation } from '../../utils' | ||
|
||
export default class TencentSecurityGroup extends BaseService implements Service { | ||
format = format.bind(this) | ||
|
||
getData = getData.bind(this) | ||
|
||
mutation = getMutation(serviceName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type tencentSecurityGroup implements tencentBaseService @key(fields: "id") { | ||
name: String @search(by: [hash, regexp]) | ||
securityGroupDesc: String @search(by: [hash, regexp]) | ||
projectId: String @search(by: [hash, regexp]) | ||
isDefault: Boolean @search | ||
createdTime: String @search(by: [hash, regexp]) | ||
tags: [tencentRawTag] | ||
updateTime: String @search(by: [hash, regexp]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters