Skip to content

Commit

Permalink
feat(backends): Enable creation of backends
Browse files Browse the repository at this point in the history
Backends can now be created using `createBackend(version, data)`. In addition, safe creation/update
is possible with `writeBackend(version, name, data)`

fixes #5
  • Loading branch information
trieloff committed Jan 15, 2019
1 parent d88ebe6 commit 3f61343
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ HTTP status code can be retrieved. Known error status codes include:</p>
* [.readDomains(version)](#Fastly+readDomains) ⇒ <code>Promise</code>
* [.readBackends(version)](#Fastly+readBackends) ⇒ <code>Promise</code>
* [.updateBackend(version, name, data)](#Fastly+updateBackend) ⇒ <code>Promise</code>
* [.createBackend(version, data)](#Fastly+createBackend) ⇒ <code>Promise</code>
* [.createSnippet(version, data)](#Fastly+createSnippet) ⇒ <code>Promise</code>
* [.updateSnippet(version, name, data)](#Fastly+updateSnippet) ⇒ <code>Promise</code>
* [.createVCL(version, data)](#Fastly+createVCL) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -749,6 +750,21 @@ instance.updateBackend('34', 'slow-server', { name: 'fast-server' })
console.log(err.message);
});
```
<a name="Fastly+createBackend"></a>

#### fastly.createBackend(version, data) ⇒ <code>Promise</code>
Create a new backend for a particular service and version.

**Kind**: instance method of [<code>Fastly</code>](#Fastly)
**Returns**: <code>Promise</code> - The reponse object.
**Fulfil**: [<code>Response</code>](#Response)
**See**: https://docs.fastly.com/api/config#backend_85c170418ee71191dbb3b5046aeb6c2c

| Param | Type | Description |
| --- | --- | --- |
| version | <code>number</code> | The version number (current if omitted). |
| data | <code>Object</code> | The backend definition. |

<a name="Fastly+createSnippet"></a>

#### fastly.createSnippet(version, data) ⇒ <code>Promise</code>
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class Fastly {

this.writeVCL = this.upsertFn(this.createVCL, this.updateVCL);
this.writeSnippet = this.upsertFn(this.createSnippet, this.updateSnippet);
this.writeBackend = this.upsertFn(this.createBackend, this.updateBackend);
}
/**
* @typedef {Object} FastlyError
Expand Down Expand Up @@ -686,6 +687,19 @@ class Fastly {
async updateBackend(version, name, data) {
return this.request.put(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/backend/${encodeURIComponent(name)}`, data);
}

/**
* Create a new backend for a particular service and version.
*
* @see https://docs.fastly.com/api/config#backend_85c170418ee71191dbb3b5046aeb6c2c
* @param {number} version - The version number (current if omitted).
* @param {Object} data - The backend definition.
* @returns {Promise} The reponse object.
* @fulfil {Response}
*/
async createBackend(version, data) {
return this.request.post(`/service/${this.service_id}/version/${await this.getVersion(version, 'current')}/backend`, data);
}
/**
* @typedef {Object} Snippet
* @property {String} name The name of the snippet, as visible in the Fastly UI
Expand Down
84 changes: 84 additions & 0 deletions test/createBackend.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

/* eslint-env mocha */

const nock = require('nock');
const expect = require('expect');
const config = require('../src/config');
const fastlyPromises = require('../src/index');
const response = require('./response/createBackend.response');

describe('#createBackend', () => {
const fastly = fastlyPromises('923b6bd5266a7f932e41962755bd4254', 'SU1Z0isxPaozGVKXdv0eY');
let res;

nock(config.mainEntryPoint)
.post('/service/SU1Z0isxPaozGVKXdv0eY/version/1/backend')
.reply(200, response.createBackend);

before(async () => {
res = await fastly.createBackend(1, {
ipv4: '127.0.0.1',
name: 'backend-name',
port: 80,
});
});

it('response should be a status 200', () => {
expect(res.status).toBe(200);
});

it('response body should exist', () => {
expect(res.data).toBeTruthy();
});

it('response body should be an object', () => {
expect(typeof res.data).toBe('object');
});

it('response body properties should be created', () => {
expect(res.data.name).toBe('backend-name');
expect(res.data.port).toBe(80);
expect(res.data.ipv4).toBe('127.0.0.1');
});

it('response body should contain all properties', () => {
[
'address',
'auto_loadbalance',
'between_bytes_timeout',
'client_cert',
'comment',
'connect_timeout',
'error_threshold',
'first_byte_timeout',
'healthcheck',
'hostname',
'ipv4',
'ipv6',
'locked',
'max_conn',
'max_tls_version',
'min_tls_version',
'name',
'port',
'request_condition',
'service_id',
'shield',
'override_host',
'ssl_ca_cert',
'ssl_cert_hostname',
'ssl_check_cert',
'ssl_ciphers',
'ssl_client_cert',
'ssl_client_key',
'ssl_hostname',
'ssl_sni_hostname',
'use_ssl',
'version',
'weight',
].forEach((e) => {
expect(Object.keys(res.data)).toContain(e);
});
});
});
37 changes: 37 additions & 0 deletions test/response/createBackend.response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

module.exports.createBackend = {
address: '127.0.0.1',
auto_loadbalance: false,
between_bytes_timeout: 10000,
client_cert: null,
comment: '',
connect_timeout: 1000,
error_threshold: 0,
first_byte_timeout: 15000,
healthcheck: null,
hostname: null,
ipv4: '127.0.0.1',
ipv6: null,
locked: true,
max_conn: 200,
max_tls_version: null,
min_tls_version: null,
name: 'backend-name',
port: 80,
request_condition: '',
service_id: 'SU1Z0isxPaozGVKXdv0eY',
shield: null,
override_host: null,
ssl_ca_cert: null,
ssl_cert_hostname: null,
ssl_check_cert: true,
ssl_ciphers: null,
ssl_client_cert: null,
ssl_client_key: null,
ssl_hostname: null,
ssl_sni_hostname: null,
use_ssl: false,
version: 1,
weight: 100,
};

0 comments on commit 3f61343

Please sign in to comment.