Skip to content

Commit

Permalink
[Saved Objects] Add a root level managed property to all saved object…
Browse files Browse the repository at this point in the history
… documents (#154515)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
TinaHeiligers and kibanamachine authored Apr 20, 2023
1 parent d6aba14 commit 45449ac
Show file tree
Hide file tree
Showing 41 changed files with 565 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ export interface SavedObjectsCreateOptions {
typeMigrationVersion?: string;
/** Array of referenced saved objects. */
references?: SavedObjectReference[];
/**
* Flag indicating if a saved object is managed by Kibana (default=false)
*
* This can be leveraged by applications to e.g. prevent edits to a managed
* saved object. Instead, users can be guided to create a copy first and
* make their edits to the copy.
*/
managed?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export interface SimpleSavedObject<T = unknown> {
* `namespaceType: 'agnostic'`.
*/
namespaces: SavedObjectType<T>['namespaces'];
/**
* Flag indicating if a saved object is managed by Kibana (default=false)
*
* This can be leveraged by applications to e.g. prevent edits to a managed
* saved object. Instead, users can be guided to create a copy first and
* make their edits to the copy.
*/
managed: SavedObjectType<T>['managed'];

/**
* Gets an attribute of this object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('getRootFields', () => {
"migrationVersion",
"coreMigrationVersion",
"typeMigrationVersion",
"managed",
"updated_at",
"created_at",
"originId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ROOT_FIELDS = [
'migrationVersion',
'coreMigrationVersion',
'typeMigrationVersion',
'managed',
'updated_at',
'created_at',
'originId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
normalizeNamespace,
rawDocExistsInNamespace,
rawDocExistsInNamespaces,
setManaged,
} from './internal_utils';

describe('#getBulkOperationError', () => {
Expand Down Expand Up @@ -99,6 +100,7 @@ describe('#getSavedObjectFromSource', () => {
const originId = 'originId';
// eslint-disable-next-line @typescript-eslint/naming-convention
const updated_at = 'updatedAt';
const managed = false;

function createRawDoc(
type: string,
Expand All @@ -115,6 +117,7 @@ describe('#getSavedObjectFromSource', () => {
migrationVersion,
coreMigrationVersion,
typeMigrationVersion,
managed,
originId,
updated_at,
...namespaceAttrs,
Expand All @@ -131,6 +134,7 @@ describe('#getSavedObjectFromSource', () => {
attributes,
coreMigrationVersion,
typeMigrationVersion,
managed,
id,
migrationVersion,
namespaces: expect.anything(), // see specific test cases below
Expand Down Expand Up @@ -406,3 +410,26 @@ describe('#getCurrentTime', () => {
expect(getCurrentTime()).toEqual('2021-09-10T21:00:00.000Z');
});
});

describe('#setManaged', () => {
it('returns false if no arguments are provided', () => {
expect(setManaged({})).toEqual(false);
});

it('returns false if only one argument is provided as false', () => {
expect(setManaged({ optionsManaged: false })).toEqual(false);
expect(setManaged({ objectManaged: false })).toEqual(false);
});

it('returns true if only one argument is provided as true', () => {
expect(setManaged({ optionsManaged: true })).toEqual(true);
expect(setManaged({ objectManaged: true })).toEqual(true);
});

it('overrides objectManaged with optionsManaged', () => {
expect(setManaged({ optionsManaged: false, objectManaged: true })).toEqual(false);
expect(setManaged({ optionsManaged: true, objectManaged: false })).toEqual(true);
expect(setManaged({ optionsManaged: false, objectManaged: false })).toEqual(false);
expect(setManaged({ optionsManaged: true, objectManaged: true })).toEqual(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export function getSavedObjectFromSource<T>(
created_at: createdAt,
coreMigrationVersion,
typeMigrationVersion,
managed,
migrationVersion = migrationVersionCompatibility === 'compatible' && typeMigrationVersion
? { [type]: typeMigrationVersion }
: undefined,
Expand All @@ -169,6 +170,7 @@ export function getSavedObjectFromSource<T>(
version: encodeHitVersion(doc),
attributes: doc._source[type],
references: doc._source.references || [],
managed,
};
}

Expand Down Expand Up @@ -272,3 +274,30 @@ export function normalizeNamespace(namespace?: string) {
export function getCurrentTime() {
return new Date(Date.now()).toISOString();
}

/**
* Returns the managed boolean to apply to a document as it's managed value.
* For use by applications to modify behavior for managed saved objects.
* The behavior is as follows:
* If `optionsManaged` is set, it will override any existing `managed` value in all the documents being created
* If `optionsManaged` is not provided, then the documents are created with whatever may be assigned to their `managed` property
* or default to `false`.
*
* @internal
*/

export function setManaged({
optionsManaged,
objectManaged,
}: {
optionsManaged?: boolean;
objectManaged?: boolean;
}): boolean {
if (optionsManaged !== undefined) {
return optionsManaged;
} else if (optionsManaged === undefined && objectManaged !== undefined) {
return objectManaged;
} else {
return false;
}
}
Loading

0 comments on commit 45449ac

Please sign in to comment.