Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smart-skip resources with CloudFormation conditional logic; fixes #12 #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install serverless-plugin-resource-tagging
```

### serverless.yml
```
```yml
provider:
name: XXX
stackTags:
Expand All @@ -34,3 +34,22 @@ AWS::ApiGateway::Stage
AWS::CloudFront::Distribution
AWS::Logs::LogGroup
```

Optionally, you can add other AWS resource types for tagging with
```yml
custom:
resourceTagging:
types:
- AWS::SNS::Topic
- AWS::IAM::Role
```

If you only want specific resources you define, add the `exclusive` flag
```yml
custom:
resourceTagging:
exclusive: true
types:
- AWS::SNS::Topic
- AWS::IAM::Role
```
56 changes: 50 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { get, uniq } = require('lodash');
const _ = require('underscore');

class ServerlessPlugin {
Expand Down Expand Up @@ -33,7 +34,40 @@ class ServerlessPlugin {
};
}

get log() {
const pluginId = 'resource-tagging-plugin';
return {
info: (msg) => {
this.serverless.cli.log(`[${pluginId}] INFO ${msg}`);
},
error: (msg) => {
this.serverless.cli.log(`[${pluginId}] ERROR ${msg}`);
},
warn: (msg) => {
this.serverless.cli.log(`[${pluginId}] WARN ${msg}`);
},
};
}

get pluginConfig() {
const {
types: userTypes = [],
exclusive = false,
} = get(this, 'serverless.service.custom.resourceTagging', {});

return {
types: exclusive
? userTypes
: uniq([
...this.supportedTypes,
...userTypes,
]),
};
}

_addTagsToResource() {
this.log.info(`config: ${JSON.stringify(this.pluginConfig, null, 2)}`);

var stackTags = [];
var self = this;
const template = this.serverless.service.provider.compiledCloudFormationTemplate;
Expand All @@ -56,18 +90,26 @@ class ServerlessPlugin {
});
}

const isValidProperties = props => {
return props && (
Object.keys(props).length > 1
||
!`${Object.keys(props)[0]}`.match(new RegExp('^!|Fn::', 'g'))
);
};

Object.keys(template.Resources).forEach(function (key) {
var resourceType = template.Resources[key]['Type']
if ((self.supportedTypes.indexOf(resourceType) !== -1) && Array.isArray(stackTags) && stackTags.length > 0) {
if (template.Resources[key]['Properties']) {
if ((self.pluginConfig.types.indexOf(resourceType) !== -1) && Array.isArray(stackTags) && stackTags.length > 0) {
if (isValidProperties(template.Resources[key]['Properties'])) {
var tags = template.Resources[key]['Properties']['Tags']
if (tags) {
template.Resources[key]['Properties']['Tags'] = tags.concat(stackTags.filter(obj => (self._getTagNames(tags).indexOf(obj["Key"]) === -1)))
} else {
template.Resources[key]['Properties']['Tags'] = stackTags
}
} else {
self.serverless.cli.log('Properties not available for ' + resourceType);
self.log.warn('Properties not available for ' + resourceType);
}
}

Expand All @@ -76,10 +118,12 @@ class ServerlessPlugin {
self.isApiGatewayStageAvailableInTemplate = true;
}
});
self.serverless.cli.log('Updated AWS resource tags..');
self.log.info('Updated AWS resource tags..');
}

_addAPIGatewayStageTags() {
this.log.info(`config: ${JSON.stringify(this.pluginConfig, null, 2)}`);

var self = this;
var stackName = this.provider.naming.getStackName();
if (!self.isApiGatewayStageAvailableInTemplate) {
Expand All @@ -93,10 +137,10 @@ class ServerlessPlugin {
};
promiseStack.push(self.provider.request('APIGateway', 'tagResource', apiStageParams))
});
return Promise.all(promiseStack).then(resp => self.serverless.cli.log('Updated APIGateway resource tags..'));
return Promise.all(promiseStack).then(resp => self.log.info('Updated APIGateway resource tags..'));
});
} else {
self.serverless.cli.log('APIGateway stage already available in serverless.yml. Tag update skipped.');
self.log.info('APIGateway stage already available in serverless.yml. Tag update skipped.');
return null;
}
}
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"homepage": "https://github.com/ilayanambi86/serverless-plugin-resource-tagging#readme",
"dependencies": {
"user": "0.0.0",
"underscore": "^1.12.1"
"lodash": "^4.17.21",
"underscore": "^1.12.1",
"user": "0.0.0"
}
}