Skip to content
This repository was archived by the owner on Mar 25, 2023. It is now read-only.

Commit

Permalink
refactor(services): Store commands (closes #215) (#980)
Browse files Browse the repository at this point in the history
Darya Baklanova authored Feb 12, 2018

Verified

This commit was signed with the committer’s verified signature.
scala-steward Scala Steward
1 parent 04872ee commit 51e9f82
Showing 17 changed files with 112 additions and 80 deletions.
18 changes: 9 additions & 9 deletions src/app/reducers/vm/redux/vm.effects.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import { VmPulseComponent } from '../../../pulse/vm-pulse/vm-pulse.component';
import { ProgressLoggerMessageStatus } from '../../../shared/components/progress-logger/progress-logger-message/progress-logger-message';
import { AffinityGroupService } from '../../../shared/services/affinity-group.service';
import { AuthService } from '../../../shared/services/auth.service';
import { CSCommands } from '../../../shared/services/base-backend.service';
import { JobsNotificationService } from '../../../shared/services/jobs-notification.service';
import { SSHKeyPairService } from '../../../shared/services/ssh-keypair.service';
import { UserTagService } from '../../../shared/services/tags/user-tag.service';
@@ -32,7 +33,6 @@ import { WebShellService } from '../../../vm/web-shell/web-shell.service';
import { State } from '../../index';
import * as volumeActions from '../../volumes/redux/volumes.actions';
import * as sgActions from '../../security-groups/redux/sg.actions';

import * as vmActions from './vm.actions';


@@ -325,7 +325,7 @@ export class VirtualMachinesEffects {
const notificationId = this.jobsNotificationService.add(
'JOB_NOTIFICATIONS.VM.STOP_IN_PROGRESS');
this.update(action.payload, VmState.Stopping);
return this.vmService.command(action.payload, 'stop')
return this.vmService.command(action.payload, CSCommands.Stop)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.STOP_DONE'
@@ -387,7 +387,7 @@ export class VirtualMachinesEffects {
}
});

