Skip to content

Commit

Permalink
Rename namespaces to initialNamespaces for create operations (#79682
Browse files Browse the repository at this point in the history
) (#79767)
  • Loading branch information
jportner authored Oct 6, 2020
1 parent d284a34 commit f7bf141
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 59 deletions.
2 changes: 1 addition & 1 deletion docs/api/saved-objects/bulk_create.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ experimental[] Create multiple {kib} saved objects.
`references`::
(Optional, array) Objects with `name`, `id`, and `type` properties that describe the other saved objects in the referenced object. To refer to the other saved object, use `name` in the attributes. Never use `id` to refer to the other saved object. `id` can be automatically updated during migrations, import, or export.

`namespaces`::
`initialNamespaces`::
(Optional, string array) Identifiers for the <<xpack-spaces,spaces>> in which this object should be created. If this is not provided, the
object will be created in the current space.

Expand Down
2 changes: 1 addition & 1 deletion docs/api/saved-objects/create.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ any data that you send to the API is properly formed.
`references`::
(Optional, array) Objects with `name`, `id`, and `type` properties that describe the other saved objects that this object references. Use `name` in attributes to refer to the other saved object, but never the `id`, which can update automatically during migrations or import/export.

`namespaces`::
`initialNamespaces`::
(Optional, string array) Identifiers for the <<xpack-spaces,spaces>> in which this object should be created. If this is not provided, the
object will be created in the current space.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsBulkCreateObject](./kibana-plugin-core-server.savedobjectsbulkcreateobject.md) &gt; [namespaces](./kibana-plugin-core-server.savedobjectsbulkcreateobject.namespaces.md)
[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsBulkCreateObject](./kibana-plugin-core-server.savedobjectsbulkcreateobject.md) &gt; [initialNamespaces](./kibana-plugin-core-server.savedobjectsbulkcreateobject.initialnamespaces.md)

## SavedObjectsBulkCreateObject.namespaces property
## SavedObjectsBulkCreateObject.initialNamespaces property

Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.

Expand All @@ -11,5 +11,5 @@ Note: this can only be used for multi-namespace object types.
<b>Signature:</b>

```typescript
namespaces?: string[];
initialNamespaces?: string[];
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export interface SavedObjectsBulkCreateObject<T = unknown>
| --- | --- | --- |
| [attributes](./kibana-plugin-core-server.savedobjectsbulkcreateobject.attributes.md) | <code>T</code> | |
| [id](./kibana-plugin-core-server.savedobjectsbulkcreateobject.id.md) | <code>string</code> | |
| [initialNamespaces](./kibana-plugin-core-server.savedobjectsbulkcreateobject.initialnamespaces.md) | <code>string[]</code> | Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.<!-- -->Note: this can only be used for multi-namespace object types. |
| [migrationVersion](./kibana-plugin-core-server.savedobjectsbulkcreateobject.migrationversion.md) | <code>SavedObjectsMigrationVersion</code> | Information about the migrations that have been applied to this SavedObject. When Kibana starts up, KibanaMigrator detects outdated documents and migrates them based on this value. For each migration that has been applied, the plugin's name is used as a key and the latest migration version as the value. |
| [namespaces](./kibana-plugin-core-server.savedobjectsbulkcreateobject.namespaces.md) | <code>string[]</code> | Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.<!-- -->Note: this can only be used for multi-namespace object types. |
| [originId](./kibana-plugin-core-server.savedobjectsbulkcreateobject.originid.md) | <code>string</code> | Optional ID of the original saved object, if this object's <code>id</code> was regenerated |
| [references](./kibana-plugin-core-server.savedobjectsbulkcreateobject.references.md) | <code>SavedObjectReference[]</code> | |
| [type](./kibana-plugin-core-server.savedobjectsbulkcreateobject.type.md) | <code>string</code> | |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md) &gt; [namespaces](./kibana-plugin-core-server.savedobjectscreateoptions.namespaces.md)
[Home](./index.md) &gt; [kibana-plugin-core-server](./kibana-plugin-core-server.md) &gt; [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md) &gt; [initialNamespaces](./kibana-plugin-core-server.savedobjectscreateoptions.initialnamespaces.md)

## SavedObjectsCreateOptions.namespaces property
## SavedObjectsCreateOptions.initialNamespaces property

Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.

Expand All @@ -11,5 +11,5 @@ Note: this can only be used for multi-namespace object types.
<b>Signature:</b>

```typescript
namespaces?: string[];
initialNamespaces?: string[];
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions
| Property | Type | Description |
| --- | --- | --- |
| [id](./kibana-plugin-core-server.savedobjectscreateoptions.id.md) | <code>string</code> | (not recommended) Specify an id for the document |
| [initialNamespaces](./kibana-plugin-core-server.savedobjectscreateoptions.initialnamespaces.md) | <code>string[]</code> | Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.<!-- -->Note: this can only be used for multi-namespace object types. |
| [migrationVersion](./kibana-plugin-core-server.savedobjectscreateoptions.migrationversion.md) | <code>SavedObjectsMigrationVersion</code> | Information about the migrations that have been applied to this SavedObject. When Kibana starts up, KibanaMigrator detects outdated documents and migrates them based on this value. For each migration that has been applied, the plugin's name is used as a key and the latest migration version as the value. |
| [namespaces](./kibana-plugin-core-server.savedobjectscreateoptions.namespaces.md) | <code>string[]</code> | Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md)<!-- -->.<!-- -->Note: this can only be used for multi-namespace object types. |
| [originId](./kibana-plugin-core-server.savedobjectscreateoptions.originid.md) | <code>string</code> | Optional ID of the original saved object, if this object's <code>id</code> was regenerated |
| [overwrite](./kibana-plugin-core-server.savedobjectscreateoptions.overwrite.md) | <code>boolean</code> | Overwrite existing documents (defaults to false) |
| [references](./kibana-plugin-core-server.savedobjectscreateoptions.references.md) | <code>SavedObjectReference[]</code> | |
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/bulk_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const registerBulkCreateRoute = (router: IRouter) => {
})
)
),
namespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
})
),
},
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export const registerCreateRoute = (router: IRouter) => {
})
)
),
namespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { type, id } = req.params;
const { overwrite } = req.query;
const { attributes, migrationVersion, references, namespaces } = req.body;
const { attributes, migrationVersion, references, initialNamespaces } = req.body;

