text:
'use strict';

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

/**
 * Queries policy events for the resources under the management group.
 *
 * @param {string} managementGroupName Management group name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForManagementGroup(managementGroupName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let managementGroupsNamespace = 'Microsoft.Management';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (managementGroupName === null || managementGroupName === undefined || typeof managementGroupName.valueOf() !== 'string') {
      throw new Error('managementGroupName 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'providers/{managementGroupsNamespace}/managementGroups/{managementGroupName}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{managementGroupsNamespace}', encodeURIComponent(managementGroupsNamespace));
  requestUrl = requestUrl.replace('{managementGroupName}', encodeURIComponent(managementGroupName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the resources under the subscription.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForSubscription(subscriptionId, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('subscriptionId 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the resources under the resource group.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {string} resourceGroupName Resource group name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForResourceGroup(subscriptionId, resourceGroupName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('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 (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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply 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.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the resource.
 *
 * @param {string} resourceId Resource ID.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForResource(resourceId, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (resourceId === null || resourceId === undefined || typeof resourceId.valueOf() !== 'string') {
      throw new Error('resourceId 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + '{resourceId}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{resourceId}', resourceId);
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the subscription level policy set definition.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {string} policySetDefinitionName Policy set definition name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForPolicySetDefinition(subscriptionId, policySetDefinitionName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let authorizationNamespace = 'Microsoft.Authorization';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (policySetDefinitionName === null || policySetDefinitionName === undefined || typeof policySetDefinitionName.valueOf() !== 'string') {
      throw new Error('policySetDefinitionName 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/providers/{authorizationNamespace}/policySetDefinitions/{policySetDefinitionName}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  requestUrl = requestUrl.replace('{authorizationNamespace}', encodeURIComponent(authorizationNamespace));
  requestUrl = requestUrl.replace('{policySetDefinitionName}', encodeURIComponent(policySetDefinitionName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the subscription level policy definition.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {string} policyDefinitionName Policy definition name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForPolicyDefinition(subscriptionId, policyDefinitionName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let authorizationNamespace = 'Microsoft.Authorization';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (policyDefinitionName === null || policyDefinitionName === undefined || typeof policyDefinitionName.valueOf() !== 'string') {
      throw new Error('policyDefinitionName 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/providers/{authorizationNamespace}/policyDefinitions/{policyDefinitionName}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  requestUrl = requestUrl.replace('{authorizationNamespace}', encodeURIComponent(authorizationNamespace));
  requestUrl = requestUrl.replace('{policyDefinitionName}', encodeURIComponent(policyDefinitionName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the subscription level policy assignment.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {string} policyAssignmentName Policy assignment name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForSubscriptionLevelPolicyAssignment(subscriptionId, policyAssignmentName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let authorizationNamespace = 'Microsoft.Authorization';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('subscriptionId cannot be null or undefined and it must be of type string.');
    }
    if (policyAssignmentName === null || policyAssignmentName === undefined || typeof policyAssignmentName.valueOf() !== 'string') {
      throw new Error('policyAssignmentName 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply must be of type string.');
      }
    }
  } catch (error) {
    return callback(error);
  }

  // Construct URL
  let baseUrl = this.client.baseUri;
  let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'subscriptions/{subscriptionId}/providers/{authorizationNamespace}/policyAssignments/{policyAssignmentName}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  requestUrl = requestUrl.replace('{authorizationNamespace}', encodeURIComponent(authorizationNamespace));
  requestUrl = requestUrl.replace('{policyAssignmentName}', encodeURIComponent(policyAssignmentName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Queries policy events for the resource group level policy assignment.
 *
 * @param {string} subscriptionId Microsoft Azure subscription ID.
 *
 * @param {string} resourceGroupName Resource group name.
 *
 * @param {string} policyAssignmentName Policy assignment name.
 *
 * @param {object} [options] Optional Parameters.
 *
 * @param {object} [options.queryOptions] Additional parameters for the
 * operation
 *
 * @param {number} [options.queryOptions.top] Maximum number of records to
 * return.
 *
 * @param {string} [options.queryOptions.filter] OData filter expression.
 *
 * @param {string} [options.queryOptions.orderBy] Ordering expression using
 * OData notation. One or more comma-separated column names with an optional
 * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
 * asc".
 *
 * @param {string} [options.queryOptions.select] Select expression using OData
 * notation. Limits the columns on each record to just those requested, e.g.
 * "$select=PolicyAssignmentId, ResourceId".
 *
 * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
 * specifying the start time of the interval to query. When not specified, the
 * service uses ($to - 1-day).
 *
 * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
 * specifying the end time of the interval to query. When not specified, the
 * service uses request time.
 *
 * @param {string} [options.queryOptions.apply] OData apply expression for
 * aggregations.
 *
 * @param {string} [options.queryOptions.expand] The $expand query parameter.
 * For example, to expand policyEvaluationDetails, use
 * $expand=policyEvaluationDetails
 *
 * @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 PolicyEventsQueryResults} 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 _listQueryResultsForResourceGroupLevelPolicyAssignment(subscriptionId, resourceGroupName, policyAssignmentName, 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 queryOptions = (options && options.queryOptions !== undefined) ? options.queryOptions : undefined;
  let policyEventsResource = 'default';
  let authorizationNamespace = 'Microsoft.Authorization';
  let apiVersion = '2018-04-04';
  // Validate
  try {
    if (subscriptionId === null || subscriptionId === undefined || typeof subscriptionId.valueOf() !== 'string') {
      throw new Error('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 (policyAssignmentName === null || policyAssignmentName === undefined || typeof policyAssignmentName.valueOf() !== 'string') {
      throw new Error('policyAssignmentName 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);
  }
  let top;
  let orderBy;
  let select;
  let from;
  let to;
  let filter;
  let apply;
  try {
    if (queryOptions !== null && queryOptions !== undefined) {
      top = queryOptions.top;
      if (top !== null && top !== undefined && typeof top !== 'number') {
        throw new Error('top must be of type number.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      orderBy = queryOptions.orderBy;
      if (orderBy !== null && orderBy !== undefined && typeof orderBy.valueOf() !== 'string') {
        throw new Error('orderBy must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      select = queryOptions.select;
      if (select !== null && select !== undefined && typeof select.valueOf() !== 'string') {
        throw new Error('select must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      from = queryOptions.from;
      if (from && !(from instanceof Date ||
          (typeof from.valueOf() === 'string' && !isNaN(Date.parse(from))))) {
            throw new Error('from must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      to = queryOptions.to;
      if (to && !(to instanceof Date ||
          (typeof to.valueOf() === 'string' && !isNaN(Date.parse(to))))) {
            throw new Error('to must be of type date.');
          }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      filter = queryOptions.filter;
      if (filter !== null && filter !== undefined && typeof filter.valueOf() !== 'string') {
        throw new Error('filter must be of type string.');
      }
    }
    if (queryOptions !== null && queryOptions !== undefined) {
      apply = queryOptions.apply;
      if (apply !== null && apply !== undefined && typeof apply.valueOf() !== 'string') {
        throw new Error('apply 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/{authorizationNamespace}/policyAssignments/{policyAssignmentName}/providers/Microsoft.PolicyInsights/policyEvents/{policyEventsResource}/queryResults';
  requestUrl = requestUrl.replace('{policyEventsResource}', encodeURIComponent(policyEventsResource));
  requestUrl = requestUrl.replace('{subscriptionId}', encodeURIComponent(subscriptionId));
  requestUrl = requestUrl.replace('{resourceGroupName}', encodeURIComponent(resourceGroupName));
  requestUrl = requestUrl.replace('{authorizationNamespace}', encodeURIComponent(authorizationNamespace));
  requestUrl = requestUrl.replace('{policyAssignmentName}', encodeURIComponent(policyAssignmentName));
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(apiVersion));
  if (top !== null && top !== undefined) {
    queryParameters.push('$top=' + encodeURIComponent(top.toString()));
  }
  if (orderBy !== null && orderBy !== undefined) {
    queryParameters.push('$orderby=' + encodeURIComponent(orderBy));
  }
  if (select !== null && select !== undefined) {
    queryParameters.push('$select=' + encodeURIComponent(select));
  }
  if (from !== null && from !== undefined) {
    queryParameters.push('$from=' + encodeURIComponent(client.serializeObject(from)));
  }
  if (to !== null && to !== undefined) {
    queryParameters.push('$to=' + encodeURIComponent(client.serializeObject(to)));
  }
  if (filter !== null && filter !== undefined) {
    queryParameters.push('$filter=' + encodeURIComponent(filter));
  }
  if (apply !== null && apply !== undefined) {
    queryParameters.push('$apply=' + encodeURIComponent(apply));
  }
  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 (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['QueryFailure']().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['PolicyEventsQueryResults']().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);
  });
}

/**
 * Gets OData metadata XML document.
 *
 * @param {string} scope A valid scope, i.e. management group, subscription,
 * resource group, or resource ID. Scope used has no effect on metadata
 * returned.
 *
 * @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.
 *
 *                      {string} [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 _getMetadata(scope, 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 apiVersion = '2018-04-04';
  // Validate
  try {
    if (scope === null || scope === undefined || typeof scope.valueOf() !== 'string') {
      throw new Error('scope 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('/') ? '' : '/') + '{scope}/providers/Microsoft.PolicyInsights/policyEvents/$metadata';
  requestUrl = requestUrl.replace('{scope}', scope);
  let queryParameters = [];
  queryParameters.push('api-version=' + encodeURIComponent(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['QueryFailure']().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 = {
            required: false,
            serializedName: 'parsedResponse',
            type: {
              name: 'String'
            }
          };
          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 PolicyEvents. */
