Skip to content

Commit

Permalink
Delete legacy URL alises w/ spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Oct 18, 2021
1 parent e9d6a07 commit a6491bf
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/core/server/saved_objects/object_types/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const legacyUrlAliasType: SavedObjectsType = {
properties: {
sourceId: { type: 'keyword' },
targetType: { type: 'keyword' },
targetNamespace: { type: 'keyword' },
resolveCounter: { type: 'long' },
disabled: { type: 'boolean' },
// other properties exist, but we aren't querying or aggregating on those, so we don't need to specify them (because we use `dynamic: false` above)
Expand Down
7 changes: 6 additions & 1 deletion src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { encodeHitVersion } from '../../version';
import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry';
import { DocumentMigrator } from '../../migrations/core/document_migrator';
import { mockKibanaMigrator } from '../../migrations/kibana/kibana_migrator.mock';
import { LEGACY_URL_ALIAS_TYPE } from '../../object_types';
import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks';
import * as esKuery from '@kbn/es-query';
import { errors as EsErrors } from '@elastic/elasticsearch';
Expand Down Expand Up @@ -2714,7 +2715,11 @@ describe('SavedObjectsRepository', () => {
const allTypes = registry.getAllTypes().map((type) => type.name);
expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, registry, {
namespaces: [namespace],
type: allTypes.filter((type) => !registry.isNamespaceAgnostic(type)),
type: [
...allTypes.filter((type) => !registry.isNamespaceAgnostic(type)),
LEGACY_URL_ALIAS_TYPE,
],
kueryNode: expect.anything(),
});
});
});
Expand Down
17 changes: 15 additions & 2 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { omit, isObject } from 'lodash';
import type { estypes } from '@elastic/elasticsearch';
import * as esKuery from '@kbn/es-query';
import type { ElasticsearchClient } from '../../../elasticsearch/';
import { isSupportedEsServer, isNotFoundFromUnsupportedServer } from '../../../elasticsearch';
import type { Logger } from '../../../logging';
Expand Down Expand Up @@ -55,6 +56,7 @@ import {
SavedObjectsBulkResolveObject,
SavedObjectsBulkResolveResponse,
} from '../saved_objects_client';
import { LEGACY_URL_ALIAS_TYPE } from '../../object_types';
import {
SavedObject,
SavedObjectsBaseOptions,
Expand Down Expand Up @@ -780,7 +782,17 @@ export class SavedObjectsRepository {
}

const allTypes = Object.keys(getRootPropertiesObjects(this._mappings));
const typesToUpdate = allTypes.filter((type) => !this._registry.isNamespaceAgnostic(type));
const typesToUpdate = [
...allTypes.filter((type) => !this._registry.isNamespaceAgnostic(type)),
LEGACY_URL_ALIAS_TYPE,
];

// Construct kueryNode to filter legacy URL aliases (these space-agnostic objects do not use root-level "namespace/s" fields)
const { buildNode } = esKuery.nodeTypes.function;
const targetNamespaceField = `${LEGACY_URL_ALIAS_TYPE}.targetNamespace`;
const match1 = buildNode('is', targetNamespaceField, namespace);
const match2 = buildNode('not', buildNode('exists', targetNamespaceField));
const kueryNode = buildNode('or', [match1, match2]);

const { body, statusCode, headers } = await this.client.updateByQuery(
{
Expand All @@ -803,8 +815,9 @@ export class SavedObjectsRepository {
},
conflicts: 'proceed',
...getSearchDsl(this._mappings, this._registry, {
namespaces: namespace ? [namespace] : undefined,
namespaces: [namespace],
type: typesToUpdate,
kueryNode,
}),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function getAggregatedSpaceData(es: KibanaClient, objectTypes: string[])
emit(doc["namespaces"].value);
} else if (doc["namespace"].size() > 0) {
emit(doc["namespace"].value);
} else if (doc["legacy-url-alias.targetNamespace"].size() > 0) {
emit(doc["legacy-url-alias.targetNamespace"].value);
}
`,
},
Expand Down
49 changes: 20 additions & 29 deletions x-pack/test/spaces_api_integration/common/suites/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function deleteTestSuiteFactory(
'dashboard',
'space',
'index-pattern',
'legacy-url-alias',
// TODO: add assertions for config objects -- these assertions were removed because of flaky behavior in #92358, but we should
// consider adding them again at some point, especially if we convert config objects to `namespaceType: 'multiple-isolated'` in
// the future.
Expand All @@ -65,47 +66,37 @@ export function deleteTestSuiteFactory(
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: 'visualization',
doc_count: 3,
},
{
key: 'dashboard',
doc_count: 2,
},
{
key: 'space',
doc_count: 2,
},
{
key: 'index-pattern',
doc_count: 1,
},
{ key: 'visualization', doc_count: 3 },
{ key: 'dashboard', doc_count: 2 },
{ key: 'space', doc_count: 2 }, // since space objects are namespace-agnostic, they appear in the "default" agg bucket
{ key: 'index-pattern', doc_count: 1 },
// legacy-url-alias objects cannot exist for the default space
],
},
},
{
doc_count: 6,
doc_count: 7,
key: 'space_1',
countByType: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: 'visualization',
doc_count: 3,
},
{
key: 'dashboard',
doc_count: 2,
},
{
key: 'index-pattern',
doc_count: 1,
},
{ key: 'visualization', doc_count: 3 },
{ key: 'dashboard', doc_count: 2 },
{ key: 'index-pattern', doc_count: 1 },
{ key: 'legacy-url-alias', doc_count: 1 },
],
},
},
{
doc_count: 1,
key: 'other_space',
countByType: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [{ key: 'legacy-url-alias', doc_count: 1 }], // this alias is in a non-existent space (for other test suites)
},
},
];

expect(buckets).to.eql(expectedBuckets);
Expand Down

0 comments on commit a6491bf

Please sign in to comment.