1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/**
* Created by ob0695 on 7/17/2018.
*/
import { Action, Selector, State, StateContext, Store } from '@ngxs/store';
import * as _ from 'lodash';
import { tap } from 'rxjs/operators';
import { ArtifactModel } from '../../../models/artifacts';
import { ArtifactGroupType } from '../../../utils/constants';
import { ComponentInstanceServiceNg2 } from '../../services/component-instance-services/component-instance.service';
import { ComponentGenericResponse } from '../../services/responses/component-generic-response';
import {
CreateInstanceArtifactAction,
DeleteInstanceArtifactAction,
GetInstanceArtifactsByTypeAction,
UpdateInstanceArtifactAction
} from '../actions/instance-artifacts.actions';
import { ArtifactsStateModel } from './artifacts.state';
export interface InstanceArtifactsStateModel {
artifacts: ArtifactModel[];
deploymentArtifacts: ArtifactModel[];
}
@State<InstanceArtifactsStateModel>({
name: 'instance_artifacts',
defaults: {
artifacts: [],
deploymentArtifacts: []
}
})
export class InstanceArtifactsState {
constructor(private store: Store, protected componentInstanceService: ComponentInstanceServiceNg2) {
}
@Selector()
static getArtifactsByType(state: InstanceArtifactsStateModel) {
return (type: string) => {
switch (type) {
case ArtifactGroupType.INFORMATION:
return state.artifacts;
case ArtifactGroupType.DEPLOYMENT:
return state.deploymentArtifacts;
}
};
}
@Action(GetInstanceArtifactsByTypeAction)
getInstanceArtifactsByType({getState, patchState}: StateContext<InstanceArtifactsStateModel>, action: GetInstanceArtifactsByTypeAction) {
const state = getState();
return this.componentInstanceService.getComponentInstanceArtifactsByGroupType(action.payload.componentType, action.payload.componentId, action.payload.instanceId, action.payload.artifactType)
.pipe(tap((resp: ComponentGenericResponse) => {
switch (action.payload.artifactType) {
case ArtifactGroupType.INFORMATION:
patchState({
artifacts: _.values(resp) as ArtifactModel[]
});
break;
case ArtifactGroupType.DEPLOYMENT:
patchState({
deploymentArtifacts: _.values(resp) as ArtifactModel[]
});
break;
}
}));
}
@Action(CreateInstanceArtifactAction)
createArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: CreateInstanceArtifactAction) {
const state = getState();
return this.componentInstanceService.addInstanceArtifact(action.payload.componentType, action.payload.componentId, action.payload.instanceId, action.payload.artifact)
.pipe(tap((resp: ArtifactModel) => {
switch (resp.artifactGroupType) {
case ArtifactGroupType.DEPLOYMENT:
patchState({
deploymentArtifacts: [...state.deploymentArtifacts, resp]
});
break;
case ArtifactGroupType.INFORMATION:
patchState({
artifacts: [...state.artifacts, resp]
});
break;
}
}));
}
@Action(UpdateInstanceArtifactAction)
updateArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: UpdateInstanceArtifactAction) {
const state = getState();
return this.componentInstanceService.updateInstanceArtifact(action.payload.componentType, action.payload.componentId, action.payload.instanceId, action.payload.artifact)
.pipe(tap((resp: ArtifactModel) => {
switch (resp.artifactGroupType) {
case ArtifactGroupType.DEPLOYMENT:
// We cannot simply update the updated artifact state because updating a deployment ENV file may cause an update to his parent HEAT
// file.
// Just dispatch an action to refresh the deployment artifacts list
this.store.dispatch(new GetInstanceArtifactsByTypeAction(({
componentType: action.payload.componentType,
componentId: action.payload.componentId,
instanceId: action.payload.instanceId,
artifactType: ArtifactGroupType.DEPLOYMENT
})));
break;
case ArtifactGroupType.INFORMATION:
patchState({
artifacts: this.updateInstanceArtifactState(state.artifacts, action.payload.artifact, resp)
});
break;
}
}));
}
@Action(DeleteInstanceArtifactAction)
deleteInstanceArtifact({getState, patchState}: StateContext<ArtifactsStateModel>, action: DeleteInstanceArtifactAction) {
const state = getState();
return this.componentInstanceService.
deleteInstanceArtifact(action.payload.componentId, action.payload.componentType, action.payload.instanceId, action.payload.artifact.uniqueId, action.payload.artifact.artifactLabel)
.pipe(tap((resp: ArtifactModel) => {
switch (resp.artifactGroupType) {
case ArtifactGroupType.DEPLOYMENT:
patchState({
deploymentArtifacts: state.deploymentArtifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
});
break;
case ArtifactGroupType.INFORMATION:
patchState({
artifacts: state.artifacts.filter(({uniqueId}) => uniqueId !== action.payload.artifact.uniqueId)
});
break;
}
}));
}
private updateInstanceArtifactState = (artifactsState: ArtifactModel[], artifactToUpdate: ArtifactModel, updatedArtifact: ArtifactModel) => {
const artifactToUpdateIndex = _.findIndex(artifactsState, (artifact) => {
return artifact.uniqueId === artifactToUpdate.uniqueId;
});
const artifacts = Array.from(artifactsState);
artifacts[artifactToUpdateIndex] = updatedArtifact;
const ret = [...artifacts];
return ret;
}
}
|