<commit_before>/* eslint camelcase: "off" */

/** @ngInject */
export function VmsService(CollectionsApi) {
  const collection = 'vms';
  const sort = {
    isAscending: true,
    currentField: {id: 'name', title: __('Name'), sortType: 'alpha'},
  };
  let filters = [];

  return {
    getVm: getVm,
    getSort: getSort,
    setSort: setSort,
    getFilters: getFilters,
    setFilters: setFilters,
    getSnapshots: getSnapshots,
    createSnapshots: createSnapshots,
    deleteSnapshots: deleteSnapshots,
  };

  function getSnapshots(vmId) {
    const options = {
      attributes: [],
      expand: ['resources'],
      filter: getQueryFilters(getFilters()),
    };

    options.sort_by = getSort().currentField.id;
    options.sort_options = getSort().currentField.sortType === 'alpha' ? 'ignore_case' : '';
    options.sort_order = getSort().isAscending ? 'asc' : 'desc';

    return CollectionsApi.query(collection + '/' + vmId + '/snapshots', options);
  }

  function getVm(vmId) {
    const options = {
      attributes: ['service'],
      expand: [],
    };

    return CollectionsApi.query(collection + '/' + vmId, options);
  }

  function setSort(currentField, isAscending) {
    sort.isAscending = isAscending;
    sort.currentField = currentField;
  }

  function getSort() {
    return sort;
  }

  function setFilters(filterArray) {
    filters = filterArray;
  }

  function getFilters() {
    return filters;
  }

  function deleteSnapshots(vmId, data) {
    const options = {
      "action": "delete",
      "resources": data,
    };

    return CollectionsApi.post(collection + '/' + vmId + '/snapshots/', null, {}, options);
  }

  function createSnapshots(vmId, data) {
    const options = {
      "action": "create",
      "resources": [data],
    };

    return CollectionsApi.post(collection + '/' + vmId + '/snapshots/', null, {}, options);
  }

  // Private
  function getQueryFilters(filters) {
    const queryFilters = [];

    filters.forEach((nextFilter) => {
      if (nextFilter.id === 'name' || nextFilter.id === 'description') {
        queryFilters.push(nextFilter.id + "='%" + nextFilter.value + "%'");
      } else {
        queryFilters.push(nextFilter.id + '=' + nextFilter.value);
      }
    });
<commit_msg>Update snapshot service to include rbac and revert snapshot action<commit_after>/* eslint camelcase: "off" */

/** @ngInject */
export function VmsService(CollectionsApi, RBAC) {
  const collection = 'vms';
  const sort = {
    isAscending: true,
    currentField: {id: 'name', title: __('Name'), sortType: 'alpha'},
  };
  let filters = [];

  return {
    getVm: getVm,
    getSort: getSort,
    setSort: setSort,
    getFilters: getFilters,
    setFilters: setFilters,
    getSnapshots: getSnapshots,
    getPermissions: getPermissions,
    revertSnapshot: revertSnapshot,
    createSnapshots: createSnapshots,
    deleteSnapshots: deleteSnapshots,
  };

  function getSnapshots(vmId) {
    const options = {
      attributes: [],
      expand: ['resources'],
      filter: getQueryFilters(getFilters()),
    };

    options.sort_by = getSort().currentField.id;
    options.sort_options = getSort().currentField.sortType === 'alpha' ? 'ignore_case' : '';
    options.sort_order = getSort().isAscending ? 'asc' : 'desc';

    return CollectionsApi.query(collection + '/' + vmId + '/snapshots', options);
  }

  function getVm(vmId) {
    const options = {
      attributes: ['service'],
      expand: [],
    };

    return CollectionsApi.query(collection + '/' + vmId, options);
  }

  function setSort(currentField, isAscending) {
    sort.isAscending = isAscending;
    sort.currentField = currentField;
  }

  function getSort() {
    return sort;
  }

  function setFilters(filterArray) {
    filters = filterArray;
  }

  function getFilters() {
    return filters;
  }

  function getPermissions() {
    return {
      create: RBAC.hasAny(['vm_snapshot_add']),
      delete: RBAC.hasAny(['vm_snapshot_delete']),
      deleteAll: RBAC.hasAny(['vm_snapshot_delete_all']),
      revert: RBAC.hasAny(['vm_snapshot_revert']),
    };
  }

  function deleteSnapshots(vmId, data) {
    const options = {
      "action": "delete",
      "resources": data,
    };

    return CollectionsApi.post(collection + '/' + vmId + '/snapshots/', null, {}, options);
  }

  function createSnapshots(vmId, data) {
    const options = {
      "action": "create",
      "resources": [data],
    };

    return CollectionsApi.post(collection + '/' + vmId + '/snapshots/', null, {}, options);
  }

  function revertSnapshot(vmId, snapshotId) {
    const options = {
      "action": "revert"
    };

    return CollectionsApi.post(collection + '/' + vmId + '/snapshots/' + snapshotId, null, {}, options);
  }

  // Private
  function getQueryFilters(filters) {
    const queryFilters = [];

    filters.forEach((nextFilter) => {
      if (nextFilter.id === 'name' || nextFilter.id === 'description') {
        queryFilters.push(nextFilter.id + "='%" + nextFilter.value + "%'");
      } else {
        queryFilters.push(nextFilter.id + '=' + nextFilter.value);
      }
    });

{'fraction_non_alphanumeric': 0.15098767846665362, 'fraction_numerical': 0.0, 'mean_word_length': 3.104333868378812, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '650067', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1612, 'n_words': 321}