react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / deployment / SoftwareProductDeploymentActionHelper.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import { actionTypes } from './SoftwareProductDeploymentConstants.js';
17 import { actionTypes as GlobalModalActions } from 'nfvo-components/modal/GlobalModalConstants.js';
18 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
19 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
20 import Configuration from 'sdc-app/config/Configuration.js';
21 import pickBy from 'lodash/pickBy';
22
23 function baseUrl(vspId, version) {
24     const versionId = version.id;
25     const restPrefix = Configuration.get('restPrefix');
26     return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${versionId}/deployment-flavors`;
27 }
28
29 function fetchDeploymentFlavorsList({ softwareProductId, version }) {
30     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
31 }
32
33 function fetchDeploymentFlavor({
34     softwareProductId,
35     deploymentFlavorId,
36     version
37 }) {
38     return RestAPIUtil.fetch(
39         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`
40     );
41 }
42
43 function deleteDeploymentFlavor({
44     softwareProductId,
45     deploymentFlavorId,
46     version
47 }) {
48     return RestAPIUtil.destroy(
49         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`
50     );
51 }
52
53 function createDeploymentFlavor({ softwareProductId, data, version }) {
54     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, data);
55 }
56
57 function editDeploymentFlavor({
58     softwareProductId,
59     deploymentFlavorId,
60     data,
61     version
62 }) {
63     return RestAPIUtil.put(
64         `${baseUrl(softwareProductId, version)}/${deploymentFlavorId}`,
65         data
66     );
67 }
68
69 const SoftwareProductDeploymentActionHelper = {
70     fetchDeploymentFlavorsList(dispatch, { softwareProductId, version }) {
71         return fetchDeploymentFlavorsList({ softwareProductId, version }).then(
72             response => {
73                 dispatch({
74                     type: actionTypes.FETCH_SOFTWARE_PRODUCT_DEPLOYMENT_FLAVORS,
75                     deploymentFlavors: response.results
76                 });
77             }
78         );
79     },
80
81     fetchDeploymentFlavor({ softwareProductId, deploymentFlavorId, version }) {
82         return fetchDeploymentFlavor({
83             softwareProductId,
84             deploymentFlavorId,
85             version
86         });
87     },
88
89     deleteDeploymentFlavor(
90         dispatch,
91         { softwareProductId, deploymentFlavorId, version }
92     ) {
93         return deleteDeploymentFlavor({
94             softwareProductId,
95             deploymentFlavorId,
96             version
97         }).then(() => {
98             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
99                 dispatch,
100                 { softwareProductId, version }
101             );
102         });
103     },
104
105     createDeploymentFlavor(dispatch, { softwareProductId, data, version }) {
106         return createDeploymentFlavor({
107             softwareProductId,
108             data,
109             version
110         }).then(() => {
111             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
112                 dispatch,
113                 { softwareProductId, version }
114             );
115         });
116     },
117
118     editDeploymentFlavor(
119         dispatch,
120         { softwareProductId, deploymentFlavorId, data, version }
121     ) {
122         let dataWithoutId = pickBy(data, (val, key) => key !== 'id');
123         return editDeploymentFlavor({
124             softwareProductId,
125             deploymentFlavorId,
126             data: dataWithoutId,
127             version
128         }).then(() => {
129             return SoftwareProductDeploymentActionHelper.fetchDeploymentFlavorsList(
130                 dispatch,
131                 { softwareProductId, version }
132             );
133         });
134     },
135
136     closeDeploymentFlavorEditor(dispatch) {
137         dispatch({
138             type:
139                 actionTypes.deploymentFlavorEditor
140                     .SOFTWARE_PRODUCT_DEPLOYMENT_CLEAR_DATA
141         });
142         dispatch({
143             type: GlobalModalActions.GLOBAL_MODAL_CLOSE
144         });
145     },
146
147     openDeploymentFlavorEditor(
148         dispatch,
149         {
150             softwareProductId,
151             modalClassName,
152             deploymentFlavor = {},
153             componentsList,
154             isEdit = false,
155             version
156         }
157     ) {
158         let alteredDeploymentFlavor = { ...deploymentFlavor };
159         if (componentsList.length) {
160             alteredDeploymentFlavor = {
161                 ...alteredDeploymentFlavor,
162                 componentComputeAssociations: deploymentFlavor.componentComputeAssociations
163                     ? [
164                           {
165                               ...deploymentFlavor
166                                   .componentComputeAssociations[0],
167                               componentId: componentsList[0].id
168                           }
169                       ]
170                     : [
171                           {
172                               componentId: componentsList[0].id,
173                               computeFlavorId: null
174                           }
175                       ]
176             };
177         }
178         dispatch({
179             type:
180                 actionTypes.deploymentFlavorEditor
181                     .SOFTWARE_PRODUCT_DEPLOYMENT_FILL_DATA,
182             deploymentFlavor: alteredDeploymentFlavor
183         });
184         dispatch({
185             type: GlobalModalActions.GLOBAL_MODAL_SHOW,
186             data: {
187                 modalComponentName: modalContentMapper.DEPLOYMENT_FLAVOR_EDITOR,
188                 modalComponentProps: { softwareProductId, version },
189                 bodyClassName: modalClassName,
190                 title: isEdit
191                     ? 'Edit Deployment Flavor'
192                     : 'Create a New Deployment Flavor'
193             }
194         });
195     }
196 };
197
198 export default SoftwareProductDeploymentActionHelper;