const options = { id, overwrite, migrationVersion, references, namespaces };
const options = { id, overwrite, migrationVersion, references, initialNamespaces };
const result = await context.core.savedObjects.client.create(type, attributes, options);
return res.ok({ body: result });
})
Expand Down
38 changes: 20 additions & 18 deletions src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,13 @@ describe('SavedObjectsRepository', () => {
await test(namespace);
});

it(`adds namespaces instead of namespace`, async () => {
it(`adds initialNamespaces instead of namespace`, async () => {
const test = async (namespace) => {
const ns2 = 'bar-namespace';
const ns3 = 'baz-namespace';
const objects = [
{ ...obj1, type: MULTI_NAMESPACE_TYPE, namespaces: [ns2] },
{ ...obj2, type: MULTI_NAMESPACE_TYPE, namespaces: [ns3] },
{ ...obj1, type: MULTI_NAMESPACE_TYPE, initialNamespaces: [ns2] },
{ ...obj2, type: MULTI_NAMESPACE_TYPE, initialNamespaces: [ns3] },
];
await bulkCreateSuccess(objects, { namespace, overwrite: true });
const body = [
Expand Down Expand Up @@ -758,30 +758,30 @@ describe('SavedObjectsRepository', () => {
).rejects.toThrowError(createBadRequestError('"options.namespace" cannot be "*"'));
});

it(`returns error when namespaces is used with a non-multi-namespace object`, async () => {
it(`returns error when initialNamespaces is used with a non-multi-namespace object`, async () => {
const test = async (objType) => {
const obj = { ...obj3, type: objType, namespaces: [] };
const obj = { ...obj3, type: objType, initialNamespaces: [] };
await bulkCreateError(
obj,
undefined,
expectErrorResult(
obj,
createBadRequestError('"namespaces" can only be used on multi-namespace types')
createBadRequestError('"initialNamespaces" can only be used on multi-namespace types')
)
);
};
await test('dashboard');
await test(NAMESPACE_AGNOSTIC_TYPE);
});

it(`throws when options.namespaces is used with a multi-namespace type and is empty`, async () => {
const obj = { ...obj3, type: MULTI_NAMESPACE_TYPE, namespaces: [] };
it(`throws when options.initialNamespaces is used with a multi-namespace type and is empty`, async () => {
const obj = { ...obj3, type: MULTI_NAMESPACE_TYPE, initialNamespaces: [] };
await bulkCreateError(
obj,
undefined,
expectErrorResult(
obj,
createBadRequestError('"namespaces" must be a non-empty array of strings')
createBadRequestError('"initialNamespaces" must be a non-empty array of strings')
)
);
});
Expand Down Expand Up @@ -1993,13 +1993,13 @@ describe('SavedObjectsRepository', () => {
);
});

it(`adds namespaces instead of namespace`, async () => {
const options = { id, namespace, namespaces: ['bar-namespace', 'baz-namespace'] };
it(`adds initialNamespaces instead of namespace`, async () => {
const options = { id, namespace, initialNamespaces: ['bar-namespace', 'baz-namespace'] };
await createSuccess(MULTI_NAMESPACE_TYPE, attributes, options);
expect(client.create).toHaveBeenCalledWith(
expect.objectContaining({
id: `${MULTI_NAMESPACE_TYPE}:${id}`,
body: expect.objectContaining({ namespaces: options.namespaces }),
body: expect.objectContaining({ namespaces: options.initialNamespaces }),
}),
expect.anything()
);
Expand All @@ -2021,23 +2021,25 @@ describe('SavedObjectsRepository', () => {
});

describe('errors', () => {
it(`throws when options.namespaces is used with a non-multi-namespace object`, async () => {
it(`throws when options.initialNamespaces is used with a non-multi-namespace object`, async () => {
const test = async (objType) => {
await expect(
savedObjectsRepository.create(objType, attributes, { namespaces: [namespace] })
savedObjectsRepository.create(objType, attributes, { initialNamespaces: [namespace] })
).rejects.toThrowError(
createBadRequestError('"options.namespaces" can only be used on multi-namespace types')
createBadRequestError(
'"options.initialNamespaces" can only be used on multi-namespace types'
)
);
};
await test('dashboard');
await test(NAMESPACE_AGNOSTIC_TYPE);
});

it(`throws when options.namespaces is used with a multi-namespace type and is empty`, async () => {
it(`throws when options.initialNamespaces is used with a multi-namespace type and is empty`, async () => {
await expect(
savedObjectsRepository.create(MULTI_NAMESPACE_TYPE, attributes, { namespaces: [] })
savedObjectsRepository.create(MULTI_NAMESPACE_TYPE, attributes, { initialNamespaces: [] })
).rejects.toThrowError(
createBadRequestError('"options.namespaces" must be a non-empty array of strings')
createBadRequestError('"options.initialNamespaces" must be a non-empty array of strings')
);
});

Expand Down
28 changes: 14 additions & 14 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,19 @@ export class SavedObjectsRepository {
references = [],
refresh = DEFAULT_REFRESH_SETTING,
originId,
namespaces,
initialNamespaces,
version,
} = options;
const namespace = normalizeNamespace(options.namespace);

if (namespaces) {
if (initialNamespaces) {
if (!this._registry.isMultiNamespace(type)) {
throw SavedObjectsErrorHelpers.createBadRequestError(
'"options.namespaces" can only be used on multi-namespace types'
'"options.initialNamespaces" can only be used on multi-namespace types'
);
} else if (!namespaces.length) {
} else if (!initialNamespaces.length) {
throw SavedObjectsErrorHelpers.createBadRequestError(
'"options.namespaces" must be a non-empty array of strings'
'"options.initialNamespaces" must be a non-empty array of strings'
);
}
}
Expand All @@ -262,9 +262,9 @@ export class SavedObjectsRepository {
// we will overwrite a multi-namespace saved object if it exists; if that happens, ensure we preserve its included namespaces
// note: this check throws an error if the object is found but does not exist in this namespace
const existingNamespaces = await this.preflightGetNamespaces(type, id, namespace);
savedObjectNamespaces = namespaces || existingNamespaces;
savedObjectNamespaces = initialNamespaces || existingNamespaces;
} else {
savedObjectNamespaces = namespaces || getSavedObjectNamespaces(namespace);
savedObjectNamespaces = initialNamespaces || getSavedObjectNamespaces(namespace);
}
}

Expand Down Expand Up @@ -323,14 +323,14 @@ export class SavedObjectsRepository {
let error: DecoratedError | undefined;
if (!this._allowedTypes.includes(object.type)) {
error = SavedObjectsErrorHelpers.createUnsupportedTypeError(object.type);
} else if (object.namespaces) {
} else if (object.initialNamespaces) {
if (!this._registry.isMultiNamespace(object.type)) {
error = SavedObjectsErrorHelpers.createBadRequestError(
'"namespaces" can only be used on multi-namespace types'
'"initialNamespaces" can only be used on multi-namespace types'
);
} else if (!object.namespaces.length) {
} else if (!object.initialNamespaces.length) {
error = SavedObjectsErrorHelpers.createBadRequestError(
'"namespaces" must be a non-empty array of strings'
'"initialNamespaces" must be a non-empty array of strings'
);
}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ export class SavedObjectsRepository {
let versionProperties;
const {
esRequestIndex,
object: { namespaces, version, ...object },
object: { initialNamespaces, version, ...object },
method,
} = expectedBulkGetResult.value;
if (esRequestIndex !== undefined) {
Expand All @@ -410,13 +410,13 @@ export class SavedObjectsRepository {
};
}
savedObjectNamespaces =
namespaces || getSavedObjectNamespaces(namespace, docFound && actualResult);
initialNamespaces || getSavedObjectNamespaces(namespace, docFound && actualResult);
versionProperties = getExpectedVersionProperties(version, actualResult);
} else {
if (this._registry.isSingleNamespace(object.type)) {
savedObjectNamespace = namespace;
} else if (this._registry.isMultiNamespace(object.type)) {
savedObjectNamespaces = namespaces || getSavedObjectNamespaces(namespace);
savedObjectNamespaces = initialNamespaces || getSavedObjectNamespaces(namespace);
}
versionProperties = getExpectedVersionProperties(version);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/service/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions {
*
* Note: this can only be used for multi-namespace object types.
*/
namespaces?: string[];
initialNamespaces?: string[];
}

/**
Expand All @@ -79,7 +79,7 @@ export interface SavedObjectsBulkCreateObject<T = unknown> {
*
* Note: this can only be used for multi-namespace object types.
*/
namespaces?: string[];
initialNamespaces?: string[];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1936,8 +1936,8 @@ export interface SavedObjectsBulkCreateObject<T = unknown> {
attributes: T;
// (undocumented)
id?: string;
initialNamespaces?: string[];
migrationVersion?: SavedObjectsMigrationVersion;
namespaces?: string[];
originId?: string;
// (undocumented)
references?: SavedObjectReference[];
Expand Down Expand Up @@ -2094,8 +2094,8 @@ export interface SavedObjectsCoreFieldMapping {
// @public (undocumented)
export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions {
id?: string;
initialNamespaces?: string[];
migrationVersion?: SavedObjectsMigrationVersion;
namespaces?: string[];
originId?: string;
overwrite?: boolean;
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ describe('#bulkCreate', () => {
await expectPrivilegeCheck(client.bulkCreate, { objects, options }, [namespace]);
});

test(`checks privileges for user, actions, namespace, and namespaces`, async () => {
test(`checks privileges for user, actions, namespace, and initialNamespaces`, async () => {
const objects = [
{ ...obj1, namespaces: 'another-ns' },
{ ...obj2, namespaces: 'yet-another-ns' },
{ ...obj1, initialNamespaces: 'another-ns' },
{ ...obj2, initialNamespaces: 'yet-another-ns' },
];
const options = { namespace };
await expectPrivilegeCheck(client.bulkCreate, { objects, options }, [
Expand Down Expand Up @@ -601,8 +601,8 @@ describe('#create', () => {
await expectPrivilegeCheck(client.create, { type, attributes, options }, [namespace]);
});

test(`checks privileges for user, actions, namespace, and namespaces`, async () => {
const options = { namespace, namespaces: ['another-ns', 'yet-another-ns'] };
test(`checks privileges for user, actions, namespace, and initialNamespaces`, async () => {
const options = { namespace, initialNamespaces: ['another-ns', 'yet-another-ns'] };
await expectPrivilegeCheck(client.create, { type, attributes, options }, [
namespace,
'another-ns',
Expand Down
Loading

0 comments on commit f7bf141

Please sign in to comment.