Icu 18506 load resource IDs for autocomplete (#3247)
* feat: 🎸 Add id auto completion * style: 💄 Fix code editor styling for dark mode * feat: 🎸 Add optimization for caching sqlite data ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-18506
This commit is contained in:
1 parent
1d126dc074
commit
e5657a8d4c
11 files changed
+860
-128
No files matched your search
@@ -22,6 +22,7 @@
|
||||
@ariaLabel={{t 'form.secret-editor.label'}}
|
||||
@value={{this.grantStringsText}}
|
||||
@onInput={{this.onInput}}
|
||||
@onSetup={{this.onSetup}}
|
||||
@customExtensions={{this.customExtensions}}
|
||||
@cspNonce={{(csp-nonce)}}
|
||||
data-test-code-editor
|
||||
|
||||
@@ -17,50 +17,155 @@ import {
|
||||
} from '@hashicorp/design-system-components/codemirror';
|
||||
import { createGrantCompletionSource } from 'admin/utils/grant-completions';
|
||||
import { createGrantLinter } from 'admin/utils/grant-linter';
|
||||
import {
|
||||
GRANT_SCOPE_THIS,
|
||||
GRANT_SCOPE_CHILDREN,
|
||||
GRANT_SCOPE_DESCENDANTS,
|
||||
GRANT_SCOPE_KEYWORDS,
|
||||
} from 'api/models/role';
|
||||
|
||||
export default class FormRoleEditGrantsComponent extends Component {
|
||||
@service intl;
|
||||
@service db;
|
||||
|
||||
// =attributes
|
||||
|
||||
#editorView;
|
||||
exportOptionsMap = { terraform: 'terraform', nativeHCL: 'native-hcl' };
|
||||
exportOptions = Object.values(this.exportOptionsMap);
|
||||
completionTranslatedStrings = {
|
||||
noSuggestions: this.intl.t('resources.role.edit-grants.no-suggestions'),
|
||||
wildcardTypes: this.intl.t(
|
||||
'resources.role.edit-grants.completion-info.wildcard-types',
|
||||
),
|
||||
wildcardIds: this.intl.t(
|
||||
'resources.role.edit-grants.completion-info.wildcard-ids',
|
||||
),
|
||||
templateValue: this.intl.t(
|
||||
'resources.role.edit-grants.completion-info.template-value',
|
||||
),
|
||||
wildcardActions: this.intl.t(
|
||||
'resources.role.edit-grants.completion-info.wildcard-actions',
|
||||
),
|
||||
allFields: this.intl.t(
|
||||
'resources.role.edit-grants.completion-info.all-fields',
|
||||
),
|
||||
};
|
||||
|
||||
translateLintingError = (key, options = {}) =>
|
||||
this.intl.t(`resources.role.edit-grants.linting-errors.${key}`, options);
|
||||
|
||||
completionSource = createGrantCompletionSource(
|
||||
this.args.grantsSchema,
|
||||
this.completionTranslatedStrings,
|
||||
);
|
||||
linterSource = createGrantLinter(
|
||||
this.args.grantsSchema,
|
||||
this.translateLintingError,
|
||||
);
|
||||
|
||||
@tracked grantStringsText = (this.args.model?.grant_strings ?? []).join('\n');
|
||||
@tracked currentLineText = this.args.model?.grant_strings?.[0] ?? '';
|
||||
@tracked showExportOptionsFlyout = false;
|
||||
@tracked selectedExportOption = this.exportOptions[0];
|
||||
|
||||
/**
|
||||
* Returns the scope IDs that should be used to filter ID suggestions.
|
||||
* @returns {Promise<string[]|null>}
|
||||
*/
|
||||
async getAllowedScopeIds() {
|
||||
const grantScopeIds = this.args.model?.grant_scope_ids ?? [];
|
||||
const roleScopeId = this.args.model?.scopeID;
|
||||
|
||||
// Descendants covers the entire subtree so no need to filter
|
||||
if (grantScopeIds.includes(GRANT_SCOPE_DESCENDANTS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const scopeIds = new Set();
|
||||
|
||||
if (grantScopeIds.includes(GRANT_SCOPE_THIS)) {
|
||||
scopeIds.add(roleScopeId);
|
||||
}
|
||||
|
||||
if (grantScopeIds.includes(GRANT_SCOPE_CHILDREN)) {
|
||||
// Grab direct children scopes
|
||||
try {
|
||||
const childScopes = await this.db.query('scope', {
|
||||
query: { filters: { scope_id: [{ equals: roleScopeId }] } },
|
||||
select: [{ field: 'id' }],
|
||||
});
|
||||
childScopes.forEach(({ id }) => {
|
||||
scopeIds.add(id);
|
||||
});
|
||||
} catch {
|
||||
// In cases of errors, just return all IDs without filtering rather than blocking completions entirely
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Any concrete org/project IDs specified directly
|
||||
grantScopeIds
|
||||
.filter((id) => !GRANT_SCOPE_KEYWORDS.includes(id))
|
||||
.forEach((id) => scopeIds.add(id));
|
||||
|
||||
return scopeIds.size ? [...scopeIds] : null;
|
||||
}
|
||||
|
||||
getIsLoading = () => {
|
||||
return this.args.loadResourcesTask?.isRunning ?? false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Looks up resource IDs from the local DB.
|
||||
* @param {string} partial - The current partial value the user has typed
|
||||
* @param {string[]|null} [compatibleResourceTypes] - Resource types to restrict results to
|
||||
* @returns {Promise<Array<{id: string, name: string|undefined}>>}
|
||||
*/
|
||||
idLookup = async (partial, compatibleResourceTypes) => {
|
||||
// When the partial already contains an underscore we know the ID prefix
|
||||
// (e.g. "ttcp_") and can narrow down which types to query.
|
||||
let typesToQuery;
|
||||
if (partial.includes('_')) {
|
||||
const idPrefix = partial.split('_')[0];
|
||||
const matchingGrantTypes = (this.args.grantsSchema?.resource_types ?? [])
|
||||
.filter((rt) => rt.id_prefixes?.includes(idPrefix))
|
||||
.map((rt) => rt.type);
|
||||
|
||||
typesToQuery = (this.args.searchableResourceTypes ?? []).filter((t) =>
|
||||
matchingGrantTypes.includes(t),
|
||||
);
|
||||
} else {
|
||||
typesToQuery = this.args.searchableResourceTypes ?? [];
|
||||
}
|
||||
|
||||
// Restrict to the compatible resource types if provided
|
||||
if (compatibleResourceTypes) {
|
||||
typesToQuery = typesToQuery.filter((t) =>
|
||||
compatibleResourceTypes.includes(t),
|
||||
);
|
||||
}
|
||||
|
||||
if (!typesToQuery.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const scopeIds = await this.getAllowedScopeIds();
|
||||
const filters = {};
|
||||
if (scopeIds) {
|
||||
filters.scope_id = scopeIds.map((id) => ({ equals: id }));
|
||||
}
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
typesToQuery.map((type) =>
|
||||
this.db.query(type, {
|
||||
query: {
|
||||
// Search both id and name so a user can type a resource name and
|
||||
// still get the ID inserted as the completion
|
||||
search: partial
|
||||
? { text: partial, fields: ['id', 'name'] }
|
||||
: undefined,
|
||||
filters,
|
||||
},
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
select: [{ field: 'id' }, { field: 'name' }],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return results
|
||||
.filter(({ status }) => status === 'fulfilled')
|
||||
.flatMap(({ value }) => value)
|
||||
.map(({ id, name }) => ({ id, name }));
|
||||
};
|
||||
|
||||
translateLintingError = (key, options = {}) =>
|
||||
this.intl.t(`resources.role.edit-grants.linting-errors.${key}`, options);
|
||||
translateCompletionString = (key, options = {}) =>
|
||||
this.intl.t(`resources.role.edit-grants.completion-info.${key}`, options);
|
||||
|
||||
completionSource = createGrantCompletionSource(
|
||||
this.args.grantsSchema,
|
||||
this.translateCompletionString,
|
||||
this.idLookup,
|
||||
this.getIsLoading,
|
||||
);
|
||||
linterSource = createGrantLinter(
|
||||
this.args.grantsSchema,
|
||||
this.translateLintingError,
|
||||
);
|
||||
|
||||
customExtensions = [
|
||||
autocompletion({
|
||||
override: [this.completionSource],
|
||||
@@ -72,6 +177,11 @@ export default class FormRoleEditGrantsComponent extends Component {
|
||||
keymap.of([...completionKeymap, ...lintKeymap]),
|
||||
];
|
||||
|
||||
willDestroy() {
|
||||
super.willDestroy();
|
||||
this.#editorView?.destroy();
|
||||
}
|
||||
|
||||
get grantStrings() {
|
||||
return this.grantStringsText
|
||||
.split('\n')
|
||||
@@ -118,6 +228,11 @@ export default class FormRoleEditGrantsComponent extends Component {
|
||||
|
||||
// =actions
|
||||
|
||||
@action
|
||||
onSetup(view) {
|
||||
this.#editorView = view;
|
||||
}
|
||||
|
||||
@action
|
||||
onInput(value, view) {
|
||||
this.grantStringsText = value;
|
||||
|
||||
Reference in new issue
Block a user