Skip to content

Commit

Permalink
feat(xo-web/self): add default tags for self service users (#6810)
Browse files Browse the repository at this point in the history
See #6812

Add default tags for Self Service users.
  • Loading branch information
MathieuRA authored May 26, 2023
1 parent 8e176ea commit 98caa89
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [OVA import] Workaround for OVA generated by Oracle VM with faulty size in metadata [#6824](https://github.com/vatesfr/xen-orchestra/issues/6824)
- [REST API] _Rolling Pool Update_ action available `pools/<uuid>/actions/rolling_update`
- [Self Service] Ability to set a default value for the "Share VM" feature for Self Service users during creation/edition (PR [#6838](https://github.com/vatesfr/xen-orchestra/pull/6838))
- [Self service] Add default tags to all VMs that will be created by a Self Service (PRs [#6810](https://github.com/vatesfr/xen-orchestra/pull/6810), [#6812](https://github.com/vatesfr/xen-orchestra/pull/6812))

### Bug fixes

Expand Down
21 changes: 18 additions & 3 deletions packages/xo-server/src/api/resource-set.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function create({ name, shareByDefault, subjects, objects, limits }) {
return this.createResourceSet(name, subjects, objects, limits, shareByDefault)
export function create({ name, shareByDefault, subjects, objects, tags, limits }) {
return this.createResourceSet(name, subjects, objects, limits, shareByDefault, tags)
}

create.permission = 'admin'
Expand All @@ -26,6 +26,13 @@ create.params = {
type: 'object',
optional: true,
},
tags: {
type: 'array',
items: {
type: 'string',
},
optional: true,
},
shareByDefault: {
type: 'boolean',
optional: true,
Expand All @@ -49,11 +56,12 @@ delete_.params = {

// -------------------------------------------------------------------

export function set({ id, name, shareByDefault, subjects, objects, ipPools, limits }) {
export function set({ id, name, shareByDefault, subjects, objects, tags, ipPools, limits }) {
return this.updateResourceSet(id, {
limits,
name,
objects,
tags,
ipPools,
shareByDefault,
subjects,
Expand Down Expand Up @@ -99,6 +107,13 @@ set.params = {
type: 'object',
optional: true,
},
tags: {
type: 'array',
items: {
type: 'string',
},
optional: true,
},
}

// -------------------------------------------------------------------
Expand Down
15 changes: 14 additions & 1 deletion packages/xo-server/src/xo-mixins/resource-sets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const normalize = set => ({
objects: set.objects || [],
subjects: set.subjects || [],
shareByDefault: set.shareByDefault || false,
tags: set.tags || [],
})

// ===================================================================
Expand Down Expand Up @@ -134,13 +135,21 @@ export default class {
return vm.type === 'VM-snapshot' ? this.computeVmSnapshotResourcesUsage(vm) : this.computeVmResourcesUsage(vm)
}

async createResourceSet(name, subjects = undefined, objects = undefined, limits = undefined, shareByDefault = false) {
async createResourceSet(
name,
subjects = undefined,
objects = undefined,
limits = undefined,
shareByDefault = false,
tags = undefined
) {
const id = await this._generateId()
const set = normalize({
id,
name,
objects,
subjects,
tags,
limits,
shareByDefault,
})
Expand Down Expand Up @@ -174,6 +183,7 @@ export default class {
shareByDefault = undefined,
subjects = undefined,
objects = undefined,
tags = undefined,
limits = undefined,
ipPools = undefined,
}
Expand Down Expand Up @@ -231,6 +241,9 @@ export default class {
if (ipPools) {
set.ipPools = ipPools
}
if (tags !== undefined) {
set.tags = tags
}

if (shareByDefault !== undefined && shareByDefault !== set.shareByDefault) {
set.shareByDefault = shareByDefault
Expand Down
1 change: 1 addition & 0 deletions packages/xo-web/src/common/intl/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,7 @@ const messages = {
saveResourceSet: 'Save',
resetResourceSet: 'Reset',
editResourceSet: 'Edit',
defaultTags: 'Default tags',
deleteResourceSet: 'Delete',
deleteResourceSetWarning: 'Delete resource set',
deleteResourceSetQuestion: 'Are you sure you want to delete this resource set?',
Expand Down
9 changes: 6 additions & 3 deletions packages/xo-web/src/common/xo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2513,16 +2513,19 @@ export const sendUsageReport = () => _call('plugin.usageReport.send')

// Resource set ------------------------------------------------------

export const createResourceSet = (name, { shareByDefault, subjects, objects, limits } = {}) =>
_call('resourceSet.create', { name, shareByDefault, subjects, objects, limits })::tap(subscribeResourceSets.forceRefresh)
export const createResourceSet = (name, { shareByDefault, subjects, objects, tags, limits } = {}) =>
_call('resourceSet.create', { name, shareByDefault, subjects, objects, tags, limits })::tap(
subscribeResourceSets.forceRefresh
)

export const editResourceSet = (id, { name, shareByDefault, subjects, objects, limits, ipPools } = {}) =>
export const editResourceSet = (id, { name, shareByDefault, subjects, objects, tags, limits, ipPools } = {}) =>
_call('resourceSet.set', {
id,
name,
shareByDefault,
subjects,
objects,
tags,
limits,
ipPools,
})::tap(subscribeResourceSets.forceRefresh)
Expand Down
42 changes: 40 additions & 2 deletions packages/xo-web/src/xo-app/self/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import remove from 'lodash/remove'
import renderXoItem from 'render-xo-item'
import ResourceSetQuotas from 'resource-set-quotas'
import some from 'lodash/some'
import Tags from 'tags'
import Upgrade from 'xoa-upgrade'
import { Container, Row, Col } from 'grid'
import { injectIntl } from 'react-intl'
Expand All @@ -43,6 +44,10 @@ import Page from '../page'

// ===================================================================

const TAGS_WRAPPER_STYLES = { fontSize: '1.4em' }

// ===================================================================

const HEADER = (
<Container>
<Row>
Expand Down Expand Up @@ -141,6 +146,7 @@ export class Edit extends Component {
srs: [],
subjects: [],
templates: [],
tags: [],
}
}

Expand Down Expand Up @@ -178,13 +184,14 @@ export class Edit extends Component {
name: resourceSet.name,
shareByDefault: resourceSet.shareByDefault || false,
subjects: resourceSet.subjects,
tags: resourceSet.tags || [],
templates: objectsByType['VM-template'] || [],
})
}
}

_save = async () => {
const { cpus, disk, ipPools, memory, name, networks, shareByDefault, srs, subjects, templates } = this.state
const { cpus, disk, ipPools, memory, name, networks, shareByDefault, srs, subjects, tags, templates } = this.state

const set = this.props.resourceSet || (await createResourceSet(name))
const objects = [...templates, ...srs, ...networks]
Expand All @@ -207,6 +214,7 @@ export class Edit extends Component {
objects: resolveIds(objects),
shareByDefault,
subjects: resolveIds(subjects),
tags,
ipPools: resolveIds(ipPools),
})

Expand All @@ -225,6 +233,7 @@ export class Edit extends Component {
newIpPoolQuantity: '',
shareByDefault: false,
subjects: [],
tags: [],
})
}

Expand Down Expand Up @@ -312,6 +321,18 @@ export class Edit extends Component {

// -----------------------------------------------------------------------------

_onRemoveTag = tag =>
this.setState(prevState => ({
tags: prevState.tags.filter(_tag => tag === _tag),
}))

_onAddTag = tag =>
this.setState(prevState => ({
tags: prevState.tags.concat(tag),
}))

// -----------------------------------------------------------------------------

render() {
const { state } = this
const { onCancel, resourceSet } = this.props
Expand Down Expand Up @@ -496,6 +517,20 @@ export class Edit extends Component {
</Col>
</Row>
</Col>
<Col mediumSize={4}>
<Row>
<Col>
<strong>{_('defaultTags')}</strong>
</Col>
</Row>
<Row>
<Col>
<span style={TAGS_WRAPPER_STYLES}>
<Tags labels={state.tags} onAdd={this._onAddTag} onDelete={this._onRemoveTag} />
</span>
</Col>
</Row>
</Col>
</Row>
</div>
<div className='mt-1'>
Expand Down Expand Up @@ -540,7 +575,7 @@ class ResourceSet extends Component {
_renderDisplay = () => {
const { resourceSet } = this.props
const resolvedIpPools = mapKeys(this.props.ipPools, 'id')
const { limits, ipPools, subjects, objectsByType } = resourceSet
const { limits, ipPools, subjects, objectsByType, tags } = resourceSet

return [
<li key='subjects' className='list-group-item'>
Expand Down Expand Up @@ -575,6 +610,9 @@ class ResourceSet extends Component {
})}
</li>
),
<li key='tags' className='list-group-item'>
<Icon icon='tags' /> {tags.join(', ')}
</li>,
<li key='graphs' className='list-group-item'>
<ResourceSetQuotas limits={limits} />
</li>,
Expand Down

0 comments on commit 98caa89

Please sign in to comment.