From 9e41fb5095583bedd5a2090745ce719960d95c27 Mon Sep 17 00:00:00 2001 From: philippschulte Date: Fri, 22 Dec 2017 22:59:08 -0800 Subject: [PATCH] feat(readBackends): Add readBackends class method Add readBackends class method to list all backends for a particular service and version --- README.md | 27 ++++++++- src/index.js | 9 +++ test/dataCenters.test.js | 2 +- test/domainCheckAll.test.js | 2 +- test/edgeCheck.test.js | 2 +- test/publicIpList.test.js | 2 +- test/purgeAll.test.js | 2 +- test/purgeIndividual.test.js | 2 +- test/purgeKey.test.js | 2 +- test/purgeKeys.test.js | 2 +- test/readBackends.test.js | 80 ++++++++++++++++++++++++++ test/readDomains.test.js | 2 +- test/readServices.test.js | 11 +++- test/readVersions.test.js | 16 +++++- test/response/readBackends.response.js | 41 +++++++++++++ test/softPurgeIndividual.test.js | 2 +- test/softPurgeKey.test.js | 2 +- 17 files changed, 189 insertions(+), 17 deletions(-) create mode 100644 test/readBackends.test.js create mode 100644 test/response/readBackends.response.js diff --git a/README.md b/README.md index 9a928206..61fdecee 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ ## Problem -There are already some promise based clients on [NPM](https://www.npmjs.com/) for the [Fastly](https://docs.fastly.com/api/) API but either way there are not well documented, tested, or don't provide the functionality I needed. The callback based [fastly](https://www.npmjs.com/package/fastly) package is still the most used client on [NPM](https://www.npmjs.com/). However, I needed a client which allows me to perform request sequentially and parallelly without ending up in an untamable [callback hell](http://callbackhell.com/)! +The callback based [fastly](https://www.npmjs.com/package/fastly) package is still the most used client on [NPM](https://www.npmjs.com/). However, I needed a client which allows me to perform request sequentially and parallelly without ending up in an untamable [callback hell](http://callbackhell.com/)! ## Solution -The `fastly-promises` package uses the promise based HTTP client [Axios](https://www.npmjs.com/package/axios) to perform requests to the [Fastly](https://docs.fastly.com/api/) API. [Axios](https://www.npmjs.com/package/axios) supports the native JavaScript [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API and automatically transforms the data into JSON. Each `fastly-promises` API method returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which represents either the completion or failure of the request. This allows us to implement modern JavaScript asynchronous programming patterns like `async/await` for a clean and simple control flow! +The `fastly-promises` package uses the promise based HTTP client [Axios](https://www.npmjs.com/package/axios) to perform requests to the [Fastly](https://docs.fastly.com/api/) API. [Axios](https://www.npmjs.com/package/axios) supports the native JavaScript [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API and automatically transforms the data into JSON. Each `fastly-promises` API method returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which represents either the completion or failure of the request. ## Table of Contents @@ -147,6 +147,7 @@ async function handler() { - [.readServices()](#readServices) - [.readVersions()](#readVersions) - [.readDomains(version)](#readDomains) + - [.readBackends(version)](#readBackends) - [.domainCheckAll(version)](#domainCheckAll) @@ -466,6 +467,28 @@ instance.readDomains('182') **Param**: version {string} The current version of a service. **Return**: schema {promise} The response object representing the completion or failure. + + +### [.readBackends(version)](https://docs.fastly.com/api/config#backend_fb0e875c9a7669f071cbf89ca32c7f69) + +*List all backends for a particular service and version.* + +**Example**: + +```javascript +instance.readBackends('12') + .then(res => { + console.log(res.data); + }) + .catch(err => { + console.log(err.message); + }); +``` + +**Kind**: method +**Param**: version {string} The current version of a service. +**Return**: schema {promise} The response object representing the completion or failure. + ### [.domainCheckAll(version)](https://docs.fastly.com/api/config#domain_e33a599694c3316f00b6b8d53a2db7d9) diff --git a/src/index.js b/src/index.js index bc637ef8..70365811 100644 --- a/src/index.js +++ b/src/index.js @@ -121,6 +121,15 @@ class Fastly { return this.request.get(`/service/${this.service_id}/version/${version}/domain`); } + /** + * List all backends for a particular service and version. + * @param version {String} The current version of a service. + * @return {Promise} The response object representing the completion or failure. + */ + readBackends(version = '') { + return this.request.get(`/service/${this.service_id}/version/${version}/backend`); + } + /** * Checks the status of all domains for a particular service and version. * @param version {String} The current version of a service. diff --git a/test/dataCenters.test.js b/test/dataCenters.test.js index e3d06dc9..51d6a5ac 100644 --- a/test/dataCenters.test.js +++ b/test/dataCenters.test.js @@ -36,7 +36,7 @@ describe('#dataCenters', () => { }); }); - it('response body items should have code, name, group, coordinates, and shield properties', () => { + it('response body should contain all properties', () => { res.data.forEach(item => { expect(item).toIncludeKeys(['code', 'name', 'group', 'coordinates', 'shield']); }); diff --git a/test/domainCheckAll.test.js b/test/domainCheckAll.test.js index 29c1a07b..7a7c4cfa 100644 --- a/test/domainCheckAll.test.js +++ b/test/domainCheckAll.test.js @@ -36,7 +36,7 @@ describe('#domainCheckAll', () => { }); }); - it('response body items should have three items', () => { + it('response body should have three items', () => { res.data.forEach(item => { expect(item.length).toEqual(3); }); diff --git a/test/edgeCheck.test.js b/test/edgeCheck.test.js index f22f5581..396c7536 100644 --- a/test/edgeCheck.test.js +++ b/test/edgeCheck.test.js @@ -36,7 +36,7 @@ describe('#edgeCheck', () => { }); }); - it('response body items should have hash, request, response, response_time, and server properties', () => { + it('response body should contain all properties', () => { res.data.forEach(item => { expect(item).toIncludeKeys(['hash', 'request', 'response', 'response_time', 'server']); }); diff --git a/test/publicIpList.test.js b/test/publicIpList.test.js index bb680cd8..cc639655 100644 --- a/test/publicIpList.test.js +++ b/test/publicIpList.test.js @@ -30,7 +30,7 @@ describe('#publicIpList', () => { expect(res.data).toBeA('object'); }); - it('response body should have addresses property', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['addresses']); }); }); diff --git a/test/purgeAll.test.js b/test/purgeAll.test.js index 97722ef4..fe428058 100644 --- a/test/purgeAll.test.js +++ b/test/purgeAll.test.js @@ -30,7 +30,7 @@ describe('#purgeAll', () => { expect(res.data).toBeA('object'); }); - it('response body should have status property', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['status']); }); }); diff --git a/test/purgeIndividual.test.js b/test/purgeIndividual.test.js index 3f83ef26..f1144ef6 100644 --- a/test/purgeIndividual.test.js +++ b/test/purgeIndividual.test.js @@ -30,7 +30,7 @@ describe('#purgeIndividual', () => { expect(res.data).toBeA('object'); }); - it('response body should have status and id properties', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['status', 'id']); }); }); diff --git a/test/purgeKey.test.js b/test/purgeKey.test.js index b2ffd025..ea037ace 100644 --- a/test/purgeKey.test.js +++ b/test/purgeKey.test.js @@ -30,7 +30,7 @@ describe('#purgeKey', () => { expect(res.data).toBeA('object'); }); - it('response body should have status and id properties', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['status', 'id']); }); }); diff --git a/test/purgeKeys.test.js b/test/purgeKeys.test.js index 68339574..c1a0d47f 100644 --- a/test/purgeKeys.test.js +++ b/test/purgeKeys.test.js @@ -30,7 +30,7 @@ describe('#purgeKeys', () => { expect(res.data).toBeA('object'); }); - it('response body should have surrogate keys property', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['key_1', 'key_2', 'key_3']); }); }); diff --git a/test/readBackends.test.js b/test/readBackends.test.js new file mode 100644 index 00000000..56b20a10 --- /dev/null +++ b/test/readBackends.test.js @@ -0,0 +1,80 @@ +'use strict'; + +const nock = require('nock'); +const expect = require('expect'); +const config = require('../src/config'); +const fastlyPromises = require('../src/index'); +const response = require('./response/readBackends.response'); + +describe('#readBackends', () => { + const fastly = fastlyPromises('923b6bd5266a7f932e41962755bd4254', 'SU1Z0isxPaozGVKXdv0eY'); + let res; + + nock(config.mainEntryPoint) + .get('/service/SU1Z0isxPaozGVKXdv0eY/version/234/backend') + .reply(200, response.readBackends); + + before(async () => { + res = await fastly.readBackends('234'); + }); + + it('response should be a status 200', () => { + expect(res.status).toBe(200); + }); + + it('response body should exist', () => { + expect(res.data).toExist(); + }); + + it('response body should be an array', () => { + expect(Array.isArray(res.data)).toBe(true); + }); + + it('response body should be an array of objects', () => { + res.data.forEach(item => { + expect(item).toBeA('object'); + }); + }); + + it('response body should contain all properties', () => { + res.data.forEach(item => { + expect(item).toIncludeKeys([ + 'max_tls_version', + 'ssl_client_cert', + 'hostname', + 'error_threshold', + 'first_byte_timeout', + 'client_cert', + 'weight', + 'address', + 'updated_at', + 'connect_timeout', + 'ipv4', + 'ssl_ciphers', + 'name', + 'port', + 'between_bytes_timeout', + 'ssl_client_key', + 'ssl_ca_cert', + 'auto_loadbalance', + 'ssl_check_cert', + 'shield', + 'service_id', + 'request_condition', + 'ssl_cert_hostname', + 'ssl_hostname', + 'ssl_sni_hostname', + 'locked', + 'min_tls_version', + 'ipv6', + 'version', + 'deleted_at', + 'healthcheck', + 'max_conn', + 'use_ssl', + 'created_at', + 'comment' + ]); + }); + }); +}); diff --git a/test/readDomains.test.js b/test/readDomains.test.js index be9c85f3..d99f58d0 100644 --- a/test/readDomains.test.js +++ b/test/readDomains.test.js @@ -36,7 +36,7 @@ describe('#readDomains', () => { }); }); - it('response body items should have comment, name, service_id, and version properties', () => { + it('response body should contain all properties', () => { res.data.forEach(item => { expect(item).toIncludeKeys(['comment', 'name', 'service_id', 'version']); }); diff --git a/test/readServices.test.js b/test/readServices.test.js index 3538fe05..cc91bc1e 100644 --- a/test/readServices.test.js +++ b/test/readServices.test.js @@ -36,9 +36,16 @@ describe('#readServices', () => { }); }); - it('response body items should have comment, customer_id, id, name, version, and versions properties', () => { + it('response body should contain all properties', () => { res.data.forEach(item => { - expect(item).toIncludeKeys(['comment', 'customer_id', 'id', 'name', 'version', 'versions']); + expect(item).toIncludeKeys([ + 'comment', + 'customer_id', + 'id', + 'name', + 'version', + 'versions' + ]); }); }); }); diff --git a/test/readVersions.test.js b/test/readVersions.test.js index b5f620b9..be897f8a 100644 --- a/test/readVersions.test.js +++ b/test/readVersions.test.js @@ -36,9 +36,21 @@ describe('#readVersions', () => { }); }); - it('response body items should have active, comment, created_at, deleted_at, deployed, locked, number, service_id, staging, testing, and updated_at properties', () => { + it('response body should contain all properties', () => { res.data.forEach(item => { - expect(item).toIncludeKeys(['active', 'comment', 'created_at', 'deleted_at', 'deployed', 'locked', 'number', 'service_id', 'staging', 'testing', 'updated_at']); + expect(item).toIncludeKeys([ + 'active', + 'comment', + 'created_at', + 'deleted_at', + 'deployed', + 'locked', + 'number', + 'service_id', + 'staging', + 'testing', + 'updated_at' + ]); }); }); }); diff --git a/test/response/readBackends.response.js b/test/response/readBackends.response.js new file mode 100644 index 00000000..3af2b31e --- /dev/null +++ b/test/response/readBackends.response.js @@ -0,0 +1,41 @@ +'use strict'; + +module.exports.readBackends = [ + { + 'max_tls_version': null, + 'ssl_client_cert': null, + 'hostname': null, + 'error_threshold': 0, + 'first_byte_timeout': 30000, + 'client_cert': null, + 'weight': 100, + 'address': '127.0.0.1', + 'updated_at': '2017-11-03T17:30:07Z', + 'connect_timeout': 30000, + 'ipv4': null, + 'ssl_ciphers': null, + 'name': 'backend-name', + 'port': 80, + 'between_bytes_timeout': 10000, + 'ssl_client_key': null, + 'ssl_ca_cert': null, + 'auto_loadbalance': false, + 'ssl_check_cert': true, + 'shield': 'sea-wa-us', + 'service_id': 'SU1Z0isxPaozGVKXdv0eY', + 'request_condition': '', + 'ssl_cert_hostname': null, + 'ssl_hostname': null, + 'ssl_sni_hostname': null, + 'locked': true, + 'min_tls_version': null, + 'ipv6': null, + 'version': 234, + 'deleted_at': null, + 'healthcheck': null, + 'max_conn': 200, + 'use_ssl': false, + 'created_at': '2017-03-07T21:56:38Z', + 'comment': '' + } +]; diff --git a/test/softPurgeIndividual.test.js b/test/softPurgeIndividual.test.js index d5d5f458..ba8fce38 100644 --- a/test/softPurgeIndividual.test.js +++ b/test/softPurgeIndividual.test.js @@ -30,7 +30,7 @@ describe('#softPurgeIndividual', () => { expect(res.data).toBeA('object'); }); - it('response body should have status and id properties', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['status', 'id']); }); }); diff --git a/test/softPurgeKey.test.js b/test/softPurgeKey.test.js index ef87c97c..a2537236 100644 --- a/test/softPurgeKey.test.js +++ b/test/softPurgeKey.test.js @@ -30,7 +30,7 @@ describe('#softPurgeKey', () => { expect(res.data).toBeA('object'); }); - it('response body should have status and id properties', () => { + it('response body should contain all properties', () => { expect(res.data).toIncludeKeys(['status', 'id']); }); });