44b25311e018548902983b1c833234a55764b41e
[sdc.git] /
1 import { actionTypes } from './SoftwareProductDeploymentConstants.js';
2 import { actionTypes as GlobalModalActions } from 'nfvo-components/modal/GlobalModalConstants.js';
3 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
4 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
5 import Configuration from 'sdc-app/config/Configuration.js';
6 import pickBy from 'lodash/pickBy';
7
8 function baseUrl(vspId, version) {
9     const versionId = version.id;
10     const restPrefix = Configuration.get('restPrefix');
11     return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${versionId}/deployment-flavors`;
12 }
13
14 function fetchDeploymentFlavorsList({ softwareProductId, version }) {
15     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
16 }
17
18 function fetchDeploymentFlavor({
19     softwareProductId,
20     deploymentFlavorId,
21     version
22 }) {
23     return RestAPIUtil.fetch(
24         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`
25     );
26 }
27
28 function deleteDeploymentFlavor({
29     softwareProductId,
30     deploymentFlavorId,
31     version
32 }) {
33     return RestAPIUtil.destroy(
34         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`
35     );
36 }
37
38 function createDeploymentFlavor({ softwareProductId, data, version }) {
39     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, data);
40 }
41
42 function editDeploymentFlavor({
43     softwareProductId,
44     deploymentFlavorId,
45     data,
46     version
47 }) {
48     return RestAPIUtil.put(
49         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`,
50         data
51     );
52 }
53
54 const SoftwareProductDeploymentActionHelper = {
55     fetchDeploymentFlavorsList(dispatch, { softwareProductId, version }) {
56         return fetchDeploymentFlavorsList({ softwareProductId, version }).then(
57             response => {
58                 dispatch({
59                     type: actionTypes.FETCH_SOFTWARE_PRODUCT_DEPLOYMENT_FLAVORS,
60                     deploymentFlavors: response.results
61                 });
62             }
63         );
64     },
65
66     fetchDeploymentFlavor({ softwareProductId, deploymentFlavorId, version }) {
67         return fetchDeploymentFlavor({
68             softwareProductId,
69             deploymentFlavorId,
70             version
71         });
72     },
73
74     deleteDeploymentFlavor(
75         dispatch,
76         { softwareProductId, deploymentFlavorId, version }
77     ) {
78         return deleteDeploymentFlavor({
79             softwareProductId,
80             deploymentFlavorId,
81             version
82         }).then(() => {
83             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
84                 dispatch,
85                 { softwareProductId, version }
86             );
87         });
88     },
89
90     createDeploymentFlavor(dispatch, { softwareProductId, data, version }) {
91         return createDeploymentFlavor({
92             softwareProductId,
93             data,
94             version
95         }).then(() => {
96             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
97                 dispatch,
98                 { softwareProductId, version }
99             );
100         });
101     },
102
103     editDeploymentFlavor(
104         dispatch,
105         { softwareProductId, deploymentFlavorId, data, version }
106     ) {
107         let dataWithoutId = pickBy(data, (val, key) => key !== 'id');
108         return editDeploymentFlavor({
109             softwareProductId,
110             deploymentFlavorId,
111             data: dataWithoutId,
112             version
113         }).then(() => {
114             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
115                 dispatch,
116                 { softwareProductId, version }
117             );
118         });
119     },
120
121     closeDeploymentFlavorEditor(dispatch) {
122         dispatch({
123             type:
124                 actionTypes.deploymentFlavorEditor
125                     .SOFTWARE_PRODUCT_DEPLOYMENT_CLEAR_DATA
126         });
127         dispatch({
128             type: GlobalModalActions.GLOBAL_MODAL_CLOSE
129         });
130     },
131
132     openDeploymentFlavorEditor(
133         dispatch,
134         {
135             softwareProductId,
136             modalClassName,
137             deploymentFlavor = {},
138             componentsList,
139             isEdit = false,
140             version
141         }
142     ) {
143         let alteredDeploymentFlavor = { ...deploymentFlavor };
144         if (componentsList.length) {
145             alteredDeploymentFlavor = {
146                 ...alteredDeploymentFlavor,
147                 componentComputeAssociations: deploymentFlavor.componentComputeAssociations
148                     ? [
149                           {
150                               ...deploymentFlavor
151                                   .componentComputeAssociations[0],
152                               componentId: componentsList[0].id
153                           }
154                       ]
155                     : [
156                           {
157                               componentId: componentsList[0].id,
158                               computeFlavorId: null
159                           }
160                       ]
161             };
162         }
163         dispatch({
164             type:
165                 actionTypes.deploymentFlavorEditor
166                     .SOFTWARE_PRODUCT_DEPLOYMENT_FILL_DATA,
167             deploymentFlavor: alteredDeploymentFlavor
168         });
169         dispatch({
170             type: GlobalModalActions.GLOBAL_MODAL_SHOW,
171             data: {
172                 modalComponentName: modalContentMapper.DEPLOYMENT_FLAVOR_EDITOR,
173                 modalComponentProps: { softwareProductId, version },
174                 modalClassName,
175                 title: isEdit
176                     ? 'Edit Deployment Flavor'
177                     : 'Create a New Deployment Flavor'
178             }
179         });
180     }
181 };
182
183 export default SoftwareProductDeploymentActionHelper;