1 import { Injectable } from '@angular/core';
2 import _ from 'lodash';
4 import { Components } from '../enum/components.enum';
5 import { FinishedTask } from '../models/finished-task';
6 import { ImageSpec } from '../models/image-spec';
7 import { Task } from '../models/task';
9 export class TaskMessageOperation {
14 constructor(running: string, failure: string, success: string) {
15 this.running = running;
16 this.failure = failure;
17 this.success = success;
22 operation: TaskMessageOperation;
23 involves: (object: any) => string;
24 errors: (metadata: any) => object;
26 failure(metadata: any): string {
27 return $localize`Failed to ${this.operation.failure} ${this.involves(metadata)}`;
30 running(metadata: any): string {
31 return `${this.operation.running} ${this.involves(metadata)}`;
34 success(metadata: any): string {
35 return `${this.operation.success} ${this.involves(metadata)}`;
39 operation: TaskMessageOperation,
40 involves: (metadata: any) => string,
41 errors?: (metadata: any) => object
43 this.operation = operation;
44 this.involves = involves;
45 this.errors = errors || (() => ({}));
52 export class TaskMessageService {
53 defaultMessage = this.newTaskMessage(
54 new TaskMessageOperation($localize`Executing`, $localize`execute`, $localize`Executed`),
57 (metadata && (Components[metadata.component] || metadata.component)) ||
58 $localize`unknown task`
67 create: new TaskMessageOperation($localize`Creating`, $localize`create`, $localize`Created`),
68 update: new TaskMessageOperation($localize`Updating`, $localize`update`, $localize`Updated`),
69 delete: new TaskMessageOperation($localize`Deleting`, $localize`delete`, $localize`Deleted`),
70 add: new TaskMessageOperation($localize`Adding`, $localize`add`, $localize`Added`),
71 remove: new TaskMessageOperation($localize`Removing`, $localize`remove`, $localize`Removed`),
72 import: new TaskMessageOperation($localize`Importing`, $localize`import`, $localize`Imported`)
76 default: (metadata: any) => $localize`RBD '${metadata.image_spec}'`,
77 create: (metadata: any) => {
78 const id = new ImageSpec(
83 return $localize`RBD '${id}'`;
85 child: (metadata: any) => {
86 const id = new ImageSpec(
87 metadata.child_pool_name,
88 metadata.child_namespace,
89 metadata.child_image_name
91 return $localize`RBD '${id}'`;
93 destination: (metadata: any) => {
94 const id = new ImageSpec(
95 metadata.dest_pool_name,
96 metadata.dest_namespace,
97 metadata.dest_image_name
99 return $localize`RBD '${id}'`;
101 snapshot: (metadata: any) =>
102 $localize`RBD snapshot '${metadata.image_spec}@${metadata.snapshot_name}'`
106 site_name: () => $localize`mirroring site name`,
107 bootstrap: () => $localize`bootstrap token`,
108 pool: (metadata: any) => $localize`mirror mode for pool '${metadata.pool_name}'`,
109 pool_peer: (metadata: any) => $localize`mirror peer for pool '${metadata.pool_name}'`
113 update_dashboards: () => $localize`all dashboards`
118 'host/add': this.newTaskMessage(this.commonOperations.add, (metadata) => this.host(metadata)),
119 'host/remove': this.newTaskMessage(this.commonOperations.remove, (metadata) =>
122 'host/identify_device': this.newTaskMessage(
123 new TaskMessageOperation($localize`Identifying`, $localize`identify`, $localize`Identified`),
124 (metadata) => $localize`device '${metadata.device}' on host '${metadata.hostname}'`
127 'osd/create': this.newTaskMessage(
128 this.commonOperations.create,
129 (metadata) => $localize`OSDs (DriveGroups: ${metadata.tracking_id})`
131 'osd/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
135 'pool/create': this.newTaskMessage(
136 this.commonOperations.create,
137 (metadata) => this.pool(metadata),
139 '17': $localize`Name is already used by ${this.pool(metadata)}.`
142 'pool/edit': this.newTaskMessage(
143 this.commonOperations.update,
144 (metadata) => this.pool(metadata),
146 '17': $localize`Name is already used by ${this.pool(metadata)}.`
149 'pool/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
152 // Erasure code profile tasks
153 'ecp/create': this.newTaskMessage(
154 this.commonOperations.create,
155 (metadata) => this.ecp(metadata),
157 '17': $localize`Name is already used by ${this.ecp(metadata)}.`
160 'ecp/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
164 'crushRule/create': this.newTaskMessage(
165 this.commonOperations.create,
166 (metadata) => this.crushRule(metadata),
168 '17': $localize`Name is already used by ${this.crushRule(metadata)}.`
171 'crushRule/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
172 this.crushRule(metadata)
175 'rbd/create': this.newTaskMessage(
176 this.commonOperations.create,
179 '17': $localize`Name is already used by ${this.rbd.create(metadata)}.`
182 'rbd/edit': this.newTaskMessage(this.commonOperations.update, this.rbd.default, (metadata) => ({
183 '17': $localize`Name is already used by ${this.rbd.default(metadata)}.`
185 'rbd/delete': this.newTaskMessage(
186 this.commonOperations.delete,
189 '16': $localize`${this.rbd.default(metadata)} is busy.`,
190 '39': $localize`${this.rbd.default(metadata)} contains snapshots.`
193 'rbd/clone': this.newTaskMessage(
194 new TaskMessageOperation($localize`Cloning`, $localize`clone`, $localize`Cloned`),
197 '17': $localize`Name is already used by ${this.rbd.child(metadata)}.`,
198 '22': $localize`Snapshot of ${this.rbd.child(metadata)} must be protected.`
201 'rbd/copy': this.newTaskMessage(
202 new TaskMessageOperation($localize`Copying`, $localize`copy`, $localize`Copied`),
203 this.rbd.destination,
205 '17': $localize`Name is already used by ${this.rbd.destination(metadata)}.`
208 'rbd/flatten': this.newTaskMessage(
209 new TaskMessageOperation($localize`Flattening`, $localize`flatten`, $localize`Flattened`),
212 // RBD snapshot tasks
213 'rbd/snap/create': this.newTaskMessage(
214 this.commonOperations.create,
217 '17': $localize`Name is already used by ${this.rbd.snapshot(metadata)}.`
220 'rbd/snap/edit': this.newTaskMessage(
221 this.commonOperations.update,
224 '16': $localize`Cannot unprotect ${this.rbd.snapshot(
226 )} because it contains child images.`
229 'rbd/snap/delete': this.newTaskMessage(
230 this.commonOperations.delete,
233 '16': $localize`Cannot delete ${this.rbd.snapshot(metadata)} because it's protected.`
236 'rbd/snap/rollback': this.newTaskMessage(
237 new TaskMessageOperation(
238 $localize`Rolling back`,
240 $localize`Rolled back`
245 'rbd/trash/move': this.newTaskMessage(
246 new TaskMessageOperation($localize`Moving`, $localize`move`, $localize`Moved`),
247 (metadata) => $localize`image '${metadata.image_spec}' to trash`,
249 2: $localize`Could not find image.`
252 'rbd/trash/restore': this.newTaskMessage(
253 new TaskMessageOperation($localize`Restoring`, $localize`restore`, $localize`Restored`),
254 (metadata) => $localize`image '${metadata.image_id_spec}' into '${metadata.new_image_name}'`,
256 17: $localize`Image name '${metadata.new_image_name}' is already in use.`
259 'rbd/trash/remove': this.newTaskMessage(
260 new TaskMessageOperation($localize`Deleting`, $localize`delete`, $localize`Deleted`),
261 (metadata) => $localize`image '${metadata.image_id_spec}'`
263 'rbd/trash/purge': this.newTaskMessage(
264 new TaskMessageOperation($localize`Purging`, $localize`purge`, $localize`Purged`),
266 let message = $localize`all pools`;
267 if (metadata.pool_name) {
268 message = `'${metadata.pool_name}'`;
270 return $localize`images from ${message}`;
273 // RBD mirroring tasks
274 'rbd/mirroring/site_name/edit': this.newTaskMessage(
275 this.commonOperations.update,
276 this.rbd_mirroring.site_name,
279 'rbd/mirroring/bootstrap/create': this.newTaskMessage(
280 this.commonOperations.create,
281 this.rbd_mirroring.bootstrap,
284 'rbd/mirroring/bootstrap/import': this.newTaskMessage(
285 this.commonOperations.import,
286 this.rbd_mirroring.bootstrap,
289 'rbd/mirroring/pool/edit': this.newTaskMessage(
290 this.commonOperations.update,
291 this.rbd_mirroring.pool,
293 16: $localize`Cannot disable mirroring because it contains a peer.`
296 'rbd/mirroring/peer/add': this.newTaskMessage(
297 this.commonOperations.create,
298 this.rbd_mirroring.pool_peer,
301 'rbd/mirroring/peer/edit': this.newTaskMessage(
302 this.commonOperations.update,
303 this.rbd_mirroring.pool_peer,
306 'rbd/mirroring/peer/delete': this.newTaskMessage(
307 this.commonOperations.delete,
308 this.rbd_mirroring.pool_peer,
311 // iSCSI target tasks
312 'iscsi/target/create': this.newTaskMessage(this.commonOperations.create, (metadata) =>
313 this.iscsiTarget(metadata)
315 'iscsi/target/edit': this.newTaskMessage(this.commonOperations.update, (metadata) =>
316 this.iscsiTarget(metadata)
318 'iscsi/target/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
319 this.iscsiTarget(metadata)
321 'nfs/create': this.newTaskMessage(this.commonOperations.create, (metadata) =>
324 'nfs/edit': this.newTaskMessage(this.commonOperations.update, (metadata) => this.nfs(metadata)),
325 'nfs/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
329 'grafana/dashboards/update': this.newTaskMessage(
330 this.commonOperations.update,
331 this.grafana.update_dashboards,
335 'service/create': this.newTaskMessage(this.commonOperations.create, (metadata) =>
336 this.service(metadata)
338 'service/edit': this.newTaskMessage(this.commonOperations.update, (metadata) =>
339 this.service(metadata)
341 'service/delete': this.newTaskMessage(this.commonOperations.delete, (metadata) =>
342 this.service(metadata)
344 'crud-component/create': this.newTaskMessage(this.commonOperations.create, (metadata) =>
345 this.crudMessage(metadata)
347 'crud-component/edit': this.newTaskMessage(this.commonOperations.update, (metadata) =>
348 this.crudMessage(metadata)
350 'crud-component/import': this.newTaskMessage(this.commonOperations.import, (metadata) =>
351 this.crudMessage(metadata)
353 'crud-component/id': this.newTaskMessage(this.commonOperations.delete, (id) =>
354 this.crudMessageId(id)
359 operation: TaskMessageOperation,
360 involves: (metadata: any) => string,
361 errors?: (metadata: any) => object
363 return new TaskMessage(operation, involves, errors);
366 host(metadata: any) {
367 return $localize`host '${metadata.hostname}'`;
371 return $localize`OSD '${metadata.svc_id}'`;
374 pool(metadata: any) {
375 return $localize`pool '${metadata.pool_name}'`;
379 return $localize`erasure code profile '${metadata.name}'`;
382 crushRule(metadata: any) {
383 return $localize`crush rule '${metadata.name}'`;
386 iscsiTarget(metadata: any) {
387 return $localize`target '${metadata.target_iqn}'`;
391 return $localize`NFS '${metadata.cluster_id}\:${
392 metadata.export_id ? metadata.export_id : metadata.path
396 service(metadata: any) {
397 return $localize`Service '${metadata.service_name}'`;
400 crudMessage(metadata: any) {
401 let message = metadata.__message;
402 _.forEach(metadata, (value, key) => {
403 if (key != '__message') {
404 let regex = '{' + key + '}';
405 message = message.replace(regex, value);
408 return $localize`${message}`;
411 crudMessageId(id: string) {
412 return $localize`${id}`;
415 _getTaskTitle(task: Task) {
416 if (task.name && task.name.startsWith('progress/')) {
417 // we don't fill the failure string because, at least for now, all
418 // progress module tasks will be considered successful
419 return this.newTaskMessage(
420 new TaskMessageOperation(
421 task.name.replace('progress/', ''),
423 task.name.replace('progress/', '')
428 return this.messages[task.name] || this.defaultMessage;
431 getSuccessTitle(task: FinishedTask) {
432 return this._getTaskTitle(task).success(task.metadata);
435 getErrorMessage(task: FinishedTask) {
437 this._getTaskTitle(task).errors(task.metadata)[task.exception.code] || task.exception.detail
441 getErrorTitle(task: Task) {
442 return this._getTaskTitle(task).failure(task.metadata);
445 getRunningTitle(task: Task) {
446 return this._getTaskTitle(task).running(task.metadata);
449 getRunningText(task: Task) {
450 return this._getTaskTitle(task).operation.running;