vdi.set() supports size as string

This commit is contained in:
Julien Fontanet 2015-06-25 16:34:10 +02:00
parent 09c28d2311
commit 93d63538bb
3 changed files with 33 additions and 6 deletions

View File

@ -1,11 +1,10 @@
# FIXME: rename to disk.* # FIXME: rename to disk.*
$isArray = require 'lodash.isarray' $isArray = require 'lodash.isarray'
#---------------------------------------------------------------------
{coroutine: $coroutine} = require 'bluebird' {coroutine: $coroutine} = require 'bluebird'
{parseSize} = require '../utils'
#===================================================================== #=====================================================================
delete_ = $coroutine ({vdi}) -> delete_ = $coroutine ({vdi}) ->
@ -34,7 +33,7 @@ set = $coroutine (params) ->
# Size. # Size.
if 'size' of params if 'size' of params
{size} = params size = parseSize(params.size)
if size < vdi.size if size < vdi.size
@throw( @throw(
@ -65,7 +64,7 @@ set.params = {
name_description: { type: 'string', optional: true } name_description: { type: 'string', optional: true }
# size of VDI # size of VDI
size: { type: 'integer', optional: true } size: { type: ['integer', 'string'], optional: true }
} }
set.resolve = { set.resolve = {

View File

@ -3,6 +3,7 @@ import forEach from 'lodash.foreach'
import has from 'lodash.has' import has from 'lodash.has'
import humanFormat from 'human-format' import humanFormat from 'human-format'
import isArray from 'lodash.isarray' import isArray from 'lodash.isarray'
import isString from 'lodash.isstring'
import multiKeyHashInt from 'multikey-hash' import multiKeyHashInt from 'multikey-hash'
import xml2js from 'xml2js' import xml2js from 'xml2js'
import {promisify, method} from 'bluebird' import {promisify, method} from 'bluebird'
@ -89,6 +90,10 @@ export const pFinally = (promise, cb) => {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
export function parseSize (size) { export function parseSize (size) {
if (!isString(size)) {
return size
}
let bytes = humanFormat.parse.raw(size, { scale: 'binary' }) let bytes = humanFormat.parse.raw(size, { scale: 'binary' })
if (bytes.unit && bytes.unit !== 'B') { if (bytes.unit && bytes.unit !== 'B') {
bytes = humanFormat.parse.raw(size) bytes = humanFormat.parse.raw(size)

View File

@ -7,7 +7,8 @@ import expect from 'must'
import { import {
ensureArray, ensureArray,
extractProperty, extractProperty,
formatXml formatXml,
parseSize
} from './utils' } from './utils'
// =================================================================== // ===================================================================
@ -66,3 +67,25 @@ describe('formatXml()', function () {
</foo>`) </foo>`)
}) })
}) })
// -------------------------------------------------------------------
describe('parseSize()', function () {
it('parses a human size', function () {
expect(parseSize('1G')).to.equal(1e9)
})
it('returns the parameter if already a number', function () {
expect(parseSize(1e6)).to.equal(1e6)
})
it('throws if the string cannot be parsed', function () {
expect(function () {
parseSize('foo')
}).to.throw()
})
it('supports the B unit as suffix', function () {
expect(parseSize('3MB')).to.equal(3e6)
})
})