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

fix(private-sg): Remove private SG after VM expunge #915

Merged
merged 10 commits into from
Feb 9, 2018
19 changes: 19 additions & 0 deletions src/app/reducers/security-groups/redux/sg.actions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Action } from '@ngrx/store';
import { SecurityGroup } from '../../../security-group/sg.model';
import { SecurityGroupCreationParams } from '../../../security-group/sg-creation/security-group-creation.component';
import { VirtualMachine } from '../../../vm/shared/vm.model';

export const LOAD_SECURITY_GROUP_REQUEST = '[SecurityGroups] LOAD_SECURITY_GROUP_REQUEST';
export const LOAD_SECURITY_GROUP_RESPONSE = '[SecurityGroups] LOAD_SECURITY_GROUP_RESPONSE';
export const SECURITY_GROUP_FILTER_UPDATE = '[SecurityGroups] SECURITY_GROUP_FILTER_UPDATE';
export const LOAD_SELECTED_SECURITY_GROUP = '[SecurityGroups] LOAD_SELECTED_SECURITY_GROUP';
export const CREATE_SECURITY_GROUP = '[SecurityGroups] CREATE_SECURITY_GROUP';
export const CREATE_SECURITY_GROUP_SUCCESS = '[SecurityGroups] CREATE_SECURITY_GROUP_SUCCESS';
export const CREATE_SECURITY_GROUPS_SUCCESS = '[SecurityGroups] CREATE_SECURITY_GROUPS_SUCCESS';
export const CREATE_SECURITY_GROUP_ERROR = '[SecurityGroups] CREATE_SECURITY_GROUP_ERROR';
export const UPDATE_SECURITY_GROUP = '[SecurityGroups] UPDATE_SECURITY_GROUP';
export const UPDATE_SECURITY_GROUP_ERROR = '[SecurityGroups] UPDATE_SECURITY_GROUP_ERROR';
export const DELETE_SECURITY_GROUP = '[SecurityGroups] DELETE_SECURITY_GROUP';
export const DELETE_PRIVATE_SECURITY_GROUP = '[SecurityGroups] DELETE_PRIVATE_SECURITY_GROUP';
export const DELETE_SECURITY_GROUP_SUCCESS = '[SecurityGroups] DELETE_SECURITY_GROUP_SUCCESS';
export const DELETE_SECURITY_GROUP_ERROR = '[SecurityGroups] DELETE_SECURITY_GROUP_ERROR';
export const CONVERT_SECURITY_GROUP = '[SecurityGroups] CONVERT_SECURITY_GROUP';
Expand Down Expand Up @@ -58,6 +61,13 @@ export class CreateSecurityGroupSuccess implements Action {
}
}

export class CreateSecurityGroupsSuccess implements Action {
type = CREATE_SECURITY_GROUPS_SUCCESS;

constructor(public payload: SecurityGroup[]) {
}
}

export class CreateSecurityGroupError implements Action {
type = CREATE_SECURITY_GROUP_ERROR;

Expand All @@ -79,6 +89,13 @@ export class DeleteSecurityGroup implements Action {
}
}

export class DeletePrivateSecurityGroup implements Action {
type = DELETE_PRIVATE_SECURITY_GROUP;

constructor(public payload: VirtualMachine) {
}
}

export class DeleteSecurityGroupSuccess implements Action {
type = DELETE_SECURITY_GROUP_SUCCESS;

Expand Down Expand Up @@ -115,9 +132,11 @@ export type Actions =
| LoadSelectedSecurityGroup
| CreateSecurityGroup
| CreateSecurityGroupSuccess
| CreateSecurityGroupsSuccess
| CreateSecurityGroupError
| UpdateSecurityGroup
| DeleteSecurityGroup
| DeletePrivateSecurityGroup
| DeleteSecurityGroupSuccess
| DeleteSecurityGroupError
| ConvertSecurityGroup;
35 changes: 28 additions & 7 deletions src/app/reducers/security-groups/redux/sg.effects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Injectable } from '@angular/core';
import {
Actions,
Effect
} from '@ngrx/effects';
import { Actions, Effect } from '@ngrx/effects';
import { Observable } from 'rxjs/Observable';
import { Action } from '@ngrx/store';
import { Action, Store } from '@ngrx/store';
import { SecurityGroupService } from '../../../security-group/services/security-group.service';
import { Rules } from '../../../shared/components/security-group-builder/rules';
import {
Expand All @@ -17,8 +14,9 @@ import { NotificationService } from '../../../shared/services/notification.servi
import { Router } from '@angular/router';
import { MatDialog } from '@angular/material';
import { SecurityGroupCreationParams } from '../../../security-group/sg-creation/security-group-creation.component';

import { State } from '../../index';
import * as securityGroup from './sg.actions';
import * as fromSecurityGroups from './sg.reducers';
import { SecurityGroupViewMode } from '../../../security-group/sg-view-mode';
import { SecurityGroupTagService } from '../../../shared/services/tags/security-group-tag.service';

Expand Down Expand Up @@ -66,6 +64,24 @@ export class SecurityGroupEffects {
.catch(error => Observable.of(new securityGroup.DeleteSecurityGroupError(error)));
});

