setSnapName(snapName) {
this.snapName = snapName;
this.snapshotForm.get('snapshotName').setValue(snapName);
- this.editing = true;
+ }
+
+ /**
+ * Set the 'editing' flag. If set to TRUE, the modal dialog is in
+ * 'Edit' mode, otherwise in 'Create' mode.
+ * @param {boolean} editing
+ */
+ setEditing(editing: boolean = true) {
+ this.editing = editing;
}
editAction() {
import { RouterTestingModule } from '@angular/router/testing';
import { ToastModule } from 'ng2-toastr';
-import { BsModalRef, ModalModule } from 'ngx-bootstrap';
-import { throwError as observableThrowError } from 'rxjs';
+import { BsModalRef, BsModalService } from 'ngx-bootstrap';
+import { Subject, throwError as observableThrowError } from 'rxjs';
import { configureTestBed } from '../../../../testing/unit-test-helper';
import { ApiModule } from '../../../shared/api/api.module';
imports: [
DataTableModule,
ComponentsModule,
- ModalModule.forRoot(),
ToastModule.forRoot(),
ServicesModule,
ApiModule,
expectImageTasks(component.snapshots[2], 'Rolling back');
});
});
+
+ describe('snapshot modal dialog', () => {
+ beforeEach(() => {
+ component.poolName = 'pool01';
+ component.rbdName = 'image01';
+ spyOn(TestBed.get(BsModalService), 'show').and.callFake((content) => {
+ const ref = new BsModalRef();
+ ref.content = new content();
+ ref.content.onSubmit = new Subject();
+ return ref;
+ });
+ });
+
+ it('should display old snapshot name', () => {
+ component.openSnapshotModal('rbd/snap/edit', 'oldname');
+ expect(component.modalRef.content.snapName).toBe('oldname');
+ expect(component.modalRef.content.editing).toBeTruthy();
+ });
+
+ it('should display suggested snapshot name', () => {
+ component.openSnapshotModal('rbd/snap/create');
+ expect(component.modalRef.content.snapName).toMatch(
+ RegExp(`^${component.rbdName}-\\d+T\\d+Z\$`)
+ );
+ });
+ });
});
import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
import * as _ from 'lodash';
+import * as moment from 'moment';
import { BsModalRef, BsModalService } from 'ngx-bootstrap';
import { of } from 'rxjs';
);
}
- private openSnapshotModal(taskName: string, oldSnapshotName: string = null) {
+ private openSnapshotModal(taskName: string, snapName: string = null) {
this.modalRef = this.modalService.show(RbdSnapshotFormComponent);
this.modalRef.content.poolName = this.poolName;
this.modalRef.content.imageName = this.rbdName;
- if (oldSnapshotName) {
- this.modalRef.content.setSnapName(this.selection.first().name);
+ if (snapName) {
+ this.modalRef.content.setEditing();
+ } else {
+ // Auto-create a name for the snapshot: <image_name>_<timestamp_ISO_8601>
+ // https://en.wikipedia.org/wiki/ISO_8601
+ snapName = `${this.rbdName}-${moment()
+ .utc()
+ .format('YYYYMMDD[T]HHmmss[Z]')}`;
}
+ this.modalRef.content.setSnapName(snapName);
this.modalRef.content.onSubmit.subscribe((snapshotName: string) => {
const executingTask = new ExecutingTask();
executingTask.name = taskName;