blob: 53276ca49baf0af31cd0fcfb1ce8f5063b07a4dc [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';
Michael Landoefa037d2017-02-19 12:57:33 +020022
AviZi280f8012017-06-09 02:39:56 +030023function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024 const restPrefix = Configuration.get('restPrefix');
25 const { id: versionId } = version;
26 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-key-groups`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
29function fetchLicenseKeyGroupsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
AviZi280f8012017-06-09 02:39:56 +030033function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020034 return RestAPIUtil.destroy(
35 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`
36 );
Michael Landoefa037d2017-02-19 12:57:33 +020037}
38
AviZi280f8012017-06-09 02:39:56 +030039function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
41 name: licenseKeyGroup.name,
42 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020043 type: licenseKeyGroup.type,
44 increments: licenseKeyGroup.increments,
45 thresholdValue: licenseKeyGroup.thresholdValue,
46 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
47 startDate: licenseKeyGroup.startDate,
svishnev8f133302018-08-06 23:08:39 +030048 expiryDate: licenseKeyGroup.expiryDate,
49 manufacturerReferenceNumber: licenseKeyGroup.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 });
Michael Landoefa037d2017-02-19 12:57:33 +020051}
52
AviZi280f8012017-06-09 02:39:56 +030053function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 return RestAPIUtil.put(
55 `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
56 {
57 name: licenseKeyGroup.name,
58 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020059 type: licenseKeyGroup.type,
60 increments: licenseKeyGroup.increments,
61 thresholdValue: licenseKeyGroup.thresholdValue,
62 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
63 startDate: licenseKeyGroup.startDate,
svishnev8f133302018-08-06 23:08:39 +030064 expiryDate: licenseKeyGroup.expiryDate,
65 manufacturerReferenceNumber:
66 licenseKeyGroup.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067 }
68 );
Michael Landoefa037d2017-02-19 12:57:33 +020069}
70
Avi Ziv61070c92017-07-26 17:37:57 +030071function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 return RestAPIUtil.fetch(
73 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
74 );
Avi Ziv61070c92017-07-26 17:37:57 +030075}
76
77function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078 return RestAPIUtil.destroy(
79 `${baseUrl(
80 licenseModelId,
81 version
82 )}/${licenseKeyGroupId}/limits/${limitId}`
83 );
Avi Ziv61070c92017-07-26 17:37:57 +030084}
85
86function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020087 return RestAPIUtil.post(
88 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
89 {
90 name: limit.name,
91 type: limit.type,
92 description: limit.description,
93 metric: getStrValue(limit.metric),
94 value: limit.value,
95 unit: getStrValue(limit.unit),
96 aggregationFunction: getValue(limit.aggregationFunction),
97 time: getValue(limit.time)
98 }
99 );
Avi Ziv61070c92017-07-26 17:37:57 +0300100}
101
102function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200103 return RestAPIUtil.put(
104 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
105 limit.id
106 }`,
107 {
108 name: limit.name,
109 type: limit.type,
110 description: limit.description,
111 metric: getStrValue(limit.metric),
112 value: limit.value,
113 unit: getStrValue(limit.unit),
114 aggregationFunction: getValue(limit.aggregationFunction),
115 time: getValue(limit.time)
116 }
117 );
Avi Ziv61070c92017-07-26 17:37:57 +0300118}
Michael Landoefa037d2017-02-19 12:57:33 +0200119
120export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200121 fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
122 return fetchLicenseKeyGroupsList(licenseModelId, version).then(
123 response =>
124 dispatch({
125 type:
126 licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
127 response
128 })
129 );
130 },
Michael Landoefa037d2017-02-19 12:57:33 +0200131
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200132 openLicenseKeyGroupsEditor(
133 dispatch,
134 { licenseKeyGroup, licenseModelId, version } = {}
135 ) {
136 if (licenseModelId && version) {
137 this.fetchLimits(dispatch, {
138 licenseModelId,
139 version,
140 licenseKeyGroup
141 });
142 }
143 dispatch({
144 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
145 licenseKeyGroup
146 });
147 },
Michael Landoefa037d2017-02-19 12:57:33 +0200148
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 closeLicenseKeyGroupEditor(dispatch) {
150 dispatch({
151 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
152 });
153 },
Michael Landoefa037d2017-02-19 12:57:33 +0200154
svishnev8f133302018-08-06 23:08:39 +0300155 async saveLicenseKeyGroup(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200156 dispatch,
157 { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
158 ) {
159 if (previousLicenseKeyGroup) {
svishnev8f133302018-08-06 23:08:39 +0300160 await putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200161 } else {
svishnev8f133302018-08-06 23:08:39 +0300162 await postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 }
svishnev8f133302018-08-06 23:08:39 +0300164 await ItemsHelper.checkItemStatus(dispatch, {
165 itemId: licenseModelId,
166 versionId: version.id
167 });
168 await this.fetchLicenseKeyGroupsList(dispatch, {
169 licenseModelId,
170 version
171 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200172 },
Michael Landoefa037d2017-02-19 12:57:33 +0200173
svishnev8f133302018-08-06 23:08:39 +0300174 async deleteLicenseKeyGroup(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200175 dispatch,
176 { licenseModelId, licenseKeyGroupId, version }
177 ) {
svishnev8f133302018-08-06 23:08:39 +0300178 await deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version);
179 await ItemsHelper.checkItemStatus(dispatch, {
180 itemId: licenseModelId,
181 versionId: version.id
182 });
183 await this.fetchLicenseKeyGroupsList(dispatch, {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200184 licenseModelId,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200185 version
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200186 });
187 },
Michael Landoefa037d2017-02-19 12:57:33 +0200188
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200189 hideDeleteConfirm(dispatch) {
190 dispatch({
191 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
192 licenseKeyGroupToDelete: false
193 });
194 },
Michael Landoefa037d2017-02-19 12:57:33 +0200195
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200196 openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
197 dispatch({
198 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
199 licenseKeyGroupToDelete: licenseKeyGroup
200 });
201 },
Michael Landoefa037d2017-02-19 12:57:33 +0200202
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200203 fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
204 return fetchLimitsList(
205 licenseModelId,
206 licenseKeyGroup.id,
207 version
208 ).then(response => {
209 dispatch({
210 type:
211 licenseKeyGroupsConstants.licenseKeyGroupsEditor
212 .LIMITS_LIST_LOADED,
213 response
214 });
215 });
216 },
Michael Landoefa037d2017-02-19 12:57:33 +0200217
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200218 submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
219 const promise = limit.id
220 ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
221 : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
222 return promise.then(() => {
223 dispatch({
224 type: limitEditorActions.CLOSE
225 });
226 this.fetchLimits(dispatch, {
227 licenseModelId,
228 version,
229 licenseKeyGroup
230 });
231 return ItemsHelper.checkItemStatus(dispatch, {
232 itemId: licenseModelId,
233 versionId: version.id
234 });
235 });
236 },
Michael Landoefa037d2017-02-19 12:57:33 +0200237
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200238 deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
239 return deleteLimit(
240 licenseModelId,
241 licenseKeyGroup.id,
242 version,
243 limit.id
244 ).then(() => {
245 this.fetchLimits(dispatch, {
246 licenseModelId,
247 version,
248 licenseKeyGroup
249 });
250 return ItemsHelper.checkItemStatus(dispatch, {
251 itemId: licenseModelId,
252 versionId: version.id
253 });
254 });
255 }
Michael Landoefa037d2017-02-19 12:57:33 +0200256};