Skip to content

Commit

Permalink
feat(readBackends): Add readBackends class method
Browse files Browse the repository at this point in the history
Add readBackends class method to list all backends for a particular service and version
  • Loading branch information
philippschulte committed Dec 23, 2017
1 parent 65f231a commit 9e41fb5
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 17 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -147,6 +147,7 @@ async function handler() {
- [.readServices()](#readServices)
- [.readVersions()](#readVersions)
- [.readDomains(version)](#readDomains)
- [.readBackends(version)](#readBackends)
- [.domainCheckAll(version)](#domainCheckAll)

<a name="constructor"></a>
Expand Down Expand Up @@ -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.

<a name="readBackends"></a>

### [.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.

<a name="domainCheckAll"></a>

### [.domainCheckAll(version)](https://docs.fastly.com/api/config#domain_e33a599694c3316f00b6b8d53a2db7d9)
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/dataCenters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
2 changes: 1 addition & 1 deletion test/domainCheckAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion test/edgeCheck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
2 changes: 1 addition & 1 deletion test/publicIpList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
2 changes: 1 addition & 1 deletion test/purgeAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
2 changes: 1 addition & 1 deletion test/purgeIndividual.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
2 changes: 1 addition & 1 deletion test/purgeKey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
2 changes: 1 addition & 1 deletion test/purgeKeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
80 changes: 80 additions & 0 deletions test/readBackends.test.js
Original file line number Diff line number Diff line change
@@ -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'
]);
});
});
});
2 changes: 1 addition & 1 deletion test/readDomains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
Expand Down
11 changes: 9 additions & 2 deletions test/readServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
});
});
});
16 changes: 14 additions & 2 deletions test/readVersions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]);
});
});
});
41 changes: 41 additions & 0 deletions test/response/readBackends.response.js
Original file line number Diff line number Diff line change
@@ -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': ''
}
];
2 changes: 1 addition & 1 deletion test/softPurgeIndividual.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
2 changes: 1 addition & 1 deletion test/softPurgeKey.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});

0 comments on commit 9e41fb5

Please sign in to comment.