From 2179f9003baf95f645ed6f41d27ef2e9aaa4f77b Mon Sep 17 00:00:00 2001 From: dilantha111 Date: Tue, 20 Mar 2018 21:21:22 +0530 Subject: [PATCH 1/3] Fix #53 Add CDN Service for aws cloud front --- examples/cdn/aws-cloud-front.js | 19 +++++ lib/cdn/aws-cloud-fornt.js | 127 ++++++++++++++++++++++++++++++++ lib/core/aws/provider.js | 14 ++++ 3 files changed, 160 insertions(+) create mode 100644 examples/cdn/aws-cloud-front.js create mode 100644 lib/cdn/aws-cloud-fornt.js diff --git a/examples/cdn/aws-cloud-front.js b/examples/cdn/aws-cloud-front.js new file mode 100644 index 0000000..a4e8555 --- /dev/null +++ b/examples/cdn/aws-cloud-front.js @@ -0,0 +1,19 @@ +const nodecloud = require('../../lib'); + +const ncAWS = nodecloud.getProvider("AWS"); + +const options = { + apiVersion: "2016-11-15" +}; + +const cloudFront = ncAWS.cdn(options); + +const params = { + MaxItems: '100' +}; + +cloudFront.listDistributions(params).then(res => { + console.log(JSON.stringify(res)); +}).catch(err => { + console.error(err); +}); \ No newline at end of file diff --git a/lib/cdn/aws-cloud-fornt.js b/lib/cdn/aws-cloud-fornt.js new file mode 100644 index 0000000..be92b27 --- /dev/null +++ b/lib/cdn/aws-cloud-fornt.js @@ -0,0 +1,127 @@ +const helpers = require("../core/helpers"); + +const { checkParams } = helpers; + +class CloudFront { + /** + * CloudFront constructor + * @constructor + * @param {object} aws - AWS SDK + * @param {object} options - { apiVersion } + */ + constructor(aws, options) { + this._AWS = aws; + if (options) { + this._apiVersion = options.apiVersion; + this._cloudFront = new this._AWS.CloudFront({ + apiVersion: this._apiVersion + }); + } else { + this._cloudFront = new this._AWS.CloudFront(); + } + } + /** + * Create Cloud Distribution + * @createDistribution + * @param {object} params + */ + createDistribution(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.createDistribution(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + /** + * list Cloud Distributions + * @listDistributions + * @param {object} params + */ + listDistributions(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.listDistributions(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + /** + * delete Cloud Distribution + * @deleteDistribution + * @param {object} params + */ + deleteDistribution(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.deleteDistribution(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + /** + * update Cloud Distribution + * @updateDistribution + * @param {object} params + */ + updateDistribution(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.updateDistribution(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + /** + * create Cloud Invalidation + * @createInvalidation + * @param {object} params + */ + createInvalidation(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.createInvalidation(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + /** + * list Cloud Invalidation + * @listInvalidations + * @param {object} params + */ + listInvalidations(params) { + checkParams(params); + return new Promise((resolve, reject) => { + this._cloudFront.listInvalidations(params, (err, data) => { + if (err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } +} + +module.exports = CloudFront; \ No newline at end of file diff --git a/lib/core/aws/provider.js b/lib/core/aws/provider.js index 66a8e56..146cd2f 100644 --- a/lib/core/aws/provider.js +++ b/lib/core/aws/provider.js @@ -9,6 +9,7 @@ const DirectConnect = require('../../network/aws-directconnect'); const RDS = require('../../database/aws-rds'); const DynamoDB = require('../../database/aws-dynamodb'); const IAM = require('../../security/aws-iam'); +const CloudFront = require('../../cdn/aws-cloud-fornt'); class AWS { /** @@ -42,6 +43,7 @@ class AWS { rdbms: this.RDS, nosql: this.DynamoDB, iam: this.IAM, + cdn: this.CloudFront }; } /** @@ -158,6 +160,18 @@ class AWS { } return new IAM(this.getSDK()); } + /** + * CloudFront wrapper + * @CloudFront + * @param {object} options - { apiVersion } + */ + CloudFront(options) { + if (options.apiVersion) { + this._apiVersion = options.apiVersion; + return new CloudFront(this.getSDK(), options); + } + return new CloudFront(this.getSDK()); + } } module.exports = AWS; From 59f63e4baf77e5d2bbb681a3d7fa381fc7f09551 Mon Sep 17 00:00:00 2001 From: dilantha111 Date: Tue, 20 Mar 2018 21:35:33 +0530 Subject: [PATCH 2/3] adding missing process.env.ncconf to aws cdn example --- examples/cdn/aws-cloud-front.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cdn/aws-cloud-front.js b/examples/cdn/aws-cloud-front.js index a4e8555..40e01f0 100644 --- a/examples/cdn/aws-cloud-front.js +++ b/examples/cdn/aws-cloud-front.js @@ -1,6 +1,6 @@ const nodecloud = require('../../lib'); -const ncAWS = nodecloud.getProvider("AWS"); +const ncAWS = nodecloud.getProvider("AWS", process.env.ncconf); const options = { apiVersion: "2016-11-15" From afb244de0517fc4aabf405e37b6d3bdd6e69bd9c Mon Sep 17 00:00:00 2001 From: dilantha111 Date: Wed, 21 Mar 2018 23:18:32 +0530 Subject: [PATCH 3/3] Fixinig linting issues --- lib/cdn/aws-cloud-fornt.js | 6 +++--- lib/core/aws/provider.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cdn/aws-cloud-fornt.js b/lib/cdn/aws-cloud-fornt.js index be92b27..22e3760 100644 --- a/lib/cdn/aws-cloud-fornt.js +++ b/lib/cdn/aws-cloud-fornt.js @@ -1,4 +1,4 @@ -const helpers = require("../core/helpers"); +const helpers = require('../core/helpers'); const { checkParams } = helpers; @@ -14,7 +14,7 @@ class CloudFront { if (options) { this._apiVersion = options.apiVersion; this._cloudFront = new this._AWS.CloudFront({ - apiVersion: this._apiVersion + apiVersion: this._apiVersion, }); } else { this._cloudFront = new this._AWS.CloudFront(); @@ -124,4 +124,4 @@ class CloudFront { } } -module.exports = CloudFront; \ No newline at end of file +module.exports = CloudFront; diff --git a/lib/core/aws/provider.js b/lib/core/aws/provider.js index 146cd2f..29c3f37 100644 --- a/lib/core/aws/provider.js +++ b/lib/core/aws/provider.js @@ -43,7 +43,7 @@ class AWS { rdbms: this.RDS, nosql: this.DynamoDB, iam: this.IAM, - cdn: this.CloudFront + cdn: this.CloudFront, }; } /**