Skip to content

Commit

Permalink
PUBAPI-1298 want cloudapi to set dns_domain based on CNS
Browse files Browse the repository at this point in the history
Reviewed by: Trent Mick <trent.mick@joyent.com>
  • Loading branch information
arekinath committed Jun 3, 2016
1 parent de96a1d commit 81ff78e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions lib/machines.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,77 @@ function create(req, res, next) {
}
}

/*
* If CNS is enabled for this user and nothing already gave us an
* explicit dns_domain for the new machine, ask CNS for the default
* DNS suffixes we should use.
*
* This lets machines on a CNS-enabled account have their primary DNS
* FQDN as their system hostname by default, and to resolve other machines
* on the account in the same DC by their alias/uuid directly.
*/
if (req.sdc.cns && req.account.triton_cns_enabled === 'true' &&
!opts.dns_domain) {

pipeline.push(function (_, cb) {
var ropts = {};
ropts.headers = {
'x-request-id': req.getId(),
'accept-version': '~1'
};
var netUuids = [];
opts.networks.forEach(function (net) {
if (net.ipv4_uuid) {
netUuids.push(net.ipv4_uuid);
}
if (net.ipv6_uuid) {
netUuids.push(net.ipv6_uuid);
}
if (net.uuid && netUuids.indexOf(net.uuid) === -1) {
netUuids.push(net.uuid);
}
});
req.sdc.cns.getSuffixesForVM(opts.owner_uuid, netUuids, ropts,
function (err, obj) {

if (err && (err.name === 'NotFoundError' ||
err.name === 'ResourceNotFoundError')) {

req.log.warn('failed to retrieve DNS suffixes from ' +
'CNS REST API because the endpoint is not supported' +
' (have you updated CNS?)');
cb();
return;
}

if (err) {
req.log.error(err, 'failed to retrieve DNS suffixes from ' +
'CNS REST API');
var rerr = new InternalError('Unable to contact the ' +
'Triton CNS API');
cb(rerr);
return;
}

/*
* VMAPI can only support a single dns_domain value, no list
* of search domains can be supplied. So, we just do something
* a little nasty here: select the first "instance" suffix and
* use only that.
*/
var sufs = obj.suffixes.filter(function (suf) {
return (suf.indexOf('inst.') === 0);
});
if (sufs.length > 0) {
opts.dns_domain = sufs[0];
/* We've already enforced that the alias is dns-safe. */
opts.hostname = opts.alias;
}
cb();
});
});
}


pipeline.push(function (_, cb) {
return req.sdc.vmapi.createVm(opts, {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"nopt": "2.0.0",
"restify": "git+ssh://git@github.com:mcavage/node-restify.git#0d7b4ba",
"bunyan": "0.22.1",
"sdc-clients": "9.2.0",
"sdc-clients": "9.4.0",
"ufds": "1.1.3",
"semver": "2.2.1",
"nodemailer": "0.3.29",
Expand Down

0 comments on commit 81ff78e

Please sign in to comment.