/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 * Changes may cause incorrect behavior and will be lost if the code is
 * regenerated.
 */

'use strict';

const msRest = require('ms-rest');
const msRestAzure = require('ms-rest-azure');
const WebResource = msRest.WebResource;

/**
 * Get the certificate from the provisioning service.
 *
 * @param {string} certificateName Name of the certificate to retrieve.
 *
 * @param {string} resourceGroupName Resource group identifier.
 *
 * @param {string} provisioningServiceName Name of the provisioning service the
 * certificate is associated with.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {string} [options.ifMatch] ETag of the certificate.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The 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 if an error did not occur.
 *                      See {@link CertificateResponse} for more information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _get(certificateName, resourceGroupName, provisioningServiceName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  let ifMatch = (options && options.ifMatch !== undefined) ? options.ifMatch : undefined;
  // Validate
  try {
    if (certificateName === null || certificateName === undefined || typeof certificateName.valueOf() !== 'string') {
      throw new Error('certificateName cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    if (ifMatch !== null && ifMatch !== undefined && typeof ifMatch.valueOf() !== 'string') {
      throw new Error('ifMatch must be of type string.');
    }
    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.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates/{certificateName}';
  requestUrl = requestUrl.replace('{certificateName}', encodeURIComponent(certificateName));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'GET';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (ifMatch !== undefined && ifMatch !== null) {
    httpRequest.headers['If-Match'] = ifMatch;
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['CertificateResponse']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * @summary Upload the certificate to the provisioning service.
 *
 * Add new certificate or update an existing certificate.
 *
 * @param {string} resourceGroupName Resource group identifier.
 *
 * @param {string} provisioningServiceName The name of the provisioning
 * service.
 *
 * @param {string} certificateName The name of the certificate create or
 * update.
 *
 * @param {object} certificateDescription The certificate body.
 *
 * @param {string} [certificateDescription.certificate] Base-64 representation
 * of the X509 leaf certificate .cer file or just .pem file content.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {string} [options.ifMatch] ETag of the certificate. This is required
 * to update an existing certificate, and ignored while creating a brand new
 * certificate.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The 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 if an error did not occur.
 *                      See {@link CertificateResponse} for more information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _createOrUpdate(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  let ifMatch = (options && options.ifMatch !== undefined) ? options.ifMatch : undefined;
  // Validate
  try {
    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.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    if (certificateName === null || certificateName === undefined || typeof certificateName.valueOf() !== 'string') {
      throw new Error('certificateName cannot be null or undefined and it must be of type string.');
    }
    if (certificateName !== null && certificateName !== undefined) {
      if (certificateName.length > 256)
      {
        throw new Error('"certificateName" should satisfy the constraint - "MaxLength": 256');
      }
    }
    if (certificateDescription === null || certificateDescription === undefined) {
      throw new Error('certificateDescription cannot be null or undefined.');
    }
    if (ifMatch !== null && ifMatch !== undefined && typeof ifMatch.valueOf() !== 'string') {
      throw new Error('ifMatch must be of type string.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates/{certificateName}';
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  requestUrl = requestUrl.replace('{certificateName}', encodeURIComponent(certificateName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'PUT';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (ifMatch !== undefined && ifMatch !== null) {
    httpRequest.headers['If-Match'] = ifMatch;
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  // Serialize Request
  let requestContent = null;
  let requestModel = null;
  try {
    if (certificateDescription !== null && certificateDescription !== undefined) {
      let requestModelMapper = new client.models['CertificateBodyDescription']().mapper();
      requestModel = client.serialize(requestModelMapper, certificateDescription, 'certificateDescription');
      requestContent = JSON.stringify(requestModel);
    }
  } catch (error) {
    let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` +
        `payload - ${JSON.stringify(certificateDescription, null, 2)}.`);
    return callback(serializationError);
  }
  httpRequest.body = requestContent;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['CertificateResponse']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * @summary Delete the Provisioning Service Certificate.
 *
 * Deletes the specified certificate assosciated with the Provisioning Service
 *
 * @param {string} resourceGroupName Resource group identifier.
 *
 * @param {string} ifMatch ETag of the certificate
 *
 * @param {string} provisioningServiceName The name of the provisioning
 * service.
 *
 * @param {string} certificateName This is a mandatory field, and is the
 * logical name of the certificate that the provisioning service will access
 * by.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {string} [options.certificatename] This is optional, and it is the
 * Common Name of the certificate.
 *
 * @param {buffer} [options.certificaterawBytes] Raw data within the
 * certificate.
 *
 * @param {boolean} [options.certificateisVerified] Indicates if certificate
 * has been verified by owner of the private key.
 *
 * @param {string} [options.certificatepurpose] A description that mentions the
 * purpose of the certificate. Possible values include: 'clientAuthentication',
 * 'serverAuthentication'
 *
 * @param {date} [options.certificatecreated] Time the certificate is created.
 *
 * @param {date} [options.certificatelastUpdated] Time the certificate is last
 * updated.
 *
 * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
 * certificate contains a private key.
 *
 * @param {string} [options.certificatenonce] Random number generated to
 * indicate Proof of Possession.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The callback.
 *
 * @returns {function} callback(err, result, request, response)
 *
 *                      {Error}  err        - The Error object if an error occurred, null otherwise.
 *
 *                      {null} [result]   - The deserialized result object if an error did not occur.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _deleteMethod(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  let certificatename = (options && options.certificatename !== undefined) ? options.certificatename : undefined;
  let certificaterawBytes = (options && options.certificaterawBytes !== undefined) ? options.certificaterawBytes : undefined;
  let certificateisVerified = (options && options.certificateisVerified !== undefined) ? options.certificateisVerified : undefined;
  let certificatepurpose = (options && options.certificatepurpose !== undefined) ? options.certificatepurpose : undefined;
  let certificatecreated = (options && options.certificatecreated !== undefined) ? options.certificatecreated : undefined;
  let certificatelastUpdated = (options && options.certificatelastUpdated !== undefined) ? options.certificatelastUpdated : undefined;
  let certificatehasPrivateKey = (options && options.certificatehasPrivateKey !== undefined) ? options.certificatehasPrivateKey : undefined;
  let certificatenonce = (options && options.certificatenonce !== undefined) ? options.certificatenonce : undefined;
  // Validate
  try {
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (ifMatch === null || ifMatch === undefined || typeof ifMatch.valueOf() !== 'string') {
      throw new Error('ifMatch cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    if (certificateName === null || certificateName === undefined || typeof certificateName.valueOf() !== 'string') {
      throw new Error('certificateName cannot be null or undefined and it must be of type string.');
    }
    if (certificatename !== null && certificatename !== undefined && typeof certificatename.valueOf() !== 'string') {
      throw new Error('certificatename must be of type string.');
    }
    if (certificaterawBytes && !Buffer.isBuffer(certificaterawBytes)) {
      throw new Error('certificaterawBytes must be of type buffer.');
    }
    if (certificateisVerified !== null && certificateisVerified !== undefined && typeof certificateisVerified !== 'boolean') {
      throw new Error('certificateisVerified must be of type boolean.');
    }
    if (certificatepurpose !== null && certificatepurpose !== undefined && typeof certificatepurpose.valueOf() !== 'string') {
      throw new Error('certificatepurpose must be of type string.');
    }
    if (certificatecreated && !(certificatecreated instanceof Date ||
        (typeof certificatecreated.valueOf() === 'string' && !isNaN(Date.parse(certificatecreated))))) {
          throw new Error('certificatecreated must be of type date.');
        }
    if (certificatelastUpdated && !(certificatelastUpdated instanceof Date ||
        (typeof certificatelastUpdated.valueOf() === 'string' && !isNaN(Date.parse(certificatelastUpdated))))) {
          throw new Error('certificatelastUpdated must be of type date.');
        }
    if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined && typeof certificatehasPrivateKey !== 'boolean') {
      throw new Error('certificatehasPrivateKey must be of type boolean.');
    }
    if (certificatenonce !== null && certificatenonce !== undefined && typeof certificatenonce.valueOf() !== 'string') {
      throw new Error('certificatenonce must be of type string.');
    }
    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.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates/{certificateName}';
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  requestUrl = requestUrl.replace('{certificateName}', encodeURIComponent(certificateName));
  let queryParameters = [];
  if (certificatename !== null && certificatename !== undefined) {
    queryParameters.push('certificate.name=' + encodeURIComponent(certificatename));
  }
  if (certificaterawBytes !== null && certificaterawBytes !== undefined) {
    queryParameters.push('certificate.rawBytes=' + encodeURIComponent(client.serializeObject(certificaterawBytes)));
  }
  if (certificateisVerified !== null && certificateisVerified !== undefined) {
    queryParameters.push('certificate.isVerified=' + encodeURIComponent(certificateisVerified.toString()));
  }
  if (certificatepurpose !== null && certificatepurpose !== undefined) {
    queryParameters.push('certificate.purpose=' + encodeURIComponent(certificatepurpose));
  }
  if (certificatecreated !== null && certificatecreated !== undefined) {
    queryParameters.push('certificate.created=' + encodeURIComponent(client.serializeObject(certificatecreated)));
  }
  if (certificatelastUpdated !== null && certificatelastUpdated !== undefined) {
    queryParameters.push('certificate.lastUpdated=' + encodeURIComponent(client.serializeObject(certificatelastUpdated)));
  }
  if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined) {
    queryParameters.push('certificate.hasPrivateKey=' + encodeURIComponent(certificatehasPrivateKey.toString()));
  }
  if (certificatenonce !== null && certificatenonce !== undefined) {
    queryParameters.push('certificate.nonce=' + encodeURIComponent(certificatenonce));
  }
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'DELETE';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (ifMatch !== undefined && ifMatch !== null) {
    httpRequest.headers['If-Match'] = ifMatch;
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200 && statusCode !== 204) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;

    return callback(null, result, httpRequest, response);
  });
}

/**
 * Get all the certificates tied to the provisioning service.
 *
 * @param {string} resourceGroupName Name of resource group.
 *
 * @param {string} provisioningServiceName Name of provisioning service to
 * retrieve certificates for.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The 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 if an error did not occur.
 *                      See {@link CertificateListDescription} for more
 *                      information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _list(resourceGroupName, provisioningServiceName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  // Validate
  try {
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    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.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates';
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'GET';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['CertificateListDescription']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * Generate verification code for Proof of Possession.
 *
 * @param {string} certificateName The mandatory logical name of the
 * certificate, that the provisioning service uses to access.
 *
 * @param {string} ifMatch ETag of the certificate. This is required to update
 * an existing certificate, and ignored while creating a brand new certificate.
 *
 * @param {string} resourceGroupName name of resource group.
 *
 * @param {string} provisioningServiceName Name of provisioning service.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {string} [options.certificatename] Common Name for the certificate.
 *
 * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
 *
 * @param {boolean} [options.certificateisVerified] Indicates if the
 * certificate has been verified by owner of the private key.
 *
 * @param {string} [options.certificatepurpose] Description mentioning the
 * purpose of the certificate. Possible values include: 'clientAuthentication',
 * 'serverAuthentication'
 *
 * @param {date} [options.certificatecreated] Certificate creation time.
 *
 * @param {date} [options.certificatelastUpdated] Certificate last updated
 * time.
 *
 * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
 * certificate contains private key.
 *
 * @param {string} [options.certificatenonce] Random number generated to
 * indicate Proof of Possession.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The 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 if an error did not occur.
 *                      See {@link VerificationCodeResponse} for more
 *                      information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  let certificatename = (options && options.certificatename !== undefined) ? options.certificatename : undefined;
  let certificaterawBytes = (options && options.certificaterawBytes !== undefined) ? options.certificaterawBytes : undefined;
  let certificateisVerified = (options && options.certificateisVerified !== undefined) ? options.certificateisVerified : undefined;
  let certificatepurpose = (options && options.certificatepurpose !== undefined) ? options.certificatepurpose : undefined;
  let certificatecreated = (options && options.certificatecreated !== undefined) ? options.certificatecreated : undefined;
  let certificatelastUpdated = (options && options.certificatelastUpdated !== undefined) ? options.certificatelastUpdated : undefined;
  let certificatehasPrivateKey = (options && options.certificatehasPrivateKey !== undefined) ? options.certificatehasPrivateKey : undefined;
  let certificatenonce = (options && options.certificatenonce !== undefined) ? options.certificatenonce : undefined;
  // Validate
  try {
    if (certificateName === null || certificateName === undefined || typeof certificateName.valueOf() !== 'string') {
      throw new Error('certificateName cannot be null or undefined and it must be of type string.');
    }
    if (ifMatch === null || ifMatch === undefined || typeof ifMatch.valueOf() !== 'string') {
      throw new Error('ifMatch cannot be null or undefined and it must be of type string.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    if (certificatename !== null && certificatename !== undefined && typeof certificatename.valueOf() !== 'string') {
      throw new Error('certificatename must be of type string.');
    }
    if (certificaterawBytes && !Buffer.isBuffer(certificaterawBytes)) {
      throw new Error('certificaterawBytes must be of type buffer.');
    }
    if (certificateisVerified !== null && certificateisVerified !== undefined && typeof certificateisVerified !== 'boolean') {
      throw new Error('certificateisVerified must be of type boolean.');
    }
    if (certificatepurpose !== null && certificatepurpose !== undefined && typeof certificatepurpose.valueOf() !== 'string') {
      throw new Error('certificatepurpose must be of type string.');
    }
    if (certificatecreated && !(certificatecreated instanceof Date ||
        (typeof certificatecreated.valueOf() === 'string' && !isNaN(Date.parse(certificatecreated))))) {
          throw new Error('certificatecreated must be of type date.');
        }
    if (certificatelastUpdated && !(certificatelastUpdated instanceof Date ||
        (typeof certificatelastUpdated.valueOf() === 'string' && !isNaN(Date.parse(certificatelastUpdated))))) {
          throw new Error('certificatelastUpdated must be of type date.');
        }
    if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined && typeof certificatehasPrivateKey !== 'boolean') {
      throw new Error('certificatehasPrivateKey must be of type boolean.');
    }
    if (certificatenonce !== null && certificatenonce !== undefined && typeof certificatenonce.valueOf() !== 'string') {
      throw new Error('certificatenonce must be of type string.');
    }
    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.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates/{certificateName}/generateVerificationCode';
  requestUrl = requestUrl.replace('{certificateName}', encodeURIComponent(certificateName));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  let queryParameters = [];
  if (certificatename !== null && certificatename !== undefined) {
    queryParameters.push('certificate.name=' + encodeURIComponent(certificatename));
  }
  if (certificaterawBytes !== null && certificaterawBytes !== undefined) {
    queryParameters.push('certificate.rawBytes=' + encodeURIComponent(client.serializeObject(certificaterawBytes)));
  }
  if (certificateisVerified !== null && certificateisVerified !== undefined) {
    queryParameters.push('certificate.isVerified=' + encodeURIComponent(certificateisVerified.toString()));
  }
  if (certificatepurpose !== null && certificatepurpose !== undefined) {
    queryParameters.push('certificate.purpose=' + encodeURIComponent(certificatepurpose));
  }
  if (certificatecreated !== null && certificatecreated !== undefined) {
    queryParameters.push('certificate.created=' + encodeURIComponent(client.serializeObject(certificatecreated)));
  }
  if (certificatelastUpdated !== null && certificatelastUpdated !== undefined) {
    queryParameters.push('certificate.lastUpdated=' + encodeURIComponent(client.serializeObject(certificatelastUpdated)));
  }
  if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined) {
    queryParameters.push('certificate.hasPrivateKey=' + encodeURIComponent(certificatehasPrivateKey.toString()));
  }
  if (certificatenonce !== null && certificatenonce !== undefined) {
    queryParameters.push('certificate.nonce=' + encodeURIComponent(certificatenonce));
  }
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'POST';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (ifMatch !== undefined && ifMatch !== null) {
    httpRequest.headers['If-Match'] = ifMatch;
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  httpRequest.body = null;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['VerificationCodeResponse']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/**
 * @summary Verify certificate's private key possession.
 *
 * Verifies the certificate's private key possession by providing the leaf cert
 * issued by the verifying pre uploaded certificate.
 *
 * @param {string} certificateName The mandatory logical name of the
 * certificate, that the provisioning service uses to access.
 *
 * @param {string} ifMatch ETag of the certificate.
 *
 * @param {object} request The name of the certificate
 *
 * @param {string} [request.certificate] base-64 representation of X509
 * certificate .cer file or just .pem file content.
 *
 * @param {string} resourceGroupName Resource group name.
 *
 * @param {string} provisioningServiceName Provisioning service name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {string} [options.certificatename] Common Name for the certificate.
 *
 * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
 *
 * @param {boolean} [options.certificateisVerified] Indicates if the
 * certificate has been verified by owner of the private key.
 *
 * @param {string} [options.certificatepurpose] Describe the purpose of the
 * certificate. Possible values include: 'clientAuthentication',
 * 'serverAuthentication'
 *
 * @param {date} [options.certificatecreated] Certificate creation time.
 *
 * @param {date} [options.certificatelastUpdated] Certificate last updated
 * time.
 *
 * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
 * certificate contains private key.
 *
 * @param {string} [options.certificatenonce] Random number generated to
 * indicate Proof of Possession.
 *
 * @param {object} [options.customHeaders] Headers that will be added to the
 * request
 *
 * @param {function} callback - The 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 if an error did not occur.
 *                      See {@link CertificateResponse} for more information.
 *
 *                      {object} [request]  - The HTTP Request object if an error did not occur.
 *
 *                      {stream} [response] - The HTTP Response stream if an error did not occur.
 */
