Files
threshold-ui/addons/api/addon/models/target.js
T
Randall MoreyandZhihe Li cde6abd445 refactor: 💡 convert brokered cred IDs array to simple attribute (#1259)
* refactor: 💡 convert brokered cred IDs array to simple attribute

* fix: 🐛 missing test assertion

* fix: 🐛 simplify computed

* refactor: 💡 convert host sources array to simple attribute (#1260)

* refactor: 💡 convert host source IDs array to simple attribute

* fix: 🐛 host source tests

* Refactor role fragment arrays (#1262)

* refactor: 💡 convert grant strings to regular attribute

* refactor: 💡 convert principals array to simple attribute

* Update ui/admin/app/routes/scopes/scope/roles/role.js

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

* fix: 🐛 remove unused import

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
2022-08-31 09:55:59 -07:00

230 lines
6.1 KiB
JavaScript

import GeneratedTargetModel from '../generated/models/target';
import { attr } from '@ember-data/model';
export default class TargetModel extends GeneratedTargetModel {
// =attributes
@attr('number', {
isNestedAttribute: true,
description: 'The default port a target should use if present.',
})
default_port;
/**
* @type Array
*/
@attr('host-source-id-array', {
readOnly: true,
emptyArrayIfMissing: true,
})
host_sources;
/**
* Credential source ids are read only and can be
* persisted via a dedicated call to `addBrokeredCredentialSources()`.
*/
@attr('string-array', {
readOnly: true,
emptyArrayIfMissing: true,
})
brokered_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}]}
*/
get hostSets() {
return this.host_sources
.map(({ host_source_id, host_catalog_id }) => ({
model: this.store.peekRecord('host-set', host_source_id),
hostCatalog: this.store.peekRecord('host-catalog', host_catalog_id),
}))
.filter((hostSetRef) => hostSetRef.model !== null);
}
/**
* An array of resolved credential library and credential 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, CredentialModel]}
*/
get brokeredCredentialSources() {
return this.brokered_credential_source_ids
.map((source) => {
if (source.value.startsWith('cred')) {
return this.store.peekRecord('credential', source.value);
} else {
return this.store.peekRecord('credential-library', source.value);
}
})
.filter(Boolean);
}
/**
* Sessions associated with this target (but only already loaded sessions).
* @type {SessionModel[]}
*/
get sessions() {
return this.store
.peekAll('session')
.filter((s) => s && s.target_id === this.id);
}
/**
* The project associated with this target (if already loaded).
* @type {ScopeModel}
*/
get project() {
return this.store.peekRecord('scope', this.scopeID);
}
/**
* True if any sessions associated with this target are active or pending.
* @type {boolean}
*/
get isActive() {
const pendingOrActiveSessions = this.sessions.filter(
(s) => s.isActive || s.isPending
);
return Boolean(pendingOrActiveSessions.length);
}
// =methods
/**
* Adds host sets via the `add-host-sources` method.
* See serializer and adapter for more information.
* @param {[string]} hostSetIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addHostSources(hostSetIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'add-host-sources',
hostSetIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete host sets via the `remove-host-sources` method.
* See serializer and adapter for more information.
* @param {[string]} hostSetIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeHostSources(hostSetIDs, options = { adapterOptions: {} }) {
const defaultAdapterOptions = {
method: 'remove-host-sources',
hostSetIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete a single host set via the `remove-host-sources` method.
* @param {number} hostSetID
* @param {object} options
* @return {Promise}
*/
removeHostSource(hostSetID, options) {
return this.removeHostSources([hostSetID], options);
}
/**
* Adds credential sources via the `add-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
addBrokeredCredentialSources(
credentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'add-credential-sources',
credentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete credential libraries and credentials via the `remove-credential-sources` method.
* See serializer and adapter for more information.
* @param {[string]} credentialSourceIDs
* @param {object} options
* @param {object} options.adapterOptions
* @return {Promise}
*/
removeBrokeredCredentialSources(
credentialSourceIDs,
options = { adapterOptions: {} }
) {
const defaultAdapterOptions = {
method: 'remove-credential-sources',
credentialSourceIDs,
};
// There is no "deep merge" in ES.
return this.save({
...options,
adapterOptions: {
...defaultAdapterOptions,
...options.adapterOptions,
},
});
}
/**
* Delete a single credential library/credential set via the `remove-credential-sources` method.
* @param {number} credentialSourceID
* @param {object} options
* @return {Promise}
*/
removeBrokeredCredentialSource(credentialSourceID, options) {
return this.removeBrokeredCredentialSources([credentialSourceID], options);
}
/**
* True if the target type is tcp.
* @type {boolean}
*/
get isTCP() {
return this.type === 'tcp';
}
/**
* True if the target type is ssh.
* @type {boolean}
*/
get isSSH() {
return this.type === 'ssh';
}
}