Skip to content

Commit a1d550b

Browse files
author
Marco Franceschi
committed
feat: Added ses service configurations
1 parent 214287f commit a1d550b

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

src/services/ses/data.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import SES, {
2+
ListConfigurationSetsResponse,
3+
ConfigurationSet,
4+
ListTemplatesResponse,
5+
TemplateMetadata,
6+
} from 'aws-sdk/clients/ses'
7+
import { AWSError } from 'aws-sdk/lib/error'
8+
import { Config } from 'aws-sdk/lib/config'
9+
10+
import CloudGraph from '@cloudgraph/sdk'
11+
import groupBy from 'lodash/groupBy'
12+
13+
import awsLoggerText from '../../properties/logger'
14+
import { initTestEndpoint } from '../../utils'
15+
import AwsErrorLog from '../../utils/errorLog'
16+
17+
const lt = { ...awsLoggerText }
18+
const { logger } = CloudGraph
19+
const serviceName = 'SES '
20+
const errorLog = new AwsErrorLog(serviceName)
21+
const endpoint = initTestEndpoint(serviceName)
22+
23+
/**
24+
* SES
25+
*/
26+
export interface RawAwsSes {
27+
ConfigurationSets: ConfigurationSet[]
28+
EmailTemplates: TemplateMetadata[]
29+
region: string
30+
}
31+
32+
33+
const getEmailTemplates = async (ses: SES): Promise<TemplateMetadata[]> =>
34+
new Promise(resolve => {
35+
try {
36+
ses.listTemplates(
37+
(err: AWSError, data: ListTemplatesResponse) => {
38+
if (err) {
39+
errorLog.generateAwsErrorLog({
40+
functionName: 'ses:listTemplates',
41+
err,
42+
})
43+
return resolve([])
44+
}
45+
const { TemplatesMetadata = [] } = data || {}
46+
resolve(TemplatesMetadata)
47+
}
48+
)
49+
} catch (error) {
50+
resolve([])
51+
}
52+
})
53+
54+
const getConfigurationSets = async (ses: SES): Promise<ConfigurationSet[]> =>
55+
new Promise(resolve => {
56+
try {
57+
ses.listConfigurationSets(
58+
(err: AWSError, data: ListConfigurationSetsResponse) => {
59+
if (err) {
60+
errorLog.generateAwsErrorLog({
61+
functionName: 'ses:listConfigurationSets',
62+
err,
63+
})
64+
return resolve([])
65+
}
66+
const { ConfigurationSets = [] } = data || {}
67+
resolve(ConfigurationSets)
68+
}
69+
)
70+
} catch (error) {
71+
resolve([])
72+
}
73+
})
74+
75+
export default async ({
76+
regions,
77+
config,
78+
}: {
79+
regions: string
80+
config: Config
81+
}): Promise<{ [property: string]: RawAwsSes[] }> =>
82+
new Promise(async resolve => {
83+
const sesData: RawAwsSes[] = []
84+
const regionPromises = []
85+
86+
regions.split(',').map(region => {
87+
const regionPromise = new Promise<void>(async resolveRegion => {
88+
const ses = new SES({ ...config, region, endpoint })
89+
90+
const configurationSets = await getConfigurationSets(ses)
91+
const emailTemplates = await getEmailTemplates(ses)
92+
93+
sesData.push({
94+
ConfigurationSets: configurationSets,
95+
EmailTemplates: emailTemplates,
96+
region
97+
})
98+
resolveRegion()
99+
100+
101+
})
102+
regionPromises.push(regionPromise)
103+
})
104+
105+
106+
await Promise.all(regionPromises)
107+
errorLog.reset()
108+
109+
resolve(groupBy(sesData, 'region'))
110+
})

src/services/ses/format.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { RawAwsSes } from './data'
2+
import { AwsSes } from '../../types/generated'
3+
import { generateUniqueId } from '@cloudgraph/sdk'
4+
5+
/**
6+
* SES
7+
*/
8+
9+
export default ({
10+
service,
11+
account,
12+
region,
13+
}: {
14+
service: RawAwsSes
15+
account: string
16+
region: string
17+
}): AwsSes => {
18+
const {
19+
ConfigurationSets = [],
20+
EmailTemplates = [],
21+
} = service
22+
23+
const configurationSets = ConfigurationSets.map(cs => cs.Name)
24+
const emailTemplates = EmailTemplates.map(e => e.Name)
25+
26+
return {
27+
id: generateUniqueId({
28+
...service,
29+
account,
30+
region
31+
}),
32+
accountId: account,
33+
region,
34+
configurationSets,
35+
emailTemplates,
36+
}
37+
}

src/services/ses/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 from './data'
5+
import mutation from './mutation'
6+
7+
export default class SES extends BaseService implements Service {
8+
format = format.bind(this)
9+
10+
getData = getData.bind(this)
11+
12+
mutation = mutation
13+
}

src/services/ses/mutation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `mutation($input: [AddawsSesInput!]!) {
2+
addawsSes(input: $input, upsert: true) {
3+
numUids
4+
}
5+
}`

src/services/ses/schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type awsSes implements awsOptionalService @key(fields: "id") {
2+
configurationSets: [String] @search(by: [hash])
3+
emailTemplates: [String] @search(by: [hash])
4+
}

0 commit comments

Comments
 (0)