<commit_before># FIXME: rename to disk.*

{coroutine: $coroutine} = require 'bluebird'

{format} = require 'json-rpc-peer'
{invalidParameters, unauthorized} = require 'xo-common/api-errors'
{isArray: $isArray, parseSize} = require '../utils'
{JsonRpcError} = require 'json-rpc-peer'

#=====================================================================

delete_ = $coroutine ({vdi}) ->
  yield @getXapi(vdi).deleteVdi(vdi._xapiId)

  return

delete_.params = {
  id: { type: 'string' },
}

delete_.resolve = {
  vdi: ['id', ['VDI', 'VDI-snapshot'], 'administrate'],
}

exports.delete = delete_

#---------------------------------------------------------------------

# FIXME: human readable strings should be handled.
set = $coroutine (params) ->
  {vdi} = params
  xapi = @getXapi vdi
  ref = vdi._xapiRef

  # Size.
  if 'size' of params
    size = parseSize(params.size)

    if size < vdi.size
      throw invalidParameters(
        "cannot set new size (#{size}) below the current size (#{vdi.size})"
      )

    vbds = vdi.$VBDs
    if (
      vbds.length == 1 &&
      (resourceSetId = xapi.xo.getData(@getObject(vbds[0], 'VBD'), 'resourceSet')) != undefined
    )
      if @user.permission != 'admin'
        yield @checkResourceSetConstraints(resourceSetId, @user.id)

      yield @allocateLimitsInResourceSet({ disk: size - vdi.size }, resourceSetId)
    else if !(
      @user.permission == 'admin' ||
      yield @hasPermissions(@user.id, [ [ vdi.$SR, 'operate' ] ])
    )
      throw unauthorized()

    yield xapi.resizeVdi(ref, size)
<commit_msg>fix(vdi.set): properly get resource set when resizing (#585)<commit_after># FIXME: rename to disk.*

{coroutine: $coroutine} = require 'bluebird'

{format} = require 'json-rpc-peer'
{invalidParameters, unauthorized} = require 'xo-common/api-errors'
{isArray: $isArray, parseSize} = require '../utils'
{JsonRpcError} = require 'json-rpc-peer'

#=====================================================================

delete_ = $coroutine ({vdi}) ->
  yield @getXapi(vdi).deleteVdi(vdi._xapiId)

  return

delete_.params = {
  id: { type: 'string' },
}

delete_.resolve = {
  vdi: ['id', ['VDI', 'VDI-snapshot'], 'administrate'],
}

exports.delete = delete_

#---------------------------------------------------------------------

# FIXME: human readable strings should be handled.
set = $coroutine (params) ->
  {vdi} = params
  xapi = @getXapi vdi
  ref = vdi._xapiRef

  # Size.
  if 'size' of params
    size = parseSize(params.size)

    if size < vdi.size
      throw invalidParameters(
        "cannot set new size (#{size}) below the current size (#{vdi.size})"
      )

    vbds = vdi.$VBDs
    if (
      vbds.length == 1 &&
      (resourceSetId = xapi.xo.getData(@getObject(vbds[0], 'VBD').VM, 'resourceSet')) != undefined
    )
      if @user.permission != 'admin'
        yield @checkResourceSetConstraints(resourceSetId, @user.id)

      yield @allocateLimitsInResourceSet({ disk: size - vdi.size }, resourceSetId)
    else if !(
      @user.permission == 'admin' ||
      yield @hasPermissions(@user.id, [ [ vdi.$SR, 'operate' ] ])
    )
      throw unauthorized()

    yield xapi.resizeVdi(ref, size)

{'fraction_non_alphanumeric': 0.2303335431088735, 'fraction_numerical': 0.0022026431718061676, 'mean_word_length': 2.8958333333333335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3469075', 'n_tokens_mistral': 1048, 'n_tokens_neox': 1029, 'n_words': 241}