52c5cdde3d63d8d9b878467387a832cf82588442
[sdc.git] /
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes } from './SoftwareProductComponentProcessesConstants.js';
19
20 function baseUrl(softwareProductId, version, componentId) {
21     const restPrefix = Configuration.get('restPrefix');
22     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${
23         version.id
24     }/components/${componentId}/processes`;
25 }
26
27 function fetchProcessesList({ softwareProductId, version, componentId }) {
28     return RestAPIUtil.fetch(
29         `${baseUrl(softwareProductId, version, componentId)}`
30     );
31 }
32
33 function deleteProcess({ softwareProductId, version, componentId, processId }) {
34     return RestAPIUtil.destroy(
35         `${baseUrl(softwareProductId, version, componentId)}/${processId}`
36     );
37 }
38
39 function putProcess({ softwareProductId, version, componentId, process }) {
40     return RestAPIUtil.put(
41         `${baseUrl(softwareProductId, version, componentId)}/${process.id}`,
42         {
43             name: process.name,
44             description: process.description,
45             type: process.type === '' ? null : process.type
46         }
47     );
48 }
49
50 function postProcess({ softwareProductId, version, componentId, process }) {
51     return RestAPIUtil.post(
52         `${baseUrl(softwareProductId, version, componentId)}`,
53         {
54             name: process.name,
55             description: process.description,
56             type: process.type === '' ? null : process.type
57         }
58     );
59 }
60
61 function uploadFileToProcess({
62     softwareProductId,
63     version,
64     processId,
65     componentId,
66     formData
67 }) {
68     return RestAPIUtil.post(
69         `${baseUrl(
70             softwareProductId,
71             version,
72             componentId
73         )}/${processId}/upload`,
74         formData
75     );
76 }
77
78 const SoftwareProductComponentProcessesActionHelper = {
79     fetchProcessesList(dispatch, { softwareProductId, version, componentId }) {
80         dispatch({
81             type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
82             processesList: []
83         });
84
85         return fetchProcessesList({
86             softwareProductId,
87             version,
88             componentId
89         }).then(response => {
90             dispatch({
91                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
92                 processesList: response.results
93             });
94         });
95     },
96
97     deleteProcess(
98         dispatch,
99         { process, softwareProductId, version, componentId }
100     ) {
101         return deleteProcess({
102             softwareProductId,
103             version,
104             processId: process.id,
105             componentId
106         }).then(() => {
107             dispatch({
108                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
109                 processId: process.id
110             });
111         });
112     },
113
114     saveProcess(
115         dispatch,
116         { softwareProductId, version, componentId, previousProcess, process }
117     ) {
118         if (previousProcess) {
119             return putProcess({
120                 softwareProductId,
121                 version,
122                 componentId,
123                 process
124             }).then(() => {
125                 if (
126                     process.formData &&
127                     process.formData.name !== previousProcess.artifactName
128                 ) {
129                     uploadFileToProcess({
130                         softwareProductId,
131                         version,
132                         processId: process.id,
133                         formData: process.formData,
134                         componentId
135                     });
136                 }
137                 dispatch({
138                     type: actionTypes.EDIT_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
139                     process
140                 });
141             });
142         } else {
143             return postProcess({
144                 softwareProductId,
145                 version,
146                 componentId,
147                 process
148             }).then(response => {
149                 if (process.formData) {
150                     uploadFileToProcess({
151                         softwareProductId,
152                         version,
153                         processId: response.value,
154                         formData: process.formData,
155                         componentId
156                     });
157                 }
158                 dispatch({
159                     type: actionTypes.ADD_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
160                     process: {
161                         ...process,
162                         id: response.value
163                     }
164                 });
165             });
166         }
167     },
168
169     hideDeleteConfirm(dispatch) {
170         dispatch({
171             type:
172                 actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
173             processToDelete: false
174         });
175     },
176
177     openDeleteProcessesConfirm(dispatch, { process }) {
178         dispatch({
179             type:
180                 actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
181             processToDelete: process
182         });
183     },
184
185     openEditor(dispatch, process = {}) {
186         dispatch({
187             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_OPEN,
188             process
189         });
190     },
191     closeEditor(dispatch) {
192         dispatch({
193             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_CLOSE
194         });
195     }
196 };
197
198 export default SoftwareProductComponentProcessesActionHelper;