return this.vmService.command(action.payload, 'destroy', params)
return this.vmService.command(action.payload, CSCommands.Destroy, params)
.pipe(actions)
.catch((error: Error) => {
this.jobsNotificationService.fail({
@@ -414,7 +414,7 @@ export class VirtualMachinesEffects {
const notificationId = this.jobsNotificationService.add(
'JOB_NOTIFICATIONS.VM.REBOOT_IN_PROGRESS');
this.update(action.payload, VmState.InProgress);
return this.vmService.command(action.payload, 'reboot')
return this.vmService.command(action.payload, CSCommands.Reboot)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.REBOOT_DONE'
@@ -446,7 +446,7 @@ export class VirtualMachinesEffects {
'JOB_NOTIFICATIONS.VM.RESTORE_IN_PROGRESS');
this.update(action.payload, VmState.InProgress);

return this.vmService.command(action.payload, 'restore')
return this.vmService.command(action.payload, CSCommands.Restore)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.RESTORE_DONE'
@@ -514,7 +514,7 @@ export class VirtualMachinesEffects {
];
});

return this.vmService.command(action.payload, 'expunge')
return this.vmService.command(action.payload, CSCommands.Expunge)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.EXPUNGE_DONE'
@@ -642,7 +642,7 @@ export class VirtualMachinesEffects {
const notificationId = this.jobsNotificationService.add(
'JOB_NOTIFICATIONS.VM.RESET_PASSWORD_IN_PROGRESS');

return this.vmService.command(resetAction.payload, 'resetPasswordFor')
return this.vmService.command(resetAction.payload, CSCommands.ResetPasswordFor)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.RESET_PASSWORD_DONE'
@@ -837,7 +837,7 @@ export class VirtualMachinesEffects {
const notificationId = this.jobsNotificationService.add(
'JOB_NOTIFICATIONS.VM.START_IN_PROGRESS');
this.update(vm, VmState.InProgress);
return this.vmService.command(vm, 'start')
return this.vmService.command(vm, CSCommands.Start)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.START_DONE'
@@ -861,7 +861,7 @@ export class VirtualMachinesEffects {
const notificationId = this.jobsNotificationService.add(
'JOB_NOTIFICATIONS.VM.STOP_IN_PROGRESS');
this.update(vm, VmState.InProgress);
return this.vmService.command(vm, 'stop')
return this.vmService.command(vm, CSCommands.Stop)
.do(() => this.jobsNotificationService.finish({
id: notificationId,
message: 'JOB_NOTIFICATIONS.VM.STOP_DONE'
16 changes: 4 additions & 12 deletions src/app/shared/services/account.service.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { Observable } from 'rxjs/Observable';
import { BackendResource } from '../decorators/backend-resource.decorator';
import { Account } from '../models/account.model';
import { AsyncJobService } from './async-job.service';
import { BaseBackendService } from './base-backend.service';
import { BaseBackendService, CSCommands } from './base-backend.service';

@Injectable()
@BackendResource({
@@ -24,29 +24,21 @@ export class AccountService extends BaseBackendService<Account> {
}

public removeAccount(account: Account): Observable<Account> {
return this.sendCommand('delete', { id: account.id })
return this.sendCommand(CSCommands.Delete, { id: account.id })
.switchMap(job => this.asyncJobService.queryJob(job))
.switchMap(response => Observable.of(account));
}

public disableAccount(account: Account): Observable<Account> {
return this.sendCommand('disable', {
return this.sendCommand(CSCommands.Disable, {
id: account.id,
lock: false
}).switchMap(job => this.asyncJobService.queryJob(job))
.switchMap(response => Observable.of(response.result.account));
}

public lockAccount(account: Account): Observable<Account> {
return this.sendCommand('disable', {
id: account.id,
lock: true
}).switchMap(job => this.asyncJobService.queryJob(job))
.switchMap(response => Observable.of(response.result.account));
}

public enableAccount(account: Account): Observable<Account> {
return this.sendCommand('enable', {
return this.sendCommand(CSCommands.Enable, {
id: account.id
}).map(res => res.account);
}
3 changes: 2 additions & 1 deletion src/app/shared/services/affinity-group.service.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { AffinityGroup } from '../models';
import { AffinityGroupType } from '../models/affinity-group.model';
import { AsyncJobService } from './async-job.service';
import { BaseBackendCachedService } from './base-backend-cached.service';
import { CSCommands } from './base-backend.service';


export interface AffinityGroupCreationData {
@@ -37,7 +38,7 @@ export class AffinityGroupService extends BaseBackendCachedService<AffinityGroup
vmId: string,
affinityGroupId: string
): Observable<VirtualMachine> {
return this.sendCommand('updateVM', {
return this.sendCommand(CSCommands.UpdateVM, {
id: vmId,
affinityGroupIds: affinityGroupId
})
4 changes: 2 additions & 2 deletions src/app/shared/services/async-job.service.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { Subject } from 'rxjs/Subject';
import { BackendResource } from '../decorators';

import { AsyncJob, mapCmd } from '../models';
import { BaseBackendService } from './base-backend.service';
import { BaseBackendService, CSCommands } from './base-backend.service';
import { ErrorService } from './error.service';


@@ -74,7 +74,7 @@ export class AsyncJobService extends BaseBackendService<AsyncJob<any>> {
entityModel: any,
interval?: any
): void {
this.sendCommand('query;Result', { jobId })
this.sendCommand(CSCommands.QueryResult, { jobId })
.map(res => res as AsyncJob<typeof entityModel>)
.subscribe((asyncJob) => {
switch (asyncJob.jobstatus) {
4 changes: 2 additions & 2 deletions src/app/shared/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { BaseModelStub } from '../models';
import { AccountType } from '../models/account.model';
import { User } from '../models/user.model';
import { AsyncJobService } from './async-job.service';
import { BaseBackendService } from './base-backend.service';
import { BaseBackendService, CSCommands } from './base-backend.service';
import { LocalStorageService } from './local-storage.service';
import { Utils } from './utils/utils.service';
import { Store } from '@ngrx/store';
@@ -133,7 +133,7 @@ export class AuthService extends BaseBackendService<BaseModelStub> {
}

private getCapabilities(): Observable<void> {
return this.sendCommand('listCapabilities', {}, '')
return this.sendCommand(CSCommands.ListCapabilities, {}, '')
.map(({ capability }) => (this.capabilities = capability))
.catch(() => this.logout());
}
47 changes: 36 additions & 11 deletions src/app/shared/services/base-backend.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import {
HttpClient,
HttpHeaders,
HttpParams
} from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import * as range from 'lodash/range';

import { BaseModel } from '../models';
import { Cache } from './cache';
import { CacheService } from './cache.service';
import { ErrorService } from './error.service';
import { BaseModelInterface } from '../models/base.model';
import * as range from 'lodash/range';

export const BACKEND_API_URL = 'client/api';

@@ -28,6 +22,37 @@ export interface FormattedResponse<M> {
}
}

export enum CSCommands {
AddIpTo = 'addIpTo',
Attach = 'attach',
ChangeServiceFor = 'changeServiceFor',
Create = 'create',
Delete = 'delete',
RemoveIpFrom = 'removeIpFrom',
Deploy = 'deploy',
Destroy = 'destroy',
Detach = 'detach',
Disable = 'disable',
Enable = 'enable',
Expunge = 'expunge',
GetKeys = 'get;Keys',
List = 'list;s',
ListCapabilities = 'listCapabilities',
QueryResult = 'query;Result',
Reboot = 'reboot',
Register = 'register',
RegisterKeys = 'register;Keys',
ResetForVM = 'reset;ForVirtualMachine',
ResetPasswordFor = 'resetPasswordFor',
Resize = 'resize',
Restore = 'restore',
Revert = 'revert',
Start = 'start',
Stop = 'stop',
Update = 'update',
UpdateVM = 'updateVM',
}

export abstract class BaseBackendService<M extends BaseModelInterface> {
protected entity: string;
protected entityModel?: { new (params?): M };
@@ -90,7 +115,7 @@ export abstract class BaseBackendService<M extends BaseModelInterface> {
}

public create(params?: {}, customApiFormat?: ApiFormat): Observable<any> {
const command = (customApiFormat && customApiFormat.command) || 'create';
const command = (customApiFormat && customApiFormat.command) || CSCommands.Create;
const _entity = customApiFormat && customApiFormat.entity;

return this.sendCommand(command, params, _entity).map(response => {
@@ -104,7 +129,7 @@ export abstract class BaseBackendService<M extends BaseModelInterface> {
}

public remove(params?: {}, customApiFormat?: ApiFormat): Observable<any> {
const command = (customApiFormat && customApiFormat.command) || 'delete';
const command = (customApiFormat && customApiFormat.command) || CSCommands.Delete;
const entity = customApiFormat && customApiFormat.entity;

return this.sendCommand(command, params, entity);
@@ -218,7 +243,7 @@ export abstract class BaseBackendService<M extends BaseModelInterface> {
if (cachedRequest) {
return cachedRequest;
}
const command = (customApiFormat && customApiFormat.command) || 'list;s';
const command = (customApiFormat && customApiFormat.command) || CSCommands.List;
const entity = customApiFormat && customApiFormat.entity;
const request = this.sendCommand(command, params, entity)
.map(response => this.formatGetListResponse(response))
4 changes: 2 additions & 2 deletions src/app/shared/services/configuration.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BackendResource } from '../decorators/backend-resource.decorator';
import { BaseBackendService } from './base-backend.service';
import { BaseBackendService, CSCommands } from './base-backend.service';
import { Configuration } from '../models/configuration.model';
import { Observable } from 'rxjs/Observable';
import { Account } from '../models/account.model';
@@ -19,7 +19,7 @@ export class ConfigurationService extends BaseBackendService<Configuration> {
configuration: Configuration,
account: Account
): Observable<Configuration> {
return this.sendCommand('update', {
return this.sendCommand(CSCommands.Update, {
accountid: account.id,
name: configuration.name,
value: configuration.value
3 changes: 2 additions & 1 deletion src/app/shared/services/resource-count.service.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { BackendResource } from '../decorators/backend-resource.decorator';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { ResourceCount } from '../models/resource-count.model';
import { CSCommands } from './base-backend.service';


@Injectable()
@@ -18,6 +19,6 @@ export class ResourceCountService extends BaseBackendCachedService<ResourceCount
public updateResourceCount(
params: { [key: string]: string; }
): Observable<Array<ResourceCount>> {
return this.sendCommand('update', params).map(response => this.formatGetListResponse(response).list);
return this.sendCommand(CSCommands.Update, params).map(response => this.formatGetListResponse(response).list);
}
}
3 changes: 2 additions & 1 deletion src/app/shared/services/resource-limit.service.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { ResourceLimit } from '../models/resource-limit.model';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
import { Account } from '../models/account.model';
import { CSCommands } from './base-backend.service';


@Injectable()
@@ -20,7 +21,7 @@ export class ResourceLimitService extends BaseBackendCachedService<ResourceLimit
resourceLimit: ResourceLimit,
account: Account
): Observable<ResourceLimit> {
return this.sendCommand('update', {
return this.sendCommand(CSCommands.Update, {
resourceType: resourceLimit.resourcetype,
max: resourceLimit.max,
domainid: account.domainid,
7 changes: 4 additions & 3 deletions src/app/shared/services/snapshot.service.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { BackendResource } from '../decorators';
import { AsyncJob, Snapshot } from '../models';
import { AsyncJobService } from './async-job.service';
import { BaseBackendCachedService } from './base-backend-cached.service';
import { CSCommands } from './base-backend.service';
import { SnapshotTagService } from './tags/snapshot-tag.service';


@@ -30,7 +31,7 @@ export class SnapshotService extends BaseBackendCachedService<Snapshot> {
this.invalidateCache();

const params = this.getSnapshotCreationParams(volumeId, name);
return this.sendCommand('create', params)
return this.sendCommand(CSCommands.Create, params)
.switchMap(job => this.asyncJobService.queryJob(job, this.entity, this.entityModel))
.switchMap((response: AsyncJob<Snapshot>) => {
const snapshot = response.jobresult.snapshot;
@@ -49,12 +50,12 @@ export class SnapshotService extends BaseBackendCachedService<Snapshot> {

public remove(id: string): Observable<any> {
this.invalidateCache();
return this.sendCommand('delete', { id })
return this.sendCommand(CSCommands.Delete, { id })
.switchMap(job => this.asyncJobService.queryJob(job.jobid));
}

public revert(id: string): Observable<AsyncJob<Snapshot>> {
return this.sendCommand('revert', { id })
return this.sendCommand(CSCommands.Revert, { id })
.switchMap(job => this.asyncJobService.queryJob(job.jobid));
}

7 changes: 4 additions & 3 deletions src/app/shared/services/ssh-keypair.service.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { BackendResource } from '../decorators';
import { SSHKeyPair } from '../models';
import { AsyncJobService } from './async-job.service';
import { BaseBackendCachedService } from './base-backend-cached.service';
import { CSCommands } from './base-backend.service';


export interface SshKeyCreationData {
@@ -33,18 +34,18 @@ export class SSHKeyPairService extends BaseBackendCachedService<SSHKeyPair> {

public create(params: SshKeyCreationData): Observable<SSHKeyPair> {
this.invalidateCache();
return this.sendCommand('create', params)
return this.sendCommand(CSCommands.Create, params)
.map(response => this.prepareModel(response['keypair']));
}

public register(params: SshKeyCreationData): Observable<SSHKeyPair> {
this.invalidateCache();
return this.sendCommand('register', params)
return this.sendCommand(CSCommands.Register, params)
.map(response => this.prepareModel(response['keypair']));
}

public reset(params): Observable<VirtualMachine> {
return this.sendCommand('reset;ForVirtualMachine', params, 'SSHKey')
return this.sendCommand(CSCommands.ResetForVM, params, 'SSHKey')
.switchMap(job =>
this.asyncJobService.queryJob(job.jobid, 'VirtualMachine', VirtualMachine)
);
Loading

0 comments on commit 51e9f82

Please sign in to comment.