Skip to content

Commit

Permalink
[Metrics UI] Add references for saved views in source configuration (#…
Browse files Browse the repository at this point in the history
…110556)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
simianhacker and kibanamachine authored Sep 16, 2021
1 parent 2282d53 commit 637edfb
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,44 @@ describe('extractSavedObjectReferences function', () => {
sourceConfigurationWithIndexPatternReference
);

expect(references).toMatchObject([{ id: 'INDEX_PATTERN_ID' }]);
expect(references).toMatchObject([
{ id: 'INDEX_PATTERN_ID' },
{ id: 'INVENTORY_DEFAULT_VIEW' },
{ id: 'METRICS_EXPLORER_DEFAULT_VIEW' },
]);
expect(attributes).toHaveProperty(['logIndices', 'indexPatternId'], references[0].name);
expect(attributes).toHaveProperty(['inventoryDefaultView'], references[1].name);
expect(attributes).toHaveProperty(['metricsExplorerDefaultView'], references[2].name);
});

it('ignores log index name references', () => {
const { attributes, references } = extractSavedObjectReferences(
sourceConfigurationWithIndexNameReference
);

expect(references).toHaveLength(0);
expect(references).toHaveLength(2);
expect(attributes).toHaveProperty(['logIndices', 'indexName'], 'INDEX_NAME');
});

it('ignores default inventory view', () => {
const { attributes, references } = extractSavedObjectReferences({
...sourceConfigurationWithIndexNameReference,
inventoryDefaultView: '0',
});

expect(references).toHaveLength(1);
expect(references).toMatchObject([{ id: 'METRICS_EXPLORER_DEFAULT_VIEW' }]);
expect(attributes).toHaveProperty(['logIndices', 'indexName'], 'INDEX_NAME');
});

it('ignores default metrics explorer view', () => {
const { attributes, references } = extractSavedObjectReferences({
...sourceConfigurationWithIndexNameReference,
metricsExplorerDefaultView: '0',
});

expect(references).toHaveLength(1);
expect(references).toMatchObject([{ id: 'INVENTORY_DEFAULT_VIEW' }]);
expect(attributes).toHaveProperty(['logIndices', 'indexName'], 'INDEX_NAME');
});
});
Expand Down
114 changes: 110 additions & 4 deletions x-pack/plugins/infra/server/lib/sources/saved_object_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ interface SavedObjectAttributesWithReferences<SavedObjectAttributes> {
export const extractSavedObjectReferences = (
sourceConfiguration: InfraSourceConfiguration
): SavedObjectAttributesWithReferences<InfraSourceConfiguration> =>
[extractLogIndicesSavedObjectReferences].reduce<
SavedObjectAttributesWithReferences<InfraSourceConfiguration>
>(
[
extractLogIndicesSavedObjectReferences,
extractInventorySavedViewReferences,
extractMetricsExplorerSavedViewReferences,
].reduce<SavedObjectAttributesWithReferences<InfraSourceConfiguration>>(
({ attributes: accumulatedAttributes, references: accumulatedReferences }, extract) => {
const { attributes, references } = extract(accumulatedAttributes);
return {
Expand All @@ -52,7 +54,11 @@ export const resolveSavedObjectReferences = (
attributes: InfraSavedSourceConfiguration,
references: SavedObjectReference[]
): InfraSavedSourceConfiguration =>
[resolveLogIndicesSavedObjectReferences].reduce<InfraSavedSourceConfiguration>(
[
resolveLogIndicesSavedObjectReferences,
resolveInventoryViewSavedObjectReferences,
resolveMetricsExplorerSavedObjectReferences,
].reduce<InfraSavedSourceConfiguration>(
(accumulatedAttributes, resolve) => resolve(accumulatedAttributes, references),
attributes
);
Expand Down Expand Up @@ -85,6 +91,58 @@ const extractLogIndicesSavedObjectReferences = (
}
};

const extractInventorySavedViewReferences = (
sourceConfiguration: InfraSourceConfiguration
): SavedObjectAttributesWithReferences<InfraSourceConfiguration> => {
const { inventoryDefaultView } = sourceConfiguration;
if (inventoryDefaultView && inventoryDefaultView !== '0') {
const inventoryDefaultViewReference: SavedObjectReference = {
id: inventoryDefaultView,
type: 'inventory-view',
name: 'inventory-saved-view-0',
};
const attributes: InfraSourceConfiguration = {
...sourceConfiguration,
inventoryDefaultView: inventoryDefaultViewReference.name,
};
return {
attributes,
references: [inventoryDefaultViewReference],
};
} else {
return {
attributes: sourceConfiguration,
references: [],
};
}
};

const extractMetricsExplorerSavedViewReferences = (
sourceConfiguration: InfraSourceConfiguration
): SavedObjectAttributesWithReferences<InfraSourceConfiguration> => {
const { metricsExplorerDefaultView } = sourceConfiguration;
if (metricsExplorerDefaultView && metricsExplorerDefaultView !== '0') {
const metricsExplorerDefaultViewReference: SavedObjectReference = {
id: metricsExplorerDefaultView,
type: 'metrics-explorer-view',
name: 'metrics-explorer-saved-view-0',
};
const attributes: InfraSourceConfiguration = {
...sourceConfiguration,
metricsExplorerDefaultView: metricsExplorerDefaultViewReference.name,
};
return {
attributes,
references: [metricsExplorerDefaultViewReference],
};
} else {
return {
attributes: sourceConfiguration,
references: [],
};
}
};

const resolveLogIndicesSavedObjectReferences = (
attributes: InfraSavedSourceConfiguration,
references: SavedObjectReference[]
Expand All @@ -111,3 +169,51 @@ const resolveLogIndicesSavedObjectReferences = (
return attributes;
}
};

const resolveInventoryViewSavedObjectReferences = (
attributes: InfraSavedSourceConfiguration,
references: SavedObjectReference[]
): InfraSavedSourceConfiguration => {
if (attributes.inventoryDefaultView && attributes.inventoryDefaultView !== '0') {
const inventoryViewReference = references.find(
(reference) => reference.name === 'inventory-saved-view-0'
);

if (inventoryViewReference == null) {
throw new SavedObjectReferenceResolutionError(
'Failed to resolve Inventory default view "inventory-saved-view-0".'
);
}

return {
...attributes,
inventoryDefaultView: inventoryViewReference.id,
};
} else {
return attributes;
}
};

const resolveMetricsExplorerSavedObjectReferences = (
attributes: InfraSavedSourceConfiguration,
references: SavedObjectReference[]
): InfraSavedSourceConfiguration => {
if (attributes.metricsExplorerDefaultView && attributes.metricsExplorerDefaultView !== '0') {
const metricsExplorerViewReference = references.find(
(reference) => reference.name === 'metrics-explorer-saved-view-0'
);

if (metricsExplorerViewReference == null) {
throw new SavedObjectReferenceResolutionError(
'Failed to resolve Metrics Explorer default view "metrics-explorer-saved-view-0".'
);
}

return {
...attributes,
metricsExplorerDefaultView: metricsExplorerViewReference.id,
};
} else {
return attributes;
}
};

0 comments on commit 637edfb

Please sign in to comment.