diff --git a/CHANGES.md b/CHANGES.md index 3484a88..31cfb23 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,13 @@ Known issues: `triton instance enable-deletion-protection ...`, and `triton instance disable-deletion-protection ...`. This flag is only supported 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 IMAGE PACKAGE` ## 5.9.0 diff --git a/lib/common.js b/lib/common.js index 4911fd4..04aee41 100644 --- a/lib/common.js +++ b/lib/common.js @@ -1427,9 +1427,9 @@ function ipv4ToLong(ip) { * ] * } * 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); var obj = objFromKeyValueArgs(nic, { @@ -1502,6 +1502,6 @@ module.exports = { readStdin: readStdin, validateObject: validateObject, ipv4ToLong: ipv4ToLong, - parseNicsCLI: parseNicsCLI + parseNicStr: parseNicStr }; // vim: set softtabstop=4 shiftwidth=4: diff --git a/lib/do_instance/do_create.js b/lib/do_instance/do_create.js index 273b1e7..12463a0 100644 --- a/lib/do_instance/do_create.js +++ b/lib/do_instance/do_create.js @@ -19,6 +19,8 @@ var common = require('../common'); var distractions = require('../distractions'); var errors = require('../errors'); var mat = require('../metadataandtags'); +var NETWORK_OBJECT_FIELDS = + require('../cloudapi2').CloudApi.prototype.NETWORK_OBJECT_FIELDS; function parseVolMount(volume) { var components; @@ -83,6 +85,9 @@ function do_create(subcmd, opts, args, cb) { return; } else if (args.length !== 2) { 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; @@ -224,6 +229,47 @@ function do_create(subcmd, opts, args, cb) { 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 * based on `ctx.affinities` parsed earlier. @@ -371,11 +417,17 @@ function do_create(subcmd, opts, args, cb) { var createOpts = { name: opts.name, image: ctx.img.id, - 'package': ctx.pkg && ctx.pkg.id, - networks: ctx.nets && ctx.nets.map( - function (net) { return net.id; }) + 'package': ctx.pkg && ctx.pkg.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) { createOpts.volumes = ctx.volMounts; } @@ -539,6 +591,15 @@ do_create.options = [ 'This option can be used multiple times.', 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 names: ['firewall'], diff --git a/lib/do_instance/do_nic/do_create.js b/lib/do_instance/do_nic/do_create.js index 47a46ae..058f221 100644 --- a/lib/do_instance/do_nic/do_create.js +++ b/lib/do_instance/do_nic/do_create.js @@ -59,7 +59,7 @@ function do_create(subcmd, opts, args, cb) { } try { - netObj = common.parseNicsCLI(netObjArgs); + netObj = common.parseNicStr(netObjArgs); } catch (err) { cb(err); return;