From 37250a6f48ca537275a6337156f3a1b4ac57609c Mon Sep 17 00:00:00 2001 From: Olivier Lambert Date: Fri, 11 Sep 2015 19:30:11 +0200 Subject: [PATCH] Support tags on Xen objects. --- src/api/index.js | 3 ++- src/api/tag.js | 31 +++++++++++++++++++++++++++++++ src/xapi-objects-to-xo.js | 6 ++++++ src/xapi.js | 22 ++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/api/tag.js diff --git a/src/api/index.js b/src/api/index.js index 10676801b..e0d69f178 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -11,16 +11,17 @@ export * as group from './group' export * as host from './host' export * as job from './job' export * as message from './message' -export * as remote from './remote' export * as pbd from './pbd' export * as pif from './pif' export * as pool from './pool' +export * as remote from './remote' export * as role from './role' export * as schedule from './schedule' export * as scheduler from './scheduler' export * as server from './server' export * as session from './session' export * as sr from './sr' +export * as tag from './tag' export * as task from './task' export * as test from './test' export * as token from './token' diff --git a/src/api/tag.js b/src/api/tag.js new file mode 100644 index 000000000..2109c9432 --- /dev/null +++ b/src/api/tag.js @@ -0,0 +1,31 @@ +export async function add ({tag, id}) { + await this.getXAPI(id).addTag(id, tag) +} + +add.description = 'add a new tag to an object' + +add.resolve = { + object: ['id', null, 'administrate'] +} + +add.params = { + tag: { type: 'string' }, + id: { type: 'string' } +} + +// ------------------------------------------------------------------- + +export async function remove ({tag, id}) { + await this.getXAPI(id).removeTag(id, tag) +} + +remove.description = 'remove an existing tag from an object' + +remove.resolve = { + object: ['id', null, 'administrate'] +} + +remove.params = { + tag: { type: 'string' }, + id: { type: 'string' } +} diff --git a/src/xapi-objects-to-xo.js b/src/xapi-objects-to-xo.js index 4bcc255e5..7cd8612f7 100644 --- a/src/xapi-objects-to-xo.js +++ b/src/xapi-objects-to-xo.js @@ -55,6 +55,7 @@ export function pool (obj) { default_SR: link(obj, 'default_SR'), HA_enabled: Boolean(obj.ha_enabled), master: link(obj, 'master'), + tags: obj.tags, name_description: obj.name_description, name_label: obj.name_label || obj.$master.name_label @@ -111,6 +112,7 @@ export function host (obj) { patches: link(obj, 'patches'), powerOnMode: obj.power_on_mode, power_state: isRunning ? 'Running' : 'Halted', + tags: obj.tags, version: obj.software_version.product_version, // TODO: dedupe. @@ -225,6 +227,7 @@ export function vm (obj) { PV_drivers_up_to_date: Boolean(guestMetrics && guestMetrics.PV_drivers_up_to_date), snapshot_time: toTimestamp(obj.snapshot_time), snapshots: link(obj, 'snapshots'), + tags: obj.tags, VIFs: link(obj, 'VIFs'), $container: ( @@ -290,6 +293,7 @@ export function sr (obj) { physical_usage: +obj.physical_utilisation, size: +obj.physical_size, SR_type: obj.type, + tags: obj.tags, usage: +obj.virtual_allocation, VDIs: link(obj, 'VDIs'), @@ -357,6 +361,7 @@ export function vdi (obj) { size: +obj.virtual_size, snapshots: link(obj, 'snapshots'), snapshot_time: toTimestamp(obj.snapshot_time), + tags: obj.tags, usage: +obj.physical_utilisation, $snapshot_of: link(obj, 'snapshot_of'), @@ -405,6 +410,7 @@ export function network (obj) { MTU: +obj.MTU, name_description: obj.name_description, name_label: obj.name_label, + tags: obj.tags, PIFs: link(obj, 'PIFs'), VIFs: link(obj, 'VIFs') } diff --git a/src/xapi.js b/src/xapi.js index aa3524abc..73a65bfd4 100644 --- a/src/xapi.js +++ b/src/xapi.js @@ -242,6 +242,28 @@ export default class Xapi extends XapiBase { // ================================================================= + async addTag (id, tag) { + const { + $ref: ref, + $type: type + } = this.getObject(id) + + const namespace = getNamespaceForType(type) + await this.call(`${namespace}.add_tags`, ref, tag) + } + + async removeTag (id, tag) { + const { + $ref: ref, + $type: type + } = this.getObject(id) + + const namespace = getNamespaceForType(type) + await this.call(`${namespace}.remove_tags`, ref, tag) + } + + // ================================================================= + // FIXME: should be static @debounce(24 * 60 * 60 * 1000) async _getXenUpdates () {