function _verifyCertificate(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options, callback) {
   /* jshint validthis: true */
  let client = this.client;
  if(!callback && typeof options === 'function') {
    callback = options;
    options = null;
  }
  if (!callback) {
    throw new Error('callback cannot be null.');
  }
  let certificatename = (options && options.certificatename !== undefined) ? options.certificatename : undefined;
  let certificaterawBytes = (options && options.certificaterawBytes !== undefined) ? options.certificaterawBytes : undefined;
  let certificateisVerified = (options && options.certificateisVerified !== undefined) ? options.certificateisVerified : undefined;
  let certificatepurpose = (options && options.certificatepurpose !== undefined) ? options.certificatepurpose : undefined;
  let certificatecreated = (options && options.certificatecreated !== undefined) ? options.certificatecreated : undefined;
  let certificatelastUpdated = (options && options.certificatelastUpdated !== undefined) ? options.certificatelastUpdated : undefined;
  let certificatehasPrivateKey = (options && options.certificatehasPrivateKey !== undefined) ? options.certificatehasPrivateKey : undefined;
  let certificatenonce = (options && options.certificatenonce !== undefined) ? options.certificatenonce : undefined;
  // Validate
  try {
    if (certificateName === null || certificateName === undefined || typeof certificateName.valueOf() !== 'string') {
      throw new Error('certificateName cannot be null or undefined and it must be of type string.');
    }
    if (ifMatch === null || ifMatch === undefined || typeof ifMatch.valueOf() !== 'string') {
      throw new Error('ifMatch cannot be null or undefined and it must be of type string.');
    }
    if (request === null || request === undefined) {
      throw new Error('request cannot be null or undefined.');
    }
    if (this.client.subscriptionId === null || this.client.subscriptionId === undefined || typeof this.client.subscriptionId.valueOf() !== 'string') {
      throw new Error('this.client.subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (resourceGroupName === null || resourceGroupName === undefined || typeof resourceGroupName.valueOf() !== 'string') {
      throw new Error('resourceGroupName cannot be null or undefined and it must be of type string.');
    }
    if (provisioningServiceName === null || provisioningServiceName === undefined || typeof provisioningServiceName.valueOf() !== 'string') {
      throw new Error('provisioningServiceName cannot be null or undefined and it must be of type string.');
    }
    if (certificatename !== null && certificatename !== undefined && typeof certificatename.valueOf() !== 'string') {
      throw new Error('certificatename must be of type string.');
    }
    if (certificaterawBytes && !Buffer.isBuffer(certificaterawBytes)) {
      throw new Error('certificaterawBytes must be of type buffer.');
    }
    if (certificateisVerified !== null && certificateisVerified !== undefined && typeof certificateisVerified !== 'boolean') {
      throw new Error('certificateisVerified must be of type boolean.');
    }
    if (certificatepurpose !== null && certificatepurpose !== undefined && typeof certificatepurpose.valueOf() !== 'string') {
      throw new Error('certificatepurpose must be of type string.');
    }
    if (certificatecreated && !(certificatecreated instanceof Date ||
        (typeof certificatecreated.valueOf() === 'string' && !isNaN(Date.parse(certificatecreated))))) {
          throw new Error('certificatecreated must be of type date.');
        }
    if (certificatelastUpdated && !(certificatelastUpdated instanceof Date ||
        (typeof certificatelastUpdated.valueOf() === 'string' && !isNaN(Date.parse(certificatelastUpdated))))) {
          throw new Error('certificatelastUpdated must be of type date.');
        }
    if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined && typeof certificatehasPrivateKey !== 'boolean') {
      throw new Error('certificatehasPrivateKey must be of type boolean.');
    }
    if (certificatenonce !== null && certificatenonce !== undefined && typeof certificatenonce.valueOf() !== 'string') {
      throw new Error('certificatenonce must be of type string.');
    }
    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.');
    }
    if (this.client.acceptLanguage !== null && this.client.acceptLanguage !== undefined && typeof this.client.acceptLanguage.valueOf() !== 'string') {
      throw new Error('this.client.acceptLanguage must be of type string.');
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Devices/provisioningServices/{provisioningServiceName}/certificates/{certificateName}/verify';
  requestUrl = requestUrl.replace('{certificateName}', encodeURIComponent(certificateName));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(this.client.subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{provisioningServiceName}', encodeURIComponent(provisioningServiceName));
  let queryParameters = [];
  if (certificatename !== null && certificatename !== undefined) {
    queryParameters.push('certificate.name=' + encodeURIComponent(certificatename));
  }
  if (certificaterawBytes !== null && certificaterawBytes !== undefined) {
    queryParameters.push('certificate.rawBytes=' + encodeURIComponent(client.serializeObject(certificaterawBytes)));
  }
  if (certificateisVerified !== null && certificateisVerified !== undefined) {
    queryParameters.push('certificate.isVerified=' + encodeURIComponent(certificateisVerified.toString()));
  }
  if (certificatepurpose !== null && certificatepurpose !== undefined) {
    queryParameters.push('certificate.purpose=' + encodeURIComponent(certificatepurpose));
  }
  if (certificatecreated !== null && certificatecreated !== undefined) {
    queryParameters.push('certificate.created=' + encodeURIComponent(client.serializeObject(certificatecreated)));
  }
  if (certificatelastUpdated !== null && certificatelastUpdated !== undefined) {
    queryParameters.push('certificate.lastUpdated=' + encodeURIComponent(client.serializeObject(certificatelastUpdated)));
  }
  if (certificatehasPrivateKey !== null && certificatehasPrivateKey !== undefined) {
    queryParameters.push('certificate.hasPrivateKey=' + encodeURIComponent(certificatehasPrivateKey.toString()));
  }
  if (certificatenonce !== null && certificatenonce !== undefined) {
    queryParameters.push('certificate.nonce=' + encodeURIComponent(certificatenonce));
  }
  queryParameters.push('api-version=' + encodeURIComponent(this.client.apiVersion));
  if (queryParameters.length > 0) {
    requestUrl += '?' + queryParameters.join('&');
  }

  // Create HTTP transport objects
  let httpRequest = new WebResource();
  httpRequest.method = 'POST';
  httpRequest.url = requestUrl;
  httpRequest.headers = {};
  // Set Headers
  httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8';
  if (this.client.generateClientRequestId) {
      httpRequest.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
  }
  if (ifMatch !== undefined && ifMatch !== null) {
    httpRequest.headers['If-Match'] = ifMatch;
  }
  if (this.client.acceptLanguage !== undefined && this.client.acceptLanguage !== null) {
    httpRequest.headers['accept-language'] = this.client.acceptLanguage;
  }
  if(options) {
    for(let headerName in options['customHeaders']) {
      if (options['customHeaders'].hasOwnProperty(headerName)) {
        httpRequest.headers[headerName] = options['customHeaders'][headerName];
      }
    }
  }
  // Serialize Request
  let requestContent = null;
  let requestModel = null;
  try {
    if (request !== null && request !== undefined) {
      let requestModelMapper = new client.models['VerificationCodeRequest']().mapper();
      requestModel = client.serialize(requestModelMapper, request, 'request');
      requestContent = JSON.stringify(requestModel);
    }
  } catch (error) {
    let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` +
        `payload - ${JSON.stringify(request, null, 2)}.`);
    return callback(serializationError);
  }
  httpRequest.body = requestContent;
  // Send Request
  return client.pipeline(httpRequest, (err, response, responseBody) => {
    if (err) {
      return callback(err);
    }
    let statusCode = response.statusCode;
    if (statusCode !== 200) {
      let error = new Error(responseBody);
      error.statusCode = response.statusCode;
      error.request = msRest.stripRequest(httpRequest);
      error.response = msRest.stripResponse(response);
      if (responseBody === '') responseBody = null;
      let parsedErrorResponse;
      try {
        parsedErrorResponse = JSON.parse(responseBody);
        if (parsedErrorResponse) {
          let internalError = null;
          if (parsedErrorResponse.error) internalError = parsedErrorResponse.error;
          error.code = internalError ? internalError.code : parsedErrorResponse.code;
          error.message = internalError ? internalError.message : parsedErrorResponse.message;
        }
        if (parsedErrorResponse !== null && parsedErrorResponse !== undefined) {
          let resultMapper = new client.models['ErrorDetails']().mapper();
          error.body = client.deserialize(resultMapper, parsedErrorResponse, 'error.body');
        }
      } catch (defaultError) {
        error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` +
                         `- "${responseBody}" for the default response.`;
        return callback(error);
      }
      return callback(error);
    }
    // Create Result
    let result = null;
    if (responseBody === '') responseBody = null;
    // Deserialize Response
    if (statusCode === 200) {
      let parsedResponse = null;
      try {
        parsedResponse = JSON.parse(responseBody);
        result = JSON.parse(responseBody);
        if (parsedResponse !== null && parsedResponse !== undefined) {
          let resultMapper = new client.models['CertificateResponse']().mapper();
          result = client.deserialize(resultMapper, parsedResponse, 'result');
        }
      } catch (error) {
        let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`);
        deserializationError.request = msRest.stripRequest(httpRequest);
        deserializationError.response = msRest.stripResponse(response);
        return callback(deserializationError);
      }
    }

    return callback(null, result, httpRequest, response);
  });
}

/** Class representing a DpsCertificate. */
class DpsCertificate {
  /**
   * Create a DpsCertificate.
   * @param {IotDpsClient} client Reference to the service client.
   */
  constructor(client) {
    this.client = client;
    this._get = _get;
    this._createOrUpdate = _createOrUpdate;
    this._deleteMethod = _deleteMethod;
    this._list = _list;
    this._generateVerificationCode = _generateVerificationCode;
    this._verifyCertificate = _verifyCertificate;
  }

  /**
   * Get the certificate from the provisioning service.
   *
   * @param {string} certificateName Name of the certificate to retrieve.
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} provisioningServiceName Name of the provisioning service the
   * certificate is associated with.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.ifMatch] ETag of the certificate.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<CertificateResponse>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  getWithHttpOperationResponse(certificateName, resourceGroupName, provisioningServiceName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._get(certificateName, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Get the certificate from the provisioning service.
   *
   * @param {string} certificateName Name of the certificate to retrieve.
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} provisioningServiceName Name of the provisioning service the
   * certificate is associated with.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.ifMatch] ETag of the certificate.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {CertificateResponse} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link CertificateResponse} for more information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  get(certificateName, resourceGroupName, provisioningServiceName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._get(certificateName, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._get(certificateName, resourceGroupName, provisioningServiceName, options, optionalCallback);
    }
  }

  /**
   * @summary Upload the certificate to the provisioning service.
   *
   * Add new certificate or update an existing certificate.
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} provisioningServiceName The name of the provisioning
   * service.
   *
   * @param {string} certificateName The name of the certificate create or
   * update.
   *
   * @param {object} certificateDescription The certificate body.
   *
   * @param {string} [certificateDescription.certificate] Base-64 representation
   * of the X509 leaf certificate .cer file or just .pem file content.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.ifMatch] ETag of the certificate. This is required
   * to update an existing certificate, and ignored while creating a brand new
   * certificate.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<CertificateResponse>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  createOrUpdateWithHttpOperationResponse(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._createOrUpdate(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * @summary Upload the certificate to the provisioning service.
   *
   * Add new certificate or update an existing certificate.
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} provisioningServiceName The name of the provisioning
   * service.
   *
   * @param {string} certificateName The name of the certificate create or
   * update.
   *
   * @param {object} certificateDescription The certificate body.
   *
   * @param {string} [certificateDescription.certificate] Base-64 representation
   * of the X509 leaf certificate .cer file or just .pem file content.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.ifMatch] ETag of the certificate. This is required
   * to update an existing certificate, and ignored while creating a brand new
   * certificate.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {CertificateResponse} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link CertificateResponse} for more information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  createOrUpdate(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._createOrUpdate(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._createOrUpdate(resourceGroupName, provisioningServiceName, certificateName, certificateDescription, options, optionalCallback);
    }
  }

  /**
   * @summary Delete the Provisioning Service Certificate.
   *
   * Deletes the specified certificate assosciated with the Provisioning Service
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} ifMatch ETag of the certificate
   *
   * @param {string} provisioningServiceName The name of the provisioning
   * service.
   *
   * @param {string} certificateName This is a mandatory field, and is the
   * logical name of the certificate that the provisioning service will access
   * by.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] This is optional, and it is the
   * Common Name of the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data within the
   * certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if certificate
   * has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] A description that mentions the
   * purpose of the certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Time the certificate is created.
   *
   * @param {date} [options.certificatelastUpdated] Time the certificate is last
   * updated.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains a private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<null>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  deleteMethodWithHttpOperationResponse(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._deleteMethod(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * @summary Delete the Provisioning Service Certificate.
   *
   * Deletes the specified certificate assosciated with the Provisioning Service
   *
   * @param {string} resourceGroupName Resource group identifier.
   *
   * @param {string} ifMatch ETag of the certificate
   *
   * @param {string} provisioningServiceName The name of the provisioning
   * service.
   *
   * @param {string} certificateName This is a mandatory field, and is the
   * logical name of the certificate that the provisioning service will access
   * by.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] This is optional, and it is the
   * Common Name of the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data within the
   * certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if certificate
   * has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] A description that mentions the
   * purpose of the certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Time the certificate is created.
   *
   * @param {date} [options.certificatelastUpdated] Time the certificate is last
   * updated.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains a private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {null} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {null} [result]   - The deserialized result object if an error did not occur.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  deleteMethod(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._deleteMethod(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._deleteMethod(resourceGroupName, ifMatch, provisioningServiceName, certificateName, options, optionalCallback);
    }
  }

  /**
   * Get all the certificates tied to the provisioning service.
   *
   * @param {string} resourceGroupName Name of resource group.
   *
   * @param {string} provisioningServiceName Name of provisioning service to
   * retrieve certificates for.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<CertificateListDescription>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listWithHttpOperationResponse(resourceGroupName, provisioningServiceName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._list(resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Get all the certificates tied to the provisioning service.
   *
   * @param {string} resourceGroupName Name of resource group.
   *
   * @param {string} provisioningServiceName Name of provisioning service to
   * retrieve certificates for.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {CertificateListDescription} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link CertificateListDescription} for more
   *                      information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  list(resourceGroupName, provisioningServiceName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._list(resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._list(resourceGroupName, provisioningServiceName, options, optionalCallback);
    }
  }

  /**
   * Generate verification code for Proof of Possession.
   *
   * @param {string} certificateName The mandatory logical name of the
   * certificate, that the provisioning service uses to access.
   *
   * @param {string} ifMatch ETag of the certificate. This is required to update
   * an existing certificate, and ignored while creating a brand new certificate.
   *
   * @param {string} resourceGroupName name of resource group.
   *
   * @param {string} provisioningServiceName Name of provisioning service.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] Common Name for the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if the
   * certificate has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] Description mentioning the
   * purpose of the certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Certificate creation time.
   *
   * @param {date} [options.certificatelastUpdated] Certificate last updated
   * time.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<VerificationCodeResponse>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  generateVerificationCodeWithHttpOperationResponse(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Generate verification code for Proof of Possession.
   *
   * @param {string} certificateName The mandatory logical name of the
   * certificate, that the provisioning service uses to access.
   *
   * @param {string} ifMatch ETag of the certificate. This is required to update
   * an existing certificate, and ignored while creating a brand new certificate.
   *
   * @param {string} resourceGroupName name of resource group.
   *
   * @param {string} provisioningServiceName Name of provisioning service.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] Common Name for the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if the
   * certificate has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] Description mentioning the
   * purpose of the certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Certificate creation time.
   *
   * @param {date} [options.certificatelastUpdated] Certificate last updated
   * time.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {VerificationCodeResponse} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link VerificationCodeResponse} for more
   *                      information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName, options, optionalCallback);
    }
  }

  /**
   * @summary Verify certificate's private key possession.
   *
   * Verifies the certificate's private key possession by providing the leaf cert
   * issued by the verifying pre uploaded certificate.
   *
   * @param {string} certificateName The mandatory logical name of the
   * certificate, that the provisioning service uses to access.
   *
   * @param {string} ifMatch ETag of the certificate.
   *
   * @param {object} request The name of the certificate
   *
   * @param {string} [request.certificate] base-64 representation of X509
   * certificate .cer file or just .pem file content.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {string} provisioningServiceName Provisioning service name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] Common Name for the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if the
   * certificate has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] Describe the purpose of the
   * certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Certificate creation time.
   *
   * @param {date} [options.certificatelastUpdated] Certificate last updated
   * time.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<CertificateResponse>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  verifyCertificateWithHttpOperationResponse(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._verifyCertificate(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * @summary Verify certificate's private key possession.
   *
   * Verifies the certificate's private key possession by providing the leaf cert
   * issued by the verifying pre uploaded certificate.
   *
   * @param {string} certificateName The mandatory logical name of the
   * certificate, that the provisioning service uses to access.
   *
   * @param {string} ifMatch ETag of the certificate.
   *
   * @param {object} request The name of the certificate
   *
   * @param {string} [request.certificate] base-64 representation of X509
   * certificate .cer file or just .pem file content.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {string} provisioningServiceName Provisioning service name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {string} [options.certificatename] Common Name for the certificate.
   *
   * @param {buffer} [options.certificaterawBytes] Raw data of certificate.
   *
   * @param {boolean} [options.certificateisVerified] Indicates if the
   * certificate has been verified by owner of the private key.
   *
   * @param {string} [options.certificatepurpose] Describe the purpose of the
   * certificate. Possible values include: 'clientAuthentication',
   * 'serverAuthentication'
   *
   * @param {date} [options.certificatecreated] Certificate creation time.
   *
   * @param {date} [options.certificatelastUpdated] Certificate last updated
   * time.
   *
   * @param {boolean} [options.certificatehasPrivateKey] Indicates if the
   * certificate contains private key.
   *
   * @param {string} [options.certificatenonce] Random number generated to
   * indicate Proof of Possession.
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @param {function} [optionalCallback] - The optional callback.
   *
   * @returns {function|Promise} If a callback was passed as the last parameter
   * then it returns the callback else returns a Promise.
   *
   * {Promise} A promise is returned
   *
   *                      @resolve {CertificateResponse} - The deserialized result object.
   *
   *                      @reject {Error} - The error object.
   *
   * {function} optionalCallback(err, result, request, response)
   *
   *                      {Error}  err        - The Error object if an error occurred, null otherwise.
   *
   *                      {object} [result]   - The deserialized result object if an error did not occur.
   *                      See {@link CertificateResponse} for more information.
   *
   *                      {object} [request]  - The HTTP Request object if an error did not occur.
   *
   *                      {stream} [response] - The HTTP Response stream if an error did not occur.
   */
  verifyCertificate(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options, optionalCallback) {
    let client = this.client;
    let self = this;
    if (!optionalCallback && typeof options === 'function') {
      optionalCallback = options;
      options = null;
    }
    if (!optionalCallback) {
      return new Promise((resolve, reject) => {
        self._verifyCertificate(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._verifyCertificate(certificateName, ifMatch, request, resourceGroupName, provisioningServiceName, options, optionalCallback);
    }
  }

}

module.exports = DpsCertificate;
