Skip to content

Commit

Permalink
PUBAPI-988 - add support for list/add/remove of nics on a VM.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsell Kukuljevic committed Dec 2, 2014
1 parent 2bb9734 commit 4b20a7c
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 1 deletion.
152 changes: 152 additions & 0 deletions bin/sdc-nics
@@ -0,0 +1,152 @@
#!/usr/bin/env node
// -*- mode: js -*-
// vim: set filetype=javascript :
// Copyright 2014 Joyent, Inc. All rights reserved.

var util = require('util');
var shared = require('../lib/shared'),
commonCb = shared.commonCb;
var cmdln = require('cmdln'),
Cmdln = cmdln.Cmdln;


var generalOptions = [
{
names: ['help', 'h', '?'],
type: 'bool',
help: 'Show this help.'
}
];



function SDCNic() {
Cmdln.call(this, {
name: 'sdc-nics',
desc: 'SmartDC machine NIC operations',
options: shared.DEFAULT_OPTIONS,
helpOpts: {
includeEnv: true
}
});
}
util.inherits(SDCNic, Cmdln);



SDCNic.prototype.init = function (opts, args, callback) {
shared.checkRequiredOptions.apply(this, arguments);
Cmdln.prototype.init.apply(this, arguments);
return false;
};



SDCNic.prototype.do_list = function (subcmd, opts, args, callback) {
if (opts.help) {
return this.do_help('help', {}, [subcmd], callback);
}

if (args.length !== 1) {
return callback(new Error('machine_id must be specified'));
}

return this.cloudapi.listNics(args[0], commonCb);
};

SDCNic.prototype.do_list.options = generalOptions;
SDCNic.prototype.do_list.help = (
'List your machine\'s NICs.\n' +
'\n' +
'Usage:\n' +
' {{name}} list [OPTIONS] machine_id\n' +
'\n' +
'{{options}}'
);



SDCNic.prototype.do_get = function (subcmd, opts, args, callback) {
if (opts.help) {
return this.do_help('help', {}, [subcmd], callback);
}

if (args.length !== 2) {
return callback(new Error('nic_mac and machine_id must be specified'));
}

return this.cloudapi.getNic(args[1], args[0], commonCb);

};

SDCNic.prototype.do_get.options = generalOptions;
SDCNic.prototype.do_get.help = (
'Get a machine\'s NIC by MAC.\n' +
'\n' +
'Usage:\n' +
' {{name}} get [OPTIONS] nic_mac machine_id\n' +
'\n' +
'{{options}}'
);



SDCNic.prototype.do_create = function (subcmd, opts, args, callback) {
var self = this;
if (opts.help) {
return this.do_help('help', {}, [subcmd], callback);
}

if (args.length !== 2) {
var msg = 'network_id and machine_id must be specified';
return callback(new Error(msg));
}

var params = {
network: args[0],
machine: args[1]
};

return self.cloudapi.createNic(params, commonCb);
};

SDCNic.prototype.do_create.options = generalOptions;
SDCNic.prototype.do_create.help = (
'Create a new NIC on one of your machines, and reboot the machine.\n' +
'\n' +
'Usage:\n' +
' {{name}} create [OPTIONS] network_id machine_id\n' +
'\n' +
'{{options}}'
);



SDCNic.prototype.do_delete = function (subcmd, opts, args, callback) {
if (opts.help) {
return this.do_help('help', {}, [subcmd], callback);
}

if (args.length !== 2) {
return callback(new Error('nic_mac and machine_id must be specified'));
}

return this.cloudapi.deleteNic(args[1], args[0], commonCb);

};

SDCNic.prototype.do_delete.options = generalOptions;
SDCNic.prototype.do_delete.help = (
'Removes a NIC from a machine, and reboots machine.\n' +
'\n' +
'Usage:\n' +
' {{name}} delete [OPTIONS] nic_mac machine_id\n' +
'\n' +
'{{options}}'
);



if (require.main === module) {
cmdln.main(SDCNic);
}
189 changes: 189 additions & 0 deletions lib/cloudapi.js
Expand Up @@ -46,6 +46,8 @@ var DATACENTERS = ROOT + '/datacenters';
var MACHINES = ROOT + '/machines';
var MACHINE = MACHINES + '/%s';
var METADATA = MACHINE + '/metadata';
var NICS = MACHINE + '/nics';
var NIC = NICS + '/%s';
var METADATA_KEY = MACHINE + '/metadata/%s';
var SNAPSHOTS = MACHINE + '/snapshots';
var SNAPSHOT = SNAPSHOTS + '/%s';
Expand Down Expand Up @@ -4071,6 +4073,193 @@ CloudAPI.prototype.setRoleTags = setRoleTags;
CloudAPI.prototype.SetRoleTags = setRoleTags;


