text: /*
   * Code generated by Microsoft (R) AutoRest Code Generator 0.16.0.0
   * Changes may cause incorrect behavior and will be lost if the code is
   * regenerated.
   */
  
  'use strict';
  
  var util = require('util');
  var msRest = require('ms-rest');
  var WebResource = msRest.WebResource;
  
  /**
   * @class
   * ServiceGroupFromTemplates
   * __NOTE__: An instance of this class is automatically created for an
   * instance of the ServiceFabricClient.
   * Initializes a new instance of the ServiceGroupFromTemplates class.
   * @constructor
   *
   * @param {ServiceFabricClient} client Reference to the service client.
   */
  function ServiceGroupFromTemplates(client) {
    this.client = client;
  }
  
  /**
   * Create service group from templates
   *
   * @param {string} applicationName The name of the application
   * 
   * @param {object} serviceDescriptionTemplate The template of the service
   * description
   * 
   * @param {string} [serviceDescriptionTemplate.serviceName]
   * 
   * @param {string} [serviceDescriptionTemplate.serviceTypeName]
   * 
   * @param {object} [options] Optional Parameters.
   * 
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   * 
   * @param {function} callback
   *
   * @returns {function} callback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  ServiceGroupFromTemplates.prototype.create = function (applicationName, serviceDescriptionTemplate, options, callback) {
    var client = this.client;
    if(!callback && typeof options === 'function') {
      callback = options;
      options = null;
    }
    if (!callback) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    try {
      if (applicationName === null || applicationName === undefined || typeof applicationName.valueOf() !== 'string') {
        throw new Error('applicationName cannot be null or undefined and it must be of type string.');
      }
      if (serviceDescriptionTemplate === null || serviceDescriptionTemplate === undefined) {
        throw new Error('serviceDescriptionTemplate cannot be null or undefined.');
      }
      if (this.client.apiVersion === null || this.client.apiVersion === undefined || typeof this.client.apiVersion.valueOf() !== 'string') {
        throw new Error('this.client.apiVersion cannot be null or undefined and it must be of type string.');
      }
    } catch (error) {
      return callback(error);
    }
  
    // Construct URL
    var requestUrl = this.client.baseUri +
                     '//Applications/{applicationName}/$/GetServiceGroups/$/CreateServiceGroupFromTemplate';
    requestUrl = requestUrl.replace('{applicationName}', applicationName);
    var queryParameters = [];
    queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
    if (queryParameters.length > 0) {
      requestUrl += '?' + queryParameters.join('&');
    }
    // trim all duplicate forward slashes in the url
    var regex = /([^:]\/)\/+/gi;
    requestUrl = requestUrl.replace(regex, '$1');
  
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'POST';
    httpRequest.headers = {};
    httpRequest.url = requestUrl;
    // Set Headers
    if(options) {
      for(var headerName in options['customHeaders']) {
        if (options['customHeaders'].hasOwnProperty(headerName)) {
          httpRequest.headers[headerName] = options['customHeaders'][headerName];
        }
      }
    }
    httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
    // Serialize Request
    var requestContent = null;
    var requestModel = null;
    try {
      if (serviceDescriptionTemplate !== null && serviceDescriptionTemplate !== undefined) {
        var requestModelMapper = new client.models['ServiceDescriptionTemplate']().mapper();
        requestModel = client.serialize(requestModelMapper, serviceDescriptionTemplate, 'serviceDescriptionTemplate');
        requestContent = JSON.stringify(requestModel);
      }
    } catch (error) {
      var serializationError = new Error(util.format('Error "%s" occurred in serializing the ' + 
          'payload - "%s"', error.message, util.inspect(serviceDescriptionTemplate, {depth: null})));
      return callback(serializationError);
    }
    httpRequest.body = requestContent;
    // Send Request
    return client.pipeline(httpRequest, function (err, response, responseBody) {
      if (err) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200 && statusCode !== 201 && statusCode !== 202) {
        var error = new Error(responseBody);
        error.statusCode = response.statusCode;
        error.request = msRest.stripRequest(httpRequest);
        error.response = msRest.stripResponse(response);
        if (responseBody === '') responseBody = null;
        var parsedErrorResponse;
        try {
          parsedErrorResponse = JSON.parse(responseBody);
          if (parsedErrorResponse) {
            if (parsedErrorResponse.error) parsedErrorResponse = parsedErrorResponse.error;
            if (parsedErrorResponse.code) error.code = parsedErrorResponse.code;
            if (parsedErrorResponse.message) error.message = parsedErrorResponse.message;
          }
          if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
            var resultMapper = new client.models['ErrorModel']().mapper();
            error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
          }
        } catch (defaultError) {
          error.message = util.format('Error "%s" occurred in deserializing the responseBody ' + 
                           '- "%s" for the default response.', defaultError.message, responseBody);
          return callback(error);
        }
        return callback(error);
      }
      // Create Result
      var result = null;
      if (responseBody === '') responseBody = null;
      // Deserialize Response
      if (statusCode === 200) {
        var parsedResponse = null;
        try {
          parsedResponse = JSON.parse(responseBody);
          result = JSON.parse(responseBody);
          if (parsedResponse !== null && parsedResponse !== undefined) {
            var resultMapper = {
              required: false,
              serializedName: 'parsedResponse',
              type: {
                name: 'String'
              }
            };
            result = client.deserialize(resultMapper, parsedResponse, 'result');
          }
        } catch (error) {
          var deserializationError = new Error(util.format('Error "%s" occurred in deserializing the responseBody - "%s"', error, responseBody));
          deserializationError.request = msRest.stripRequest(httpRequest);
          deserializationError.response = msRest.stripResponse(response);
          return callback(deserializationError);
        }
      }
      // Deserialize Response
      if (statusCode === 201) {
        var parsedResponse = null;
        try {
          parsedResponse = JSON.parse(responseBody);
          result = JSON.parse(responseBody);
          if (parsedResponse !== null && parsedResponse !== undefined) {
            var resultMapper = {
              required: false,
              serializedName: 'parsedResponse',
              type: {
                name: 'String'
              }
            };
            result = client.deserialize(resultMapper, parsedResponse, 'result');
          }
        } catch (error) {
          var deserializationError1 = new Error(util.format('Error "%s" occurred in deserializing the responseBody - "%s"', error, responseBody));
          deserializationError1.request = msRest.stripRequest(httpRequest);
          deserializationError1.response = msRest.stripResponse(response);
          return callback(deserializationError1);
        }
      }
      // Deserialize Response
      if (statusCode === 202) {
        var parsedResponse = null;
        try {
          parsedResponse = JSON.parse(responseBody);
          result = JSON.parse(responseBody);
          if (parsedResponse !== null && parsedResponse !== undefined) {
            var resultMapper = {
              required: false,
              serializedName: 'parsedResponse',
              type: {
                name: 'String'
              }
            };
            result = client.deserialize(resultMapper, parsedResponse, 'result');
          }
        } catch (error) {
          var deserializationError2 = new Error(util.format('Error "%s" occurred in deserializing the responseBody - "%s"', error, responseBody));
          deserializationError2.request = msRest.stripRequest(httpRequest);
          deserializationError2.response = msRest.stripResponse(response);
          return callback(deserializationError2);
        }
      }
  
      return callback(null, result, httpRequest, response);
    });
  };
  
  
  module.exports = ServiceGroupFromTemplates;
  