chore(api): remove legacy helpers

This commit is contained in:
Julien Fontanet 2016-07-28 12:03:51 +02:00
parent 6df85ecadd
commit 2bd31f4560
5 changed files with 17 additions and 41 deletions

View File

@ -1,4 +1,4 @@
import {deprecate} from 'util' import { deprecate, getUserPublicProperties } from 'util'
import {InvalidCredential, AlreadyAuthenticated} from '../api-errors' import {InvalidCredential, AlreadyAuthenticated} from '../api-errors'
@ -15,7 +15,7 @@ export async function signIn (credentials) {
} }
this.session.set('user_id', user.id) this.session.set('user_id', user.id)
return this.getUserPublicProperties(user) return getUserPublicProperties(user)
} }
signIn.description = 'sign in' signIn.description = 'sign in'
@ -55,7 +55,7 @@ export async function getUser () {
return userId === undefined return userId === undefined
? null ? null
: this.getUserPublicProperties(await this.getUser(userId)) : getUserPublicProperties(await this.getUser(userId))
} }
getUser.description = 'return the currently connected user' getUser.description = 'return the currently connected user'

View File

@ -1,5 +1,5 @@
import {InvalidParameters} from '../api-errors' import {InvalidParameters} from '../api-errors'
import { mapToArray } from '../utils' import { getUserPublicProperties, mapToArray } from '../utils'
// =================================================================== // ===================================================================
@ -48,7 +48,7 @@ export async function getAll () {
const users = await this.getAllUsers() const users = await this.getAllUsers()
// Filters out private properties. // Filters out private properties.
return mapToArray(users, this.getUserPublicProperties) return mapToArray(users, getUserPublicProperties)
} }
getAll.description = 'returns all the existing users' getAll.description = 'returns all the existing users'

View File

@ -819,8 +819,7 @@ stop = $coroutine ({vm, force}) ->
yield xapi.call 'VM.clean_shutdown', vm._xapiRef yield xapi.call 'VM.clean_shutdown', vm._xapiRef
catch error catch error
if error.code is 'VM_MISSING_PV_DRIVERS' or error.code is 'VM_LACKS_FEATURE_SHUTDOWN' if error.code is 'VM_MISSING_PV_DRIVERS' or error.code is 'VM_LACKS_FEATURE_SHUTDOWN'
# TODO: Improve reporting: this message is unclear. throw new InvalidParameters('clean shutdown requires PV drivers')
@throw 'INVALID_PARAMS'
else else
throw error throw error
@ -1116,10 +1115,7 @@ setBootOrder = $coroutine ({vm, order}) ->
yield xapi.call 'VM.set_HVM_boot_params', vm._xapiRef, order yield xapi.call 'VM.set_HVM_boot_params', vm._xapiRef, order
return true return true
@throw( throw new InvalidParameters('You can only set the boot order on a HVM guest')
'INVALID_PARAMS'
'You can only set the boot order on a HVM guest'
)
setBootOrder.params = { setBootOrder.params = {
vm: { type: 'string' }, vm: { type: 'string' },

View File

@ -9,7 +9,6 @@ import eventToPromise from 'event-to-promise'
import has from 'lodash/has' import has from 'lodash/has'
import helmet from 'helmet' import helmet from 'helmet'
import includes from 'lodash/includes' import includes from 'lodash/includes'
import pick from 'lodash/pick'
import proxyConsole from './proxy-console' import proxyConsole from './proxy-console'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import startsWith from 'lodash/startsWith' import startsWith from 'lodash/startsWith'
@ -18,14 +17,8 @@ import { compile as compilePug } from 'pug'
import { createServer as createProxyServer } from 'http-proxy' import { createServer as createProxyServer } from 'http-proxy'
import { join as joinPath } from 'path' import { join as joinPath } from 'path'
import {
AlreadyAuthenticated,
InvalidCredential,
InvalidParameters,
NoSuchObject,
NotImplemented
} from './api-errors'
import JsonRpcPeer from 'json-rpc-peer' import JsonRpcPeer from 'json-rpc-peer'
import { InvalidCredential } from './api-errors'
import { import {
readFile, readFile,
readdir readdir
@ -407,27 +400,6 @@ const setUpStaticFiles = (express, opts) => {
// =================================================================== // ===================================================================
const errorClasses = {
ALREADY_AUTHENTICATED: AlreadyAuthenticated,
INVALID_CREDENTIAL: InvalidCredential,
INVALID_PARAMS: InvalidParameters,
NO_SUCH_OBJECT: NoSuchObject,
NOT_IMPLEMENTED: NotImplemented
}
const apiHelpers = {
getUserPublicProperties (user) {
// Handles both properties and wrapped models.
const properties = user.properties || user
return pick(properties, 'id', 'email', 'groups', 'permission', 'preferences', 'provider')
},
throw (errorId, data) {
throw new (errorClasses[errorId])(data)
}
}
const setUpApi = (webServer, xo, verboseLogsOnErrors) => { const setUpApi = (webServer, xo, verboseLogsOnErrors) => {
const webSocketServer = new WebSocket.Server({ const webSocketServer = new WebSocket.Server({
server: webServer, server: webServer,
@ -437,7 +409,7 @@ const setUpApi = (webServer, xo, verboseLogsOnErrors) => {
// FIXME: it can cause issues if there any property assignments in // FIXME: it can cause issues if there any property assignments in
// XO methods called from the API. // XO methods called from the API.
const context = { __proto__: xo, ...apiHelpers } const context = { __proto__: xo }
const api = new Api({ const api = new Api({
context, context,

View File

@ -11,6 +11,7 @@ import isString from 'lodash/isString'
import keys from 'lodash/keys' import keys from 'lodash/keys'
import kindOf from 'kindof' import kindOf from 'kindof'
import multiKeyHashInt from 'multikey-hash' import multiKeyHashInt from 'multikey-hash'
import pick from 'lodash/pick'
import xml2js from 'xml2js' import xml2js from 'xml2js'
// Moment timezone can be loaded only one time, it's a workaround to load // Moment timezone can be loaded only one time, it's a workaround to load
@ -177,6 +178,13 @@ export function extractProperty (obj, prop) {
// ------------------------------------------------------------------- // -------------------------------------------------------------------
export const getUserPublicProperties = user => pick(
user.properties || user,
'id', 'email', 'groups', 'permission', 'preferences', 'provider'
)
// -------------------------------------------------------------------
export const getPseudoRandomBytes = n => { export const getPseudoRandomBytes = n => {
const bytes = new Buffer(n) const bytes = new Buffer(n)