blob: c9f0505ad1cede189267f01835beb3c8851705ba [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
svishnev8f133302018-08-06 23:08:39 +03002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.js';
19import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
20import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
talig8e9c0652017-12-20 14:30:43 +020021import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030022import i18n from 'nfvo-utils/i18n/i18n.js';
23import {
24 actionTypes as modalActionTypes,
25 modalSizes
26} from 'nfvo-components/modal/GlobalModalConstants.js';
27import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020028
AviZi280f8012017-06-09 02:39:56 +030029function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 const restPrefix = Configuration.get('restPrefix');
31 const { id: versionId } = version;
32 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-key-groups`;
Michael Landoefa037d2017-02-19 12:57:33 +020033}
34
35function fetchLicenseKeyGroupsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020037}
38
AviZi280f8012017-06-09 02:39:56 +030039function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 return RestAPIUtil.destroy(
41 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`
42 );
Michael Landoefa037d2017-02-19 12:57:33 +020043}
44
AviZi280f8012017-06-09 02:39:56 +030045function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020046 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
47 name: licenseKeyGroup.name,
48 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 type: licenseKeyGroup.type,
50 increments: licenseKeyGroup.increments,
51 thresholdValue: licenseKeyGroup.thresholdValue,
52 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
53 startDate: licenseKeyGroup.startDate,
svishnev8f133302018-08-06 23:08:39 +030054 expiryDate: licenseKeyGroup.expiryDate,
55 manufacturerReferenceNumber: licenseKeyGroup.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020056 });
Michael Landoefa037d2017-02-19 12:57:33 +020057}
58
AviZi280f8012017-06-09 02:39:56 +030059function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020060 return RestAPIUtil.put(
61 `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
62 {
63 name: licenseKeyGroup.name,
64 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 type: licenseKeyGroup.type,
66 increments: licenseKeyGroup.increments,
67 thresholdValue: licenseKeyGroup.thresholdValue,
68 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
69 startDate: licenseKeyGroup.startDate,
svishnev8f133302018-08-06 23:08:39 +030070 expiryDate: licenseKeyGroup.expiryDate,
71 manufacturerReferenceNumber:
72 licenseKeyGroup.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020073 }
74 );
Michael Landoefa037d2017-02-19 12:57:33 +020075}
76
Avi Ziv61070c92017-07-26 17:37:57 +030077function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078 return RestAPIUtil.fetch(
79 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
80 );
Avi Ziv61070c92017-07-26 17:37:57 +030081}
82
83function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 return RestAPIUtil.destroy(
85 `${baseUrl(
86 licenseModelId,
87 version
88 )}/${licenseKeyGroupId}/limits/${limitId}`
89 );
Avi Ziv61070c92017-07-26 17:37:57 +030090}
91
92function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020093 return RestAPIUtil.post(
94 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
95 {
96 name: limit.name,
97 type: limit.type,
98 description: limit.description,
99 metric: getStrValue(limit.metric),
100 value: limit.value,
101 unit: getStrValue(limit.unit),
102 aggregationFunction: getValue(limit.aggregationFunction),
103 time: getValue(limit.time)
104 }
105 );
Avi Ziv61070c92017-07-26 17:37:57 +0300106}
107
108function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200109 return RestAPIUtil.put(
110 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
111 limit.id
112 }`,
113 {
114 name: limit.name,
115 type: limit.type,
116 description: limit.description,
117 metric: getStrValue(limit.metric),
118 value: limit.value,
119 unit: getStrValue(limit.unit),
120 aggregationFunction: getValue(limit.aggregationFunction),
121 time: getValue(limit.time)
122 }
123 );
Avi Ziv61070c92017-07-26 17:37:57 +0300124}
Michael Landoefa037d2017-02-19 12:57:33 +0200125
126export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200127 fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
128 return fetchLicenseKeyGroupsList(licenseModelId, version).then(
129 response =>
130 dispatch({
131 type:
132 licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
133 response
134 })
135 );
136 },
Michael Landoefa037d2017-02-19 12:57:33 +0200137
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 openLicenseKeyGroupsEditor(
139 dispatch,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300140 { licenseKeyGroup, licenseModelId, version, isReadOnlyMode } = {}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200141 ) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300142 if (licenseModelId && version && licenseKeyGroup) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200143 this.fetchLimits(dispatch, {
144 licenseModelId,
145 version,
146 licenseKeyGroup
147 });
148 }
149 dispatch({
150 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
151 licenseKeyGroup
152 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300153 dispatch({
154 type: modalActionTypes.GLOBAL_MODAL_SHOW,
155 data: {
156 modalComponentName: modalContentMapper.LKG_EDITOR,
157 modalComponentProps: {
158 version,
159 licenseModelId,
160 isReadOnlyMode,
161 size: modalSizes.LARGE
162 },
163 title:
164 licenseModelId && version && licenseKeyGroup
165 ? i18n('Edit License Key Group')
166 : i18n('Create New License Key Group')
167 }
168 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200169 },
Michael Landoefa037d2017-02-19 12:57:33 +0200170
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200171 closeLicenseKeyGroupEditor(dispatch) {
172 dispatch({
173 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
174 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300175 dispatch({
176 type: modalActionTypes.GLOBAL_MODAL_CLOSE
177 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200178 },
Michael Landoefa037d2017-02-19 12:57:33 +0200179
svishnev8f133302018-08-06 23:08:39 +0300180 async saveLicenseKeyGroup(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200181 dispatch,
182 { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
183 ) {
184 if (previousLicenseKeyGroup) {
svishnev8f133302018-08-06 23:08:39 +0300185 await putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200186 } else {
svishnev8f133302018-08-06 23:08:39 +0300187 await postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200188 }
svishnev8f133302018-08-06 23:08:39 +0300189 await ItemsHelper.checkItemStatus(dispatch, {
190 itemId: licenseModelId,
191 versionId: version.id
192 });
193 await this.fetchLicenseKeyGroupsList(dispatch, {
194 licenseModelId,
195 version
196 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200197 },
Michael Landoefa037d2017-02-19 12:57:33 +0200198
svishnev8f133302018-08-06 23:08:39 +0300199 async deleteLicenseKeyGroup(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200200 dispatch,
201 { licenseModelId, licenseKeyGroupId, version }
202 ) {
svishnev8f133302018-08-06 23:08:39 +0300203 await deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version);
204 await ItemsHelper.checkItemStatus(dispatch, {
205 itemId: licenseModelId,
206 versionId: version.id
207 });
208 await this.fetchLicenseKeyGroupsList(dispatch, {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200209 licenseModelId,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200210 version
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200211 });
212 },
Michael Landoefa037d2017-02-19 12:57:33 +0200213
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200214 hideDeleteConfirm(dispatch) {
215 dispatch({
216 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
217 licenseKeyGroupToDelete: false
218 });
219 },
Michael Landoefa037d2017-02-19 12:57:33 +0200220
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200221 openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
222 dispatch({
223 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
224 licenseKeyGroupToDelete: licenseKeyGroup
225 });
226 },
Michael Landoefa037d2017-02-19 12:57:33 +0200227
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200228 fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
229 return fetchLimitsList(
230 licenseModelId,
231 licenseKeyGroup.id,
232 version
233 ).then(response => {
234 dispatch({
235 type:
236 licenseKeyGroupsConstants.licenseKeyGroupsEditor
237 .LIMITS_LIST_LOADED,
238 response
239 });
240 });
241 },
Michael Landoefa037d2017-02-19 12:57:33 +0200242
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200243 submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
244 const promise = limit.id
245 ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
246 : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
247 return promise.then(() => {
248 dispatch({
249 type: limitEditorActions.CLOSE
250 });
251 this.fetchLimits(dispatch, {
252 licenseModelId,
253 version,
254 licenseKeyGroup
255 });
256 return ItemsHelper.checkItemStatus(dispatch, {
257 itemId: licenseModelId,
258 versionId: version.id
259 });
260 });
261 },
Michael Landoefa037d2017-02-19 12:57:33 +0200262
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200263 deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
264 return deleteLimit(
265 licenseModelId,
266 licenseKeyGroup.id,
267 version,
268 limit.id
269 ).then(() => {
270 this.fetchLimits(dispatch, {
271 licenseModelId,
272 version,
273 licenseKeyGroup
274 });
275 return ItemsHelper.checkItemStatus(dispatch, {
276 itemId: licenseModelId,
277 versionId: version.id
278 });
279 });
280 }
Michael Landoefa037d2017-02-19 12:57:33 +0200281};