Files
threshold-ui/addons/api/addon/models/target.js
T

210 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-08-21 11:53:53 -04:00
import GeneratedTargetModel from '../generated/models/target';
2022-01-18 13:44:27 -05:00
import { attr } from '@ember-data/model';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
import { computed } from '@ember/object';
2020-08-21 11:48:24 -04:00
export default class TargetModel extends GeneratedTargetModel {
// =attributes
2022-01-18 13:44:27 -05:00
@attr('number', {
isNestedAttribute: true,
description: 'The default port a target should use if present.',
})
default_port;
/**
* @type {[FragmentHostSourceModel]}
*/
@fragmentArray('fragment-host-source', {
readOnly: true,
2020-09-16 14:29:02 -04:00
emptyArrayIfMissing: true,
})
2021-09-22 19:15:09 -04:00
host_sources;
/**
2021-09-23 10:04:23 -04:00
* Credential source ids are read only and can be
2021-09-21 14:53:56 -04:00
* persisted via a dedicated call to `addCredentialSources()`.
*/
@fragmentArray('fragment-string', {
readOnly: true,
emptyArrayIfMissing: true,
})
application_credential_source_ids;
/**
* An array of resolved host set and host catalog instances. Model instances
* must already be loaded into the store (this method will not load unloaded
* instances). Unresolvable instances are excluded from the array.
* @type {[{model: HostSetModel, hostCatalog: HostCatalogModel}]}
*/
2021-09-22 19:15:09 -04:00
@computed('host_sources.[]', 'store')
get hostSets() {
2021-09-22 19:15:09 -04:00
return this.host_sources
.map(({ host_source_id, host_catalog_id }) => ({
model: this.store.peekRecord('host-set', host_source_id),
2020-09-16 14:29:02 -04:00
hostCatalog: this.store.peekRecord('host-catalog', host_catalog_id),
}))
2020-09-16 14:29:02 -04:00
.filter((hostSetRef) => hostSetRef.model !== null);
}
/**
* An array of resolved credential library instances. Model instances
* must already be loaded into the store (this method will not load unloaded
* instances). Unresolvable instances are excluded from the array.
* @type {[CredentialLibraryModel]}
*/
@computed('application_credential_source_ids.[]', 'store')
get credentialLibraries() {
return this.application_credential_source_ids
2021-09-23 10:04:23 -04:00
.map((source) =>
this.store.peekRecord('credential-library', source.value)
)
.filter(Boolean);
}
2021-02-05 16:57:06 -05:00
/**
* Sessions associated with this target (but only already loaded sessions).
* @type {SessionModel[]}
*/
get sessions() {
2021-03-04 15:47:38 -05:00
return this.store
.peekAll('session')
.filter((s) => s && s.target_id === this.id);
2021-02-05 16:57:06 -05:00
}
/**
* The project associated with this target (if already loaded).
* @type {ScopeModel}
*/
get project() {
return this.store.peekRecord('scope', this.scopeID);
}
2021-02-05 16:57:06 -05:00
/**
* True if any sessions associated with this target are active or pending.
* @type {boolean}
*/
get isActive() {
2021-03-04 15:47:38 -05:00
const pendingOrActiveSessions = this.sessions.filter(
(s) => s.isActive || s.isPending
);
2021-02-05 16:57:06 -05:00
return Boolean(pendingOrActiveSessions.length);
}
// =methods
/**
2021-09-22 18:52:38 -04:00
* Adds host sets via the `add-host-sources` method.
* See serializer and adapter for more information.
2020-08-26 14:03:43 -04:00
* @param {[string]} hostSetIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
addHostSources(hostSetIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
2021-09-22 18:52:38 -04:00
method: 'add-host-sources',
2020-09-16 14:29:02 -04:00
hostSetIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
});
}
/**
2021-09-22 18:52:38 -04:00
* Delete host sets via the `remove-host-sources` method.
* See serializer and adapter for more information.
2020-08-26 14:03:43 -04:00
* @param {[string]} hostSetIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
removeHostSources(hostSetIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
2021-09-22 18:52:38 -04:00
method: 'remove-host-sources',
2020-09-16 14:29:02 -04:00
hostSetIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
2020-09-16 14:29:02 -04:00
...options.adapterOptions,
},
});
}
/**
2021-09-22 18:52:38 -04:00
* Delete a single host set via the `remove-host-sources` method.
* @param {number} hostSetID
* @param {object} options
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
removeHostSource(hostSetID, options) {
return this.removeHostSources([hostSetID], options);
}
/**
2021-09-22 18:34:35 -04:00
* Adds credential libraries via the `add-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialLibraryIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
addCredentialSources(credentialLibraryIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
2021-09-22 18:34:35 -04:00
method: 'add-credential-sources',
credentialLibraryIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
2021-09-22 18:34:35 -04:00
* Delete credential libraries via the `remove-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialLibraryIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
removeCredentialSources(
credentialLibraryIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
2021-09-22 18:34:35 -04:00
method: 'remove-credential-sources',
credentialLibraryIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
2021-09-22 18:34:35 -04:00
* Delete a single credential library set via the `remove-credential-sources` method.
* @param {number} credentialLibraryID
* @param {object} options
* @return {Promise}
*/
2021-09-21 14:53:56 -04:00
removeCredentialSource(credentialLibraryID, options) {
return this.removeCredentialSources([credentialLibraryID], options);
}
}