From 841cb90d394534e4223aebc39560c7d1ab2b3814 Mon Sep 17 00:00:00 2001 From: Darya Baklanova Date: Fri, 19 Jan 2018 13:41:33 +0700 Subject: [PATCH] feat(sg): set name for private security groups from vm name (closes #862) --- .../redux/service-offerings.reducers.spec.ts | 14 +++--- .../reducers/vm/redux/vm-creation.effects.ts | 2 +- .../services/service-offering.service.spec.ts | 2 +- ...vm-creation-security-group.service.spec.ts | 48 +++++++++++++++++++ .../vm-creation-security-group.service.ts | 30 +++++++----- 5 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 src/app/vm/vm-creation/services/vm-creation-security-group.service.spec.ts diff --git a/src/app/reducers/service-offerings/redux/service-offerings.reducers.spec.ts b/src/app/reducers/service-offerings/redux/service-offerings.reducers.spec.ts index e17c13164a..920110cf87 100644 --- a/src/app/reducers/service-offerings/redux/service-offerings.reducers.spec.ts +++ b/src/app/reducers/service-offerings/redux/service-offerings.reducers.spec.ts @@ -57,7 +57,7 @@ describe('Test service offering reducer', () => { payload: [{ id: '1', name: 'off1' }, { id: '2', name: 'off2' }] }); expect(state) - .toEqual({ + .toEqual({ ids: ['1', '2'], entities: { 1: { id: '1', name: 'off1' }, 2: { id: '2', name: 'off2' } }, loading: false, @@ -76,7 +76,7 @@ describe('Test service offering reducer', () => { payload: { 'filterOfferings': false } }); expect(state) - .toEqual({ + .toEqual({ ids: [], entities: {}, loading: false, @@ -94,7 +94,7 @@ describe('Test service offering reducer', () => { }); expect(state) - .toEqual({ + .toEqual({ ids: [], entities: {}, loading: false, @@ -112,7 +112,7 @@ describe('Test service offering reducer', () => { }); expect(state) - .toEqual({ + .toEqual({ ids: [], entities: {}, loading: false, @@ -133,7 +133,7 @@ describe('Test service offering reducer', () => { }); expect(state) - .toEqual({ + .toEqual({ ids: [], entities: {}, loading: false, @@ -153,7 +153,7 @@ describe('Test service offering reducer', () => { payload: [{ id: '1', name: 'off1' }, { id: '2', name: 'off2' }] }); expect(state) - .toEqual({ + .toEqual({ ids: ['1', '2'], entities: { 1: { id: '1', name: 'off1' }, 2: { id: '2', name: 'off2' } }, loading: false, @@ -323,7 +323,7 @@ describe('Test service offering reducer', () => { }, resourceUsage ); - expect(result3).toEqual({ + expect(result3).toEqual({ cpunumber: { max: 2, min: 1 }, memory: { max: 2, min: 1 }, cpuspeed: { min: 1, max: 2 } diff --git a/src/app/reducers/vm/redux/vm-creation.effects.ts b/src/app/reducers/vm/redux/vm-creation.effects.ts index 663aa537bb..8d2947b899 100644 --- a/src/app/reducers/vm/redux/vm-creation.effects.ts +++ b/src/app/reducers/vm/redux/vm-creation.effects.ts @@ -559,7 +559,7 @@ export class VirtualMachineCreationEffects { if (this.createSecurityGroup(state)) { this.handleDeploymentMessages({ stage: VmDeploymentStage.SG_GROUP_CREATION }); - return this.vmCreationSecurityGroupService.getSecurityGroupCreationRequest(state.securityGroupData); + return this.vmCreationSecurityGroupService.getSecurityGroupCreationRequest(state); } else { return Observable.of(null); } diff --git a/src/app/shared/services/service-offering.service.spec.ts b/src/app/shared/services/service-offering.service.spec.ts index 9afed8bc4e..778efea0a1 100644 --- a/src/app/shared/services/service-offering.service.spec.ts +++ b/src/app/shared/services/service-offering.service.spec.ts @@ -152,7 +152,7 @@ describe('Service-offering service', () => { deploymentplanner: '', domain: 'domainId' }; - const availableOfferings = [ + const availableOfferings = [ { id: '1', name: 'Service Offering', diff --git a/src/app/vm/vm-creation/services/vm-creation-security-group.service.spec.ts b/src/app/vm/vm-creation/services/vm-creation-security-group.service.spec.ts new file mode 100644 index 0000000000..61cef7ccdb --- /dev/null +++ b/src/app/vm/vm-creation/services/vm-creation-security-group.service.spec.ts @@ -0,0 +1,48 @@ +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { Injectable } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +// tslint:disable-next-line +import { PrivateSecurityGroupCreationService } from '../../../security-group/services/creation-services/private-security-group-creation.service'; +// tslint:disable-next-line +import { SharedSecurityGroupCreationService } from '../../../security-group/services/creation-services/shared-security-group-creation.service'; +// tslint:disable-next-line +import { TemplateSecurityGroupCreationService } from '../../../security-group/services/creation-services/template-security-group-creation.service'; +import { NetworkRuleService } from '../../../security-group/services/network-rule.service'; +import { SecurityGroupService } from '../../../security-group/services/security-group.service'; +import { AsyncJobService } from '../../../shared/services/async-job.service'; +import { ConfigService } from '../../../shared/services/config.service'; +import { MarkForRemovalService } from '../../../shared/services/tags/mark-for-removal.service'; +import { SecurityGroupTagService } from '../../../shared/services/tags/security-group-tag.service'; +import { TagService } from '../../../shared/services/tags/tag.service'; +import { VmCreationSecurityGroupService } from './vm-creation-security-group.service'; + +describe('VM creation security group service', () => { + let vmCreationSecurityGroupService: VmCreationSecurityGroupService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + ConfigService, + SecurityGroupService, + VmCreationSecurityGroupService, + TemplateSecurityGroupCreationService, + PrivateSecurityGroupCreationService, + SharedSecurityGroupCreationService, + SecurityGroupTagService, + MarkForRemovalService, + TagService, + AsyncJobService, + NetworkRuleService + ], + imports: [ + HttpClientTestingModule + ] + }); + vmCreationSecurityGroupService = TestBed.get(VmCreationSecurityGroupService); + }); + + it('should assign VM name to the private security group', () => { + const result = vmCreationSecurityGroupService['securityGroupCreationData']('new-vm'); + expect(result.name).toBe('sg-new-vm'); + }); +}); diff --git a/src/app/vm/vm-creation/services/vm-creation-security-group.service.ts b/src/app/vm/vm-creation/services/vm-creation-security-group.service.ts index a5e7630537..4fe01f2e7e 100644 --- a/src/app/vm/vm-creation/services/vm-creation-security-group.service.ts +++ b/src/app/vm/vm-creation/services/vm-creation-security-group.service.ts @@ -1,8 +1,12 @@ import { Injectable } from '@angular/core'; +import { VmCreationState } from '../data/vm-creation-state'; import { VmCreationSecurityGroupData } from '../security-group/vm-creation-security-group-data'; import { Observable } from 'rxjs/Observable'; import { VmCreationSecurityGroupMode } from '../security-group/vm-creation-security-group-mode'; -import { GROUP_POSTFIX, SecurityGroupService } from '../../../security-group/services/security-group.service'; +import { + GROUP_POSTFIX, + SecurityGroupService +} from '../../../security-group/services/security-group.service'; import { NetworkRule } from '../../../security-group/network-rule.model'; import { Rules } from '../../../shared/components/security-group-builder/rules'; import { Utils } from '../../../shared/services/utils/utils.service'; @@ -14,21 +18,17 @@ export class VmCreationSecurityGroupService { constructor(private securityGroupService: SecurityGroupService) { } - public getSecurityGroupCreationRequest(securityGroupData: VmCreationSecurityGroupData): Observable { - if (securityGroupData.mode === VmCreationSecurityGroupMode.Builder) { - const data = this.securityGroupCreationData; - const rules = this.getSecurityGroupCreationRules(securityGroupData.rules); - return this.securityGroupService.createPrivate(data, rules).map(securityGroup => [securityGroup]); + public getSecurityGroupCreationRequest(state: VmCreationState): Observable { + if (state.securityGroupData.mode === VmCreationSecurityGroupMode.Builder) { + const data = this.securityGroupCreationData(state.displayName); + const rules = this.getSecurityGroupCreationRules(state.securityGroupData.rules); + return this.securityGroupService.createPrivate(data, rules) + .map(securityGroup => [securityGroup]); } else { - return Observable.of(securityGroupData.securityGroups); + return Observable.of(state.securityGroupData.securityGroups); } } - private get securityGroupCreationData(): any { - return { - name: this.name - }; - } private get name(): string { return Utils.getUniqueId() + GROUP_POSTFIX; @@ -40,4 +40,10 @@ export class VmCreationSecurityGroupService { egress: rules.egress }; } + + private securityGroupCreationData(vmDisplayName?: string): any { + return { + name: vmDisplayName ? `sg-${ vmDisplayName }` : this.name + }; + } }