From 260dad17298fdd872123df490ca5795369b1f0a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stephan=20M=C3=BCller?= Date: Wed, 30 May 2018 13:46:29 +0200 Subject: [PATCH] mgr/dashboard: Use task wrapper in RBD form MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stephan Müller --- .../block/rbd-form/rbd-form.component.spec.ts | 44 ++--- .../ceph/block/rbd-form/rbd-form.component.ts | 182 ++++++------------ 2 files changed, 73 insertions(+), 153 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.spec.ts index cf76a3ef7bd..762db57511d 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.spec.ts @@ -1,17 +1,11 @@ -import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ReactiveFormsModule } from '@angular/forms'; import { RouterTestingModule } from '@angular/router/testing'; -import { of as observableOf } from 'rxjs'; +import { ToastModule } from 'ng2-toastr'; -import { PoolService } from '../../../shared/api/pool.service'; -import { RbdService } from '../../../shared/api/rbd.service'; -import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe'; -import { FormatterService } from '../../../shared/services/formatter.service'; -import { NotificationService } from '../../../shared/services/notification.service'; -import { TaskManagerMessageService } from '../../../shared/services/task-manager-message.service'; -import { TaskManagerService } from '../../../shared/services/task-manager.service'; +import { SharedModule } from '../../../shared/shared.module'; import { configureTestBed } from '../../../shared/unit-test-helper'; import { RbdFormComponent } from './rbd-form.component'; @@ -19,31 +13,15 @@ describe('RbdFormComponent', () => { let component: RbdFormComponent; let fixture: ComponentFixture; - const fakeService = { - subscribe: (name, metadata, onTaskFinished: (finishedTask: any) => any) => { - return null; - }, - defaultFeatures: () => observableOf([]), - list: (attrs = []) => { - return new Promise(function(resolve, reject) { - return; - }); - } - }; - configureTestBed({ - imports: [ReactiveFormsModule, RouterTestingModule], - declarations: [RbdFormComponent], - schemas: [NO_ERRORS_SCHEMA], - providers: [ - DimlessBinaryPipe, - FormatterService, - TaskManagerMessageService, - { provide: NotificationService, useValue: fakeService }, - { provide: PoolService, useValue: fakeService }, - { provide: RbdService, useValue: fakeService }, - { provide: TaskManagerService, useValue: fakeService } - ] + imports: [ + HttpClientTestingModule, + ReactiveFormsModule, + RouterTestingModule, + ToastModule.forRoot(), + SharedModule + ], + declarations: [RbdFormComponent] }); beforeEach(() => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.ts index be8d5922e59..631bfaa2547 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/rbd-form/rbd-form.component.ts @@ -3,16 +3,14 @@ import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms' import { ActivatedRoute, Router } from '@angular/router'; import * as _ from 'lodash'; +import { Observable } from 'rxjs/Observable'; import { PoolService } from '../../../shared/api/pool.service'; import { RbdService } from '../../../shared/api/rbd.service'; -import { NotificationType } from '../../../shared/enum/notification-type.enum'; import { FinishedTask } from '../../../shared/models/finished-task'; import { DimlessBinaryPipe } from '../../../shared/pipes/dimless-binary.pipe'; import { FormatterService } from '../../../shared/services/formatter.service'; -import { NotificationService } from '../../../shared/services/notification.service'; -import { TaskManagerMessageService } from '../../../shared/services/task-manager-message.service'; -import { TaskManagerService } from '../../../shared/services/task-manager.service'; +import { TaskWrapperService } from '../../../shared/services/task-wrapper.service'; import { RbdFormCloneRequestModel } from './rbd-form-clone-request.model'; import { RbdFormCopyRequestModel } from './rbd-form-copy-request.model'; import { RbdFormCreateRequestModel } from './rbd-form-create-request.model'; @@ -72,15 +70,15 @@ export class RbdFormComponent implements OnInit { '32MiB' ]; - constructor(private route: ActivatedRoute, - private router: Router, - private poolService: PoolService, - private rbdService: RbdService, - private formatter: FormatterService, - private dimlessBinaryPipe: DimlessBinaryPipe, - private taskManagerService: TaskManagerService, - private taskManagerMessageService: TaskManagerMessageService, - private notificationService: NotificationService) { + constructor( + private route: ActivatedRoute, + private router: Router, + private poolService: PoolService, + private rbdService: RbdService, + private formatter: FormatterService, + private taskWrapper: TaskWrapperService, + private dimlessBinaryPipe: DimlessBinaryPipe + ) { this.features = { 'deep-flatten': { desc: 'Deep flatten', @@ -442,27 +440,14 @@ export class RbdFormComponent implements OnInit { return request; } - createAction() { + createAction(): Observable { const request = this.createRequest(); - const finishedTask = new FinishedTask(); - finishedTask.name = 'rbd/create'; - finishedTask.metadata = {'pool_name': request.pool_name, 'image_name': request.name}; - this.rbdService.create(request).toPromise().then((resp) => { - if (resp.status === 202) { - this.notificationService.show(NotificationType.info, - `RBD creation in progress...`, - this.taskManagerMessageService.getDescription(finishedTask)); - this.taskManagerService.subscribe(finishedTask.name, finishedTask.metadata, - (asyncFinishedTask: FinishedTask) => { - this.notificationService.notifyTask(asyncFinishedTask); - }); - } else { - finishedTask.success = true; - this.notificationService.notifyTask(finishedTask); - } - this.router.navigate(['/block/rbd']); - }, (resp) => { - this.rbdForm.setErrors({'cdSubmitButton': true}); + return this.taskWrapper.wrapTaskAroundCall({ + task: new FinishedTask('rbd/create', { + pool_name: request.pool_name, + image_name: request.name + }), + call: this.rbdService.create(request) }); } @@ -494,64 +479,33 @@ export class RbdFormComponent implements OnInit { return request; } - editAction() { - const request = this.editRequest(); - const finishedTask = new FinishedTask(); - finishedTask.name = 'rbd/edit'; - finishedTask.metadata = { - 'pool_name': this.response.pool_name, - 'image_name': this.response.name - }; - this.rbdService.update(this.response.pool_name, this.response.name, request) - .toPromise().then((resp) => { - if (resp.status === 202) { - this.notificationService.show(NotificationType.info, - `RBD update in progress...`, - this.taskManagerMessageService.getDescription(finishedTask)); - this.taskManagerService.subscribe(finishedTask.name, finishedTask.metadata, - (asyncFinishedTask: FinishedTask) => { - this.notificationService.notifyTask(asyncFinishedTask); - }); - } else { - finishedTask.success = true; - this.notificationService.notifyTask(finishedTask); - } - this.router.navigate(['/block/rbd']); - }).catch((resp) => { - this.rbdForm.setErrors({'cdSubmitButton': true}); - }); + editAction(): Observable { + return this.taskWrapper.wrapTaskAroundCall({ + task: new FinishedTask('rbd/edit', { + pool_name: this.response.pool_name, + image_name: this.response.name + }), + call: this.rbdService.update(this.response.pool_name, this.response.name, this.editRequest()) + }); } - cloneAction() { + cloneAction(): Observable { const request = this.cloneRequest(); - const finishedTask = new FinishedTask(); - finishedTask.name = 'rbd/clone'; - finishedTask.metadata = { - 'parent_pool_name': this.response.pool_name, - 'parent_image_name': this.response.name, - 'parent_snap_name': this.snapName, - 'child_pool_name': request.child_pool_name, - 'child_image_name': request.child_image_name - }; - this.rbdService - .cloneSnapshot(this.response.pool_name, this.response.name, this.snapName, request) - .subscribe((resp) => { - if (resp.status === 202) { - this.notificationService.show(NotificationType.info, - `RBD clone in progress...`, - this.taskManagerMessageService.getDescription(finishedTask)); - this.taskManagerService.subscribe(finishedTask.name, finishedTask.metadata, - (asyncFinishedTask: FinishedTask) => { - this.notificationService.notifyTask(asyncFinishedTask); - }); - } else { - finishedTask.success = true; - this.notificationService.notifyTask(finishedTask); - } - this.router.navigate(['/block/rbd']); - }, (resp) => { - this.rbdForm.setErrors({'cdSubmitButton': true}); - }); + return this.taskWrapper.wrapTaskAroundCall({ + task: new FinishedTask('rbd/clone', { + parent_pool_name: this.response.pool_name, + parent_image_name: this.response.name, + parent_snap_name: this.snapName, + child_pool_name: request.child_pool_name, + child_image_name: request.child_image_name + }), + call: this.rbdService.cloneSnapshot( + this.response.pool_name, + this.response.name, + this.snapName, + request + ) + }); } copyRequest(): RbdFormCopyRequestModel { @@ -573,46 +527,34 @@ export class RbdFormComponent implements OnInit { return request; } - copyAction() { + copyAction(): Observable { const request = this.copyRequest(); - const finishedTask = new FinishedTask(); - finishedTask.name = 'rbd/copy'; - finishedTask.metadata = { - 'src_pool_name': this.response.pool_name, - 'src_image_name': this.response.name, - 'dest_pool_name': request.dest_pool_name, - 'dest_image_name': request.dest_image_name - }; - this.rbdService.copy(this.response.pool_name, this.response.name, request) - .toPromise().then((resp) => { - if (resp.status === 202) { - this.notificationService.show(NotificationType.info, - `RBD copy in progress...`, - this.taskManagerMessageService.getDescription(finishedTask)); - this.taskManagerService.subscribe(finishedTask.name, finishedTask.metadata, - (asyncFinishedTask: FinishedTask) => { - this.notificationService.notifyTask(asyncFinishedTask); - }); - } else { - finishedTask.success = true; - this.notificationService.notifyTask(finishedTask); - } - this.router.navigate(['/block/rbd']); - }).catch((resp) => { - this.rbdForm.setErrors({'cdSubmitButton': true}); - }); + return this.taskWrapper.wrapTaskAroundCall({ + task: new FinishedTask('rbd/copy', { + src_pool_name: this.response.pool_name, + src_image_name: this.response.name, + dest_pool_name: request.dest_pool_name, + dest_image_name: request.dest_image_name + }), + call: this.rbdService.copy(this.response.pool_name, this.response.name, request) + }); } submit() { + let action: Observable; if (this.mode === this.rbdFormMode.editing) { - this.editAction(); + action = this.editAction(); } else if (this.mode === this.rbdFormMode.cloning) { - this.cloneAction(); + action = this.cloneAction(); } else if (this.mode === this.rbdFormMode.copying) { - this.copyAction(); + action = this.copyAction(); } else { - this.createAction(); + action = this.createAction(); } + action.subscribe( + undefined, + () => this.rbdForm.setErrors({ cdSubmitButton: true }), + () => this.router.navigate(['/block/rbd']) + ); } - } -- 2.39.5