Skip to content
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

[Cases] Split attachment types for versioned http apis #155440

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions x-pack/plugins/cases/server/common/types/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { JsonValue } from '@kbn/utility-types';
import type { User } from './user';

interface AttachmentCommonPersistedAttributes {
created_at: string;
created_by: User;
owner: string;
pushed_at: string | null;
pushed_by: User | null;
updated_at: string | null;
updated_by: User | null;
}

export interface AttachmentRequestAttributes {
type: string;
alertId?: string | string[];
index?: string | string[];
rule?: {
id: string | null;
name: string | null;
};
comment?: string;
actions?: {
targets: Array<{
hostname: string;
endpointId: string;
}>;
type: string;
};
externalReferenceMetadata?: Record<string, JsonValue> | null;
externalReferenceAttachmentTypeId?: string;
externalReferenceStorage?: {
type: string;
soType?: string;
};
persistableStateAttachmentState?: Record<string, JsonValue>;
persistableStateAttachmentTypeId?: string;
}

export type AttachmentPersistedAttributes = AttachmentRequestAttributes &
AttachmentCommonPersistedAttributes;
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/common/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface User {
email: string | null | undefined;
full_name: string | null | undefined;
username: string | null | undefined;
profile_uid?: string | undefined;
profile_uid?: string;
}

export interface UserProfile {
Expand Down
14 changes: 0 additions & 14 deletions x-pack/plugins/cases/server/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import type {
CommentRequest,
CommentRequestActionsType,
CommentRequestAlertType,
CommentRequestExternalReferenceSOType,
CommentRequestUserType,
CommentResponse,
CommentsResponse,
Expand All @@ -45,7 +44,6 @@ import {
CaseStatuses,
CommentType,
ConnectorTypes,
ExternalReferenceStorageType,
ExternalReferenceSORt,
FileAttachmentMetadataRt,
} from '../../common/api';
Expand Down Expand Up @@ -254,18 +252,6 @@ export const isCommentRequestTypeAlert = (
return context.type === CommentType.alert;
};

/**
* A type narrowing function for external reference so attachments.
*/
export const isCommentRequestTypeExternalReferenceSO = (
context: Partial<CommentRequest>
): context is CommentRequestExternalReferenceSOType => {
return (
context.type === CommentType.externalReference &&
context.externalReferenceStorage?.type === ExternalReferenceStorageType.savedObject
);
};

/**
* A type narrowing function for file attachments.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
} from '@kbn/core/server';
import { mergeSavedObjectMigrationMaps } from '@kbn/core/server';
import type { LensServerPluginSetup } from '@kbn/lens-plugin/server';
import type { CommentAttributes } from '../../../common/api';
import { CommentType } from '../../../common/api';
import type { LensMarkdownNode, MarkdownNode } from '../../../common/utils/markdown_plugins/utils';
import {
Expand All @@ -32,6 +31,7 @@ import { GENERATED_ALERT, SUB_CASE_SAVED_OBJECT } from './constants';
import type { PersistableStateAttachmentTypeRegistry } from '../../attachment_framework/persistable_state_registry';
import { getAllPersistableAttachmentMigrations } from './get_all_persistable_attachment_migrations';
import type { PersistableStateAttachmentState } from '../../attachment_framework/types';
import type { AttachmentPersistedAttributes } from '../../common/types/attachments';

interface UnsanitizedComment {
comment: string;
Expand Down Expand Up @@ -71,7 +71,7 @@ export const createCommentsMigrations = (

const persistableStateAttachmentMigrations = mapValues<
MigrateFunctionsObject,
SavedObjectMigrationFn<CommentAttributes>
SavedObjectMigrationFn<AttachmentPersistedAttributes>
>(
getAllPersistableAttachmentMigrations(migrationDeps.persistableStateAttachmentTypeRegistry),
migratePersistableStateAttachments
Expand Down Expand Up @@ -134,8 +134,10 @@ export const createCommentsMigrations = (
};

export const migratePersistableStateAttachments =
(migrate: MigrateFunction): SavedObjectMigrationFn<CommentAttributes, CommentAttributes> =>
(doc: SavedObjectUnsanitizedDoc<CommentAttributes>) => {
(
migrate: MigrateFunction
): SavedObjectMigrationFn<AttachmentPersistedAttributes, AttachmentPersistedAttributes> =>
(doc: SavedObjectUnsanitizedDoc<AttachmentPersistedAttributes>) => {
if (doc.attributes.type !== CommentType.persistableState) {
return doc;
}
Expand Down
78 changes: 19 additions & 59 deletions x-pack/plugins/cases/server/services/attachments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@

import type {
SavedObject,
SavedObjectReference,
SavedObjectsBulkResponse,
SavedObjectsBulkUpdateResponse,
SavedObjectsFindResponse,
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
} from '@kbn/core/server';

import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type {
CommentAttributes as AttachmentAttributes,
CommentAttributesWithoutRefs as AttachmentAttributesWithoutRefs,
CommentPatchAttributes as AttachmentPatchAttributes,
} from '../../../common/api';
import type { CommentAttributes as AttachmentAttributes } from '../../../common/api';
import { CommentType } from '../../../common/api';
import { CASE_COMMENT_SAVED_OBJECT, CASE_SAVED_OBJECT } from '../../../common/constants';
import { buildFilter, combineFilters } from '../../client/utils';
Expand All @@ -32,50 +26,19 @@ import {
injectAttachmentSOAttributesFromRefsForPatch,
} from '../so_references';
import type { SavedObjectFindOptionsKueryNode } from '../../common/types';
import type { IndexRefresh } from '../types';
import type { AttachedToCaseArgs, ServiceContext } from './types';
import type {
AlertsAttachedToCaseArgs,
AttachmentsAttachedToCaseArgs,
BulkCreateAttachments,
BulkUpdateAttachmentArgs,
CountActionsAttachedToCaseArgs,
CreateAttachmentArgs,
DeleteAttachmentArgs,
ServiceContext,
UpdateAttachmentArgs,
} from './types';
import { AttachmentGetter } from './operations/get';

type AlertsAttachedToCaseArgs = AttachedToCaseArgs;

interface AttachmentsAttachedToCaseArgs extends AttachedToCaseArgs {
attachmentType: CommentType;
aggregations: Record<string, estypes.AggregationsAggregationContainer>;
}

interface CountActionsAttachedToCaseArgs extends AttachedToCaseArgs {
aggregations: Record<string, estypes.AggregationsAggregationContainer>;
}

interface DeleteAttachmentArgs extends IndexRefresh {
attachmentIds: string[];
}

interface CreateAttachmentArgs extends IndexRefresh {
attributes: AttachmentAttributes;
references: SavedObjectReference[];
id: string;
}

interface BulkCreateAttachments extends IndexRefresh {
attachments: Array<{
attributes: AttachmentAttributes;
references: SavedObjectReference[];
id: string;
}>;
}

interface UpdateArgs {
attachmentId: string;
updatedAttributes: AttachmentPatchAttributes;
options?: Omit<SavedObjectsUpdateOptions<AttachmentAttributes>, 'upsert'>;
}

export type UpdateAttachmentArgs = UpdateArgs;

interface BulkUpdateAttachmentArgs extends IndexRefresh {
comments: UpdateArgs[];
}
import type { AttachmentPersistedAttributes } from '../../common/types/attachments';

export class AttachmentService {
private readonly _getter: AttachmentGetter;
Expand Down Expand Up @@ -136,10 +99,7 @@ export class AttachmentService {

const combinedFilter = combineFilters([attachmentFilter, filter]);

const response = await this.context.unsecuredSavedObjectsClient.find<
AttachmentAttributes,
Agg
>({
const response = await this.context.unsecuredSavedObjectsClient.find<unknown, Agg>({
type: CASE_COMMENT_SAVED_OBJECT,
hasReference: { type: CASE_SAVED_OBJECT, id: caseId },
page: 1,
Expand Down Expand Up @@ -207,7 +167,7 @@ export class AttachmentService {
);

const attachment =
await this.context.unsecuredSavedObjectsClient.create<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.create<AttachmentPersistedAttributes>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about bulkDelete, update, and bulkUpdate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ don't know why I missed all that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like bulkDelete doesn't take a type. I'll update the others though.

CASE_COMMENT_SAVED_OBJECT,
extractedAttributes,
{
Expand All @@ -234,7 +194,7 @@ export class AttachmentService {
try {
this.context.log.debug(`Attempting to bulk create attachments`);
const res =
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.bulkCreate<AttachmentPersistedAttributes>(
attachments.map((attachment) => {
const { attributes: extractedAttributes, references: extractedReferences } =
extractAttachmentSORefsFromAttributes(
Expand Down Expand Up @@ -288,7 +248,7 @@ export class AttachmentService {
const shouldUpdateRefs = extractedReferences.length > 0 || didDeleteOperation;

const res =
await this.context.unsecuredSavedObjectsClient.update<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.update<AttachmentPersistedAttributes>(
CASE_COMMENT_SAVED_OBJECT,
attachmentId,
extractedAttributes,
Expand Down Expand Up @@ -325,7 +285,7 @@ export class AttachmentService {
);

const res =
await this.context.unsecuredSavedObjectsClient.bulkUpdate<AttachmentAttributesWithoutRefs>(
await this.context.unsecuredSavedObjectsClient.bulkUpdate<AttachmentPersistedAttributes>(
comments.map((c) => {
const {
attributes: extractedAttributes,
Expand Down Expand Up @@ -380,7 +340,7 @@ export class AttachmentService {
try {
this.context.log.debug(`Attempting to find comments`);
const res =
await this.context.unsecuredSavedObjectsClient.find<AttachmentAttributesWithoutRefs>({
await this.context.unsecuredSavedObjectsClient.find<AttachmentPersistedAttributes>({
sortField: defaultSortField,
...options,
type: CASE_COMMENT_SAVED_OBJECT,
Expand Down
Loading