/*jshint -W069 */
/**
*
* @class cloudDataCenters
* @param {(string|object)} [domainOrOptions] - The project domain or options object. If object, see the object's optional properties.
* @param {string} [domainOrOptions.domain] - The project domain
* @param {object} [domainOrOptions.token] - auth token - object with value property and optional headerOrQueryName and isQuery properties
*/
var cloudDataCenters = (function() {
'use strict';
var request = require('request');
var Q = require('q');
function cloudDataCenters(options) {
var domain = (typeof options === 'object') ? options.domain : options;
this.domain = domain ? domain : 'https://api.godaddy.com';
if (this.domain.length === 0) {
throw new Error('Domain parameter must be specified as a string.');
}
this.token = (typeof options === 'object') ? (options.token ? options.token : {}) : {};
}
/**
* Set Token
* @method
* @name cloudDataCenters#setToken
* @param {string} value - token's value
* @param {string} headerOrQueryName - the header or query name to send the token at
* @param {boolean} isQuery - true if send the token as query param, otherwise, send as header param
*
*/
cloudDataCenters.prototype.setToken = function(value, headerOrQueryName, isQuery) {
this.token.value = value;
this.token.headerOrQueryName = headerOrQueryName;
this.token.isQuery = isQuery;
};
/**
* Find data center by dataCenterId
* @method
* @name cloudDataCenters#getDataCenterById
* @param {string} dataCenterId - Id of data center to be fetched
*
*/
cloudDataCenters.prototype.getDataCenterById = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/cloud/dataCenters/{dataCenterId}';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
path = path.replace('{dataCenterId}', parameters['dataCenterId']);
if (parameters['dataCenterId'] === undefined) {
deferred.reject(new Error('Missing required path parameter: dataCenterId'));
return deferred.promise;
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
/**
* Get a list of data centers
* @method
* @name cloudDataCenters#getDataCenterList
* @param {string} name - Data center name filter (exact match)
* @param {string} region - Region the data center is in (exact match)
* @param {string} status - Data center status filter (exact match)
* @param {integer} limit - Number of results to display
* @param {integer} offset - The starting position of the query
*
*/
cloudDataCenters.prototype.getDataCenterList = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/cloud/dataCenters';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
if (parameters['name'] !== undefined) {
queryParameters['name'] = parameters['name'];
}
if (parameters['region'] !== undefined) {
queryParameters['region'] = parameters['region'];
}
if (parameters['status'] !== undefined) {
queryParameters['status'] = parameters['status'];
}
if (parameters['limit'] !== undefined) {
queryParameters['limit'] = parameters['limit'];
}
if (parameters['offset'] !== undefined) {
queryParameters['offset'] = parameters['offset'];
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
/**
* Get Zone by id
* @method
* @name cloudDataCenters#getZoneById
* @param {string} dataCenterId - Id of parent Data Center
* @param {string} zoneId - Id of Zone to be fetched
*
*/
cloudDataCenters.prototype.getZoneById = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/cloud/dataCenters/{dataCenterId}/zones/{zoneId}';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
path = path.replace('{dataCenterId}', parameters['dataCenterId']);
if (parameters['dataCenterId'] === undefined) {
deferred.reject(new Error('Missing required path parameter: dataCenterId'));
return deferred.promise;
}
path = path.replace('{zoneId}', parameters['zoneId']);
if (parameters['zoneId'] === undefined) {
deferred.reject(new Error('Missing required path parameter: zoneId'));
return deferred.promise;
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
/**
* List of zones in data center
* @method
* @name cloudDataCenters#getDataCenterZoneList
* @param {string} dataCenterId - Retrieve all zones for a given data center
* @param {string} status - Retrieve all zones with this status
* @param {integer} limit - Number of results to display
* @param {integer} offset - The starting position of the query
*
*/
cloudDataCenters.prototype.getDataCenterZoneList = function(parameters) {
if (parameters === undefined) {
parameters = {};
}
var deferred = Q.defer();
var domain = this.domain;
var path = '/v1/cloud/dataCenters/{dataCenterId}/zones';
var body;
var queryParameters = {};
var headers = {};
var form = {};
if (this.token.isQuery) {
queryParameters[this.token.headerOrQueryName] = this.token.value;
} else if (this.token.headerOrQueryName) {
headers[this.token.headerOrQueryName] = this.token.value;
} else {
headers['Authorization'] = 'Bearer ' + this.token.value;
}
path = path.replace('{dataCenterId}', parameters['dataCenterId']);
if (parameters['dataCenterId'] === undefined) {
deferred.reject(new Error('Missing required path parameter: dataCenterId'));
return deferred.promise;
}
if (parameters['status'] !== undefined) {
queryParameters['status'] = parameters['status'];
}
if (parameters['limit'] !== undefined) {
queryParameters['limit'] = parameters['limit'];
}
if (parameters['offset'] !== undefined) {
queryParameters['offset'] = parameters['offset'];
}
if (parameters.$queryParameters) {
Object.keys(parameters.$queryParameters)
.forEach(function(parameterName) {
var parameter = parameters.$queryParameters[parameterName];
queryParameters[parameterName] = parameter;
});
}
var req = {
method: 'GET',
uri: domain + path,
qs: queryParameters,
headers: headers,
body: body
};
if (Object.keys(form).length > 0) {
req.form = form;
}
if (typeof(body) === 'object' && !(body instanceof Buffer)) {
req.json = true;
}
request(req, function(error, response, body) {
if (error) {
deferred.reject(error);
} else {
if (/^application\/(.*\\+)?json/.test(response.headers['content-type'])) {
try {
body = JSON.parse(body);
} catch (e) {
}
}
if (response.statusCode === 204) {
deferred.resolve({
response: response
});
} else if (response.statusCode >= 200 && response.statusCode <= 299) {
deferred.resolve({
response: response,
body: body
});
} else {
deferred.reject({
response: response,
body: body
});
}
}
});
return deferred.promise;
};
return cloudDataCenters;
})();
exports.cloudDataCenters = cloudDataCenters;