@Effect()
deletePrivateSecurityGroup$: Observable<Action> = this.actions$
.ofType(securityGroup.DELETE_PRIVATE_SECURITY_GROUP)
.withLatestFrom(this.store.select(fromSecurityGroups.selectAll))
.map(([action, groups]: [securityGroup.DeletePrivateSecurityGroup, Array<SecurityGroup>]) => {
const vmGroup = groups.find((group: SecurityGroup) =>
action.payload.securityGroup &&
!!action.payload.securityGroup.find(vmGroup => vmGroup.id === group.id) &&
getType(group) === SecurityGroupType.Private
);
return vmGroup;
})
.filter((group: SecurityGroup) => !!group)
.switchMap((group: SecurityGroup) => {
return this.deleteSecurityGroup(group)
.map(() => new securityGroup.DeleteSecurityGroupSuccess(group));
});

@Effect({ dispatch: false })
deleteSecurityGroupSuccessNavigate$ = this.actions$
.ofType(securityGroup.DELETE_SECURITY_GROUP_SUCCESS)
Expand Down Expand Up @@ -112,6 +128,7 @@ export class SecurityGroupEffects {
};

constructor(
private store: Store<State>,
private actions$: Actions,
private securityGroupService: SecurityGroupService,
private dialogService: DialogService,
Expand Down Expand Up @@ -164,12 +181,16 @@ export class SecurityGroupEffects {
}

public onDeleteConfirmation(securityGroup: SecurityGroup): Observable<any> {
return this.securityGroupService.deleteGroup(securityGroup)
return this.deleteSecurityGroup(securityGroup)
.map(() => {
this.notificationService.message({
translationToken: this.deleteSuccessMessage[getType(securityGroup)],
interpolateParams: { name: securityGroup.name }
});
});
}

private deleteSecurityGroup(securityGroup: SecurityGroup): Observable<any> {
return this.securityGroupService.deleteGroup(securityGroup);
}
}
5 changes: 5 additions & 0 deletions src/app/reducers/security-groups/redux/sg.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export function listReducer(
...adapter.addOne(action.payload, state)
};
}
case securityGroup.CREATE_SECURITY_GROUPS_SUCCESS: {
return {
...adapter.addMany(action.payload, state)
};
}
case securityGroup.DELETE_SECURITY_GROUP_SUCCESS: {
return adapter.removeOne(action.payload.id, state);
}
Expand Down
5 changes: 4 additions & 1 deletion src/app/reducers/vm/redux/vm-creation.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { VirtualMachine, VmResourceType, VmState } from '../../../vm/shared/vm.m

import * as fromZones from '../../zones/redux/zones.reducers';
import * as vmActions from './vm.actions';
import * as securityGroupActions from '../../security-groups/redux/sg.actions';
import * as fromServiceOfferings from '../../service-offerings/redux/service-offerings.reducers';
import * as fromDiskOfferings from '../../disk-offerings/redux/disk-offerings.reducers';
import * as fromSecurityGroups from '../../security-groups/redux/sg.reducers';
Expand Down Expand Up @@ -255,6 +256,9 @@ export class VirtualMachineCreationEffects {
return this.doCreateAffinityGroup(action.payload)
.switchMap(() => this.doCreateSecurityGroup(action.payload)
.switchMap((securityGroups) => {
if (action.payload.securityGroupData.mode === VmCreationSecurityGroupMode.Builder) {
this.store.dispatch(new securityGroupActions.CreateSecurityGroupsSuccess(securityGroups));
}
this.store.dispatch(new vmActions.DeploymentChangeStatus({
stage: VmDeploymentStage.SG_GROUP_CREATION_FINISHED
}));
Expand Down Expand Up @@ -291,7 +295,6 @@ export class VirtualMachineCreationEffects {
if (action.payload.doStartVm) {
vmWithTags.state = VmState.Running;
}

return new vmActions.DeploymentRequestSuccess(vmWithTags);
})
.catch((error) => Observable.of(new vmActions.DeploymentRequestError(error)));
Expand Down
Loading