/* jshint latedef:false */
/* jshint forin:false */
/* jshint noempty:false */

// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 

// Warning: This code was generated by a tool.
// 
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

'use strict';

var util = require('util');
var azureCommon = require('azure-common');
var xml = azureCommon.xml2js;
var js2xml = azureCommon.js2xml;
var Service = azureCommon.Service;
var WebResource = azureCommon.WebResource;

var HDInsightCluster2ManagementClient = ( /** @lends HDInsightCluster2ManagementClient */ function() {
  /**
   * @class
   * Initializes a new instance of the HDInsightCluster2ManagementClient class.
   * @constructor
   * 
   * @param {string} cloudServiceName
   * 
   * @param {SubscriptionCloudCredentials} credentials Gets subscription
   * credentials which uniquely identify Microsoft Azure subscription. The
   * subscription ID forms part of the URI for every service call.
   * 
   * @param {string} [credentials.subscriptionId]
   * 
   * @param {string} [baseUri] Gets the URI used as the base for all cloud
   * service requests.
   * 
   * @param {Array} filters
   */
  function HDInsightCluster2ManagementClient(credentials, baseUri, filters) {
    if (credentials === null || credentials === undefined) {
      throw new Error('credentials cannot be null.');
    }
    
    HDInsightCluster2ManagementClient['super_'].call(this, credentials, filters);
    
    this.credentials = credentials;
    this.baseUri = baseUri;
    if (this.baseUri === null || this.baseUri === undefined) {
      this.baseUri = 'https://management.core.windows.net/';
    }
    if (this.apiVersion === null || this.apiVersion === undefined) {
      this.apiVersion = '2014-10-01';
    }
    if (this.longRunningOperationInitialTimeout === null || this.longRunningOperationInitialTimeout === undefined) {
      this.longRunningOperationInitialTimeout = -1;
    }
    if (this.longRunningOperationRetryTimeout === null || this.longRunningOperationRetryTimeout === undefined) {
      this.longRunningOperationRetryTimeout = -1;
    }
    /**
     * Provides an instance of the [ClusterOperations](-ClusterOperations.html)
     * object.
     * @type {object}
     */
    this.clusterManagement = new ClusterOperations(this);
  }
  
  util.inherits(HDInsightCluster2ManagementClient, Service);
  
  /**
   * The Get Operation Status operation returns the status of the specified
   * operation. After calling an asynchronous operation, you can call Get
   * Operation Status to determine whether the operation has succeeded,
   * failed, or is still in progress.
   * 
   * @param {string} requestId The request ID for the request you wish to
   * track. The request ID is returned in the x-ms-request-id response header
   * for every request.
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  HDInsightCluster2ManagementClient.prototype.getOperationStatus = function(requestId, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    if (requestId === null || requestId === undefined) {
      return callback(new Error('requestId cannot be null.'));
    }
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/';
    if (this.credentials.subscriptionId !== null && this.credentials.subscriptionId !== undefined) {
      url2 = url2 + encodeURIComponent(this.credentials.subscriptionId);
    }
    url2 = url2 + '/operations/';
    url2 = url2 + encodeURIComponent(requestId);
    var baseUrl = this.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'GET';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['x-ms-version'] = '2012-08-01';
    
    // Send Request
    return this.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      if (statusCode === 200) {
        var responseContent = body;
        result = {};
        var options = {};
        options.trim = false;
        options.strict = false;
        xml.parseString(responseContent, options, function (err2, responseDoc) {
          if (err2 !== null && err2 !== undefined) {
            return callback(err2);
          }
          
          var operationElement = js2xml.getElement(responseDoc, responseDoc, 'OPERATION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
          if (operationElement !== null && operationElement !== undefined) {
            var idElement = js2xml.getElement(responseDoc, operationElement, 'ID', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
            if (idElement !== null && idElement !== undefined) {
              var idInstance = idElement;
              result.id = idInstance;
            }
            
            var statusElement = js2xml.getElement(responseDoc, operationElement, 'STATUS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
            if (statusElement !== null && statusElement !== undefined && statusElement.length !== 0) {
              var statusInstance = statusElement;
              result.status = statusInstance;
            }
            
            var httpStatusCodeElement = js2xml.getElement(responseDoc, operationElement, 'HTTPSTATUSCODE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
            if (httpStatusCodeElement !== null && httpStatusCodeElement !== undefined && httpStatusCodeElement.length !== 0) {
              var httpStatusCodeInstance = httpStatusCodeElement;
              result.httpStatusCode = httpStatusCodeInstance;
            }
            
            var errorElement = js2xml.getElement(responseDoc, operationElement, 'ERROR', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
            if (errorElement !== null && errorElement !== undefined) {
              var errorInstance = {};
              result.error = errorInstance;
              
              var codeElement = js2xml.getElement(responseDoc, errorElement, 'CODE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (codeElement !== null && codeElement !== undefined) {
                var codeInstance = codeElement;
                errorInstance.code = codeInstance;
              }
              
              var messageElement = js2xml.getElement(responseDoc, errorElement, 'MESSAGE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (messageElement !== null && messageElement !== undefined) {
                var messageInstance = messageElement;
                errorInstance.message = messageInstance;
              }
            }
          }
          
        });
      }
      result.statusCode = statusCode;
      result.requestId = response.headers['x-ms-request-id'];
      
      return callback(null, result);
    });
  };
  
  return HDInsightCluster2ManagementClient;
})();
exports.HDInsightCluster2ManagementClient = HDInsightCluster2ManagementClient;

var ClusterOperations = ( /** @lends ClusterOperations */ function() {
  /**
   * @class
   * Operations for managing HDInsight clusters.
   * __NOTE__: An instance of this class is automatically created for an
   * instance of the [HDInsightCluster2ManagementClient] {@link
   * HDInsightCluster2ManagementClient~HDInsightCluster2ManagementClient}.
   * See [clusterManagement] {@link
   * HDInsightCluster2ManagementClient~HDInsightCluster2ManagementClient#clusterManagement}.
   * Initializes a new instance of the ClusterOperations class.
   * @constructor
   * 
   * @param {HDInsightCluster2ManagementClient} client Reference to the service
   * client.
   */
  function ClusterOperations(client) {
    this.client = client;
  }
  
  /**
   * Creates an HDInsight cluster.
   * 
   * @param {string} clusterName Cluster name to create.
   * 
   * @param {ClusterCreationParameters} parameters Parameters supplied to the
   * Create Cluster operation.
   * 
   * @param {string} parameters.payload
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.beginCreating = function(clusterName, parameters, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    if (clusterName === null || clusterName === undefined) {
      return callback(new Error('clusterName cannot be null.'));
    }
    if (parameters === null || parameters === undefined) {
      return callback(new Error('parameters cannot be null.'));
    }
    if (parameters.payload === null || parameters.payload === undefined) {
      return callback(new Error('parameters.payload cannot be null.'));
    }
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/';
    if (this.client.credentials.subscriptionId !== null && this.client.credentials.subscriptionId !== undefined) {
      url2 = url2 + encodeURIComponent(this.client.credentials.subscriptionId);
    }
    url2 = url2 + '/cloudservices/';
    url2 = url2 + encodeURIComponent(this.client.cloudServiceName);
    url2 = url2 + '/resources/HDInsight/iaasclusters/';
    url2 = url2 + encodeURIComponent(clusterName);
    var baseUrl = this.client.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'PUT';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['Content-Type'] = 'application/xml';
    httpRequest.headers['x-ms-version'] = '2012-08-01';
    
    // Serialize Request
    var requestContent = parameters.payload;
    httpRequest.body = requestContent;
    httpRequest.headers['Content-Length'] = Buffer.isBuffer(requestContent) ? requestContent.length : Buffer.byteLength(requestContent, 'UTF8');
    // Send Request
    return this.client.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200 && statusCode !== 202) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      result = {};
      result.statusCode = statusCode;
      result.requestId = response.headers['x-ms-request-id'];
      
      return callback(null, result);
    });
  };
  
  /**
   * Deletes an HDInsight cluster.
   * 
   * @param {string} clusterName Parameters supplied to the Delete Cluster
   * operation.
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.beginDeleting = function(clusterName, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    if (clusterName === null || clusterName === undefined) {
      return callback(new Error('clusterName cannot be null.'));
    }
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/';
    if (this.client.credentials.subscriptionId !== null && this.client.credentials.subscriptionId !== undefined) {
      url2 = url2 + encodeURIComponent(this.client.credentials.subscriptionId.toString());
    }
    url2 = url2 + '/cloudservices/';
    url2 = url2 + encodeURIComponent(this.client.cloudServiceName);
    url2 = url2 + '/resources/hdinsight/iaasclusters/';
    url2 = url2 + encodeURIComponent(clusterName);
    var baseUrl = this.client.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'DELETE';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['schema-version'] = '1.0';
    httpRequest.headers['x-ms-version'] = '2012-08-01';
    
    // Send Request
    return this.client.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200 && statusCode !== 202) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      result = {};
      result.statusCode = statusCode;
      result.requestId = response.headers['x-ms-request-id'];
      
      return callback(null, result);
    });
  };
  
  /**
   * Creates an HDInsight cluster.
   * 
   * @param {string} clusterName Cluster name to create.
   * 
   * @param {ClusterCreationParameters} parameters Parameters supplied to the
   * Create Cluster operation.
   * 
   * @param {string} parameters.payload
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.create = function(clusterName, parameters, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    var client2 = this.client;
    
    return client2.clusterManagement.beginCreating(clusterName, parameters, function (err, result) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      if (result.status === 'Succeeded') {
        return callback(null, result);
      }
      var delayInSeconds = 30;
      if (client2.longRunningOperationInitialTimeout >= 0) {
        delayInSeconds = client2.longRunningOperationInitialTimeout;
      }
      
      var pollOperation = function () {
        setTimeout(function () {
          return client2.getOperationStatus(result.requestId, function (err2, result2) {
            if (err2 !== null && err2 !== undefined) {
              return callback(err2);
            } else {
              if (result2.status !== 'InProgress') {
                if (result2.status !== 'Succeeded') {
                  if (result2.error !== null && result2.error !== undefined) {
                    var ex = new Error(result2.error.code + ' : ' + result2.error.message);
                    ex.error = new Error();
                    ex.error.code = result2.error.code;
                    ex.error.message = result2.error.message;
                    return callback(ex);
                  } else {
                    var ex = new Error('');
                    return callback(ex);
                  }
                }
                
                return callback(null, result2);
              } else {
                delayInSeconds = 30;
                if (client2.longRunningOperationRetryTimeout >= 0) {
                  delayInSeconds = client2.longRunningOperationRetryTimeout;
                }
                pollOperation();
              }
            }
          });
        }, delayInSeconds * 1000);
      };
      
      pollOperation();
    });
  };
  
  /**
   * Deletes an HDInsight cluster.
   * 
   * @param {string} clusterName Parameters supplied to the Delete Cluster
   * operation.
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.deleteMethod = function(clusterName, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    var client2 = this.client;
    
    return client2.clusterManagement.beginDeleting(clusterName, function (err, result) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      if (result.status === 'Succeeded') {
        return callback(null, result);
      }
      var delayInSeconds = 30;
      if (client2.longRunningOperationInitialTimeout >= 0) {
        delayInSeconds = client2.longRunningOperationInitialTimeout;
      }
      
      var pollOperation = function () {
        setTimeout(function () {
          return client2.getOperationStatus(result.requestId, function (err2, result2) {
            if (err2 !== null && err2 !== undefined) {
              return callback(err2);
            } else {
              if (result2.status !== 'InProgress') {
                if (result2.status !== 'Succeeded') {
                  if (result2.error !== null && result2.error !== undefined) {
                    var ex = new Error(result2.error.code + ' : ' + result2.error.message);
                    ex.error = new Error();
                    ex.error.code = result2.error.code;
                    ex.error.message = result2.error.message;
                    return callback(ex);
                  } else {
                    var ex = new Error('');
                    return callback(ex);
                  }
                }
                
                return callback(null, result2);
              } else {
                delayInSeconds = 30;
                if (client2.longRunningOperationRetryTimeout >= 0) {
                  delayInSeconds = client2.longRunningOperationRetryTimeout;
                }
                pollOperation();
              }
            }
          });
        }, delayInSeconds * 1000);
      };
      
      pollOperation();
    });
  };
  
  /**
   * The Get Clusters operation gets the details of the specified cluster.
   * (see http://msdn.microsoft.com/en-us/library/windowsazure/ee460787.aspx
   * for more information)
   * 
   * @param {string} clusterName Name of the clusters for which details are to
   * be retrieved.
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.get = function(clusterName, callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    if (clusterName === null || clusterName === undefined) {
      return callback(new Error('clusterName cannot be null.'));
    }
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/';
    if (this.client.credentials.subscriptionId !== null && this.client.credentials.subscriptionId !== undefined) {
      url2 = url2 + encodeURIComponent(this.client.credentials.subscriptionId);
    }
    url2 = url2 + '/cloudservices/';
    url2 = url2 + encodeURIComponent(this.client.cloudServiceName);
    url2 = url2 + '/resources/hdinsight/~/iaasclusters/';
    url2 = url2 + encodeURIComponent(clusterName);
    var baseUrl = this.client.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'GET';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['accept'] = 'application/xml';
    httpRequest.headers['x-ms-version'] = '2012-08-01';
    
    // Send Request
    return this.client.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      if (statusCode === 200) {
        var responseContent = body;
        result = {};
        var options = {};
        options.trim = false;
        options.strict = false;
        xml.parseString(responseContent, options, function (err2, responseDoc) {
          if (err2 !== null && err2 !== undefined) {
            return callback(err2);
          }
          
          var passthroughResponseElement = js2xml.getElement(responseDoc, responseDoc, 'PASSTHROUGHRESPONSE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2013/05/MANAGEMENT');
          if (passthroughResponseElement !== null && passthroughResponseElement !== undefined) {
            var passthroughResponseInstance = {};
            result.passthroughResponse = passthroughResponseInstance;
            
            var dataElement = js2xml.getElement(responseDoc, passthroughResponseElement, 'DATA', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2013/05/MANAGEMENT');
            if (dataElement !== null && dataElement !== undefined) {
              var dataInstance = { connectivityEndpoints: [], deploymentDocuments: [] };
              passthroughResponseInstance.data = dataInstance;
              
              var apiVersionElement = js2xml.getElement(responseDoc, dataElement, 'APIVERSION', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (apiVersionElement !== null && apiVersionElement !== undefined && apiVersionElement.length !== 0) {
                var apiVersionInstance = parseFloat(apiVersionElement, 10);
                dataInstance.apiVersion = apiVersionInstance;
              }
              
              var connectivityEndpointsSequenceElement = js2xml.getElement(responseDoc, dataElement, 'CONNECTIVITYENDPOINTS', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (connectivityEndpointsSequenceElement !== null && connectivityEndpointsSequenceElement !== undefined) {
                for (var loweredIndex1 = 0; loweredIndex1 < js2xml.getElements(responseDoc, connectivityEndpointsSequenceElement, 'CONNECTIVITYENDPOINT', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT').length; loweredIndex1 = loweredIndex1 + 1) {
                  var connectivityEndpointsElement = js2xml.getElements(responseDoc, connectivityEndpointsSequenceElement, 'CONNECTIVITYENDPOINT', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT')[loweredIndex1];
                  var connectivityEndpointInstance = {};
                  dataInstance.connectivityEndpoints.push(connectivityEndpointInstance);
                  
                  var locationElement = js2xml.getElement(responseDoc, connectivityEndpointsElement, 'LOCATION', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (locationElement !== null && locationElement !== undefined) {
                    var locationInstance = locationElement;
                    connectivityEndpointInstance.location = locationInstance;
                  }
                  
                  var nameElement = js2xml.getElement(responseDoc, connectivityEndpointsElement, 'NAME', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (nameElement !== null && nameElement !== undefined) {
                    var nameInstance = nameElement;
                    connectivityEndpointInstance.name = nameInstance;
                  }
                  
                  var portElement = js2xml.getElement(responseDoc, connectivityEndpointsElement, 'PORT', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (portElement !== null && portElement !== undefined && portElement.length !== 0) {
                    var portInstance = parseInt(portElement, 10);
                    connectivityEndpointInstance.port = portInstance;
                  }
                  
                  var protocolElement = js2xml.getElement(responseDoc, connectivityEndpointsElement, 'PROTOCOL', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (protocolElement !== null && protocolElement !== undefined) {
                    var protocolInstance = protocolElement;
                    connectivityEndpointInstance.protocol = protocolInstance;
                  }
                  
                  var credentialsElement = js2xml.getElement(responseDoc, connectivityEndpointsElement, 'CREDENTIALS', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (credentialsElement !== null && credentialsElement !== undefined) {
                    var credentialsInstance = {};
                    connectivityEndpointInstance.credentials = credentialsInstance;
                    
                    var webConnectivityCredentialElement = js2xml.getElement(responseDoc, credentialsElement, 'WEBCONNECTIVITYCREDENTIAL', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                    if (webConnectivityCredentialElement !== null && webConnectivityCredentialElement !== undefined) {
                      var webConnectivityCredentialInstance = {};
                      credentialsInstance.webConnectivityCredential = webConnectivityCredentialInstance;
                      
                      var typeElement = js2xml.getElement(responseDoc, webConnectivityCredentialElement, 'TYPE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                      if (typeElement !== null && typeElement !== undefined) {
                        var typeInstance = typeElement;
                        webConnectivityCredentialInstance.type = typeInstance;
                      }
                      
                      var passwordElement = js2xml.getElement(responseDoc, webConnectivityCredentialElement, 'PASSWORD', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                      if (passwordElement !== null && passwordElement !== undefined) {
                        var passwordInstance = passwordElement;
                        webConnectivityCredentialInstance.password = passwordInstance;
                      }
                      
                      var usernameElement = js2xml.getElement(responseDoc, webConnectivityCredentialElement, 'USERNAME', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                      if (usernameElement !== null && usernameElement !== undefined) {
                        var usernameInstance = usernameElement;
                        webConnectivityCredentialInstance.username = usernameInstance;
                      }
                    }
                  }
                }
              }
              
              var createdDateElement = js2xml.getElement(responseDoc, dataElement, 'CREATEDDATE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (createdDateElement !== null && createdDateElement !== undefined && createdDateElement.length !== 0) {
                var createdDateInstance = new Date(createdDateElement);
                dataInstance.createdDate = createdDateInstance;
              }
              
              var deploymentDocumentsSequenceElement = js2xml.getElement(responseDoc, dataElement, 'DEPLOYMENTDOCUMENTS', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (deploymentDocumentsSequenceElement !== null && deploymentDocumentsSequenceElement !== undefined) {
                for (var loweredIndex2 = 0; loweredIndex2 < js2xml.getElements(responseDoc, deploymentDocumentsSequenceElement, 'KEYVALUEOFSTRINGSTRING', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT').length; loweredIndex2 = loweredIndex2 + 1) {
                  var deploymentDocumentsElement = js2xml.getElements(responseDoc, deploymentDocumentsSequenceElement, 'KEYVALUEOFSTRINGSTRING', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT')[loweredIndex2];
                  var keyValueOfstringstringInstance = {};
                  dataInstance.deploymentDocuments.push(keyValueOfstringstringInstance);
                  
                  var keyElement = js2xml.getElement(responseDoc, deploymentDocumentsElement, 'KEY', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (keyElement !== null && keyElement !== undefined) {
                    var keyInstance = keyElement;
                    keyValueOfstringstringInstance.key = keyInstance;
                  }
                  
                  var valueElement = js2xml.getElement(responseDoc, deploymentDocumentsElement, 'VALUE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (valueElement !== null && valueElement !== undefined) {
                    var valueInstance = valueElement;
                    keyValueOfstringstringInstance.value = valueInstance;
                  }
                }
              }
              
              var hdiVersionElement = js2xml.getElement(responseDoc, dataElement, 'HDIVERSION', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (hdiVersionElement !== null && hdiVersionElement !== undefined) {
                var hdiVersionInstance = hdiVersionElement;
                dataInstance.hdiVersion = hdiVersionInstance;
              }
              
              var idElement = js2xml.getElement(responseDoc, dataElement, 'ID', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (idElement !== null && idElement !== undefined) {
                var idInstance = idElement;
                dataInstance.id = idInstance;
              }
              
              var locationElement2 = js2xml.getElement(responseDoc, dataElement, 'LOCATION', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (locationElement2 !== null && locationElement2 !== undefined) {
                var locationInstance2 = locationElement2;
                dataInstance.location = locationInstance2;
              }
              
              var stateElement = js2xml.getElement(responseDoc, dataElement, 'STATE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (stateElement !== null && stateElement !== undefined) {
                var stateInstance = stateElement;
                dataInstance.state = stateInstance;
              }
              
              var tenantIdElement = js2xml.getElement(responseDoc, dataElement, 'TENANTID', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (tenantIdElement !== null && tenantIdElement !== undefined && tenantIdElement.length !== 0) {
                var tenantIdInstance = parseInt(tenantIdElement, 10);
                dataInstance.tenantId = tenantIdInstance;
              }
              
              var userSubscriptionIdElement = js2xml.getElement(responseDoc, dataElement, 'USERSUBSCRIPTIONID', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (userSubscriptionIdElement !== null && userSubscriptionIdElement !== undefined) {
                var userSubscriptionIdInstance = userSubscriptionIdElement;
                dataInstance.userSubscriptionId = userSubscriptionIdInstance;
              }
              
              var userTagsElement = js2xml.getElement(responseDoc, dataElement, 'USERTAGS', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
              if (userTagsElement !== null && userTagsElement !== undefined) {
                var userTagsInstance = {};
                dataInstance.userTags = userTagsInstance;
                
                var keyValueOfstringstringElement = js2xml.getElement(responseDoc, userTagsElement, 'KEYVALUEOFSTRINGSTRING', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                if (keyValueOfstringstringElement !== null && keyValueOfstringstringElement !== undefined) {
                  var keyValueOfstringstringInstance2 = {};
                  userTagsInstance.keyValueOfstringstring = keyValueOfstringstringInstance2;
                  
                  var keyElement2 = js2xml.getElement(responseDoc, keyValueOfstringstringElement, 'KEY', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (keyElement2 !== null && keyElement2 !== undefined) {
                    var keyInstance2 = keyElement2;
                    keyValueOfstringstringInstance2.key = keyInstance2;
                  }
                  
                  var valueElement2 = js2xml.getElement(responseDoc, keyValueOfstringstringElement, 'VALUE', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2014/05/MANAGEMENT');
                  if (valueElement2 !== null && valueElement2 !== undefined) {
                    var valueInstance2 = valueElement2;
                    keyValueOfstringstringInstance2.value = valueInstance2;
                  }
                }
              }
            }
            
            var errorElement = js2xml.getElement(responseDoc, passthroughResponseElement, 'ERROR', 'HTTP://SCHEMAS.MICROSOFT.COM/HDINSIGHT/2013/05/MANAGEMENT');
            if (errorElement !== null && errorElement !== undefined) {
              var errorInstance = errorElement;
              passthroughResponseInstance.error = errorInstance;
            }
          }
          
        });
      }
      result.statusCode = statusCode;
      
      return callback(null, result);
    });
  };
  
  /**
   * The List Clusters operation lists the storage accounts available under the
   * current subscription.  (see
   * http://msdn.microsoft.com/en-us/library/windowsazure/ee460787.aspx for
   * more information)
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  ClusterOperations.prototype.list = function(callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/';
    if (this.client.credentials.subscriptionId !== null && this.client.credentials.subscriptionId !== undefined) {
      url2 = url2 + encodeURIComponent(this.client.credentials.subscriptionId);
    }
    url2 = url2 + '/cloudservices';
    var baseUrl = this.client.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'GET';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['accept'] = 'application/xml';
    httpRequest.headers['x-ms-version'] = '2012-08-01';
    
    // Send Request
    return this.client.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      if (statusCode === 200) {
        var responseContent = body;
        result = { cloudService: [] };
        var options = {};
        options.trim = false;
        options.strict = false;
        xml.parseString(responseContent, options, function (err2, responseDoc) {
          if (err2 !== null && err2 !== undefined) {
            return callback(err2);
          }
          
          var cloudServicesSequenceElement = js2xml.getElement(responseDoc, responseDoc, 'CLOUDSERVICES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
          if (cloudServicesSequenceElement !== null && cloudServicesSequenceElement !== undefined) {
            for (var loweredIndex1 = 0; loweredIndex1 < js2xml.getElements(responseDoc, cloudServicesSequenceElement, 'CLOUDSERVICE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE').length; loweredIndex1 = loweredIndex1 + 1) {
              var cloudServicesElement = js2xml.getElements(responseDoc, cloudServicesSequenceElement, 'CLOUDSERVICE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE')[loweredIndex1];
              var cloudServiceInstance = { resources: [] };
              result.cloudService.push(cloudServiceInstance);
              
              var nameElement = js2xml.getElement(responseDoc, cloudServicesElement, 'NAME', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (nameElement !== null && nameElement !== undefined) {
                var nameInstance = nameElement;
                cloudServiceInstance.name = nameInstance;
              }
              
              var labelElement = js2xml.getElement(responseDoc, cloudServicesElement, 'LABEL', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (labelElement !== null && labelElement !== undefined) {
                var labelInstance = labelElement;
                cloudServiceInstance.label = labelInstance;
              }
              
              var descriptionElement = js2xml.getElement(responseDoc, cloudServicesElement, 'DESCRIPTION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (descriptionElement !== null && descriptionElement !== undefined) {
                var descriptionInstance = descriptionElement;
                cloudServiceInstance.description = descriptionInstance;
              }
              
              var geoRegionElement = js2xml.getElement(responseDoc, cloudServicesElement, 'GEOREGION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (geoRegionElement !== null && geoRegionElement !== undefined) {
                var geoRegionInstance = geoRegionElement;
                cloudServiceInstance.geoRegion = geoRegionInstance;
              }
              
              var resourcesSequenceElement = js2xml.getElement(responseDoc, cloudServicesElement, 'RESOURCES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
              if (resourcesSequenceElement !== null && resourcesSequenceElement !== undefined) {
                for (var loweredIndex2 = 0; loweredIndex2 < js2xml.getElements(responseDoc, resourcesSequenceElement, 'RESOURCE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE').length; loweredIndex2 = loweredIndex2 + 1) {
                  var resourcesElement = js2xml.getElements(responseDoc, resourcesSequenceElement, 'RESOURCE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE')[loweredIndex2];
                  var resourceInstance = { outputItems: [] };
                  cloudServiceInstance.resources.push(resourceInstance);
                  
                  var resourceProviderNamespaceElement = js2xml.getElement(responseDoc, resourcesElement, 'RESOURCEPROVIDERNAMESPACE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (resourceProviderNamespaceElement !== null && resourceProviderNamespaceElement !== undefined) {
                    var resourceProviderNamespaceInstance = resourceProviderNamespaceElement;
                    resourceInstance.resourceProviderNamespace = resourceProviderNamespaceInstance;
                  }
                  
                  var typeElement = js2xml.getElement(responseDoc, resourcesElement, 'TYPE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (typeElement !== null && typeElement !== undefined) {
                    var typeInstance = typeElement;
                    resourceInstance.type = typeInstance;
                  }
                  
                  var nameElement2 = js2xml.getElement(responseDoc, resourcesElement, 'NAME', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (nameElement2 !== null && nameElement2 !== undefined) {
                    var nameInstance2 = nameElement2;
                    resourceInstance.name = nameInstance2;
                  }
                  
                  var planElement = js2xml.getElement(responseDoc, resourcesElement, 'PLAN', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (planElement !== null && planElement !== undefined) {
                    var planInstance = planElement;
                    resourceInstance.plan = planInstance;
                  }
                  
                  var schemaVersionElement = js2xml.getElement(responseDoc, resourcesElement, 'SCHEMAVERSION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (schemaVersionElement !== null && schemaVersionElement !== undefined && schemaVersionElement.length !== 0) {
                    var schemaVersionInstance = parseFloat(schemaVersionElement, 10);
                    resourceInstance.schemaVersion = schemaVersionInstance;
                  }
                  
                  var schemaVersionSpecifiedElement = js2xml.getElement(responseDoc, resourcesElement, 'SCHEMAVERSIONSPECIFIED', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (schemaVersionSpecifiedElement !== null && schemaVersionSpecifiedElement !== undefined && schemaVersionSpecifiedElement.length !== 0) {
                    var schemaVersionSpecifiedInstance = schemaVersionSpecifiedElement === 'true';
                    resourceInstance.schemaVersionSpecified = schemaVersionSpecifiedInstance;
                  }
                  
                  var eTagElement = js2xml.getElement(responseDoc, resourcesElement, 'ETAG', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (eTagElement !== null && eTagElement !== undefined) {
                    var eTagInstance = eTagElement;
                    resourceInstance.eTag = eTagInstance;
                  }
                  
                  var stateElement = js2xml.getElement(responseDoc, resourcesElement, 'STATE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (stateElement !== null && stateElement !== undefined) {
                    var stateInstance = stateElement;
                    resourceInstance.state = stateInstance;
                  }
                  
                  var subStateElement = js2xml.getElement(responseDoc, resourcesElement, 'SUBSTATE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (subStateElement !== null && subStateElement !== undefined) {
                    var subStateInstance = subStateElement;
                    resourceInstance.subState = subStateInstance;
                  }
                  
                  var outputItemsSequenceElement = js2xml.getElement(responseDoc, resourcesElement, 'OUTPUTITEMS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (outputItemsSequenceElement !== null && outputItemsSequenceElement !== undefined) {
                    for (var loweredIndex3 = 0; loweredIndex3 < js2xml.getElements(responseDoc, outputItemsSequenceElement, 'OUTPUTITEM', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE').length; loweredIndex3 = loweredIndex3 + 1) {
                      var outputItemsElement = js2xml.getElements(responseDoc, outputItemsSequenceElement, 'OUTPUTITEM', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE')[loweredIndex3];
                      var outputItemInstance = {};
                      resourceInstance.outputItems.push(outputItemInstance);
                      
                      var keyElement = js2xml.getElement(responseDoc, outputItemsElement, 'KEY', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                      if (keyElement !== null && keyElement !== undefined) {
                        var keyInstance = keyElement;
                        outputItemInstance.key = keyInstance;
                      }
                      
                      var valueElement = js2xml.getElement(responseDoc, outputItemsElement, 'VALUE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                      if (valueElement !== null && valueElement !== undefined) {
                        var valueInstance = valueElement;
                        outputItemInstance.value = valueInstance;
                      }
                    }
                  }
                  
                  var operationStatusElement = js2xml.getElement(responseDoc, resourcesElement, 'OPERATIONSTATUS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (operationStatusElement !== null && operationStatusElement !== undefined) {
                    var operationStatusInstance = {};
                    resourceInstance.operationStatus = operationStatusInstance;
                    
                    var typeElement2 = js2xml.getElement(responseDoc, operationStatusElement, 'TYPE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                    if (typeElement2 !== null && typeElement2 !== undefined) {
                      var typeInstance2 = typeElement2;
                      operationStatusInstance.type = typeInstance2;
                    }
                    
                    var resultElement = js2xml.getElement(responseDoc, operationStatusElement, 'RESULT', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                    if (resultElement !== null && resultElement !== undefined) {
                      var resultInstance = resultElement;
                      operationStatusInstance.result = resultInstance;
                    }
                    
                    var errorElement = js2xml.getElement(responseDoc, operationStatusElement, 'ERROR', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                    if (errorElement !== null && errorElement !== undefined) {
                      var errorInstance = {};
                      operationStatusInstance.error = errorInstance;
                      
                      var httpCodeElement = js2xml.getElement(responseDoc, errorElement, 'HTTPCODE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                      if (httpCodeElement !== null && httpCodeElement !== undefined && httpCodeElement.length !== 0) {
                        var httpCodeInstance = parseInt(httpCodeElement, 10);
                        errorInstance.httpCode = httpCodeInstance;
                      }
                      
                      var messageElement = js2xml.getElement(responseDoc, errorElement, 'MESSAGE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                      if (messageElement !== null && messageElement !== undefined) {
                        var messageInstance = messageElement;
                        errorInstance.message = messageInstance;
                      }
                      
                      var extendedCodeElement = js2xml.getElement(responseDoc, errorElement, 'EXTENDEDCODE', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                      if (extendedCodeElement !== null && extendedCodeElement !== undefined) {
                        var extendedCodeInstance = extendedCodeElement;
                        errorInstance.extendedCode = extendedCodeInstance;
                      }
                    }
                  }
                  
                  var labelElement2 = js2xml.getElement(responseDoc, resourcesElement, 'LABEL', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                  if (labelElement2 !== null && labelElement2 !== undefined) {
                    var labelInstance2 = labelElement2;
                    resourceInstance.label = labelInstance2;
                  }
                }
              }
            }
          }
          
        });
      }
      result.statusCode = statusCode;
      
      return callback(null, result);
    });
  };
  
  return ClusterOperations;
})();
