Skip to content

Commit

Permalink
Add enabledOperators parameter for workspace name unique check
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <[email protected]>
  • Loading branch information
Hailong-am committed Mar 27, 2024
1 parent e3ce2e2 commit e2273ad
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ export class SavedObjectsClient {
namespaces: 'namespaces',
preference: 'preference',
workspaces: 'workspaces',
enabledOperators: 'enabledOperators',
};

const renamedQuery = renameKeys<SavedObjectsFindOptions, any>(renameMap, {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ export class SavedObjectsRepository {
workspaces,
workspacesSearchOperator,
ACLSearchParams,
enabledOperators,
} = options;

if (!type && !typeToNamespacesMap) {
Expand Down Expand Up @@ -877,6 +878,7 @@ export class SavedObjectsRepository {
workspaces,
workspacesSearchOperator,
ACLSearchParams,
enabledOperators,
}),
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,25 @@ describe('#getQueryParams', () => {
);
});
});

describe('`enabledOperators` parameter', () => {
it('does not include flags when `enabledOperators` is not specified', () => {
const result = getQueryParams({
registry,
search,
});
expectResult(result, expect.not.objectContaining({ flags: expect.anything() }));
});

it('includes flags when `enabledOperators` specified', () => {
const result = getQueryParams({
registry,
search,
enabledOperators: 'all',
});
expectResult(result, expect.objectContaining({ flags: expect.stringMatching('all') }));
});
});
});

describe('when using prefix search (query.bool.should)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ interface QueryParams {
workspaces?: string[];
workspacesSearchOperator?: 'AND' | 'OR';
ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams'];
enabledOperators?: SavedObjectsFindOptions['enabledOperators'];
}

export function getClauseForReference(reference: HasReferenceQueryParams) {
Expand Down Expand Up @@ -229,6 +230,7 @@ export function getQueryParams({
workspaces,
workspacesSearchOperator = 'AND',
ACLSearchParams,
enabledOperators,
}: QueryParams) {
const types = getTypes(
registry,
Expand Down Expand Up @@ -261,6 +263,7 @@ export function getQueryParams({
searchFields,
rootSearchFields,
defaultSearchOperator,
enabledOperators,
});

if (useMatchPhrasePrefix) {
Expand Down Expand Up @@ -432,18 +435,21 @@ const getSimpleQueryStringClause = ({
searchFields,
rootSearchFields,
defaultSearchOperator,
enabledOperators,
}: {
search: string;
types: string[];
searchFields?: string[];
rootSearchFields?: string[];
defaultSearchOperator?: string;
enabledOperators?: SavedObjectsFindOptions['enabledOperators'];
}) => {
return {
simple_query_string: {
query: search,
...getSimpleQueryStringTypeFields(types, searchFields, rootSearchFields),
...(defaultSearchOperator ? { default_operator: defaultSearchOperator } : {}),
...(enabledOperators ? { flags: enabledOperators } : {}),
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface GetSearchDslOptions {
workspaces?: string[];
workspacesSearchOperator?: 'AND' | 'OR';
ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams'];
enabledOperators?: SavedObjectsFindOptions['enabledOperators'];
}

export function getSearchDsl(
Expand All @@ -78,6 +79,7 @@ export function getSearchDsl(
workspaces,
workspacesSearchOperator,
ACLSearchParams,
enabledOperators,
} = options;

if (!type) {
Expand All @@ -103,6 +105,7 @@ export function getSearchDsl(
workspaces,
workspacesSearchOperator,
ACLSearchParams,
enabledOperators,
}),
...getSortingParams(mappings, type, sortField, sortOrder),
};
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export interface SavedObjectsFindOptions {
search?: string;
/** The fields to perform the parsed query against. See OpenSearch Simple Query String `fields` argument for more information */
searchFields?: string[];
/** The enabled operators for OpenSearch Simple Query String. See OpenSearch Simple Query String `flags` argument for more information */
enabledOperators?: string;
/**
* The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not
* be modified. If used in conjunction with `searchFields`, both are concatenated together.
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
type: WORKSPACE_TYPE,
search: attributes.name,
searchFields: ['name'],
enabledOperators: 'NONE', // disable all operators, treat workspace as literal string
}
);
if (existingWorkspaceRes && existingWorkspaceRes.total > 0) {
Expand Down Expand Up @@ -250,6 +251,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
search: attributes.name,
searchFields: ['name'],
fields: ['_id'],
enabledOperators: 'NONE', // disable all operators, treat workspace as literal string
});
if (existingWorkspaceRes && existingWorkspaceRes.total > 0) {
throw new Error(DUPLICATE_WORKSPACE_NAME_ERROR);
Expand Down

0 comments on commit e2273ad

Please sign in to comment.