Skip to content

Commit

Permalink
NAPI-403 Allow creating network pools whose networks vary in "nic_tag…
Browse files Browse the repository at this point in the history
…" values

Reviewed by: Jason King <jason.king@joyent.com>
Reviewed by: Richard Kiene <richard.kiene@joyent.com>
Approved by: Richard Kiene <richard.kiene@joyent.com>
  • Loading branch information
melloc committed Jun 1, 2017
1 parent 96ae773 commit 6d628d7
Show file tree
Hide file tree
Showing 15 changed files with 865 additions and 96 deletions.
5 changes: 5 additions & 0 deletions docs/index.md
Expand Up @@ -1276,6 +1276,7 @@ if it matches *all* of the input parameters.
"uuid": "3b5913ec-42e6-4803-9c0b-c9b1c5603520",
"name": "internal networks",
"nic_tag": "internal",
"nic_tags_present": [ "internal" ],
"family": "ipv4",
"networks": [
"0e70de36-a40b-4ac0-9429-819f5ff822bd",
Expand All @@ -1287,6 +1288,7 @@ if it matches *all* of the input parameters.
"name": "external v6 networks",
"description": "Logical pool of public IPv6 addresses",
"nic_tag": "external",
"nic_tags_present": [ "external" ],
"family": "ipv6",
"networks": [
"57a83e2b-527c-41c1-983c-be9b792011dc",
Expand Down Expand Up @@ -1325,6 +1327,7 @@ Creates a new logical network provisioning pool.
"uuid": "3b5913ec-42e6-4803-9c0b-c9b1c5603520",
"name": "internal networks",
"nic_tag": "internal",
"nic_tags_present": [ "internal" ],
"family": "ipv4",
"networks": [
"0e70de36-a40b-4ac0-9429-819f5ff822bd",
Expand All @@ -1344,6 +1347,7 @@ Gets a logical network provisioning pool by UUID.
"uuid": "3b5913ec-42e6-4803-9c0b-c9b1c5603520",
"name": "internal networks",
"nic_tag": "internal",
"nic_tags_present": [ "internal" ],
"family": "ipv4",
"networks": [
"0e70de36-a40b-4ac0-9429-819f5ff822bd",
Expand Down Expand Up @@ -1375,6 +1379,7 @@ Must specify at least one of:
"uuid": "3b5913ec-42e6-4803-9c0b-c9b1c5603520",
"name": "internal-pool",
"nic_tag": "internal",
"nic_tags_present": [ "internal" ],
"family": "ipv4",
"networks": [
"0e70de36-a40b-4ac0-9429-819f5ff822bd",
Expand Down
67 changes: 35 additions & 32 deletions lib/models/network-pool.js
Expand Up @@ -209,8 +209,6 @@ function validateNetworks(opts, name, value, callback) {
function _validateNetworks(opts, name, uuids, callback) {
var nets = [];
var notFound = [];
var tag;
var tagsNotMatching = [];
var pool_family;
var poolTypeNotMatching = [];
var validated = [];
Expand Down Expand Up @@ -256,16 +254,6 @@ function _validateNetworks(opts, name, uuids, callback) {
return;
}

if (tag === undefined) {
tag = net.nic_tag;
}

if (net.nic_tag !== tag) {
tagsNotMatching.push(uuid);
cb();
return;
}

if (pool_family === undefined) {
pool_family = net.family;
}
Expand Down Expand Up @@ -296,19 +284,13 @@ function _validateNetworks(opts, name, uuids, callback) {
return;
}

if (tagsNotMatching.length !== 0) {
callback(errors.invalidParam(name,
constants.POOL_TAGS_MATCH_MSG));
return;
}

if (poolTypeNotMatching.length !== 0) {
callback(errors.invalidParam(name,
constants.POOL_AF_MATCH_MSG));
return;
}

var toReturn = { _networks: nets };
var toReturn = { _netobjs: nets };
toReturn[name] = validated;

callback(null, null, toReturn);
Expand All @@ -321,9 +303,9 @@ function _validateNetworks(opts, name, uuids, callback) {
* match that owner_uuid or have no owner_uuid.
*/
function validateNetworkOwners(_opts, _, parsed, callback) {
if (!parsed.owner_uuids || !parsed._networks ||
if (!parsed.owner_uuids || !parsed._netobjs ||
parsed.owner_uuids.length === 0 ||
parsed._networks.length === 0) {
parsed._netobjs.length === 0) {
callback();
return;
}
Expand All @@ -334,7 +316,7 @@ function validateNetworkOwners(_opts, _, parsed, callback) {
});

var notMatching = [];
parsed._networks.forEach(function (net) {
parsed._netobjs.forEach(function (net) {
if (net.params.hasOwnProperty('owner_uuids')) {
for (var o in net.params.owner_uuids) {
if (owners.hasOwnProperty(net.params.owner_uuids[o])) {
Expand Down Expand Up @@ -367,11 +349,26 @@ function validateNetworkOwners(_opts, _, parsed, callback) {
* Network pool model constructor
*/
function NetworkPool(params) {
assert.object(params, 'params');

delete params.nic_tag;
if (params._networks && util.isArray(params._networks) &&
params._networks.length !== 0) {
params.family = params._networks[0].family;
params.nic_tag = params._networks[0].params.nic_tag;

/*
* If we're migrating the network pool objects in Moray after a bucket
* upgrade, then the "_netobjs" array won't be present. If we're
* responding to a request, then we'll have the networks, and can set
* fields based on that information.
*/
if (params._netobjs && Array.isArray(params._netobjs) &&
params._netobjs.length !== 0) {
params.family = params._netobjs[0].family;
params.nic_tags_present = [];
params._netobjs.forEach(function (network) {
var nic_tag = network.nic_tag;
if (params.nic_tags_present.indexOf(nic_tag) === -1) {
params.nic_tags_present.push(nic_tag);
}
});
}

mod_moray.valToArray(params, 'owner_uuids');
Expand All @@ -391,7 +388,7 @@ function NetworkPool(params) {
}

Object.defineProperty(NetworkPool.prototype, 'networks', {
get: function () { return this.params.networks.sort(); }
get: function () { return this.params._netobjs; }
});

Object.defineProperty(NetworkPool.prototype, 'family', {
Expand Down Expand Up @@ -459,8 +456,14 @@ NetworkPool.prototype.serialize = function poolSerialize() {
networks: this.params.networks.sort()
};

if (this.params.hasOwnProperty('nic_tag')) {
ser.nic_tag = this.params.nic_tag;
if (this.params.hasOwnProperty('nic_tags_present')) {
ser.nic_tags_present = this.params.nic_tags_present;

/*
* Set a representative "nic_tag" to provide backwards
* compatibility with older NAPI consumers:
*/
ser.nic_tag = this.params.nic_tags_present[0];
}

if (this.params.owner_uuids) {
Expand Down Expand Up @@ -546,7 +549,7 @@ function getNetworkPool(app, log, params, callback) {
return;
}

rec.value._networks = res;
rec.value._netobjs = res;
callback(null, new NetworkPool(rec.value));
});
});
Expand Down Expand Up @@ -625,7 +628,7 @@ function listNetworkPools(app, log, oparams, callback) {

getAllNetworks(app, log, nets, function (err, res) {
if (res) {
val._networks = res;
val._netobjs = res;
}

cb(err);
Expand Down Expand Up @@ -687,7 +690,7 @@ function updateNetworkPool(app, log, params, callback) {
}

var updatedParams = oldPool.raw();
updatedParams._networks = oldPool.params._networks;
updatedParams._netobjs = oldPool.params._netobjs;

for (var p in validatedParams) {
updatedParams[p] = validatedParams[p];
Expand Down
30 changes: 30 additions & 0 deletions lib/models/network.js
Expand Up @@ -1138,6 +1138,10 @@ Object.defineProperty(Network.prototype, 'family', {
get: function () { return this.params.subnet_type; }
});

Object.defineProperty(Network.prototype, 'mtu', {
get: function () { return this.params.mtu; }
});

Object.defineProperty(Network.prototype, 'uuid', {
get: function () { return this.params.uuid; }
});
Expand All @@ -1146,6 +1150,10 @@ Object.defineProperty(Network.prototype, 'vnet_id', {
get: function () { return this.params.vnet_id; }
});

Object.defineProperty(Network.prototype, 'vlan_id', {
get: function () { return this.params.vlan_id; }
});


/**
* Raw form suitable for adding to a moray batch
Expand Down Expand Up @@ -1349,6 +1357,28 @@ Network.prototype.isOwner = function networkHasOwner(owner) {
};


/**
* Check whether the properties of this network match under the given
* intersection. Fields that get checked are:
*
* - "mtu"
* - "nic_tag"
* - "vlan_id"
* - "vnet_id"
*
* See lib/util/intersect.js for more information.
*/
Network.prototype.matches = function matchesIntersection(intersection) {
for (var prop in intersection) {
if (this[prop] !== intersection[prop]) {
return false;
}
}

return true;
};



// --- Exported functions

Expand Down
12 changes: 12 additions & 0 deletions lib/models/nic/common.js
Expand Up @@ -23,6 +23,7 @@ var mod_nicTag = require('../nic-tag');
var mod_pool = require('../network-pool');
var mod_portolan_moray = require('portolan-moray');
var util = require('util');
var util_intersect = require('../../util/intersect');
var validate = require('../../util/validate');


Expand All @@ -32,6 +33,7 @@ var BUCKET = require('./bucket').BUCKET;
var BELONGS_TO_TYPES = [ 'other', 'server', 'zone' ];
var VALID_NIC_STATES = [ 'provisioning', 'stopped', 'running' ];

var getPoolIntersections = util_intersect.getPoolIntersections;

// --- Internal helpers

Expand Down Expand Up @@ -304,6 +306,16 @@ function validateNetworkParams(opts, _, parsedParams, callback) {
}
}

if (parsedParams.network_pool) {
try {
parsedParams.intersections = getPoolIntersections('network_uuid',
parsedParams, [ parsedParams.network_pool ]);
} catch (e) {
callback(e);
return;
}
}

if (!parsedParams.ip) {
callback();
return;
Expand Down
1 change: 1 addition & 0 deletions lib/models/nic/create.js
Expand Up @@ -46,6 +46,7 @@ var CREATE_SCHEMA = {
model: validate.string,
network_uuid: common.validateIPv4Network,
nic_tag: common.validateNicTag,
nic_tags_available: mod_nicTag.validateExists.bind(null, false),
nic_tags_provided: mod_nicTag.validateExists.bind(null, false),
primary: validate.bool,
reserved: validate.bool,
Expand Down

0 comments on commit 6d628d7

Please sign in to comment.