Skip to content

Commit

Permalink
[Tags] Expose TagsClient and utils on SOTagging server (elastic#208109)
Browse files Browse the repository at this point in the history
Fixes elastic#206948

## Summary

Adds TagsClient and utility methods on SavedObjectsTagging server
plugin.

This will unblock the Dashboard HTTP API handling tag references rather
than requiring consumers to provide references.


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...
  • Loading branch information
nickpeihl authored Jan 24, 2025
1 parent 000d682 commit c30e87a
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 146 deletions.
3 changes: 2 additions & 1 deletion src/platform/plugins/shared/dashboard/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"home",
"spaces",
"savedObjectsTaggingOss",
"savedObjectsTagging",
"screenshotMode",
"usageCollection",
"taskManager",
Expand All @@ -51,4 +52,4 @@
"savedObjects"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('getSpaceAwareSaveobjectsClients', () => {
const mockedSavedObjectTagging = {
createInternalAssignmentService: jest.fn(),
createTagClient: jest.fn(),
getTagsFromReferences: jest.fn(),
convertTagNameToId: jest.fn(),
replaceTagReferences: jest.fn(),
};

const scoppedSoClient = savedObjectsClientMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ export {
tagNameMaxLength,
tagDescriptionMaxLength,
} from './validation';
export {
convertTagNameToId,
getObjectTags,
getTag,
getTagIdsFromReferences,
getTagsFromReferences,
replaceTagReferences,
tagIdToReference,
} from './references';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
* 2.0.
*/

import { SavedObjectReference } from '@kbn/core/types';
import { tagIdToReference, replaceTagReferences, updateTagReferences } from './references';
import type { SavedObject, SavedObjectReference } from '@kbn/core/server';
import {
convertTagNameToId,
getObjectTags,
getTag,
getTagIdsFromReferences,
replaceTagReferences,
tagIdToReference,
updateTagReferences,
} from './references';

const ref = (type: string, id: string): SavedObjectReference => ({
id,
Expand All @@ -16,6 +24,80 @@ const ref = (type: string, id: string): SavedObjectReference => ({

const tagRef = (id: string) => ref('tag', id);

const createObject = (refs: SavedObjectReference[]): SavedObject => {
return {
type: 'unkown',
id: 'irrelevant',
references: refs,
} as SavedObject;
};

const createTag = (id: string, name: string = id) => ({
id,
name,
description: `desc ${id}`,
color: '#FFCC00',
managed: false,
});

const tag1 = createTag('id-1', 'name-1');
const tag2 = createTag('id-2', 'name-2');
const tag3 = createTag('id-3', 'name-3');

const allTags = [tag1, tag2, tag3];

describe('convertTagNameToId', () => {
it('returns the id for the given tag name', () => {
expect(convertTagNameToId('name-2', allTags)).toBe('id-2');
});

it('returns undefined if no tag was found', () => {
expect(convertTagNameToId('name-4', allTags)).toBeUndefined();
});
});

describe('getObjectTags', () => {
it('returns the tags for the tag references of the object', () => {
const { tags } = getObjectTags(
createObject([tagRef('id-1'), ref('dashboard', 'dash-1'), tagRef('id-3')]),
allTags
);

expect(tags).toEqual([tag1, tag3]);
});

it('returns the missing references for tags that were not found', () => {
const missingRef = tagRef('missing-tag');
const refs = [tagRef('id-1'), ref('dashboard', 'dash-1'), missingRef];
const { tags, missingRefs } = getObjectTags(createObject(refs), allTags);

expect(tags).toEqual([tag1]);
expect(missingRefs).toEqual([missingRef]);
});
});

describe('getTag', () => {
it('returns the tag for the given id', () => {
expect(getTag('id-2', allTags)).toEqual(tag2);
});
it('returns undefined if no tag was found', () => {
expect(getTag('id-4', allTags)).toBeUndefined();
});
});

describe('getTagIdsFromReferences', () => {
it('returns the tag ids from the given references', () => {
expect(
getTagIdsFromReferences([
tagRef('tag-1'),
ref('dashboard', 'dash-1'),
tagRef('tag-2'),
ref('lens', 'lens-1'),
])
).toEqual(['tag-1', 'tag-2']);
});
});

describe('tagIdToReference', () => {
it('returns a reference for given tag id', () => {
expect(tagIdToReference('some-tag-id')).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
*/

import { uniq, intersection } from 'lodash';
import { SavedObjectReference } from '@kbn/core/types';
import type {
SavedObject,
SavedObjectReference,
SavedObjectsFindOptionsReference,
} from '@kbn/core/server';
import { tagSavedObjectTypeName } from './constants';
import { Tag } from './types';

type SavedObjectReferenceLike = SavedObjectReference | SavedObjectsFindOptionsReference;

/**
* Create a {@link SavedObjectReference | reference} for given tag id.
Expand Down Expand Up @@ -64,3 +71,43 @@ export const updateTagReferences = ({

return [...nonTagReferences, ...newTagIds.map(tagIdToReference)];
};

export const getTagsFromReferences = (references: SavedObjectReference[], allTags: Tag[]) => {
const tagReferences = references.filter((ref) => ref.type === tagSavedObjectTypeName);

const foundTags: Tag[] = [];
const missingRefs: SavedObjectReference[] = [];

tagReferences.forEach((ref) => {
const found = allTags.find((tag) => tag.id === ref.id);
if (found) {
foundTags.push(found);
} else {
missingRefs.push(ref);
}
});

return {
tags: foundTags,
missingRefs,
};
};

export const convertTagNameToId = (tagName: string, allTags: Tag[]): string | undefined => {
const found = allTags.find((tag) => tag.name.toLowerCase() === tagName.toLowerCase());
return found?.id;
};

export const getObjectTags = (
object: { references: SavedObject['references'] },
allTags: Tag[]
) => {
return getTagsFromReferences(object.references, allTags);
};

export const getTag = (tagId: string, allTags: Tag[]): Tag | undefined => {
return allTags.find(({ id }) => id === tagId);
};
export const getTagIdsFromReferences = (references: SavedObjectReferenceLike[]): string[] => {
return references.filter((ref) => ref.type === tagSavedObjectTypeName).map(({ id }) => id);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import useObservable from 'react-use/lib/useObservable';
import type { SavedObjectReference } from '@kbn/core/types';
import type { TagListComponentProps } from '@kbn/saved-objects-tagging-oss-plugin/public';
import type { Tag, TagWithOptionalId } from '../../../common/types';
import { getObjectTags } from '../../utils';
import { TagList } from '../base';
import type { ITagsCache } from '../../services';
import { byNameTagSorter } from '../../utils';
import { getObjectTags } from '../../../common';

interface SavedObjectTagListProps {
object: { references: SavedObjectReference[] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { SavedObjectsTaggingApiUi } from '@kbn/saved-objects-tagging-oss-plugin/public';
import { convertTagNameToId } from '../../common';
import { ITagsCache } from '../services';
import { convertTagNameToId } from '../utils';

export interface BuildConvertNameToReferenceOptions {
cache: ITagsCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
GetTableColumnDefinitionOptions,
} from '@kbn/saved-objects-tagging-oss-plugin/public';
import { ITagsCache } from '../services';
import { getTagsFromReferences, byNameTagSorter } from '../utils';
import { byNameTagSorter } from '../utils';
import { getTagsFromReferences } from '../../common';

export interface BuildGetTableColumnDefinitionOptions {
components: SavedObjectsTaggingApiUiComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { ITagsCache, ITagInternalClient } from '../services';
import { StartServices } from '../types';
import {
getTagIdsFromReferences,
updateTagsReferences,
replaceTagReferences,
convertTagNameToId,
getTag,
} from '../utils';
} from '../../common';
import { getComponents } from './components';
import { buildGetTableColumnDefinition } from './get_table_column_definition';
import { buildGetSearchBarFilter } from './get_search_bar_filter';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const getUiApi = ({
convertNameToReference: buildConvertNameToReference({ cache }),
getTagIdsFromReferences,
getTagIdFromName: (tagName: string) => convertTagNameToId(tagName, cache.getState()),
updateTagsReferences,
updateTagsReferences: replaceTagReferences,
getTag: (tagId: string) => getTag(tagId, cache.getState()),
getTagList,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
* 2.0.
*/

import { SavedObject, SavedObjectReference } from '@kbn/core/types';
import {
getObjectTags,
convertTagNameToId,
byNameTagSorter,
getTagIdsFromReferences,
getTag,
} from './utils';
import { byNameTagSorter } from './utils';

const createTag = (id: string, name: string = id) => ({
id,
Expand All @@ -22,67 +15,6 @@ const createTag = (id: string, name: string = id) => ({
managed: false,
});

const ref = (type: string, id: string): SavedObjectReference => ({
id,
type,
name: `${type}-ref-${id}`,
});

const tagRef = (id: string) => ref('tag', id);

const createObject = (refs: SavedObjectReference[]): SavedObject => {
return {
type: 'unkown',
id: 'irrelevant',
references: refs,
} as SavedObject;
};

const tag1 = createTag('id-1', 'name-1');
const tag2 = createTag('id-2', 'name-2');
const tag3 = createTag('id-3', 'name-3');

const allTags = [tag1, tag2, tag3];

describe('getObjectTags', () => {
it('returns the tags for the tag references of the object', () => {
const { tags } = getObjectTags(
createObject([tagRef('id-1'), ref('dashboard', 'dash-1'), tagRef('id-3')]),
allTags
);

expect(tags).toEqual([tag1, tag3]);
});

it('returns the missing references for tags that were not found', () => {
const missingRef = tagRef('missing-tag');
const refs = [tagRef('id-1'), ref('dashboard', 'dash-1'), missingRef];
const { tags, missingRefs } = getObjectTags(createObject(refs), allTags);

expect(tags).toEqual([tag1]);
expect(missingRefs).toEqual([missingRef]);
});
});

describe('convertTagNameToId', () => {
it('returns the id for the given tag name', () => {
expect(convertTagNameToId('name-2', allTags)).toBe('id-2');
});

it('returns undefined if no tag was found', () => {
expect(convertTagNameToId('name-4', allTags)).toBeUndefined();
});
});

describe('getTag', () => {
it('returns the tag for the given id', () => {
expect(getTag('id-2', allTags)).toEqual(tag2);
});
it('returns undefined if no tag was found', () => {
expect(getTag('id-4', allTags)).toBeUndefined();
});
});

describe('byNameTagSorter', () => {
it('sorts tags by name', () => {
const tags = [
Expand All @@ -97,16 +29,3 @@ describe('byNameTagSorter', () => {
expect(tags.map(({ id }) => id)).toEqual(['id-2', 'id-1', 'id-4', 'id-3']);
});
});

describe('getTagIdsFromReferences', () => {
it('returns the tag ids from the given references', () => {
expect(
getTagIdsFromReferences([
tagRef('tag-1'),
ref('dashboard', 'dash-1'),
tagRef('tag-2'),
ref('lens', 'lens-1'),
])
).toEqual(['tag-1', 'tag-2']);
});
});
Loading

0 comments on commit c30e87a

Please sign in to comment.