2020-08-21 11:53:53 -04:00
|
|
|
import GeneratedTargetModel from '../generated/models/target';
|
2020-09-15 16:36:40 -04:00
|
|
|
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
|
2020-08-25 10:39:48 -04:00
|
|
|
import { computed } from '@ember/object';
|
2020-08-21 11:48:24 -04:00
|
|
|
|
2020-08-24 18:03:45 -04:00
|
|
|
export default class TargetModel extends GeneratedTargetModel {
|
|
|
|
|
// =attributes
|
|
|
|
|
|
2020-09-15 16:36:40 -04:00
|
|
|
/**
|
|
|
|
|
* Attributes of this resource, if any, represented as a JSON fragment.
|
|
|
|
|
* @type {FragmentTargetAttributesModel}
|
|
|
|
|
*/
|
|
|
|
|
@fragment('fragment-target-attributes', { defaultValue: {} }) attributes;
|
|
|
|
|
|
2020-08-25 10:39:48 -04:00
|
|
|
/**
|
2021-09-20 15:10:04 -04:00
|
|
|
* @type {[FragmentHostSourceModel]}
|
2020-08-25 10:39:48 -04:00
|
|
|
*/
|
2021-09-20 15:10:04 -04:00
|
|
|
@fragmentArray('fragment-host-source', {
|
2020-09-12 10:42:23 -04:00
|
|
|
readOnly: true,
|
2020-09-16 14:29:02 -04:00
|
|
|
emptyArrayIfMissing: true,
|
|
|
|
|
})
|
2021-09-22 19:15:09 -04:00
|
|
|
host_sources;
|
2020-08-24 18:03:45 -04:00
|
|
|
|
2021-07-20 16:09:59 -04:00
|
|
|
/**
|
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()`.
|
2021-07-20 16:09:59 -04:00
|
|
|
*/
|
2021-07-21 15:45:45 -04:00
|
|
|
@fragmentArray('fragment-string', {
|
2021-07-20 16:09:59 -04:00
|
|
|
readOnly: true,
|
|
|
|
|
emptyArrayIfMissing: true,
|
|
|
|
|
})
|
2021-09-22 17:39:15 -04:00
|
|
|
application_credential_source_ids;
|
2021-07-20 16:09:59 -04:00
|
|
|
|
2020-08-25 10:39:48 -04:00
|
|
|
/**
|
|
|
|
|
* 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')
|
2020-08-25 10:39:48 -04:00
|
|
|
get hostSets() {
|
2021-09-22 19:15:09 -04:00
|
|
|
return this.host_sources
|
2021-09-21 09:37:47 -04:00
|
|
|
.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-08-25 10:39:48 -04:00
|
|
|
}))
|
2020-09-16 14:29:02 -04:00
|
|
|
.filter((hostSetRef) => hostSetRef.model !== null);
|
2020-08-25 10:39:48 -04:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 15:45:45 -04:00
|
|
|
/**
|
|
|
|
|
* 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]}
|
|
|
|
|
*/
|
2021-09-22 17:39:15 -04:00
|
|
|
@computed('application_credential_source_ids.[]', 'store')
|
2021-07-21 15:45:45 -04:00
|
|
|
get credentialLibraries() {
|
2021-09-22 17:39:15 -04:00
|
|
|
return this.application_credential_source_ids
|
2021-09-23 10:04:23 -04:00
|
|
|
.map((source) =>
|
|
|
|
|
this.store.peekRecord('credential-library', source.value)
|
2021-07-21 15:45:45 -04:00
|
|
|
)
|
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
2021-02-10 13:19:24 -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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 18:03:45 -04:00
|
|
|
// =methods
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-22 18:52:38 -04:00
|
|
|
* Adds host sets via the `add-host-sources` method.
|
2020-08-24 18:03:45 -04:00
|
|
|
* See serializer and adapter for more information.
|
2020-08-26 14:03:43 -04:00
|
|
|
* @param {[string]} hostSetIDs
|
2020-08-24 18:03:45 -04:00
|
|
|
* @param {object} options
|
|
|
|
|
* @param {object} options.adapterOptions
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2021-09-21 14:53:56 -04:00
|
|
|
addHostSources(hostSetIDs, options = { adapterOptions: {} }) {
|
2020-08-25 10:39:48 -04:00
|
|
|
const defaultAdapterOptions = {
|
2021-09-22 18:52:38 -04:00
|
|
|
method: 'add-host-sources',
|
2020-09-16 14:29:02 -04:00
|
|
|
hostSetIDs,
|
2020-08-26 13:52:54 -04:00
|
|
|
};
|
|
|
|
|
// There is no "deep merge" in ES.
|
|
|
|
|
return this.save({
|
|
|
|
|
...options,
|
|
|
|
|
adapterOptions: {
|
|
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-08-26 13:52:54 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-09-22 18:52:38 -04:00
|
|
|
* Delete host sets via the `remove-host-sources` method.
|
2020-08-26 13:52:54 -04:00
|
|
|
* See serializer and adapter for more information.
|
2020-08-26 14:03:43 -04:00
|
|
|
* @param {[string]} hostSetIDs
|
2020-08-26 13:52:54 -04:00
|
|
|
* @param {object} options
|
|
|
|
|
* @param {object} options.adapterOptions
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2021-09-21 14:53:56 -04:00
|
|
|
removeHostSources(hostSetIDs, options = { adapterOptions: {} }) {
|
2020-08-26 13:52:54 -04:00
|
|
|
const defaultAdapterOptions = {
|
2021-09-22 18:52:38 -04:00
|
|
|
method: 'remove-host-sources',
|
2020-09-16 14:29:02 -04:00
|
|
|
hostSetIDs,
|
2020-08-24 18:03:45 -04:00
|
|
|
};
|
|
|
|
|
// There is no "deep merge" in ES.
|
|
|
|
|
return this.save({
|
|
|
|
|
...options,
|
|
|
|
|
adapterOptions: {
|
2020-08-25 10:39:48 -04:00
|
|
|
...defaultAdapterOptions,
|
2020-09-16 14:29:02 -04:00
|
|
|
...options.adapterOptions,
|
|
|
|
|
},
|
2020-08-24 18:03:45 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 12:59:26 -04:00
|
|
|
/**
|
2021-09-22 18:52:38 -04:00
|
|
|
* Delete a single host set via the `remove-host-sources` method.
|
2020-08-27 15:21:40 -04:00
|
|
|
* @param {number} hostSetID
|
2020-08-27 12:59:26 -04:00
|
|
|
* @param {object} options
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2021-09-21 14:53:56 -04:00
|
|
|
removeHostSource(hostSetID, options) {
|
|
|
|
|
return this.removeHostSources([hostSetID], options);
|
2020-08-27 12:59:26 -04:00
|
|
|
}
|
2021-07-21 15:45:45 -04:00
|
|
|
|
|
|
|
|
/**
|
2021-09-22 18:34:35 -04:00
|
|
|
* Adds credential libraries via the `add-credential-sources` method.
|
2021-07-21 15:45:45 -04:00
|
|
|
* 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: {} }) {
|
2021-07-21 15:45:45 -04:00
|
|
|
const defaultAdapterOptions = {
|
2021-09-22 18:34:35 -04:00
|
|
|
method: 'add-credential-sources',
|
2021-07-21 15:45:45 -04:00
|
|
|
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.
|
2021-07-21 15:45:45 -04:00
|
|
|
* 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(
|
2021-07-21 15:45:45 -04:00
|
|
|
credentialLibraryIDs,
|
|
|
|
|
options = { adapterOptions: {} }
|
|
|
|
|
) {
|
|
|
|
|
const defaultAdapterOptions = {
|
2021-09-22 18:34:35 -04:00
|
|
|
method: 'remove-credential-sources',
|
2021-07-21 15:45:45 -04:00
|
|
|
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.
|
2021-07-21 15:45:45 -04:00
|
|
|
* @param {number} credentialLibraryID
|
|
|
|
|
* @param {object} options
|
|
|
|
|
* @return {Promise}
|
|
|
|
|
*/
|
2021-09-21 14:53:56 -04:00
|
|
|
removeCredentialSource(credentialLibraryID, options) {
|
|
|
|
|
return this.removeCredentialSources([credentialLibraryID], options);
|
2021-07-21 15:45:45 -04:00
|
|
|
}
|
2020-08-24 18:03:45 -04:00
|
|
|
}
|