Skip to content

Commit

Permalink
Tidy up #1
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed Apr 16, 2020
1 parent ca7929b commit 6b9e79a
Show file tree
Hide file tree
Showing 15 changed files with 397 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<mat-select [(value)]="service.state[row.guid][backupType.CONNECT]"
[disabled]="!service.state[row.guid][backupType.ENDPOINT]">
<mat-option [value]="connectionTypes.NONE">None</mat-option>
<mat-option [value]="connectionTypes.CURRENT">Current User</mat-option>
<mat-option *ngIf="!!row.user" [value]="connectionTypes.CURRENT">Current User</mat-option>
<mat-option [value]="connectionTypes.ALL">All Users</mat-option>
</mat-select>
</mat-form-field>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component } from '@angular/core';
import { EndpointModel } from '../../../../../../store/src/types/endpoint.types';
import { TableCellCustom } from '../../../../shared/components/list/list.types';
import { BackupEndpointsService } from '../backup-endpoints.service';
import { BackupEndpointConnectionTypes, BackupEndpointTypes } from '../backup-restore-endpoints.service';
import { BackupEndpointConnectionTypes, BackupEndpointTypes } from '../backup-restore.types';

@Component({
selector: 'app-backup-connection-cell',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import {
BackupEndpointConnectionTypes,
BackupEndpointsConfig,
BackupEndpointTypes,
BackupRestoreEndpointService,
BaseEndpointConfig,
} from './backup-restore-endpoints.service';

} from './backup-restore.types';


interface BackupRequest {
Expand All @@ -28,7 +26,7 @@ interface BackupRequest {
}

@Injectable()
export class BackupEndpointsService extends BackupRestoreEndpointService {
export class BackupEndpointsService {

hasChanges = new BehaviorSubject(false);
hasChanges$ = this.hasChanges.asObservable();
Expand All @@ -42,7 +40,6 @@ export class BackupEndpointsService extends BackupRestoreEndpointService {
private store: Store<GeneralEntityAppState>,
private http: HttpClient
) {
super();
}

// State Related
Expand Down Expand Up @@ -106,7 +103,7 @@ export class BackupEndpointsService extends BackupRestoreEndpointService {
endpoint[BackupEndpointTypes.ENDPOINT] = true;
}
if (this.canBackup(endpoint.entity, BackupEndpointTypes.CONNECT)) {
endpoint[BackupEndpointTypes.CONNECT] = BackupEndpointConnectionTypes.CURRENT;
endpoint[BackupEndpointTypes.CONNECT] = BackupEndpointConnectionTypes.ALL;
}
});
this.validate();
Expand All @@ -120,6 +117,10 @@ export class BackupEndpointsService extends BackupRestoreEndpointService {
this.validate();
}

hasConnectionDetails(): boolean {
return !!Object.values(this.state).find(e => e[BackupEndpointTypes.CONNECT] !== BackupEndpointConnectionTypes.NONE);
}

// Request Related

createBackup(): Observable<Blob> {
Expand All @@ -144,11 +145,13 @@ export class BackupEndpointsService extends BackupRestoreEndpointService {

private createBodyToSend(sd: SessionData): BackupRequest {
const state: BackupEndpointsConfig<BaseEndpointConfig> = Object.entries(this.state).reduce((res, [endpointId, endpoint]) => {
const { entity, ...rest } = endpoint;
const requestConfig: BaseEndpointConfig = {
...rest,
};
res[endpointId] = requestConfig;
if (endpoint[BackupEndpointTypes.ENDPOINT]) {
const { entity, ...rest } = endpoint;
const requestConfig: BaseEndpointConfig = {
...rest,
};
res[endpointId] = requestConfig;
}
return res;
}, {});
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { entityCatalog } from '../../../../../../store/src/entity-catalog/entity
import { PaginationMonitorFactory } from '../../../../../../store/src/monitors/pagination-monitor.factory';
import { getPaginationObservables } from '../../../../../../store/src/reducers/pagination-reducer/pagination-reducer.helper';
import { EndpointModel } from '../../../../../../store/src/types/endpoint.types';
import { httpErrorResponseToSafeString } from '../../../../jetstream.helpers';
import { ConfirmationDialogConfig } from '../../../../shared/components/confirmation-dialog.config';
import { ConfirmationDialogService } from '../../../../shared/components/confirmation-dialog.service';
import { ITableListDataSource } from '../../../../shared/components/list/data-sources-controllers/list-data-source-types';
Expand All @@ -19,7 +20,7 @@ import { StepOnNextFunction, StepOnNextResult } from '../../../../shared/compone
import { BackupConnectionCellComponent } from '../backup-connection-cell/backup-connection-cell.component';
import { BackupEndpointsService } from '../backup-endpoints.service';
import { BackupRestoreCellComponent } from '../backup-restore-cell/backup-restore-cell.component';
import { BackupEndpointTypes } from '../backup-restore-endpoints.service';
import { BackupEndpointTypes } from '../backup-restore.types';

@Component({
selector: 'app-backup-endpoints',
Expand Down Expand Up @@ -133,10 +134,9 @@ export class BackupEndpointsComponent implements OnInit {
}

onNext: StepOnNextFunction = () => {
// TODO: RC Complete/Finish token warning
const confirmation = new ConfirmationDialogConfig(
'Backup',
'Backing up connection details ?????????',
'This backup contains endpoint connection details. The contents will be encrypted, but please still ensure the safety of the file',
'Continue',
true
);
Expand All @@ -157,13 +157,14 @@ export class BackupEndpointsComponent implements OnInit {
const downloadURL = window.URL.createObjectURL(data);
const link = document.createElement('a');
link.href = downloadURL;
const dateTime = moment().format('YYYYMMDD-HHmmss'); // TODO: RC timezone?
// Time of client, not server
const dateTime = moment().format('YYYYMMDD-HHmmss');
link.download = `stratos_backup_${dateTime}.bk`;
link.click();
};

const backupFailure = err => {
const errorMessage = this.service.createError(err);
const errorMessage = httpErrorResponseToSafeString(err);
result.next({
success: false,
message: `Failed to create backup` + (errorMessage ? `: ${errorMessage}` : '')
Expand All @@ -173,8 +174,11 @@ export class BackupEndpointsComponent implements OnInit {

const createBackup = () => this.service.createBackup().pipe(first()).subscribe(backupSuccess, backupFailure);

// TODO: RC tie in progress indicator (not sure if possible)
this.confirmDialog.openWithCancel(confirmation, createBackup, userCancelledDialog);
if (this.service.hasConnectionDetails()) {
this.confirmDialog.openWithCancel(confirmation, createBackup, userCancelledDialog);
} else {
createBackup();
}

// TODO: RC Remove console.log
return result.asObservable().pipe(tap(console.log));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { EndpointModel } from '../../../../../store/src/types/endpoint.types';

export enum BackupEndpointTypes {
ENDPOINT = 'endpoint',
CONNECT = 'connect',
}

export enum BackupEndpointConnectionTypes {
NONE = 'NONE',
CURRENT = 'CURRENT',
ALL = 'ALL'
}

export interface BackupEndpointsConfig<T> {
[endpointId: string]: T;
}

export interface BaseEndpointConfig {
[BackupEndpointTypes.ENDPOINT]: boolean;
[BackupEndpointTypes.CONNECT]: BackupEndpointConnectionTypes;
}

export interface BackupEndpointConfigUI extends BaseEndpointConfig {
entity: EndpointModel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { selectSessionData } from '../../../../../store/src/reducers/auth.reduce
import { SessionData } from '../../../../../store/src/types/auth.types';
import { LoggerService } from '../../../core/logger.service';
import { BrowserStandardEncoder } from '../../../helper';
import { BackupRestoreEndpointService } from './backup-restore-endpoints.service';

interface BackupContent {
payload: string;
Expand All @@ -23,7 +22,7 @@ interface RestoreEndpointsData {
}

@Injectable()
export class RestoreEndpointsService extends BackupRestoreEndpointService {
export class RestoreEndpointsService {

// Step 1
validFileContent = new BehaviorSubject(false);
Expand All @@ -42,14 +41,13 @@ export class RestoreEndpointsService extends BackupRestoreEndpointService {
ignoreDbVersion$ = this.ignoreDbVersion.asObservable();

// Step 2
password: string; // TODO: RC use set password in both services
private password: string;

constructor(
private store: Store<GeneralEntityAppState>,
private http: HttpClient,
private logger: LoggerService
) {
super();
this.setupStep1();
}

Expand Down Expand Up @@ -114,6 +112,10 @@ export class RestoreEndpointsService extends BackupRestoreEndpointService {
this.ignoreDbVersion.next(ignore);
}

setPassword(password: string) {
this.password = password;
}

restoreBackup(): Observable<any> {
const url = '/pp/v1/endpoints/restore';
const fromObject = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ <h1>Restore Endpoints</h1>
<p>Provide the password that was given at the time the backup was created</p>
<form [formGroup]="passwordForm" class="stepper-form">
<mat-form-field>
<input matInput formControlName="password" placeholder="Password" required type="password">
<mat-label>Password</mat-label>
<input matInput formControlName="password" required [type]="!show ? 'password' : 'text'">
<button mat-icon-button matSuffix (click)="show = !show" [attr.aria-label]="'Hide password'"
[attr.aria-pressed]="!show">
<mat-icon>{{!show ? 'visibility_off' : 'visibility'}}</mat-icon>
</button>
</mat-form-field>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Observable, of, Subject } from 'rxjs';
import { first, map, tap } from 'rxjs/operators';

import { getEventFiles } from '../../../../core/browser-helper';
import { httpErrorResponseToSafeString } from '../../../../jetstream.helpers';
import { ConfirmationDialogConfig } from '../../../../shared/components/confirmation-dialog.config';
import { ConfirmationDialogService } from '../../../../shared/components/confirmation-dialog.service';
import { StepOnNextFunction, StepOnNextResult } from '../../../../shared/components/stepper/step/step.component';
Expand All @@ -24,6 +25,7 @@ export class RestoreEndpointsComponent {
// Step 2
passwordValid$: Observable<boolean>;
passwordForm: FormGroup;
show = false;

constructor(
public service: RestoreEndpointsService,
Expand All @@ -38,7 +40,7 @@ export class RestoreEndpointsComponent {
});
this.passwordValid$ = this.passwordForm.statusChanges.pipe(
map(() => {
this.service.password = this.passwordForm.controls.password.value;
this.service.setPassword(this.passwordForm.controls.password.value);
return this.passwordForm.valid;
})
);
Expand Down Expand Up @@ -80,18 +82,16 @@ export class RestoreEndpointsComponent {
};

const backupFailure = err => {
const errorMessage = this.service.createError(err);
const errorMessage = httpErrorResponseToSafeString(err);
result.next({
success: false,
message: `Failed to restore backup` + (errorMessage ? `: ${errorMessage}` : '')
});
return of(false);
};
// TODO: RC make generic in base

const createBackup = () => this.service.restoreBackup().pipe(first()).subscribe(restoreSuccess, backupFailure);

// TODO: RC tie in progress indicator (not sure if possible)
this.confirmDialog.openWithCancel(confirmation, createBackup, userCancelledDialog);

// TODO: RC Remove console.log
Expand Down
17 changes: 17 additions & 0 deletions src/frontend/packages/core/src/jetstream.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ export function isHttpErrorResponse(obj: any): HttpErrorResponse {
) ? obj as HttpErrorResponse : null;
}

/**
* Attempt to create a sensible string explaining the error object returned from a failed http request
* @param err The raw error from a http request
*/
export function httpErrorResponseToSafeString(err: any): string {
const httpResponse: HttpErrorResponse = isHttpErrorResponse(err);
if (httpResponse) {
if (httpResponse.error) {
if (typeof (httpResponse.error) === 'string') {
return httpResponse.error + ` (${httpResponse.status})`;
}
return httpResponse.error.error + ` (${httpResponse.status})`;
}
return JSON.stringify(httpResponse.error) + ` (${httpResponse.status})`;
}
return err.message;
}
Loading

0 comments on commit 6b9e79a

Please sign in to comment.