From 4cefdad67e761de01ccf5ca6899640299a7276fc Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Fri, 30 May 2014 18:50:56 -0700 Subject: [PATCH] Adding limited nova network management support --- nova/v1.1/client.js | 2 ++ nova/v1.1/networks.js | 39 +++++++++++++++++++++++++++++++++++++++ nova/v1.1/servers.js | 11 +++++++++++ package.json | 2 +- 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 nova/v1.1/networks.js diff --git a/nova/v1.1/client.js b/nova/v1.1/client.js index 2b97668..538b1b6 100644 --- a/nova/v1.1/client.js +++ b/nova/v1.1/client.js @@ -7,6 +7,7 @@ var AZManager = require("./availability_zones"), ImageManager = require('./images'), KeypairManager = require('./keypairs'), LimitManager = require('./limits'), + NetworkManager = require('./networks'), QuotaManager = require('./quotas'), SecurityGroupManager = require('./security_groups'), SecurityGroupRuleManager = require('./security_group_rules'), @@ -28,6 +29,7 @@ var Nova = base.Client.extend({ this.images = new ImageManager(this); this.keypairs = new KeypairManager(this); this.limits = new LimitManager(this); + this.networks = new NetworkManager(this); this.quotas = new QuotaManager(this); this.security_groups = new SecurityGroupManager(this); this.security_group_rules = new SecurityGroupRuleManager(this); diff --git a/nova/v1.1/networks.js b/nova/v1.1/networks.js new file mode 100644 index 0000000..4882c5a --- /dev/null +++ b/nova/v1.1/networks.js @@ -0,0 +1,39 @@ +var base = require("../../client/base"), + error = require("../../client/error"); + + +var NetworkManager = base.Manager.extend({ + namespace: "os-networks", + plural: "networks", + + create: function (params, callback) { + // Nova will choke with an error trying to parse an int if MTU is sent + // but anything other than an integer or null; + if (!params.data.mtu) params.data.mtu = null; + return this._super(params, callback); + }, + + del: function (params, callback) { + var manager = this, + id = params.id || params.data.id; + params.url = this.urljoin(this.get_base_url(params), id); + // Always try to disassociate first; it's slightly inefficient, but it + // ensures that delete won't fail because you forgot to do the disassocation + // manually. This doesn't seem like a place where two steps are necessary. + return this.disassociate({id: id}, function (err, result, xhr) { + if (err) return manager.safe_complete(err, null, xhr, params, callback); + manager.client.del(params, callback); + }); + }, + + disassociate: function (params, callback) { + // Unlike novaclient, this method defaults to disassociating everything. + var url = this.urljoin(this.get_base_url(params), params.id || params.data.id, "action"); + params = this.prepare_params(params, url, "singular"); + params.data = {disassociate: null}; + return this.client.post(params, callback); + } +}); + + +module.exports = NetworkManager; diff --git a/nova/v1.1/servers.js b/nova/v1.1/servers.js index f7a3fd3..e912005 100644 --- a/nova/v1.1/servers.js +++ b/nova/v1.1/servers.js @@ -37,6 +37,17 @@ var ServerManager = base.Manager.extend({ delete params.data.scheduler_hints; } + if (params.data.networks) { + params.data.nics = []; + if (Object.prototype.toString.call(params.data.networks) !== '[object Array]') { + params.data.networks = [params.data.networks]; + } + params.data.networks.forEach(function (network) { + params.data.nics.push({"net-id": network, "v4-fixed-ip": ""}); + }); + delete params.data.networks; + } + // Base64 encode user data if present if (params.data.user_data) { // Use Buffer built-in if in Node, otherwise use btoa in the browser diff --git a/package.json b/package.json index f8030a0..4e34f1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openclient", - "version": "1.5.11", + "version": "1.5.12", "description": "An opinionated client for RESTful APIs (particularly OpenStack's).", "homepage": "https://github.com/gabrielhurley/js-openclient", "keywords": ["client", "rest", "openstack"],