<commit_before>'use strict'

const identity = require('lodash/identity.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors')
const { Ref } = require('xen-api')

const isVmRunning = require('./_isVmRunning.js')

const noop = Function.prototype

module.exports = class Vbd {
  async create({
    bootable = false,
    currently_attached = false,
    device = '',
    other_config = {},
    qos_algorithm_params = {},
    qos_algorithm_type = '',
    type = 'Disk',
    unpluggable = false,
    userdevice,
    VDI,
    VM,

    empty = !Ref.isNotEmpty(VDI),
    mode = type === 'Disk' ? 'RW' : 'RO',
  }) {
    if (userdevice == null) {
      const allowed = await this.call('VM.get_allowed_VBD_devices', VM)
      const { length } = allowed
      if (length === 0) {
        throw new Error('no allowed VBD devices')
      }

      if (type === 'CD') {
        // Choose position 3 if allowed.
        userdevice = allowed.includes('3') ? '3' : allowed[0]
      } else {
        userdevice = allowed[0]

        // Avoid userdevice 3 if possible.
        if (userdevice === '3' && length > 1) {
          userdevice = allowed[1]
        }
      }
    }

    const powerState = await this.getField('VM', VM, 'power_state')
    const ifVmSuspended = powerState === 'Suspended' ? identity : noop

    // By default a VBD is unpluggable.
    const vbdRef = await this.call('VBD.create', {
      bootable,
      currently_attached: ifVmSuspended(currently_attached),
      device: ifVmSuspended(device),
      empty,
      mode,
      other_config,
      qos_algorithm_params,
      qos_algorithm_type,
      type,
      unpluggable,
      userdevice,
      VDI,
      VM,
    })

    if (isVmRunning(powerState)) {
      await this.callAsync('VBD.plug', vbdRef)
    }
  }
<commit_msg>feat(xapi/VBD_create): returns the new VBD's ref<commit_after>'use strict'

const identity = require('lodash/identity.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors')
const { Ref } = require('xen-api')

const isVmRunning = require('./_isVmRunning.js')

const noop = Function.prototype

module.exports = class Vbd {
  async create({
    bootable = false,
    currently_attached = false,
    device = '',
    other_config = {},
    qos_algorithm_params = {},
    qos_algorithm_type = '',
    type = 'Disk',
    unpluggable = false,
    userdevice,
    VDI,
    VM,

    empty = !Ref.isNotEmpty(VDI),
    mode = type === 'Disk' ? 'RW' : 'RO',
  }) {
    if (userdevice == null) {
      const allowed = await this.call('VM.get_allowed_VBD_devices', VM)
      const { length } = allowed
      if (length === 0) {
        throw new Error('no allowed VBD devices')
      }

      if (type === 'CD') {
        // Choose position 3 if allowed.
        userdevice = allowed.includes('3') ? '3' : allowed[0]
      } else {
        userdevice = allowed[0]

        // Avoid userdevice 3 if possible.
        if (userdevice === '3' && length > 1) {
          userdevice = allowed[1]
        }
      }
    }

    const powerState = await this.getField('VM', VM, 'power_state')
    const ifVmSuspended = powerState === 'Suspended' ? identity : noop

    // By default a VBD is unpluggable.
    const vbdRef = await this.call('VBD.create', {
      bootable,
      currently_attached: ifVmSuspended(currently_attached),
      device: ifVmSuspended(device),
      empty,
      mode,
      other_config,
      qos_algorithm_params,
      qos_algorithm_type,
      type,
      unpluggable,
      userdevice,
      VDI,
      VM,
    })

    if (isVmRunning(powerState)) {
      await this.callAsync('VBD.plug', vbdRef)
    }

    return vbdRef
  }

{'fraction_non_alphanumeric': 0.12898789878987899, 'fraction_numerical': 0.005500550055005501, 'mean_word_length': 2.4246704331450095, 'pattern_counts': {'":': 0, '<': 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': '7027008', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1178, 'n_words': 273}