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

Commit

Permalink
#28 move getParams from vmCreationComponent to vmCreationState
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Shakhov committed Jun 26, 2017
1 parent 853c73c commit 889f932
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 93 deletions.
4 changes: 3 additions & 1 deletion src/app/template/shared/base-template.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { OsType } from '../../shared/models/os-type.model';
zoneid: 'zoneId',
zonename: 'zoneName',
})
export class BaseTemplateModel extends BaseModel {
export abstract class BaseTemplateModel extends BaseModel {
public path: string;

public id: string;
Expand Down Expand Up @@ -49,4 +49,6 @@ export class BaseTemplateModel extends BaseModel {
super(json);
this.created = moment(json.created).toDate();
}

public abstract get isTemplate(): boolean;
}
4 changes: 4 additions & 0 deletions src/app/template/shared/iso.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ export class Iso extends BaseTemplateModel {
public bootable: boolean;
public checksum: string;
public size: number;

public get isTemplate(): boolean {
return false;
}
}
4 changes: 4 additions & 0 deletions src/app/template/shared/template.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ export class Template extends BaseTemplateModel {
public status: string;
public type: string;
public size: number;

public get isTemplate(): boolean {
return true;
}
}
4 changes: 2 additions & 2 deletions src/app/vm/vm-creation/keyboards/keyboards.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs/Observable';


type KeyboardLayout = 'us' | 'uk' | 'jp' | 'sc';
type NamedLayout = { value: KeyboardLayout, name: string };
const KeyboardLayouts = {
export type KeyboardLayout = 'us' | 'uk' | 'jp' | 'sc';
export const KeyboardLayouts = {
us: 'us' as KeyboardLayout,
uk: 'uk' as KeyboardLayout,
jp: 'jp' as KeyboardLayout,
Expand Down
138 changes: 98 additions & 40 deletions src/app/vm/vm-creation/vm-creation-data/vm-creation-state.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
import { Rules } from '../../../security-group/sg-creation/sg-creation.component';
import { AffinityGroup, DiskOffering, InstanceGroup, ServiceOffering, Zone } from '../../../shared/models';
import { AffinityGroup, DiskOffering, InstanceGroup, ServiceOffering, SSHKeyPair, Zone } from '../../../shared/models';
import { AuthService } from '../../../shared/services';
import { ServiceLocator } from '../../../shared/services/service-locator';
import { UtilsService } from '../../../shared/services/utils.service';
import { BaseTemplateModel } from '../../../template/shared';
import { VmService } from '../../shared/vm.service';
import { KeyboardLayouts } from '../keyboards/keyboards.component';
import { VmCreationData } from './vm-creation-data';

import { NetworkRule } from '../../../security-group/sg.model';


interface VmCreationParams {
affinityGroupName?: string;
details?: Array<any>;
diskofferingid?: string; // todo: check
doStartVm?: string;
hypervisor?: string;
ingress?: Array<NetworkRule>;
egress?: Array<NetworkRule>;
keyboard?: string;
keyPair?: string;
name?: string;
serviceOfferingId?: string;
response?: 'json';
rootDiskSize?: number;
size?: number;
templateId?: string;
zoneId?: string;
}

export class VmCreationState {
public affinityGroup: AffinityGroup;
public diskOffering: DiskOffering;
public displayName: string;
public doStartVm: boolean;
public instanceGroup: InstanceGroup;
public keyboard: string;
public keyPair: string;
public keyPair: SSHKeyPair;
public rootDiskSize: number;
public rootDiskSizeMin: number;
public securityRules: Rules;
public serviceOffering: ServiceOffering;
public showRootDiskResize = false;
public zone: Zone;

private _diskOffering: DiskOffering;
private _template: BaseTemplateModel;

private auth: AuthService;
Expand All @@ -33,26 +54,13 @@ export class VmCreationState {
this.utils = ServiceLocator.injector.get(UtilsService);
this.vmService = ServiceLocator.injector.get(VmService);

this.init(data);
this.reset(data);
}

public get showSecurityGroups(): boolean {
return !this.zone.networkTypeIsBasic;
};

public reset(): void {
Object.keys(this).forEach(key => {
if (key !== 'utils') {
this[key] = null;
}
});
}

public set diskOffering(offering: DiskOffering) {
this.showRootDiskResize = offering.isCustomized;
this._diskOffering = offering;
}

public get template(): BaseTemplateModel {
return this._template;
}
Expand All @@ -66,6 +74,75 @@ export class VmCreationState {
}
}

public get showRootDiskResize(): boolean {
return this.diskOffering && this.diskOffering.isCustomized;
}

public reset(data?: VmCreationData): void {
if (!data) { data = this.data; }

const preselectedSecurityGroups = data.securityGroupTemplates.filter(securityGroup => securityGroup.preselected);
this.securityRules = Rules.createWithAllRulesSelected(preselectedSecurityGroups);

this.displayName = data.defaultName;
this.doStartVm = true;
this.keyboard = KeyboardLayouts.us;

if (data.affinityGroupList.length) { this.affinityGroup = data.affinityGroupList[0]; }
if (data.defaultTemplate) { this.template = data.defaultTemplate; }
if (data.diskOfferings.length) { this.diskOffering = data.diskOfferings[0]; }
if (data.instanceGroups.length) { this.instanceGroup = data.instanceGroups[0]; }
if (data.serviceOfferings.length) { this.serviceOffering = data.serviceOfferings[0]; }
if (data.sshKeyPairs.length) { this.keyPair = data.sshKeyPairs[0]; }
if (data.serviceOfferings.length) { this.serviceOffering = data.serviceOfferings[0]; }
if (data.zones.length) { this.zone = data.zones[0]; }
}

public getVmCreationParams(): VmCreationParams {
let params: VmCreationParams = {};

params['affinityGroupName'] = this.affinityGroup && this.affinityGroup.name;
params['doStartVm'] = this.doStartVm ? undefined : 'false';
params['keyboard'] = this.keyboard;
params['keyPair'] = this.keyPair.name; // todo: check
params['name'] = this.displayName || this.data.defaultName;
params['serviceOfferingId'] = this.serviceOffering && this.serviceOffering.id;
params['templateId'] = this.template && this.template.id;
params['zoneId'] = this.zone && this.zone.id;
params['response'] = 'json';

if (this.diskOffering && !this.template.isTemplate) {
params['diskofferingid'] = this.diskOffering.id;
params['hypervisor'] = 'KVM';
}

if (this.serviceOffering.areCustomParamsSet) {
params['details'] = [{
cpuNumber: this.serviceOffering.cpuNumber,
cpuSpeed: this.serviceOffering.cpuSpeed,
memory: this.serviceOffering.memory
}];
}

if (this.template.isTemplate || this.showRootDiskResize) {
if (this.template.isTemplate) {
params['rootDiskSize'] = this.rootDiskSize;
} else {
params['size'] = this.rootDiskSize;
}
}

if (this.securityRules && this.securityRules.ingress) {
params['ingress'] = this.securityRules.ingress;
}

if (this.securityRules && this.securityRules.egress) {
params['egress'] = this.securityRules.egress;
}

return params;
}

private setMinDiskSize(): void {
const t = this.template;
if (!t) {
Expand All @@ -75,29 +152,10 @@ export class VmCreationState {
if (t.size != null) {
const newSize = t.size / Math.pow(2, 30);
this.rootDiskSize = newSize;
this.rootDiskSizeMin = newSize;
} else {
this.rootDiskSize = 1;
this.rootDiskSizeMin = 1;
}
}

private init(data: VmCreationData): void {
if (data.affinityGroupList.length) {
this.affinityGroup = data.affinityGroupList[0];
}

if (data.serviceOfferings.length) {
this.serviceOffering = data.serviceOfferings[0];
}

if (data.instanceGroups.length) {
this.instanceGroup = data.instanceGroups[0];
}

this.displayName = data.defaultName;
this.doStartVm = true;

this.rootDiskSize = 1;
this.keyboard = 'us';
this.keyPair = '';
}
}
51 changes: 1 addition & 50 deletions src/app/vm/vm-creation/vm-creation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class VmCreationComponent implements OnInit {

// todo: move to vmCreationService
public deployVm(): void {
let params: any = this.vmCreateParams;
const params = this.vmCreationState.getVmCreationParams();

let shouldCreateAffinityGroup = false;
let affinityGroupName = params['affinityGroupNames'];
Expand Down Expand Up @@ -226,10 +226,6 @@ export class VmCreationComponent implements OnInit {
return this.vmCreationState.template instanceof Template;
}

public setDiskOffering(diskOffering: DiskOffering): void {
this.vmCreationState.diskOffering = diskOffering;
}

private getVmCreateData(): Observable<void> {
return this.zoneService.getList()
.switchMap(zoneList => {
Expand All @@ -238,51 +234,6 @@ export class VmCreationComponent implements OnInit {
});
}

// todo: move to vmCreationState?
private get vmCreateParams(): {} {
let params = {
'serviceOfferingId': this.vmCreationState.serviceOffering.id,
'templateId': this.vmCreationState.template.id,
'zoneId': this.vmCreationState.zone.id,
'keyPair': this.vmCreationState.keyPair,
'keyboard': this.vmCreationState.keyboard,
'response': 'json'
};

if (this.vmCreationState.serviceOffering.areCustomParamsSet) {
params['details'] = [{
cpuNumber: this.vmCreationState.serviceOffering.cpuNumber,
cpuSpeed: this.vmCreationState.serviceOffering.cpuSpeed,
memory: this.vmCreationState.serviceOffering.memory
}];
}

const affinityGroupName = this.vmCreationState.affinityGroup.name;
params['name'] = this.vmCreationState.displayName || this.defaultName;

if (affinityGroupName) {
params['affinityGroupNames'] = affinityGroupName;
}
if (this.vmCreationState.diskOffering && !this.templateSelected) {
params['diskofferingid'] = this.vmCreationState.diskOffering.id;
params['hypervisor'] = 'KVM';
}
if (this.templateSelected || this.vmCreationState.showRootDiskResize) {
const key = this.templateSelected ? 'rootDiskSize' : 'size';
params[key] = this.vmCreationState.rootDiskSize;
}
if (!this.vmCreationState.doStartVm) {
params['startVm'] = 'false';
}
if (this.vmCreationState.securityRules && this.vmCreationState.securityRules.ingress) {
params['ingress'] = this.vmCreationState.securityRules.ingress;
}
if (this.vmCreationState.securityRules && this.vmCreationState.securityRules.egress) {
params['egress'] = this.vmCreationState.securityRules.egress;
}
return params;
}

// todo: move to vmCreationService and return Subject<VmCreationState>
private deploy(params): void {
let deployObservable = new Subject();
Expand Down

0 comments on commit 889f932

Please sign in to comment.