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

Commit

Permalink
refactor(vm): get rid of BaseModel for VM model (#1180)
Browse files Browse the repository at this point in the history
PR close #1104
  • Loading branch information
HeyRoach authored Oct 17, 2018
1 parent 8813fd7 commit 825b92c
Show file tree
Hide file tree
Showing 95 changed files with 288 additions and 587 deletions.
4 changes: 2 additions & 2 deletions src/app/events/event.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModelInterface } from '../shared/models/base.model';
import { BaseModel } from '../shared/models/base.model';

export interface Event extends BaseModelInterface {
export interface Event extends BaseModel {
id: string;
type: string;
time: string;
Expand Down
14 changes: 10 additions & 4 deletions src/app/events/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { BackendResource } from '../shared/decorators';
import { BaseBackendService } from '../shared/services/base-backend.service';
import { BaseBackendService, FormattedResponse } from '../shared/services/base-backend.service';

import { Event } from './event.model';
import { HttpClient } from '@angular/common/http';
Expand All @@ -19,10 +19,16 @@ export class EventService extends BaseBackendService<Event> {
super(http);
}

protected prepareModel(res, entityModel?): Event {
const event = super.prepareModel(res, this.entityModel);
protected formatGetListResponse(response: any): FormattedResponse<Event> {
const result = super.formatGetListResponse(response);
return {
list: result.list.map(m => this.prepareEventModel(m)) as Array<Event>,
meta: result.meta
};
}

event.created = moment(res.created).toDate();
private prepareEventModel(event): Event {
event.created = moment(event.created).toDate();
event.time = this.dateTimeFormatterService.stringifyToTime(event.created);
return event;
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/reducers/security-groups/redux/sg.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class SecurityGroupEffects {
withLatestFrom(this.store.pipe(select(fromSecurityGroups.selectAll))),
map(([action, groups]: [securityGroupActions.DeletePrivateSecurityGroup, Array<SecurityGroup>]) => {
const vmGroup = groups.find((group: SecurityGroup) =>
action.payload.securityGroup &&
!!action.payload.securityGroup.find(sg => sg.id === group.id) &&
action.payload.securitygroup &&
!!action.payload.securitygroup.find(sg => sg.id === group.id) &&
getType(group) === SecurityGroupType.Private
);
return vmGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Test service offering reducer', () => {
expect(fromSOs.isLoading.projector(state)).toBe(false);
expect(fromSOs.getSelectedOffering.projector(
state.entities,
{ serviceOfferingId: 1 }
{ serviceofferingid: 1 }
))
.toEqual({ id: '1', name: 'off1' });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const filterQuery = createSelector(
export const getSelectedOffering = createSelector(
selectEntities,
fromVMs.getSelectedVM,
(entities, vm) => vm && entities[vm.serviceOfferingId]
(entities, vm) => vm && entities[vm.serviceofferingid]
);

export const classesFilter = (offering: ServiceOffering, soClasses: ComputeOfferingClass[], classesMap: any) => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/reducers/templates/redux/template.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export const getSelectedTemplate = createSelector(
export const getVMTemplate = createSelector(
selectEntities,
fromVMs.getSelectedVM,
(entities, vm) => vm && entities[vm.isoId]
(entities, vm) => vm && entities[vm.isoid]
);

export const getSelectedTemplateTags = createSelector(
Expand Down Expand Up @@ -458,7 +458,7 @@ export const selectTemplatesForIsoAttachment = createSelector(
fromVMs.getSelectedVM,
(templates, account, vm) => {
const selectedZoneFilter = (template: BaseTemplateModel) => {
return template.zoneid === vm.zoneId || template.crossZones;
return template.zoneid === vm.zoneid || template.crossZones;
};

const selectedViewModeFilter = (template: BaseTemplateModel) => {
Expand Down
10 changes: 5 additions & 5 deletions src/app/reducers/vm/redux/vm.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe('Virtual machine Effects', () => {
offering: <ServiceOffering>{},
vm: list[0]
});
const completion = new vmActions.UpdateVM(new VirtualMachine(list[0]));
const completion = new vmActions.UpdateVM(list[0] as VirtualMachine);

actions$.stream = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('Virtual machine Effects', () => {
affinityGroupId: 'af1_id',
vm: list[0]
});
const completion = new vmActions.UpdateVM(new VirtualMachine(list[0]));
const completion = new vmActions.UpdateVM(list[0] as VirtualMachine);

actions$.stream = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down Expand Up @@ -619,7 +619,7 @@ describe('Virtual machine Effects', () => {
spyOn(dialogService, 'confirm').and.returnValue(of(true));

const action = new vmActions.StartVm(list[1]);
const completion = new vmActions.UpdateVM(new VirtualMachine(list[1]));
const completion = new vmActions.UpdateVM(list[1] as VirtualMachine);

actions$.stream = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down Expand Up @@ -1024,7 +1024,7 @@ describe('Virtual machine Effects', () => {
keyPair: <SSHKeyPair>{},
vm: list[0]
});
const completion = new vmActions.UpdateVM(new VirtualMachine(list[0]));
const completion = new vmActions.UpdateVM(list[0] as VirtualMachine);

actions$.stream = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down Expand Up @@ -1100,7 +1100,7 @@ describe('Virtual machine Effects', () => {
spyOn(matDialog, 'open');

const action = new vmActions.ResetPasswordVm(list[0]);
const completion = new vmActions.UpdateVM(new VirtualMachine(list[0]));
const completion = new vmActions.UpdateVM(list[0] as VirtualMachine);

actions$.stream = hot('-a', { a: action });
const expected = cold('-b', { b: completion });
Expand Down
15 changes: 6 additions & 9 deletions src/app/reducers/vm/redux/vm.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ export class VirtualMachinesEffects {
return vm.state === VmState.Stopped;
}

private start(vm) {
private start(vm: VirtualMachine) {
const notificationId = this.jobsNotificationService.add(
'NOTIFICATIONS.VM.START_IN_PROGRESS');
this.update(vm, VmState.InProgress);
Expand All @@ -753,8 +753,7 @@ export class VirtualMachinesEffects {
this.showPasswordDialog(runningVm, 'VM_PASSWORD.PASSWORD_HAS_BEEN_SET');
}
}),
map((newVm) => new vmActions.UpdateVM(new VirtualMachine(
Object.assign({}, vm, newVm)))),
map((newVm: VirtualMachine) => new vmActions.UpdateVM(newVm)),
catchError((error: Error) => {
const message = 'NOTIFICATIONS.VM.START_FAILED';
this.showNotificationsOnFail(error, message, notificationId);
Expand Down Expand Up @@ -787,12 +786,10 @@ export class VirtualMachinesEffects {
}));
}

private update(vm, state: VmState) {
this.store.dispatch(new vmActions.UpdateVM(new VirtualMachine(Object.assign(
{},
vm,
{ state: state }
))));
private update(vm: VirtualMachine, state: VmState) {
this.store.dispatch(new vmActions.UpdateVM(
{ ...vm, state }
));
}

private askToStopVM(vm: VirtualMachine, message: string): Observable<any> {
Expand Down
10 changes: 4 additions & 6 deletions src/app/reducers/vm/redux/vm.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ export function listReducer(
}

case vmActions.UPDATE_VM: {
return {
...adapter.updateOne({ id: action.payload.id, changes: action.payload }, state),
};
return adapter.updateOne({ id: action.payload.id, changes: action.payload }, state);
}

case vmActions.REPLACE_VM: {
Expand Down Expand Up @@ -289,8 +287,8 @@ export const selectVmGroups = createSelector(
export const getUsingSGVMs = createSelector(
selectAll,
fromSGroup.getSelectedId,
(vms, sGroupId) => {
const sGroupFilter = vm => vm.securityGroup.find(group => group.id === sGroupId);
(vms: VirtualMachine[], sGroupId: string) => {
const sGroupFilter = (vm: VirtualMachine) => vm.securitygroup.find(group => group.id === sGroupId);
return vms.filter(vm => sGroupFilter(vm));
}
);
Expand Down Expand Up @@ -341,7 +339,7 @@ export const selectFilteredVMs = createSelector(
(!vm.instanceGroup && groupNamesMap[noGroup]) || (vm.instanceGroup && groupNamesMap[vm.instanceGroup.name]);

const selectedZoneIdsFilter =
vm => !selectedZoneIds.length || !!zoneIdsMap[vm.zoneId];
vm => !selectedZoneIds.length || !!zoneIdsMap[vm.zoneid];

const selectedAccountIdsFilter = vm => !selectedAccountIds.length ||
(accountsMap[vm.account] && domainsMap[vm.domainid]);
Expand Down
5 changes: 2 additions & 3 deletions src/app/reducers/volumes/redux/volumes.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ export class VolumesEffects {
const message = 'NOTIFICATIONS.VOLUME.CREATION_DONE';
this.showNotificationsOnFinish(message, notificationId);
}),
map(job => {
const createdVolume = job.jobresult['volume'];
map((volume: Volume) => {
this.dialog.closeAll();
return new volumeActions.CreateVolumeFromSnapshotSuccess(createdVolume);
return new volumeActions.CreateVolumeFromSnapshotSuccess(volume);
}),
catchError((error: Error) => {
const message = 'NOTIFICATIONS.VOLUME.CREATION_FAILED';
Expand Down
2 changes: 1 addition & 1 deletion src/app/reducers/volumes/redux/volumes.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const selectSpareOnlyVolumes = createSelector(
selectAll,
fromVMs.getSelectedVM,
(volumes: Volume[], vm: VirtualMachine) => {
const zoneFilter = (volume: Volume) => vm && volume.zoneid === vm.zoneId;
const zoneFilter = (volume: Volume) => vm && volume.zoneid === vm.zoneid;
const spareOnlyFilter = (volume: Volume) => !volume.virtualmachineid;
const accountFilter =
(volume: Volume) => vm && (volume.account === vm.account && volume.domainid === vm.domainid);
Expand Down
2 changes: 0 additions & 2 deletions src/app/security-group/services/network-rule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { NetworkRuleType, SecurityGroup } from '../sg.model';
import { BackendResource } from '../../shared/decorators/backend-resource.decorator';
import { BaseBackendCachedService } from '../../shared/services/base-backend-cached.service';
import { AsyncJobService } from '../../shared/services/async-job.service';
import { AsyncJob } from '../../shared/models';


@Injectable()
Expand All @@ -27,7 +26,6 @@ export class NetworkRuleService extends BaseBackendCachedService<SecurityGroup>
const command = 'authorize';
return this.sendCommand(`${command};${type}`, data).pipe(
switchMap(job => this.asyncJobService.queryJob(job.jobid, this.entity)),
map((job: AsyncJob<any>) => job.jobresult.securitygroup),
map(securityGroup => {
return securityGroup[`${type.toLowerCase()}rule`][0];
}));
Expand Down
4 changes: 0 additions & 4 deletions src/app/security-group/services/security-group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,4 @@ export class SecurityGroupService extends BaseBackendService<SecurityGroup> {
}
}));
}

public markForRemoval(securityGroup: SecurityGroupNative): Observable<SecurityGroupNative> {
return this.securityGroupTagService.markForRemoval(securityGroup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export class ServiceOfferingDialogComponent implements OnInit, OnChanges {
return true;
}

const isDifferentOfferingId = this.virtualMachine.serviceOfferingId !== this.serviceOffering.id;
const isDifferentOfferingId = this.virtualMachine.serviceofferingid !== this.serviceOffering.id;
const isSameCustomOfferingWithDifferentParams =
!isDifferentOfferingId
&& (this.virtualMachine.cpuNumber !== this.serviceOffering.cpunumber
|| this.virtualMachine.cpuSpeed !== this.serviceOffering.cpuspeed
&& (this.virtualMachine.cpunumber !== this.serviceOffering.cpunumber
|| this.virtualMachine.cpuspeed !== this.serviceOffering.cpuspeed
|| this.virtualMachine.memory !== this.serviceOffering.memory);

return isDifferentOfferingId || isSameCustomOfferingWithDifferentParams;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Input, OnChanges, Type } from '@angular/core';
import { BaseModelInterface } from '../../models/base.model';
import { BaseModel } from '../../models/base.model';
import { Grouping } from '../../models/grouping.model';
import * as groupBy from 'lodash/groupBy';

Expand All @@ -10,7 +10,7 @@ import * as groupBy from 'lodash/groupBy';
})
export class GroupedListComponent implements OnChanges {
@Input() public component: Type<any>;
@Input() public list: Array<BaseModelInterface>;
@Input() public list: Array<BaseModel>;
@Input() public level = 0;
@Input() public groupings: Array<Grouping>;
@Input() dynamicInputs: { [k: string]: any } = {};
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/components/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { ActivatedRoute, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { filter, pluck, switchMap } from 'rxjs/operators';

import { BaseModelInterface } from '../../models/base.model';
import { BaseModel } from '../../models/base.model';
import { BaseBackendService } from '../../services/base-backend.service';
import { SnackBarService } from '../../../core/services';
import { EntityDoesNotExistError } from './entity-does-not-exist-error';


export abstract class SidebarComponent<M extends BaseModelInterface> implements OnInit {
export abstract class SidebarComponent<M extends BaseModel> implements OnInit {
public entity: M;
public notFound: boolean;

Expand Down
2 changes: 0 additions & 2 deletions src/app/shared/decorators/backend-resource.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
interface EntityDecoratorData<M> {
entity: string;
entityModel?: { new(params?): M };
}

export function BackendResource<T>(data: EntityDecoratorData<T>): ClassDecorator {
return function<M extends Function>(target: M): typeof target {
target.prototype.entity = data.entity;
target.prototype.entityModel = data.entityModel;

return target;
};
Expand Down
11 changes: 0 additions & 11 deletions src/app/shared/decorators/field-mapper.decorator.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/shared/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './backend-resource.decorator';
export * from './field-mapper.decorator';
export * from './zone-name.decorator';
4 changes: 2 additions & 2 deletions src/app/shared/interfaces/action-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseModelInterface } from '../models';
import { BaseModel } from '../models';
import { Action } from './action.interface';


export interface ActionsService<M extends BaseModelInterface, A extends Action<M>> {
export interface ActionsService<M extends BaseModel, A extends Action<M>> {
actions: Array<A>;
}
4 changes: 2 additions & 2 deletions src/app/shared/interfaces/action.interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Observable } from 'rxjs';
import { BaseModelInterface } from '../models';
import { BaseModel } from '../models';


export interface Action<M extends BaseModelInterface> {
export interface Action<M extends BaseModel> {
name: string;
icon?: string;
hidden?(model: M): boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/models/account-user.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TimeZone } from '../components/time-zone/time-zone.service';
import { AccountType } from './account.model';
import { BaseModelInterface } from './base.model';
import { BaseModel } from './base.model';

export interface AccountUser extends BaseModelInterface {
export interface AccountUser extends BaseModel {
id: string;
username: string;
firstname: string;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/models/account.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TimeZone } from '../components/time-zone/time-zone.service';
import { AccountUser } from './account-user.model';
import { BaseModelInterface } from './base.model';
import { BaseModel } from './base.model';

export const enum AccountType {
User = '0',
Expand Down Expand Up @@ -39,7 +39,7 @@ export class AccountData {
networkdomain?: string;
}

export interface Account extends BaseModelInterface {
export interface Account extends BaseModel {
accounttype: AccountType;
cpuavailable: string;
cpulimit: string;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/models/affinity-group.model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BaseModelInterface } from './base.model';
import { BaseModel } from './base.model';


export enum AffinityGroupType {
hostAntiAffinity = 'host anti-affinity'
}

export interface AffinityGroup extends BaseModelInterface {
export interface AffinityGroup extends BaseModel {
id: string;
name: string;
description: string;
Expand Down
4 changes: 2 additions & 2 deletions src/app/shared/models/async-job.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseModelInterface } from './base.model';
import { BaseModel } from './base.model';

export interface AsyncJob<T> extends BaseModelInterface {
export interface AsyncJob<T> extends BaseModel {
jobid: string;
jobstatus: number;
jobresultcode: number;
Expand Down
Loading

0 comments on commit 825b92c

Please sign in to comment.