// --- NIC-related functions

/**
* Retrieves a NIC on one of an account's machines.
*
* Returns an object.
*
* @param {String} account (optional) the login name of the account.
* @param {String} machine is the UUID of the machine.
* @param {String} mac is the MAC address of the NIC.
* @param {Function} callback of the form f(err, nic).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
CloudAPI.prototype.getNic =
function getNic(account, machine, mac, callback, noCache) {
var self = this;

if (typeof (mac) === 'function') {
callback = mac;
mac = machine;
machine = account;
account = this.account;
}

if (typeof (callback) !== 'function') {
throw new TypeError('callback (function) required');
}

if (typeof (account) === 'object') {
account = account.login;
}

var path = sprintf(NIC, account, machine, mac.replace(/:/g, ''));

return self._request(path, null, function (req) {
return self._get(req, callback, noCache);
});

};
CloudAPI.prototype.GetNic = CloudAPI.prototype.getNic;


/**
* Creates a NIC on a machine. Note that this reboots the machine as part of
* the process.
*
* Returns a JS object (the created nic).
*
* @param {String} account (optional) the login name of the account.
* @param {Object} options object containing:
* - {String} network UUID of network to attach NIC to.
* - {Object} machine to add NIC to.
* @param {Function} callback of the form f(err, nic).
* @throws {TypeError} on bad input.
*/
CloudAPI.prototype.createNic =
function createNic(account, options, callback) {
var self = this;

if (typeof (options) === 'function') {
callback = options;
options = account;
account = this.account;
}

if (typeof (callback) !== 'function') {
throw new TypeError('callback (function) required');
}

if (typeof (options) !== 'object') {
throw new TypeError('options (object) required');
}

if (typeof (account) === 'object') {
account = account.login;
}

var machine = options.machine;
var network = options.network;

if (typeof (network) !== 'string') {
throw new TypeError('network (string) required in options');
}

if (typeof (machine) !== 'string') {
throw new TypeError('machine (string) required in options');
}

var path = sprintf(NICS, account, machine);

return self._request(path, options, function (req) {
return self._post(req, callback);
});
};
CloudAPI.prototype.CreateNic = CloudAPI.prototype.createNic;


/**
* Lists all NICs on a given machine.
*
* Returns an array of objects.
*
* @param {String} account (optional) the login name of the account.
* @param {String} machine the UUID of the machine.
* @param {Function} callback of the form f(err, macs).
* @param {Boolean} noCache optional flag to force skipping the cache.
* @throws {TypeError} on bad input.
*/
CloudAPI.prototype.listNics =
function listNics(account, machine, callback, noCache) {
var self = this;

if (typeof (machine) === 'function') {
noCache = callback;
callback = machine;
machine = account;
account = this.account;
}

if (typeof (callback) !== 'function') {
throw new TypeError('callback (function) required');
}

if (typeof (machine) !== 'string') {
throw new TypeError('machine (string) required');
}

if (typeof (account) === 'object') {
account = account.login;
}

var path = sprintf(NICS, account, machine);

self._request(path, null, function (req) {
return self._get(req, callback, noCache);
});
};
CloudAPI.prototype.ListNics = CloudAPI.prototype.listNics;


/**
* Removes a NIC from a machine. Note that this reboots the machine as part of
* the process.
*
* @param {String} account (optional) the login name of the account.
* @param {String} machine is the UUID of the machine.
* @param {String} mac is the MAC address of the NIC.
* @param {Function} callback of the form f(err).
* @throws {TypeError} on bad input.
*/
CloudAPI.prototype.deleteNic =
function deleteNic(account, machine, mac, callback) {
var self = this;

if (typeof (mac) === 'function') {
callback = mac;
mac = machine;
machine = account;
account = this.account;
}

if (typeof (callback) !== 'function') {
throw new TypeError('callback (function) required');
}

if (typeof (mac) !== 'string') {
throw new TypeError('mac (string) required');
}

if (typeof (machine) !== 'string') {
throw new TypeError('machine (string) required');
}

if (typeof (account) === 'object') {
account = account.login;
}

var path = sprintf(NIC, account, machine, mac.replace(/:/g, ''));

return self._request(path, null, function (req) {
return self._del(req, callback);
});
};
CloudAPI.prototype.DeleteNic = CloudAPI.prototype.deleteNic;


// --- Private Functions


Expand Down

0 comments on commit 4b20a7c

Please sign in to comment.