TRITON-42 node-triton should support nics when creating an instance

Reviewed by: Michael Zeller <mike.zeller@joyent.com>
Reviewed by: Trent Mick <trentm@gmail.com>
Approved by: Trent Mick <trentm@gmail.com>
This commit is contained in:
Dave Eddy 2018-03-19 18:41:32 -04:00
parent 8e6cf27121
commit 06812c9cd4
4 changed files with 75 additions and 7 deletions

View File

@ -13,6 +13,13 @@ Known issues:
`triton instance enable-deletion-protection ...`, and `triton instance enable-deletion-protection ...`, and
`triton instance disable-deletion-protection ...`. This flag is only supported `triton instance disable-deletion-protection ...`. This flag is only supported
on cloudapi versions 8.7.0 or above. on cloudapi versions 8.7.0 or above.
- [TRITON-59] node-triton should support nic operations
`triton instance nic get ...`
`triton instance nic create ...`
`triton instance nic list ...`
`triton instance nic delete ...`
- [TRITON-42] node-triton should support nics when creating an instance, e.g.
`triton instance create --nic <Network Object> IMAGE PACKAGE`
## 5.9.0 ## 5.9.0

View File

@ -1427,9 +1427,9 @@ function ipv4ToLong(ip) {
* ] * ]
* } * }
* Note: "1234" is used as the UUID for this example, but would actually cause * Note: "1234" is used as the UUID for this example, but would actually cause
* `parseNicsCLI` to throw as it is not a valid UUID. * `parseNicStr` to throw as it is not a valid UUID.
*/ */
function parseNicsCLI(nic) { function parseNicStr(nic) {
assert.arrayOfString(nic); assert.arrayOfString(nic);
var obj = objFromKeyValueArgs(nic, { var obj = objFromKeyValueArgs(nic, {
@ -1502,6 +1502,6 @@ module.exports = {
readStdin: readStdin, readStdin: readStdin,
validateObject: validateObject, validateObject: validateObject,
ipv4ToLong: ipv4ToLong, ipv4ToLong: ipv4ToLong,
parseNicsCLI: parseNicsCLI parseNicStr: parseNicStr
}; };
// vim: set softtabstop=4 shiftwidth=4: // vim: set softtabstop=4 shiftwidth=4:

View File

@ -19,6 +19,8 @@ var common = require('../common');
var distractions = require('../distractions'); var distractions = require('../distractions');
var errors = require('../errors'); var errors = require('../errors');
var mat = require('../metadataandtags'); var mat = require('../metadataandtags');
var NETWORK_OBJECT_FIELDS =
require('../cloudapi2').CloudApi.prototype.NETWORK_OBJECT_FIELDS;
function parseVolMount(volume) { function parseVolMount(volume) {
var components; var components;
@ -83,6 +85,9 @@ function do_create(subcmd, opts, args, cb) {
return; return;
} else if (args.length !== 2) { } else if (args.length !== 2) {
return cb(new errors.UsageError('incorrect number of args')); return cb(new errors.UsageError('incorrect number of args'));
} else if (opts.nic && opts.network) {
return cb(new errors.UsageError(
'--network and --nic cannot be specified together'));
} }
var log = this.top.log; var log = this.top.log;
@ -224,6 +229,47 @@ function do_create(subcmd, opts, args, cb) {
next(); next();
}, },
/*
* Parse any nics given via `--nic`
*/
function parseNics(ctx, next) {
if (!opts.nic) {
next();
return;
}
ctx.nics = [];
var i;
var networksSeen = {};
var nic;
var nics = opts.nic;
log.trace({nics: nics}, 'parsing nics');
for (i = 0; i < nics.length; i++) {
nic = nics[i].split(',');
try {
nic = common.parseNicStr(nic);
if (networksSeen[nic.ipv4_uuid]) {
throw new errors.UsageError(format(
'only 1 ip on a network allowed '
+ '(network %s specified multiple times)',
nic.ipv4_uuid));
}
networksSeen[nic.ipv4_uuid] = true;
ctx.nics.push(nic);
} catch (err) {
next(err);
return;
}
}
log.trace({nics: ctx.nics}, 'parsed nics');
next();
},
/* /*
* Determine `ctx.locality` according to what CloudAPI supports * Determine `ctx.locality` according to what CloudAPI supports
* based on `ctx.affinities` parsed earlier. * based on `ctx.affinities` parsed earlier.
@ -371,11 +417,17 @@ function do_create(subcmd, opts, args, cb) {
var createOpts = { var createOpts = {
name: opts.name, name: opts.name,
image: ctx.img.id, image: ctx.img.id,
'package': ctx.pkg && ctx.pkg.id, 'package': ctx.pkg && ctx.pkg.id
networks: ctx.nets && ctx.nets.map(
function (net) { return net.id; })
}; };
if (ctx.nets) {
createOpts.networks = ctx.nets.map(function (net) {
return net.id;
});
} else if (ctx.nics) {
createOpts.networks = ctx.nics;
}
if (ctx.volMounts) { if (ctx.volMounts) {
createOpts.volumes = ctx.volMounts; createOpts.volumes = ctx.volMounts;
} }
@ -539,6 +591,15 @@ do_create.options = [
'This option can be used multiple times.', 'This option can be used multiple times.',
completionType: 'tritonnetwork' completionType: 'tritonnetwork'
}, },
{
names: ['nic'],
type: 'arrayOfString',
helpArg: 'NICOPTS',
help: 'A network interface object containing comma separated ' +
'key=value pairs (Network object format). ' +
'This option can be used multiple times for multiple NICs. ' +
'Valid keys are: ' + Object.keys(NETWORK_OBJECT_FIELDS).join(', ')
},
{ {
// TODO: add boolNegationPrefix:'no-' when that cmdln pull is in // TODO: add boolNegationPrefix:'no-' when that cmdln pull is in
names: ['firewall'], names: ['firewall'],

View File

@ -59,7 +59,7 @@ function do_create(subcmd, opts, args, cb) {
} }
try { try {
netObj = common.parseNicsCLI(netObjArgs); netObj = common.parseNicStr(netObjArgs);
} catch (err) { } catch (err) {
cb(err); cb(err);
return; return;