2018-06-05 07:32:58 +03:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-10-04 18:51:28 +03:00
|
|
|
* Copyright (c) 2018, Joyent, Inc.
|
2018-06-05 07:32:58 +03:00
|
|
|
*
|
|
|
|
* `triton vlan create ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
2018-10-04 18:51:28 +03:00
|
|
|
var jsprim = require('jsprim');
|
2018-06-05 07:32:58 +03:00
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
|
|
|
|
|
|
|
|
|
|
|
function do_create(subcmd, opts, args, cb) {
|
|
|
|
assert.optionalString(opts.name, 'opts.name');
|
|
|
|
assert.optionalString(opts.description, 'opts.description');
|
|
|
|
assert.optionalBool(opts.json, 'opts.json');
|
|
|
|
assert.optionalBool(opts.help, 'opts.help');
|
|
|
|
assert.func(cb, 'cb');
|
|
|
|
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length === 0) {
|
|
|
|
cb(new errors.UsageError('missing VLAN argument'));
|
|
|
|
return;
|
|
|
|
} else if (args.length > 1) {
|
|
|
|
cb(new errors.UsageError('incorrect number of arguments'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:51:28 +03:00
|
|
|
var vlanId = jsprim.parseInteger(args[0], { allowSign: false });
|
|
|
|
if (typeof (vlanId) !== 'number') {
|
|
|
|
cb(new errors.UsageError('VLAN must be an integer'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opts.name) {
|
|
|
|
cb(new errors.UsageError('must provide a --name (-n)'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-05 07:32:58 +03:00
|
|
|
var createOpts = {
|
2018-10-04 18:51:28 +03:00
|
|
|
vlan_id: vlanId,
|
|
|
|
name: opts.name
|
2018-06-05 07:32:58 +03:00
|
|
|
};
|
2018-10-04 18:51:28 +03:00
|
|
|
|
2018-06-05 07:32:58 +03:00
|
|
|
if (opts.description) {
|
|
|
|
createOpts.description = opts.description;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cli = this.top;
|
|
|
|
|
|
|
|
common.cliSetupTritonApi({cli: cli}, function onSetup(setupErr) {
|
|
|
|
if (setupErr) {
|
|
|
|
cb(setupErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cloudapi = cli.tritonapi.cloudapi;
|
|
|
|
cloudapi.createFabricVlan(createOpts, function onCreate(err, vlan) {
|
|
|
|
if (err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.json) {
|
|
|
|
console.log(JSON.stringify(vlan));
|
|
|
|
} else {
|
|
|
|
if (vlan.name) {
|
|
|
|
console.log('Created vlan %s (%d)', vlan.name,
|
|
|
|
vlan.vlan_id);
|
|
|
|
} else {
|
|
|
|
console.log('Created vlan %d', vlan.vlan_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
do_create.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
2018-10-04 18:51:28 +03:00
|
|
|
group: 'Create options'
|
2018-06-05 07:32:58 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['name', 'n'],
|
|
|
|
type: 'string',
|
|
|
|
helpArg: 'NAME',
|
|
|
|
help: 'Name of the VLAN.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['description', 'D'],
|
|
|
|
type: 'string',
|
|
|
|
helpArg: 'DESC',
|
|
|
|
help: 'Description of the VLAN.'
|
2018-10-04 18:51:28 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Other options'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['json', 'j'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'JSON stream output.'
|
2018-06-05 07:32:58 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
do_create.synopses = ['{{name}} {{cmd}} [OPTIONS] VLAN'];
|
|
|
|
|
|
|
|
do_create.help = [
|
|
|
|
'Create a VLAN.',
|
|
|
|
'',
|
|
|
|
'{{usage}}',
|
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
'Example:',
|
|
|
|
' triton vlan create -n "dmz" -D "Demilitarized zone" 73'
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_create.helpOpts = {
|
2018-10-04 18:51:28 +03:00
|
|
|
helpCol: 16
|
2018-06-05 07:32:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = do_create;
|