-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI: Add capabilities service (#28168)
* add capabilities service * remove from kv engine for now * add canRead * move await helper to addon * add test * update capabilities service to accommodate multiple paths * address comments, make methods more explicit * remove namespace key * fix typo in test * add namespace back! * round out tests for other methods * add test * add comment
- Loading branch information
1 parent
111d6a8
commit 09c92b8
Showing
9 changed files
with
404 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
import Service, { service } from '@ember/service'; | ||
import { assert } from '@ember/debug'; | ||
|
||
import type AdapterError from '@ember-data/adapter/error'; | ||
import type CapabilitiesModel from 'vault/vault/models/capabilities'; | ||
import type StoreService from 'vault/services/store'; | ||
|
||
interface Query { | ||
paths?: string[]; | ||
path?: string; | ||
} | ||
|
||
export default class CapabilitiesService extends Service { | ||
@service declare readonly store: StoreService; | ||
|
||
async request(query: Query) { | ||
if (query?.paths) { | ||
const { paths } = query; | ||
return this.store.query('capabilities', { paths }); | ||
} | ||
if (query?.path) { | ||
const { path } = query; | ||
const storeData = await this.store.peekRecord('capabilities', path); | ||
return storeData ? storeData : this.store.findRecord('capabilities', path); | ||
} | ||
return assert('query object must contain "paths" or "path" key', false); | ||
} | ||
|
||
/* | ||
this method returns a capabilities model for each path in the array of paths | ||
*/ | ||
async fetchMultiplePaths(paths: string[]): Promise<Array<CapabilitiesModel>> | AdapterError { | ||
try { | ||
return await this.request({ paths }); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
/* | ||
this method returns all of the capabilities for a singular path | ||
*/ | ||
async fetchPathCapabilities(path: string): Promise<CapabilitiesModel> | AdapterError { | ||
try { | ||
return await this.request({ path }); | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
|
||
/* | ||
internal method for specific capability checks below | ||
checks the capability model for the passed capability, ie "canRead" | ||
*/ | ||
async _fetchSpecificCapability( | ||
path: string, | ||
capability: string | ||
): Promise<CapabilitiesModel> | AdapterError { | ||
try { | ||
const capabilities = await this.request({ path }); | ||
return capabilities[capability]; | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
async canRead(path: string) { | ||
try { | ||
return await this._fetchSpecificCapability(path, 'canRead'); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
async canUpdate(path: string) { | ||
try { | ||
return await this._fetchSpecificCapability(path, 'canUpdate'); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
export { default } from 'core/helpers/await'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.