class PolicyEvents {
  /**
   * Create a PolicyEvents.
   * @param {PolicyInsightsClient} client Reference to the service client.
   */
  constructor(client) {
    this.client = client;
    this._listQueryResultsForManagementGroup = _listQueryResultsForManagementGroup;
    this._listQueryResultsForSubscription = _listQueryResultsForSubscription;
    this._listQueryResultsForResourceGroup = _listQueryResultsForResourceGroup;
    this._listQueryResultsForResource = _listQueryResultsForResource;
    this._listQueryResultsForPolicySetDefinition = _listQueryResultsForPolicySetDefinition;
    this._listQueryResultsForPolicyDefinition = _listQueryResultsForPolicyDefinition;
    this._listQueryResultsForSubscriptionLevelPolicyAssignment = _listQueryResultsForSubscriptionLevelPolicyAssignment;
    this._listQueryResultsForResourceGroupLevelPolicyAssignment = _listQueryResultsForResourceGroupLevelPolicyAssignment;
    this._getMetadata = _getMetadata;
  }

  /**
   * Queries policy events for the resources under the management group.
   *
   * @param {string} managementGroupName Management group name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForManagementGroupWithHttpOperationResponse(managementGroupName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForManagementGroup(managementGroupName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the resources under the management group.
   *
   * @param {string} managementGroupName Management group name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForManagementGroup(managementGroupName, 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._listQueryResultsForManagementGroup(managementGroupName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForManagementGroup(managementGroupName, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the resources under the subscription.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForSubscriptionWithHttpOperationResponse(subscriptionId, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForSubscription(subscriptionId, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the resources under the subscription.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForSubscription(subscriptionId, 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._listQueryResultsForSubscription(subscriptionId, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForSubscription(subscriptionId, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the resources under the resource group.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForResourceGroupWithHttpOperationResponse(subscriptionId, resourceGroupName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForResourceGroup(subscriptionId, resourceGroupName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the resources under the resource group.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForResourceGroup(subscriptionId, resourceGroupName, 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._listQueryResultsForResourceGroup(subscriptionId, resourceGroupName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForResourceGroup(subscriptionId, resourceGroupName, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the resource.
   *
   * @param {string} resourceId Resource ID.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForResourceWithHttpOperationResponse(resourceId, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForResource(resourceId, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the resource.
   *
   * @param {string} resourceId Resource ID.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForResource(resourceId, 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._listQueryResultsForResource(resourceId, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForResource(resourceId, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the subscription level policy set definition.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policySetDefinitionName Policy set definition name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForPolicySetDefinitionWithHttpOperationResponse(subscriptionId, policySetDefinitionName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForPolicySetDefinition(subscriptionId, policySetDefinitionName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the subscription level policy set definition.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policySetDefinitionName Policy set definition name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForPolicySetDefinition(subscriptionId, policySetDefinitionName, 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._listQueryResultsForPolicySetDefinition(subscriptionId, policySetDefinitionName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForPolicySetDefinition(subscriptionId, policySetDefinitionName, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the subscription level policy definition.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policyDefinitionName Policy definition name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForPolicyDefinitionWithHttpOperationResponse(subscriptionId, policyDefinitionName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForPolicyDefinition(subscriptionId, policyDefinitionName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the subscription level policy definition.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policyDefinitionName Policy definition name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForPolicyDefinition(subscriptionId, policyDefinitionName, 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._listQueryResultsForPolicyDefinition(subscriptionId, policyDefinitionName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForPolicyDefinition(subscriptionId, policyDefinitionName, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the subscription level policy assignment.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policyAssignmentName Policy assignment name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForSubscriptionLevelPolicyAssignmentWithHttpOperationResponse(subscriptionId, policyAssignmentName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForSubscriptionLevelPolicyAssignment(subscriptionId, policyAssignmentName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the subscription level policy assignment.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} policyAssignmentName Policy assignment name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForSubscriptionLevelPolicyAssignment(subscriptionId, policyAssignmentName, 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._listQueryResultsForSubscriptionLevelPolicyAssignment(subscriptionId, policyAssignmentName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForSubscriptionLevelPolicyAssignment(subscriptionId, policyAssignmentName, options, optionalCallback);
    }
  }

  /**
   * Queries policy events for the resource group level policy assignment.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {string} policyAssignmentName Policy assignment name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @param {object} [options.customHeaders] Headers that will be added to the
   * request
   *
   * @returns {Promise} A promise is returned
   *
   * @resolve {HttpOperationResponse<PolicyEventsQueryResults>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  listQueryResultsForResourceGroupLevelPolicyAssignmentWithHttpOperationResponse(subscriptionId, resourceGroupName, policyAssignmentName, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._listQueryResultsForResourceGroupLevelPolicyAssignment(subscriptionId, resourceGroupName, policyAssignmentName, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Queries policy events for the resource group level policy assignment.
   *
   * @param {string} subscriptionId Microsoft Azure subscription ID.
   *
   * @param {string} resourceGroupName Resource group name.
   *
   * @param {string} policyAssignmentName Policy assignment name.
   *
   * @param {object} [options] Optional Parameters.
   *
   * @param {object} [options.queryOptions] Additional parameters for the
   * operation
   *
   * @param {number} [options.queryOptions.top] Maximum number of records to
   * return.
   *
   * @param {string} [options.queryOptions.filter] OData filter expression.
   *
   * @param {string} [options.queryOptions.orderBy] Ordering expression using
   * OData notation. One or more comma-separated column names with an optional
   * "desc" (the default) or "asc", e.g. "$orderby=PolicyAssignmentId, ResourceId
   * asc".
   *
   * @param {string} [options.queryOptions.select] Select expression using OData
   * notation. Limits the columns on each record to just those requested, e.g.
   * "$select=PolicyAssignmentId, ResourceId".
   *
   * @param {date} [options.queryOptions.from] ISO 8601 formatted timestamp
   * specifying the start time of the interval to query. When not specified, the
   * service uses ($to - 1-day).
   *
   * @param {date} [options.queryOptions.to] ISO 8601 formatted timestamp
   * specifying the end time of the interval to query. When not specified, the
   * service uses request time.
   *
   * @param {string} [options.queryOptions.apply] OData apply expression for
   * aggregations.
   *
   * @param {string} [options.queryOptions.expand] The $expand query parameter.
   * For example, to expand policyEvaluationDetails, use
   * $expand=policyEvaluationDetails
   *
   * @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 {PolicyEventsQueryResults} - 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 PolicyEventsQueryResults} 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.
   */
  listQueryResultsForResourceGroupLevelPolicyAssignment(subscriptionId, resourceGroupName, policyAssignmentName, 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._listQueryResultsForResourceGroupLevelPolicyAssignment(subscriptionId, resourceGroupName, policyAssignmentName, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._listQueryResultsForResourceGroupLevelPolicyAssignment(subscriptionId, resourceGroupName, policyAssignmentName, options, optionalCallback);
    }
  }

