Files
threshold-ui/ui/admin/app/routes/scopes/scope/targets/target/add-brokered-credential-sources.js
T
hashicorp-copywrite[bot]andCameron Perera 3d610427d0 [COMPLIANCE] Add Copyright and License Headers (#1617)
* [COMPLIANCE] Add Copyright and License Headers

* chore: 🤖 update Copyright headers to satisfy our lint rules

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Cameron Perera <cameron.perera@hashicorp.com>
2023-03-01 09:39:41 -06:00

91 lines
2.4 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { all } from 'rsvp';
import { action } from '@ember/object';
import { loading } from 'ember-loading';
import { notifySuccess, notifyError } from 'core/decorators/notify';
export default class ScopesScopeTargetsTargetAddBrokeredCredentialSourcesRoute extends Route {
// =services
@service store;
@service intl;
@service router;
// =methods
/**
* Empty out any previously loaded credential sources.
*/
beforeModel() {
this.store.unloadAll('credential-library');
this.store.unloadAll('credential');
}
/**
* Returns the current target and credential sources.
* @return {{target: TargetModel, credentialLibraries: [CredentialLibraryModel], credentials: [CredentialModel]}}
*/
async model() {
const target = this.modelFor('scopes.scope.targets.target');
const { id: scope_id } = this.modelFor('scopes.scope');
const credentialStores = await this.store.query('credential-store', {
scope_id,
});
await all(
credentialStores.map(({ id: credential_store_id, isStatic }) => {
if (isStatic) {
return this.store.query('credential', {
credential_store_id,
});
} else {
return this.store.query('credential-library', {
credential_store_id,
});
}
})
);
const credentialLibraries = this.store.peekAll('credential-library');
const credentials = this.store.peekAll('credential');
return {
target,
credentialLibraries,
credentials,
};
}
// =actions
/**
* Add credential libraries to current target
* @param {TargetModel} target
* @param {[string]} credentialLibraryIDs
*/
@action
@loading
@notifyError(({ message }) => message, { catch: true })
@notifySuccess('notifications.add-success')
async save(target, credentialLibraryIDs) {
await target.addBrokeredCredentialSources(credentialLibraryIDs);
this.router.replaceWith(
'scopes.scope.targets.target.brokered-credential-sources'
);
}
/**
* Redirect to target credential sources as if nothing ever happened.
*/
@action
cancel() {
this.router.replaceWith(
'scopes.scope.targets.target.brokered-credential-sources'
);
}
}