-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fleet] Allow snake cased Kibana assets #77515
Changes from 6 commits
a2469da
a3d768b
06094fa
6c9ed86
639f90d
f532be5
a9fb9da
b05aa37
ae1e0fd
0f1e622
f100adf
0ecb8c6
a3919d1
b55f4f6
77389ed
a9ed63e
4294a13
c206f21
8e774ef
f0ecbc4
40cf944
f84206a
52daf8b
5b83a9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,8 +11,9 @@ import { | |||||
} from 'src/core/server'; | ||||||
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../../../common'; | ||||||
import * as Registry from '../../registry'; | ||||||
import { AssetType, KibanaAssetType, AssetReference } from '../../../../types'; | ||||||
import { AssetType, KibanaAssetType, AssetReference, AssetParts } from '../../../../types'; | ||||||
import { savedObjectTypes } from '../../packages'; | ||||||
import { IndexPatternType } from '../index_pattern/install'; | ||||||
|
||||||
type SavedObjectToBe = Required<Pick<SavedObjectsBulkCreateObject, keyof ArchiveAsset>> & { | ||||||
type: AssetType; | ||||||
|
@@ -24,6 +25,31 @@ export type ArchiveAsset = Pick< | |||||
type: AssetType; | ||||||
}; | ||||||
|
||||||
// KibanaSavedObjectTypes are used to ensure saved objects being created for a given | ||||||
// KibanaAssetType have the correct type | ||||||
const KibanaSavedObjectTypes: Record<KibanaAssetType, string> = { | ||||||
[KibanaAssetType.dashboard]: 'dashboard', | ||||||
[KibanaAssetType.indexPattern]: 'index-pattern', | ||||||
[KibanaAssetType.map]: 'map', | ||||||
[KibanaAssetType.search]: 'search', | ||||||
[KibanaAssetType.visualization]: 'visualization', | ||||||
}; | ||||||
|
||||||
// Define how each asset type will be installed | ||||||
const AssetInstallers: Record< | ||||||
KibanaAssetType, | ||||||
(args: { | ||||||
savedObjectsClient: SavedObjectsClientContract; | ||||||
kibanaAssets: ArchiveAsset[]; | ||||||
}) => Promise<Array<SavedObject<unknown>>> | ||||||
> = { | ||||||
[KibanaAssetType.dashboard]: installKibanaSavedObjects, | ||||||
[KibanaAssetType.indexPattern]: installKibanaIndexPatterns, | ||||||
[KibanaAssetType.map]: installKibanaSavedObjects, | ||||||
[KibanaAssetType.search]: installKibanaSavedObjects, | ||||||
[KibanaAssetType.visualization]: installKibanaSavedObjects, | ||||||
}; | ||||||
|
||||||
export async function getKibanaAsset(key: string): Promise<ArchiveAsset> { | ||||||
const buffer = Registry.getAsset(key); | ||||||
|
||||||
|
@@ -47,16 +73,22 @@ export function createSavedObjectKibanaAsset(asset: ArchiveAsset): SavedObjectTo | |||||
export async function installKibanaAssets(options: { | ||||||
savedObjectsClient: SavedObjectsClientContract; | ||||||
pkgName: string; | ||||||
kibanaAssets: ArchiveAsset[]; | ||||||
kibanaAssets: Record<KibanaAssetType, ArchiveAsset[]>; | ||||||
}): Promise<SavedObject[]> { | ||||||
const { savedObjectsClient, kibanaAssets } = options; | ||||||
|
||||||
// install the assets | ||||||
const kibanaAssetTypes = Object.values(KibanaAssetType); | ||||||
const installedAssets = await Promise.all( | ||||||
kibanaAssetTypes.map((assetType) => | ||||||
installKibanaSavedObjects({ savedObjectsClient, assetType, kibanaAssets }) | ||||||
) | ||||||
kibanaAssetTypes.map((assetType) => { | ||||||
if (kibanaAssets[assetType]) { | ||||||
return AssetInstallers[assetType]({ | ||||||
savedObjectsClient, | ||||||
kibanaAssets: kibanaAssets[assetType], | ||||||
}); | ||||||
} | ||||||
return []; | ||||||
}) | ||||||
); | ||||||
return installedAssets.flat(); | ||||||
} | ||||||
|
@@ -75,24 +107,47 @@ export const deleteKibanaInstalledRefs = async ( | |||||
}); | ||||||
}; | ||||||
export async function getKibanaAssets(paths: string[]) { | ||||||
const isKibanaAssetType = (path: string) => Registry.pathParts(path).type in KibanaAssetType; | ||||||
const filteredPaths = paths.filter(isKibanaAssetType); | ||||||
const kibanaAssets = await Promise.all(filteredPaths.map((path) => getKibanaAsset(path))); | ||||||
return kibanaAssets; | ||||||
const kibanaAssetTypes = Object.values(KibanaAssetType); | ||||||
const isKibanaAssetType = (path: string) => { | ||||||
const parts = Registry.pathParts(path); | ||||||
|
||||||
return parts.service === 'kibana' && (kibanaAssetTypes as string[]).includes(parts.type); | ||||||
}; | ||||||
|
||||||
const filteredPaths = paths | ||||||
.filter(isKibanaAssetType) | ||||||
.map<[string, AssetParts]>((path) => [path, Registry.pathParts(path)]); | ||||||
|
||||||
const assetArrays: Array<Promise<ArchiveAsset[]>> = []; | ||||||
for (const assetType of kibanaAssetTypes) { | ||||||
const matching = filteredPaths.filter(([path, parts]) => parts.type === assetType); | ||||||
|
||||||
assetArrays.push(Promise.all(matching.map(([path]) => path).map(getKibanaAsset))); | ||||||
} | ||||||
|
||||||
const resolvedAssets = await Promise.all(assetArrays); | ||||||
|
||||||
const result = {} as Record<KibanaAssetType, ArchiveAsset[]>; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind moving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the remove the
Suggested change
|
||||||
|
||||||
for (const [index, assetType] of kibanaAssetTypes.entries()) { | ||||||
const expectedType = KibanaSavedObjectTypes[assetType]; | ||||||
const properlyTypedAssets = resolvedAssets[index].filter(({ type }) => type === expectedType); | ||||||
|
||||||
result[assetType] = properlyTypedAssets; | ||||||
} | ||||||
|
||||||
return result; | ||||||
} | ||||||
|
||||||
async function installKibanaSavedObjects({ | ||||||
savedObjectsClient, | ||||||
assetType, | ||||||
kibanaAssets, | ||||||
}: { | ||||||
savedObjectsClient: SavedObjectsClientContract; | ||||||
assetType: KibanaAssetType; | ||||||
kibanaAssets: ArchiveAsset[]; | ||||||
}) { | ||||||
const isSameType = (asset: ArchiveAsset) => assetType === asset.type; | ||||||
const filteredKibanaAssets = kibanaAssets.filter((asset) => isSameType(asset)); | ||||||
const toBeSavedObjects = await Promise.all( | ||||||
filteredKibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset)) | ||||||
kibanaAssets.map((asset) => createSavedObjectKibanaAsset(asset)) | ||||||
); | ||||||
|
||||||
if (toBeSavedObjects.length === 0) { | ||||||
|
@@ -105,8 +160,23 @@ async function installKibanaSavedObjects({ | |||||
} | ||||||
} | ||||||
|
||||||
async function installKibanaIndexPatterns({ | ||||||
savedObjectsClient, | ||||||
kibanaAssets, | ||||||
}: { | ||||||
savedObjectsClient: SavedObjectsClientContract; | ||||||
kibanaAssets: ArchiveAsset[]; | ||||||
}) { | ||||||
// Filter out any reserved index patterns | ||||||
const reservedPatterns = Object.values<string>(IndexPatternType).map((pattern) => `${pattern}-*`); | ||||||
|
||||||
const nonReservedPatterns = kibanaAssets.filter((asset) => !reservedPatterns.includes(asset.id)); | ||||||
|
||||||
return installKibanaSavedObjects({ savedObjectsClient, kibanaAssets: nonReservedPatterns }); | ||||||
} | ||||||
|
||||||
export function toAssetReference({ id, type }: SavedObject) { | ||||||
const reference: AssetReference = { id, type: type as KibanaAssetType }; | ||||||
const reference: AssetReference = { id, type }; | ||||||
|
||||||
return reference; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC,
SavedObjectReference['type']
is astring
vs aKibanaAssetType
. I'd like to keep as-is if we can. Was this causing an issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jfsiii
KibanaAssetType
is now the type that matches file paths, which do not match to saved object types (index_pattern vs index-pattern).Maybe we bring the
KibanaSavedObjectsType
over into this file from the kibana/install file and use that type instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crob611 I'll re-read but that seems OK. We definitely want a name and type more refined than
string
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jfsiii OK just pushed a commit that adds a "KibanaSavedObjectType" and uses that type for KibanaAssetReference.