Skip to content

Commit

Permalink
Rename initialNamespaces to namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Oct 2, 2020
1 parent e0ad362 commit 790a437
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 61 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 @@ -41,7 +41,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.

`initialNamespaces`::
`namespaces`::
(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 @@ -46,7 +46,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.

`initialNamespaces`::
`namespaces`::
(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
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; [SavedObjectsBulkCreateObject](./kibana-plugin-core-server.savedobjectsbulkcreateobject.md) &gt; [initialNamespaces](./kibana-plugin-core-server.savedobjectsbulkcreateobject.initialnamespaces.md)
[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)

## SavedObjectsBulkCreateObject.initialNamespaces property
## SavedObjectsBulkCreateObject.namespaces 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
initialNamespaces?: string[];
namespaces?: 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
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; [initialNamespaces](./kibana-plugin-core-server.savedobjectscreateoptions.initialnamespaces.md)
[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)

## SavedObjectsCreateOptions.initialNamespaces property
## SavedObjectsCreateOptions.namespaces 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
initialNamespaces?: string[];
namespaces?: string[];
```
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) => {
})
)
),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
namespaces: 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) => {
})
)
),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
namespaces: 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, initialNamespaces } = req.body;
const { attributes, migrationVersion, references, namespaces } = req.body;

const options = { id, overwrite, migrationVersion, references, initialNamespaces };
const options = { id, overwrite, migrationVersion, references, namespaces };
const result = await context.core.savedObjects.client.create(type, attributes, options);
return res.ok({ body: result });
})
Expand Down
38 changes: 18 additions & 20 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 initialNamespaces instead of namespaces`, async () => {
it(`adds namespaces instead of namespace`, async () => {
const test = async (namespace) => {
const ns2 = 'bar-namespace';
const ns3 = 'baz-namespace';
const objects = [
{ ...obj1, type: MULTI_NAMESPACE_TYPE, initialNamespaces: [ns2] },
{ ...obj2, type: MULTI_NAMESPACE_TYPE, initialNamespaces: [ns3] },
{ ...obj1, type: MULTI_NAMESPACE_TYPE, namespaces: [ns2] },
{ ...obj2, type: MULTI_NAMESPACE_TYPE, namespaces: [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 initialNamespaces is used with a non-multi-namespace object`, async () => {
it(`returns error when namespaces is used with a non-multi-namespace object`, async () => {
const test = async (objType) => {
const obj = { ...obj3, type: objType, initialNamespaces: [] };
const obj = { ...obj3, type: objType, namespaces: [] };
await bulkCreateError(
obj,
undefined,
expectErrorResult(
obj,
createBadRequestError('"initialNamespaces" can only be used on multi-namespace types')
createBadRequestError('"namespaces" can only be used on multi-namespace types')
)
);
};
await test('dashboard');
await test(NAMESPACE_AGNOSTIC_TYPE);
});

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

it(`adds initialNamespaces instead of namespaces`, async () => {
const options = { id, namespace, initialNamespaces: ['bar-namespace', 'baz-namespace'] };
it(`adds namespaces instead of namespace`, async () => {
const options = { id, namespace, namespaces: ['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.initialNamespaces }),
body: expect.objectContaining({ namespaces: options.namespaces }),
}),
expect.anything()
);
Expand All @@ -2021,25 +2021,23 @@ describe('SavedObjectsRepository', () => {
});

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

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

if (initialNamespaces) {
if (namespaces) {
if (!this._registry.isMultiNamespace(type)) {
throw SavedObjectsErrorHelpers.createBadRequestError(
'"options.initialNamespaces" can only be used on multi-namespace types'
'"options.namespaces" can only be used on multi-namespace types'
);
} else if (!initialNamespaces.length) {
} else if (!namespaces.length) {
throw SavedObjectsErrorHelpers.createBadRequestError(
'"options.initialNamespaces" must be a non-empty array of strings'
'"options.namespaces" 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 = initialNamespaces || existingNamespaces;
savedObjectNamespaces = namespaces || existingNamespaces;
} else {
savedObjectNamespaces = initialNamespaces || getSavedObjectNamespaces(namespace);
savedObjectNamespaces = namespaces || 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.initialNamespaces) {
} else if (object.namespaces) {
if (!this._registry.isMultiNamespace(object.type)) {
error = SavedObjectsErrorHelpers.createBadRequestError(
'"initialNamespaces" can only be used on multi-namespace types'
'"namespaces" can only be used on multi-namespace types'
);
} else if (!object.initialNamespaces.length) {
} else if (!object.namespaces.length) {
error = SavedObjectsErrorHelpers.createBadRequestError(
'"initialNamespaces" must be a non-empty array of strings'
'"namespaces" must be a non-empty array of strings'
);
}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ export class SavedObjectsRepository {
let versionProperties;
const {
esRequestIndex,
object: { initialNamespaces, version, ...object },
object: { namespaces, version, ...object },
method,
} = expectedBulkGetResult.value;
if (esRequestIndex !== undefined) {
Expand All @@ -410,13 +410,13 @@ export class SavedObjectsRepository {
};
}
savedObjectNamespaces =
initialNamespaces || getSavedObjectNamespaces(namespace, docFound && actualResult);
namespaces || 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 = initialNamespaces || getSavedObjectNamespaces(namespace);
savedObjectNamespaces = namespaces || 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.
*/
initialNamespaces?: string[];
namespaces?: string[];
}

/**
Expand All @@ -79,7 +79,7 @@ export interface SavedObjectsBulkCreateObject<T = unknown> {
*
* Note: this can only be used for multi-namespace object types.
*/
initialNamespaces?: string[];
namespaces?: 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 @@ -1819,8 +1819,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 @@ -1977,8 +1977,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 initialNamespaces`, async () => {
test(`checks privileges for user, actions, namespace, and namespaces`, async () => {
const objects = [
{ ...obj1, initialNamespaces: 'another-ns' },
{ ...obj2, initialNamespaces: 'yet-another-ns' },
{ ...obj1, namespaces: 'another-ns' },
{ ...obj2, namespaces: '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 initialNamespaces`, async () => {
const options = { namespace, initialNamespaces: ['another-ns', 'yet-another-ns'] };
test(`checks privileges for user, actions, namespace, and namespaces`, async () => {
const options = { namespace, namespaces: ['another-ns', 'yet-another-ns'] };
await expectPrivilegeCheck(client.create, { type, attributes, options }, [
namespace,
'another-ns',
Expand Down
Loading

0 comments on commit 790a437

Please sign in to comment.