  /**
   * Gets OData metadata XML document.
   *
   * @param {string} scope A valid scope, i.e. management group, subscription,
   * resource group, or resource ID. Scope used has no effect on metadata
   * returned.
   *
   * @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<String>} - The deserialized result object.
   *
   * @reject {Error} - The error object.
   */
  getMetadataWithHttpOperationResponse(scope, options) {
    let client = this.client;
    let self = this;
    return new Promise((resolve, reject) => {
      self._getMetadata(scope, options, (err, result, request, response) => {
        let httpOperationResponse = new msRest.HttpOperationResponse(request, response);
        httpOperationResponse.body = result;
        if (err) { reject(err); }
        else { resolve(httpOperationResponse); }
        return;
      });
    });
  }

  /**
   * Gets OData metadata XML document.
   *
   * @param {string} scope A valid scope, i.e. management group, subscription,
   * resource group, or resource ID. Scope used has no effect on metadata
   * returned.
   *
   * @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 {String} - 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.
   *
   *                      {string} [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.
   */
  getMetadata(scope, 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._getMetadata(scope, options, (err, result, request, response) => {
          if (err) { reject(err); }
          else { resolve(result); }
          return;
        });
      });
    } else {
      return self._getMetadata(scope, options, optionalCallback);
    }
  }

}

module.exports = PolicyEvents;