|
| 1 | +/*! |
| 2 | + * Copyright 2014 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/*! |
| 18 | + * @module dns |
| 19 | + */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +var extend = require('extend'); |
| 24 | + |
| 25 | +/** |
| 26 | + * @type {module:common/streamrouter} |
| 27 | + * @private |
| 28 | + */ |
| 29 | +var streamRouter = require('../common/stream-router.js'); |
| 30 | + |
| 31 | +/** |
| 32 | + * @type {module:common/util} |
| 33 | + * @private |
| 34 | + */ |
| 35 | +var util = require('../common/util.js'); |
| 36 | + |
| 37 | +/** |
| 38 | + * @type {module:dns/zone} |
| 39 | + * @private |
| 40 | + */ |
| 41 | +var Zone = require('./zone.js'); |
| 42 | + |
| 43 | +/** |
| 44 | + * @const {string} Base URL for DNS API. |
| 45 | + * @private |
| 46 | + */ |
| 47 | +var DNS_BASE_URL = 'https://www.googleapis.com/dns/v1/projects/'; |
| 48 | + |
| 49 | +/** |
| 50 | + * @const {array} Required scopes for the DNS API. |
| 51 | + * @private |
| 52 | + */ |
| 53 | +var SCOPES = [ |
| 54 | + 'https://www.googleapis.com/auth/ndev.clouddns.readwrite', |
| 55 | + 'https://www.googleapis.com/auth/cloud-platform' |
| 56 | +]; |
| 57 | + |
| 58 | +/** |
| 59 | + * [Google Cloud DNS](https://cloud.google.com/dns) is a reliable, resilient, |
| 60 | + * low-latency DNS serving from Google’s worldwide network of Anycast DNS |
| 61 | + * servers. |
| 62 | + * |
| 63 | + * @constructor |
| 64 | + * @alias module:dns |
| 65 | + * |
| 66 | + * @param {object} options - [Configuration object](#/docs/?method=gcloud). |
| 67 | + * |
| 68 | + * @example |
| 69 | + * var gcloud = require('gcloud')({ |
| 70 | + * keyFilename: '/path/to/keyfile.json', |
| 71 | + * projectId: 'grape-spaceship-123' |
| 72 | + * }); |
| 73 | + * |
| 74 | + * var dns = gcloud.dns(); |
| 75 | + */ |
| 76 | +function DNS(options) { |
| 77 | + if (!(this instanceof DNS)) { |
| 78 | + return new DNS(options); |
| 79 | + } |
| 80 | + |
| 81 | + options = options || {}; |
| 82 | + |
| 83 | + if (!options.projectId) { |
| 84 | + throw util.missingProjectIdError; |
| 85 | + } |
| 86 | + |
| 87 | + this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({ |
| 88 | + credentials: options.credentials, |
| 89 | + keyFile: options.keyFilename, |
| 90 | + scopes: SCOPES, |
| 91 | + email: options.email |
| 92 | + }); |
| 93 | + |
| 94 | + this.projectId_ = options.projectId; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Create a managed zone. |
| 99 | + * |
| 100 | + * @throws {error} If a zone name is not provided. |
| 101 | + * @throws {error} If a zone dnsName is not provided. |
| 102 | + * |
| 103 | + * @param {string} name - Unique name for the zone. E.g. "my-zone" |
| 104 | + * @param {object} config - Configuration object. |
| 105 | + * @param {string} config.dnsName - DNS name for the zone. E.g. "example.com." |
| 106 | + * @param {string=} config.description - Description text for the zone. |
| 107 | + * @param {function} callback - The callback function. |
| 108 | + * @param {?error} callback.err - An API error. |
| 109 | + * @param {?module:dns/zone} callback.zone - A new {module:dns/zone} object. |
| 110 | + * @param {object} callback.apiResponse - Raw API response. |
| 111 | + * |
| 112 | + * @example |
| 113 | + * dns.createZone('my-awesome-zone', { |
| 114 | + * dnsName: 'example.com.', // note the period at the end of the domain. |
| 115 | + * description: 'This zone is awesome!' |
| 116 | + * }, function(err, zone, apiResponse) { |
| 117 | + * if (!err) { |
| 118 | + * // The zone was created successfully. |
| 119 | + * } |
| 120 | + * }); |
| 121 | + */ |
| 122 | +DNS.prototype.createZone = function(name, config, callback) { |
| 123 | + var self = this; |
| 124 | + |
| 125 | + if (!name) { |
| 126 | + throw new Error('A zone name is required.'); |
| 127 | + } |
| 128 | + |
| 129 | + if (!config || !config.dnsName) { |
| 130 | + throw new Error('A zone dnsName is required.'); |
| 131 | + } |
| 132 | + |
| 133 | + config.name = name; |
| 134 | + |
| 135 | + // Required by the API. |
| 136 | + config.description = config.description || ''; |
| 137 | + |
| 138 | + this.makeReq_('POST', '/managedZones', null, config, function(err, resp) { |
| 139 | + if (err) { |
| 140 | + callback(err, null, resp); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + var zone = self.zone(resp.name); |
| 145 | + zone.metadata = resp; |
| 146 | + |
| 147 | + callback(null, zone, resp); |
| 148 | + }); |
| 149 | +}; |
| 150 | + |
| 151 | +/** |
| 152 | + * Gets a list of managed zones for the project. |
| 153 | + * |
| 154 | + * @param {object=} query - Query object. |
| 155 | + * @param {number} query.maxResults - Maximum number of results to return. |
| 156 | + * @param {string} query.pageToken - Page token. |
| 157 | + * @param {function} callback - The callback function. |
| 158 | + * @param {?error} callback.err - An API error. |
| 159 | + * @param {?module:dns/zone[]} callback.zones - An array of {module:dns/zone} |
| 160 | + * objects. |
| 161 | + * @param {object} callback.apiResponse - Raw API response. |
| 162 | + * |
| 163 | + * @example |
| 164 | + * dns.getZones(function(err, zones, apiResponse) {}); |
| 165 | + */ |
| 166 | +DNS.prototype.getZones = function(query, callback) { |
| 167 | + var self = this; |
| 168 | + |
| 169 | + if (util.is(query, 'function')) { |
| 170 | + callback = query; |
| 171 | + query = {}; |
| 172 | + } |
| 173 | + |
| 174 | + this.makeReq_('GET', '/managedZones', query, null, function(err, resp) { |
| 175 | + if (err) { |
| 176 | + callback(err, null, null, resp); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + var zones = (resp.managedZones || []).map(function(zone) { |
| 181 | + var zoneInstance = self.zone(zone.name); |
| 182 | + zoneInstance.metadata = zone; |
| 183 | + return zoneInstance; |
| 184 | + }); |
| 185 | + |
| 186 | + var nextQuery = null; |
| 187 | + |
| 188 | + if (resp.nextPageToken) { |
| 189 | + nextQuery = extend({}, query, { |
| 190 | + pageToken: resp.nextPageToken |
| 191 | + }); |
| 192 | + } |
| 193 | + |
| 194 | + callback(null, zones, nextQuery, resp); |
| 195 | + }); |
| 196 | +}; |
| 197 | + |
| 198 | +/** |
| 199 | + * Create a zone object representing an existing managed zone. |
| 200 | + * |
| 201 | + * @throws {error} If a zone name is not provided. |
| 202 | + * |
| 203 | + * @param {string} name - The unique name of the zone. |
| 204 | + * @return {module:dns/zone} |
| 205 | + * |
| 206 | + * @example |
| 207 | + * var zone = dns.zone('my-zone'); |
| 208 | + */ |
| 209 | +DNS.prototype.zone = function(name) { |
| 210 | + if (!name) { |
| 211 | + throw new Error('A zone name is required.'); |
| 212 | + } |
| 213 | + |
| 214 | + return new Zone(this, name); |
| 215 | +}; |
| 216 | + |
| 217 | +/** |
| 218 | + * Make a new request object from the provided arguments and wrap the callback |
| 219 | + * to intercept non-successful responses. |
| 220 | + * |
| 221 | + * @private |
| 222 | + * |
| 223 | + * @param {string} method - Action. |
| 224 | + * @param {string} path - Request path. |
| 225 | + * @param {*} query - Request query object. |
| 226 | + * @param {*} body - Request body contents. |
| 227 | + * @param {function} callback - The callback function. |
| 228 | + */ |
| 229 | +DNS.prototype.makeReq_ = function(method, path, query, body, callback) { |
| 230 | + var reqOpts = { |
| 231 | + method: method, |
| 232 | + qs: query, |
| 233 | + uri: DNS_BASE_URL + this.projectId_ + path |
| 234 | + }; |
| 235 | + |
| 236 | + if (body) { |
| 237 | + reqOpts.json = body; |
| 238 | + } |
| 239 | + |
| 240 | + this.makeAuthorizedRequest_(reqOpts, callback); |
| 241 | +}; |
| 242 | + |
| 243 | +/*! Developer Documentation |
| 244 | + * |
| 245 | + * These methods can be used with either a callback or as a readable object |
| 246 | + * stream. `streamRouter` is used to add this dual behavior. |
| 247 | + */ |
| 248 | +streamRouter.extend(DNS, 'getZones'); |
| 249 | + |
| 250 | +module.exports = DNS; |
0 commit comments