/
mergeCustomProviderResources.js
88 lines (73 loc) · 3.07 KB
/
mergeCustomProviderResources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
module.exports = {
mergeCustomProviderResources() {
if (this.serverless.service.resources && !this.serverless.service.resources.Resources) {
this.serverless.service.resources.Resources = {};
}
if (this.serverless.service.resources && !this.serverless.service.resources.Outputs) {
this.serverless.service.resources.Outputs = {};
}
const extensions = this.serverless.service.resources
? this.serverless.service.resources.extensions
: null;
if (extensions) {
delete this.serverless.service.resources.extensions;
}
_.merge(
this.serverless.service.provider.compiledCloudFormationTemplate,
this.serverless.service.resources
);
if (extensions) {
const template = this.serverless.service.provider.compiledCloudFormationTemplate;
for (const [resourceName, resourceDefinition] of _.entries(extensions)) {
for (const [extensionAttributeName, value] of _.entries(resourceDefinition)) {
if (!template.Resources[resourceName]) {
template.Resources[resourceName] = {};
this.serverless._logDeprecation(
'RESOURCES_EXTENSIONS_REFERENCE_TO_NONEXISTENT_RESOURCE',
'Starting with next major version, extensions to nonexistent resources will throw an error instead of passing silently.'
);
}
switch (extensionAttributeName) {
case 'Condition':
case 'CreationPolicy':
case 'DeletionPolicy':
case 'UpdatePolicy':
case 'UpdateReplacePolicy':
template.Resources[resourceName][extensionAttributeName] = value;
break;
case 'Properties':
if (!template.Resources[resourceName].Properties) {
template.Resources[resourceName].Properties = {};
}
Object.assign(template.Resources[resourceName].Properties, value);
break;
case 'DependsOn':
if (!template.Resources[resourceName].DependsOn) {
template.Resources[resourceName].DependsOn = [];
}
template.Resources[resourceName].DependsOn.push(...value);
break;
case 'Metadata':
if (!template.Resources[resourceName].Metadata) {
template.Resources[resourceName].Metadata = {};
}
Object.assign(template.Resources[resourceName].Metadata, value);
break;
// default includes any future attributes we don't know about yet.
default:
throw new Error(
`${resourceName}: Sorry, extending the ${extensionAttributeName} resource ` +
'attribute at this point is not supported. Feel free to propose support ' +
'for it in the Framework issue tracker: ' +
'https://github.com/serverless/serverless/issues'
);
}
}
}
}
return BbPromise.resolve();
},
};