-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.js
84 lines (77 loc) · 2.41 KB
/
util.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
'use strict'
const stringcase = require('case')
/**
* Filter an object to produce a new object with entries matching the supplied predicate
*
* @param {object} obj The object
* @param {function} pred A function accepting value, key arguments and returning a boolean
*/
function filterObject (obj, predicate) {
return Object.entries(obj)
.filter(([key, value]) => predicate(value, key))
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
}
function makeResourceName (service, givenName, alarm) {
const normalisedName = stringcase.pascal(givenName)
return `slicWatch${service}${alarm}Alarm${normalisedName}`
}
/**
* Given CloudFormation syntax for an ECS cluster, derive CloudFormation syntax for
* the cluster's name
*
* @param cluster CloudFormation syntax for an ECS cluster
* @returns CloudFormation syntax for the cluster's name
*/
function resolveEcsClusterNameAsCfn (cluster) {
if (typeof cluster === 'string') {
if (cluster.startsWith('arn:')) {
return cluster.split(':').pop().split('/').pop()
}
return cluster
}
if (cluster.GetAtt && cluster.GetAtt[1] === 'Arn') {
// AWS::ECS::Cluster returns the cluster name for 'Ref'
return { Ref: cluster.GetAtt[0] }
}
return cluster // Fallback to name
}
/**
* Given CloudFormation syntax for an ECS cluster, derive a string value or
* CloudFormation 'Fn::Sub' variable syntax for the cluster's name
*
* @param } cluster CloudFormation syntax for an ECS cluster
* @returns Literal string or Sub variable syntax
*/
function resolveEcsClusterNameForSub (cluster) {
if (typeof cluster === 'string') {
if (cluster.startsWith('arn:')) {
return cluster.split(':').pop().split('/').pop()
}
return cluster
}
// AWS::ECS::Cluster returns the cluster name for 'Ref'
// This can be used as a 'Fn::Sub' variable
if (cluster.GetAtt && cluster.GetAtt[1] === 'Arn') {
return '${' + cluster.GetAtt[0] + '}'
} else if (cluster.Ref) {
return '${' + cluster.Ref + '}'
} else if (cluster['Fn::Sub']) {
return cluster['Fn::Sub']
}
return cluster
}
/*
* Determine the presentation name for an alarm statistic
*
* @param {*} alarmConfig Alarm configuration
*/
function getStatisticName (alarmConfig) {
return alarmConfig.Statistic || alarmConfig.ExtendedStatistic
}
module.exports = {
filterObject,
makeResourceName,
resolveEcsClusterNameAsCfn,
resolveEcsClusterNameForSub,
getStatisticName
}