FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ADD: use existing DynamoTable and S3Storage · jaystack/functionly@e1dee85 · GitHub

Commit e1dee85

Browse files
committed
ADD: use existing DynamoTable and S3Storage
1 parent c3abe10 commit e1dee85

7 files changed

Lines changed: 51 additions & 28 deletions

File tree

‎src/annotations/classes/dynamoTable.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export const __dynamoDBDefaults = {
2525
export const dynamoTable = (tableConfig?: {
2626
tableName?: string,
2727
environmentKey?: string,
28-
nativeConfig?: any
28+
nativeConfig?: any,
29+
exists?: boolean
2930
}) => (target: Function) => {
3031
let tableDefinitions = getMetadata(CLASS_DYNAMOTABLECONFIGURATIONKEY, target) || [];
3132

‎src/annotations/classes/s3Storage.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const s3Storage = (s3Config?: {
1212
eventSourceConfiguration?: {
1313
Event?: any,
1414
Filter?: any
15-
}
15+
},
16+
exists?: boolean
1617
}) => (target: Function) => {
1718
let s3Definitions = getMetadata(CLASS_S3CONFIGURATIONKEY, target) || [];
1819

‎src/classes/api/aws/dynamoTable.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ export class DynamoTable extends Api {
127127
const tableName = ({ TableName: tableConfig.tableName, ...tableConfig.nativeConfig }).TableName
128128

129129
const calcTableName = tableConfig.environmentKey && process.env[tableConfig.environmentKey] ? process.env[tableConfig.environmentKey] : ''
130+
const suffix = tableConfig.exists ? '' : `-${process.env.FUNCTIONAL_STAGE}`
130131
const initParams = {
131-
TableName: (calcTableName || tableName) + `-${process.env.FUNCTIONAL_STAGE}`
132+
TableName: (calcTableName || tableName) + suffix
132133
}
133134

134135
return { ...initParams, ...params }

‎src/classes/api/aws/s3Storage.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ export class S3Storage extends Api {
8888
const bucketName = bucketConfig.bucketName
8989

9090
const calcBucketName = bucketConfig.environmentKey && process.env[bucketConfig.environmentKey] ? process.env[bucketConfig.environmentKey] : ''
91+
const suffix = bucketConfig.exists ? '' : `-${process.env.FUNCTIONAL_STAGE}`
9192
const initParams = {
92-
Bucket: (calcBucketName || bucketName) + `-${process.env.FUNCTIONAL_STAGE}`
93+
Bucket: (calcBucketName || bucketName) + suffix
9394
}
9495

9596
return { ...initParams, ...params }

‎src/cli/providers/cloudFormation/context/dynamoTable.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ export const tableResources = ExecuteStep.register('DynamoDB-Tables', async (con
2525
export const tableResource = async (context) => {
2626
const { tableConfig } = context
2727

28+
tableConfig.AWSTableName = tableConfig.tableName
29+
30+
if (tableConfig.exists) return
31+
32+
tableConfig.AWSTableName = `${tableConfig.tableName}-${context.stage}`
33+
2834
const properties = {
2935
...__dynamoDBDefaults,
30-
TableName: `${tableConfig.tableName}-${context.stage}`,
36+
TableName: tableConfig.AWSTableName,
3137
...tableConfig.nativeConfig
3238
};
3339

@@ -55,7 +61,6 @@ export const tableResource = async (context) => {
5561
sourceStackName: DYNAMODB_TABLE_STACK
5662
})
5763

58-
tableConfig.tableName = properties.TableName
5964
tableConfig.resourceName = resourceName
6065

6166
}
@@ -75,6 +80,8 @@ export const tableSubscribers = ExecuteStep.register('DynamoDB-Table-Subscriptio
7580

7681
export const tableSubscriber = async (context) => {
7782
const { tableConfig, subscriber } = context
83+
84+
if (tableConfig.exists) return
7885

7986
const properties = {
8087
"BatchSize": 1,
@@ -115,6 +122,8 @@ export const tableSubscriber = async (context) => {
115122

116123
export const dynamoStreamingPolicy = async (context) => {
117124
const { tableConfig, serviceDefinition } = context
125+
126+
if (tableConfig.exists) return
118127

119128
let policy = serviceDefinition.roleResource.Properties.Policies.find(p => p.PolicyDocument.Statement[0].Action.includes('dynamodb:GetRecords'))
120129
if (!policy) {
@@ -149,6 +158,6 @@ export const dynamoStreamingPolicy = async (context) => {
149158
}
150159

151160
policy.PolicyDocument.Statement[0].Resource.push({
152-
"Fn::Sub": "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/" + tableConfig.tableName + "/stream/*"
161+
"Fn::Sub": "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/" + tableConfig.AWSTableName + "/stream/*"
153162
})
154163
}

‎src/cli/providers/cloudFormation/context/resources.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export const dynamoPolicy = async (context) => {
227227
],
228228
"Resource": usedTableConfigs.map(t => {
229229
return {
230-
"Fn::Sub": "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/" + t.tableName
230+
"Fn::Sub": "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/" + t.AWSTableName
231231
}
232232
})
233233
}]

‎src/cli/providers/cloudFormation/context/s3Storage.ts‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,41 @@ export const s3Storages = ExecuteStep.register('S3-Storages', async (context) =>
5656
})
5757

5858
for (const s3Config of configs) {
59-
const s3BucketDefinition = await executor({
59+
await executor({
6060
context: { ...context, s3Config },
6161
name: `S3-Storage-${s3Config.bucketName}`,
6262
method: s3Storage
6363
})
64-
65-
await executor({
66-
context: { ...context, s3Config, s3BucketDefinition },
67-
name: `S3-Storage-Subscription-${s3Config.bucketName}`,
68-
method: s3StorageSubscriptions
69-
})
7064
}
7165
})
7266

7367
export const s3Storage = async (context) => {
7468
const { s3Config } = context
7569

76-
const s3Properties = {
77-
"BucketName": `${s3Config.bucketName}-${context.stage}`
78-
}
70+
s3Config.AWSBucketName = s3Config.bucketName
7971

80-
const s3Bucket = {
81-
"Type": "AWS::S3::Bucket",
82-
"Properties": s3Properties
83-
}
72+
if (!s3Config.exists) {
73+
s3Config.AWSBucketName = `${s3Config.bucketName}-${context.stage}`
74+
75+
const s3Properties = {
76+
"BucketName": s3Config.AWSBucketName
77+
}
8478

85-
const resourceName = `S3${s3Config.bucketName}`
86-
const bucketResourceName = setResource(context, resourceName, s3Bucket, S3_STORAGE_STACK)
87-
s3Config.resourceName = bucketResourceName
79+
const s3Bucket = {
80+
"Type": "AWS::S3::Bucket",
81+
"Properties": s3Properties
82+
}
83+
84+
const resourceName = `S3${s3Config.bucketName}`
85+
const bucketResourceName = setResource(context, resourceName, s3Bucket, S3_STORAGE_STACK)
86+
s3Config.resourceName = bucketResourceName
8887

88+
await executor({
89+
context: { ...context, s3BucketDefinition: s3Bucket },
90+
name: `S3-Storage-Subscription-${s3Config.bucketName}`,
91+
method: s3StorageSubscriptions
92+
})
93+
}
8994

9095
for (const { serviceDefinition, serviceConfig } of s3Config.services) {
9196
if (!serviceConfig.injected) continue
@@ -96,8 +101,6 @@ export const s3Storage = async (context) => {
96101
method: s3StoragePolicy
97102
})
98103
}
99-
100-
return s3Bucket
101104
}
102105

103106

@@ -142,7 +145,7 @@ export const s3StoragePolicy = async (context) => {
142145
"",
143146
[
144147
"arn:aws:s3:::",
145-
`${s3Config.bucketName}-${context.stage}`,
148+
`${s3Config.AWSBucketName}`,
146149
"/*"
147150
]
148151
]
@@ -152,6 +155,8 @@ export const s3StoragePolicy = async (context) => {
152155
export const s3StorageSubscriptions = async (context) => {
153156
const { s3Config } = context
154157

158+
if (s3Config.exists) return
159+
155160
for (const { serviceDefinition, serviceConfig } of s3Config.services) {
156161
if (!serviceConfig.eventSource) continue
157162

@@ -171,6 +176,8 @@ export const s3StorageSubscriptions = async (context) => {
171176

172177
export const s3BucketSubscription = async (context) => {
173178
const { serviceDefinition, serviceConfig, s3Config, s3BucketDefinition } = context
179+
180+
if (s3Config.exists) return
174181

175182
await setStackParameter({
176183
...context,
@@ -204,6 +211,9 @@ export const s3BucketSubscription = async (context) => {
204211

205212
export const s3Permissions = (context) => {
206213
const { serviceDefinition, serviceConfig, s3Config, s3BucketDefinition } = context
214+
215+
if (s3Config.exists) return
216+
207217
const properties = {
208218
"FunctionName": {
209219
"Ref": serviceDefinition.resourceName

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL