Skip to content

Commit 6d72042

Browse files
[ML] Fixing endpoint schema for can_delete_job endpoint (#86436)
* [ML] Fixing endpoint schema for can_delete_job endpoint * changing get to post in docs * renaming canUntag * fixing typo Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent c05533e commit 6d72042

7 files changed

Lines changed: 45 additions & 30 deletions

File tree

x-pack/plugins/ml/common/types/saved_objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface SyncSavedObjectResponse {
2121
export interface CanDeleteJobResponse {
2222
[jobId: string]: {
2323
canDelete: boolean;
24-
canUntag: boolean;
24+
canRemoveFromSpace: boolean;
2525
};
2626
}
2727

@@ -41,5 +41,5 @@ export interface DeleteJobCheckResponse {
4141

4242
export interface DeleteJobPermission {
4343
canDelete: boolean;
44-
canUntag: boolean;
44+
canRemoveFromSpace: boolean;
4545
}

x-pack/plugins/ml/public/application/components/delete_job_check_modal/delete_job_check_modal.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,31 @@ interface ModalContentReturnType {
3636

3737
interface JobCheckRespSummary {
3838
canDelete: boolean;
39-
canUntag: boolean;
39+
canRemoveFromSpace: boolean;
4040
canTakeAnyAction: boolean;
4141
}
4242

4343
function getRespSummary(resp: CanDeleteJobResponse): JobCheckRespSummary {
4444
const jobsChecked = Object.keys(resp);
4545
// Default to first job's permissions
46-
const { canDelete, canUntag } = resp[jobsChecked[0]];
46+
const { canDelete, canRemoveFromSpace } = resp[jobsChecked[0]];
4747
let canTakeAnyAction = true;
4848

4949
if (jobsChecked.length > 1) {
5050
// Check all jobs and make sure they have the same permissions - otherwise no action can be taken
5151
canTakeAnyAction = jobsChecked.every(
52-
(id) => resp[id].canDelete === canDelete && resp[id].canUntag === canUntag
52+
(id) => resp[id].canDelete === canDelete && resp[id].canRemoveFromSpace === canRemoveFromSpace
5353
);
5454
}
5555

56-
return { canDelete, canUntag, canTakeAnyAction };
56+
return { canDelete, canRemoveFromSpace, canTakeAnyAction };
5757
}
5858

5959
function getModalContent(
6060
jobIds: string[],
6161
respSummary: JobCheckRespSummary
6262
): ModalContentReturnType {
63-
const { canDelete, canUntag, canTakeAnyAction } = respSummary;
63+
const { canDelete, canRemoveFromSpace, canTakeAnyAction } = respSummary;
6464

6565
if (canTakeAnyAction === false) {
6666
return {
@@ -116,7 +116,7 @@ function getModalContent(
116116
</EuiText>
117117
),
118118
};
119-
} else if (canUntag) {
119+
} else if (canRemoveFromSpace) {
120120
return {
121121
buttonText: (
122122
<FormattedMessage
@@ -173,8 +173,8 @@ export const DeleteJobCheckModal: FC<Props> = ({
173173
// Do the spaces check and set the content for the modal and buttons depending on results
174174
canDeleteJob(jobType, jobIds).then((resp) => {
175175
const respSummary = getRespSummary(resp);
176-
const { canDelete, canUntag, canTakeAnyAction } = respSummary;
177-
if (canTakeAnyAction && canDelete && !canUntag) {
176+
const { canDelete, canRemoveFromSpace, canTakeAnyAction } = respSummary;
177+
if (canTakeAnyAction && canDelete && !canRemoveFromSpace) {
178178
// Go straight to delete flow if that's the only action available
179179
canDeleteCallback();
180180
return;
@@ -260,7 +260,7 @@ export const DeleteJobCheckModal: FC<Props> = ({
260260
<EuiFlexItem grow={false}>
261261
{!hasUntagged &&
262262
jobCheckRespSummary?.canTakeAnyAction &&
263-
jobCheckRespSummary?.canUntag &&
263+
jobCheckRespSummary?.canRemoveFromSpace &&
264264
jobCheckRespSummary?.canDelete && (
265265
<EuiButtonEmpty
266266
isLoading={isUntagging}
@@ -277,7 +277,7 @@ export const DeleteJobCheckModal: FC<Props> = ({
277277
size="s"
278278
onClick={
279279
jobCheckRespSummary?.canTakeAnyAction &&
280-
jobCheckRespSummary?.canUntag &&
280+
jobCheckRespSummary?.canRemoveFromSpace &&
281281
!jobCheckRespSummary?.canDelete
282282
? onUntagClick
283283
: onClick

x-pack/plugins/ml/server/routes/apidoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
"RemoveJobsFromSpaces",
152152
"RemoveJobsFromCurrentSpace",
153153
"JobsSpaces",
154-
"DeleteJobCheck",
154+
"CanDeleteJob",
155155

156156
"TrainedModels",
157157
"GetTrainedModel",

x-pack/plugins/ml/server/routes/saved_objects.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
jobsAndCurrentSpace,
1313
syncJobObjects,
1414
jobTypeSchema,
15+
canDeleteJobSchema,
1516
} from './schemas/saved_objects';
16-
import { jobIdsSchema } from './schemas/job_service_schema';
1717
import { spacesUtilsProvider } from '../lib/spaces_utils';
1818
import { JobType } from '../../common/types/saved_objects';
1919

@@ -284,21 +284,31 @@ export function savedObjectsRoutes(
284284
/**
285285
* @apiGroup JobSavedObjects
286286
*
287-
* @api {get} /api/ml/saved_objects/delete_job_check Check whether user can delete a job
288-
* @apiName DeleteJobCheck
287+
* @api {post} /api/ml/saved_objects/can_delete_job Check whether user can delete a job
288+
* @apiName CanDeleteJob
289289
* @apiDescription Check the user's ability to delete jobs. Returns whether they are able
290290
* to fully delete the job and whether they are able to remove it from
291291
* the current space.
292+
* Note, this is only for enabling UI controls. A user calling endpoints
293+
* directly will still be able to delete or remove the job from a space.
292294
*
293-
* @apiSchema (body) jobIdsSchema (params) jobTypeSchema
295+
* @apiSchema (params) jobTypeSchema
296+
* @apiSchema (body) jobIdsSchema
297+
* @apiSuccessExample {json} Error-Response:
298+
* {
299+
* "my_job": {
300+
* "canDelete": false,
301+
* "canRemoveFromSpace": true
302+
* }
303+
* }
294304
*
295305
*/
296306
router.post(
297307
{
298308
path: '/api/ml/saved_objects/can_delete_job/{jobType}',
299309
validate: {
300310
params: jobTypeSchema,
301-
body: jobIdsSchema,
311+
body: canDeleteJobSchema,
302312
},
303313
options: {
304314
tags: ['access:ml:canGetJobs', 'access:ml:canGetDataFrameAnalytics'],

x-pack/plugins/ml/server/routes/schemas/saved_objects.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ export const syncJobObjects = schema.object({ simulate: schema.maybe(schema.bool
2222
export const jobTypeSchema = schema.object({
2323
jobType: schema.string(),
2424
});
25+
26+
export const canDeleteJobSchema = schema.object({
27+
/** List of job IDs. */
28+
jobIds: schema.arrayOf(schema.maybe(schema.string())),
29+
});

x-pack/plugins/ml/server/saved_objects/checks.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export function checksFactory(
180180
return jobIds.reduce((results, jobId) => {
181181
results[jobId] = {
182182
canDelete: false,
183-
canUntag: false,
183+
canRemoveFromSpace: false,
184184
};
185185
return results;
186186
}, {} as DeleteJobCheckResponse);
@@ -191,7 +191,7 @@ export function checksFactory(
191191
return jobIds.reduce((results, jobId) => {
192192
results[jobId] = {
193193
canDelete: true,
194-
canUntag: false,
194+
canRemoveFromSpace: false,
195195
};
196196
return results;
197197
}, {} as DeleteJobCheckResponse);
@@ -208,7 +208,7 @@ export function checksFactory(
208208
// job saved object not found
209209
results[jobId] = {
210210
canDelete: false,
211-
canUntag: false,
211+
canRemoveFromSpace: false,
212212
};
213213
return results;
214214
}
@@ -220,7 +220,7 @@ export function checksFactory(
220220
if (canCreateGlobalJobs && isGlobalJob) {
221221
results[jobId] = {
222222
canDelete: true,
223-
canUntag: false,
223+
canRemoveFromSpace: false,
224224
};
225225
return results;
226226
}
@@ -229,28 +229,28 @@ export function checksFactory(
229229
if (isGlobalJob) {
230230
results[jobId] = {
231231
canDelete: false,
232-
canUntag: false,
232+
canRemoveFromSpace: false,
233233
};
234234
return results;
235235
}
236236

237237
// jobs with are in individual spaces can only be untagged
238238
// from current space if the job is in more than 1 space
239-
const canUntag = namespaces.length > 1;
239+
const canRemoveFromSpace = namespaces.length > 1;
240240

241241
// job is in individual spaces, user cannot see all of them - untag only, no delete
242242
if (namespaces.includes('?')) {
243243
results[jobId] = {
244244
canDelete: false,
245-
canUntag,
245+
canRemoveFromSpace,
246246
};
247247
return results;
248248
}
249249

250250
// job is individual spaces, user can see all of them - delete and option to untag
251251
results[jobId] = {
252252
canDelete: true,
253-
canUntag,
253+
canRemoveFromSpace,
254254
};
255255
return results;
256256
}, {} as DeleteJobCheckResponse);

x-pack/test/api_integration/apis/ml/saved_objects/can_delete_job.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default ({ getService }: FtrProviderContext) => {
7575
idSpace1
7676
);
7777

78-
expect(body).to.eql({ [adJobIdSpace12]: { canDelete: false, canUntag: true } });
78+
expect(body).to.eql({ [adJobIdSpace12]: { canDelete: false, canRemoveFromSpace: true } });
7979
});
8080

8181
it('job in individual spaces, all spaces user can delete and untag', async () => {
@@ -87,7 +87,7 @@ export default ({ getService }: FtrProviderContext) => {
8787
idSpace1
8888
);
8989

90-
expect(body).to.eql({ [adJobIdSpace12]: { canDelete: true, canUntag: true } });
90+
expect(body).to.eql({ [adJobIdSpace12]: { canDelete: true, canRemoveFromSpace: true } });
9191
});
9292

9393
it('job in * space, single space user can not untag or delete', async () => {
@@ -99,7 +99,7 @@ export default ({ getService }: FtrProviderContext) => {
9999
idSpace1
100100
);
101101

102-
expect(body).to.eql({ [adJobIdStarSpace]: { canDelete: false, canUntag: false } });
102+
expect(body).to.eql({ [adJobIdStarSpace]: { canDelete: false, canRemoveFromSpace: false } });
103103
});
104104

105105
it('job in * space, all spaces user can delete but not untag', async () => {
@@ -111,7 +111,7 @@ export default ({ getService }: FtrProviderContext) => {
111111
idStarSpace
112112
);
113113

114-
expect(body).to.eql({ [adJobIdStarSpace]: { canDelete: true, canUntag: false } });
114+
expect(body).to.eql({ [adJobIdStarSpace]: { canDelete: true, canRemoveFromSpace: false } });
115115
});
116116
});
117117
};

0 commit comments

Comments
 (0)