Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
feat(zone): support editing zone properties
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed May 9, 2016
1 parent 8c59937 commit 3beab80
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ Retrives the list of `Zone` objects, optionally filtered and sorted by `query` o

Retrives the `Zone` for the zone identifier `z_id`.

#### `editZone(z, [options]): Promise<Zone>`

* z: `Zone` object
* options: *see Request Options*
* *Returns*: A Promise that resolves to a `Zone`.
* *API Reference*: [Edit Zone Properties](https://api.cloudflare.com/#zone-edit-zone-properties)

Saves the modifications to the `Zone` object.

**Note**: The CloudFlare API currently doesn't support patching multiple
properties at once. This method executes the modifications in series, but if an
error occurs, there are no guarantees which properties would have been applied.

#### `deleteZone(z, [options]): Promise<{id: string}>`

* z: `Zone` object or string zone id
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module.exports = prototypal({
readIPs: ips.read,
browseZones: zones.browse,
readZone: zones.read,
editZone: zones.edit,
deleteZone: zones.delete,
deleteCache: purge.delete,
browseDNS: dns.browse,
Expand Down
42 changes: 42 additions & 0 deletions lib/zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@ function read(zid, options) {
});
}

function edit(z, options) {
options = options || {};

var uri = join('zones', z.id);
var changes = z.getChanges();

var patched = Object.keys(changes).map(function (key) {
var change = changes[key];
change.key = Zone.alias[key];
return change;
}).filter(function (change) {
return change.changed;
}).map(function (change) {
var body = {};
body[change.key] = change.now;
return body;
});

if (!patched.length) {
return Promise.resolve(z.makeClone());
}

options.method = 'PATCH';

function sendPatched() {
var patch = patched.pop();

options.body = JSON.stringify(patch);

return this._got(uri, options).then(function (response) {
if (patched.length) {
return sendPatched.call(this);
}

return Zone.create(response.body.result);
}.bind(this));
}

return sendPatched.call(this);
}

function remove(z, options) {
var zid = Zone.is(z) ? z.id : z;
var uri = join('zones', zid);
Expand All @@ -88,5 +129,6 @@ function remove(z, options) {

module.exports.browse = browse;
module.exports.read = read;
module.exports.edit = edit;
module.exports.delete = remove;
module.exports.Zone = Zone;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"es-class": "^2.1.1",
"got": "^6.3.0",
"verymodel": "^3.5.1",
"verymodel": "git+https://github.com/terinjokes/VeryModel.git#old_data",
"spdy": "^3.3.2",
"url-join": "^1.1.0"
},
Expand Down
95 changes: 95 additions & 0 deletions test/zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,98 @@ test('delete a Zone by Zone', async t => {
t.false(Zone.is(zone));
t.true(zone.id === '1');
});

test('edit paused status', async t => {
let z = Zone.create({
id: '1',
name: 'example.com',
paused: false
});

z.paused = true;

nock('https://api.cloudflare.com')
.patch('/client/v4/zones/1', {
paused: true
})
.reply(200, {
result: {
id: '1',
name: 'example.com',
paused: true
}
});

let zone = await t.context.cf.editZone(z);
t.true(Zone.is(zone));
t.true(zone.paused);
});

test('edit vanity name servers', async t => {
let z = Zone.create({
id: '1',
name: 'example.com',
vanity_name_servers: ['ns1.example.com']
});

z.vanityNameservers = z.vanityNameservers.concat('ns2.example.com');

nock('https://api.cloudflare.com')
.patch('/client/v4/zones/1', {
vanity_name_servers: ['ns1.example.com', 'ns2.example.com']
})
.reply(200, {
result: {
id: '1',
name: 'example.com',
vanity_name_servers: ['ns1.example.com', 'ns2.example.com']
}
});

let zone = await t.context.cf.editZone(z);
t.true(Zone.is(zone));
t.deepEqual(zone.vanityNameservers, ['ns1.example.com', 'ns2.example.com']);
});

test('edit paused and vanity name servers', async t => {
var result = {
id: '1',
name: 'example.com',
vanity_name_servers: ['ns1.example.com'],
paused: false
};

let z = Zone.create({
id: '1',
name: 'example.com',
vanity_name_servers: ['ns1.example.com']
});

z.vanityNameservers = z.vanityNameservers.concat('ns2.example.com');
z.paused = true;

nock('https://api.cloudflare.com')
.patch('/client/v4/zones/1', {
vanity_name_servers: ['ns1.example.com', 'ns2.example.com']
})
.reply(200, function (uri, requestBody) {
result.vanity_name_servers = requestBody.vanity_name_servers;
return {
result: result
};
})
.patch('/client/v4/zones/1', {
paused: true
})
.reply(200, function (uri, requestBody) {
result.paused = requestBody.paused;
return {
result: result
};
});

let zone = await t.context.cf.editZone(z);
t.true(Zone.is(zone));
t.true(zone.paused);
t.deepEqual(zone.vanityNameservers, ['ns1.example.com', 'ns2.example.com']);
});

0 comments on commit 3beab80

Please sign in to comment.