AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 2 | * Copyright © 2016-2018 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 16 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 18 | import Configuration from 'sdc-app/config/Configuration.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 19 | import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js'; |
| 20 | import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
| 21 | import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 22 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 23 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 24 | import { |
| 25 | actionTypes as modalActionTypes, |
| 26 | modalSizes |
| 27 | } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 28 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 29 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 30 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | const restPrefix = Configuration.get('restPrefix'); |
| 32 | const { id: versionId } = version; |
| 33 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | } |
| 35 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 36 | function fetchEntitlementPoolsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 37 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 38 | } |
| 39 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 40 | function postEntitlementPool(licenseModelId, entitlementPool, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 41 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 42 | name: entitlementPool.name, |
| 43 | description: entitlementPool.description, |
Ben David, Elad (eb7504) | ec7da6a | 2020-03-17 18:12:17 +0200 | [diff] [blame^] | 44 | type: entitlementPool.type, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 45 | thresholdValue: entitlementPool.thresholdValue, |
| 46 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 47 | increments: entitlementPool.increments, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | time: entitlementPool.time, |
| 49 | startDate: entitlementPool.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 50 | expiryDate: entitlementPool.expiryDate, |
| 51 | manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 52 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 55 | function putEntitlementPool( |
| 56 | licenseModelId, |
| 57 | previousEntitlementPool, |
| 58 | entitlementPool, |
| 59 | version |
| 60 | ) { |
| 61 | return RestAPIUtil.put( |
| 62 | `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, |
| 63 | { |
| 64 | name: entitlementPool.name, |
| 65 | description: entitlementPool.description, |
Ben David, Elad (eb7504) | ec7da6a | 2020-03-17 18:12:17 +0200 | [diff] [blame^] | 66 | type: entitlementPool.type, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 67 | thresholdValue: entitlementPool.thresholdValue, |
| 68 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 69 | increments: entitlementPool.increments, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | time: entitlementPool.time, |
| 71 | startDate: entitlementPool.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 72 | expiryDate: entitlementPool.expiryDate, |
| 73 | manufacturerReferenceNumber: |
| 74 | entitlementPool.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 75 | } |
| 76 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 77 | } |
| 78 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 79 | function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 80 | return RestAPIUtil.destroy( |
| 81 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}` |
| 82 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 83 | } |
| 84 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 85 | function fetchLimitsList(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 86 | return RestAPIUtil.fetch( |
| 87 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits` |
| 88 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 89 | } |
| 90 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 91 | function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 92 | return RestAPIUtil.destroy( |
| 93 | `${baseUrl( |
| 94 | licenseModelId, |
| 95 | version |
| 96 | )}/${entitlementPoolId}/limits/${limitId}` |
| 97 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 98 | } |
| 99 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 100 | function postLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 101 | return RestAPIUtil.post( |
| 102 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, |
| 103 | { |
| 104 | name: limit.name, |
| 105 | type: limit.type, |
| 106 | description: limit.description, |
| 107 | metric: getStrValue(limit.metric), |
| 108 | value: limit.value, |
| 109 | unit: getStrValue(limit.unit), |
| 110 | aggregationFunction: getValue(limit.aggregationFunction), |
| 111 | time: getValue(limit.time) |
| 112 | } |
| 113 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | function putLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 117 | return RestAPIUtil.put( |
| 118 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${ |
| 119 | limit.id |
| 120 | }`, |
| 121 | { |
| 122 | name: limit.name, |
| 123 | type: limit.type, |
| 124 | description: limit.description, |
| 125 | metric: getStrValue(limit.metric), |
| 126 | value: limit.value, |
| 127 | unit: getStrValue(limit.unit), |
| 128 | aggregationFunction: getValue(limit.aggregationFunction), |
| 129 | time: getValue(limit.time) |
| 130 | } |
| 131 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 132 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 133 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 134 | const EntitlementPoolsActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 135 | fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) { |
| 136 | return fetchEntitlementPoolsList(licenseModelId, version).then( |
| 137 | response => |
| 138 | dispatch({ |
| 139 | type: |
| 140 | entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED, |
| 141 | response |
| 142 | }) |
| 143 | ); |
| 144 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 145 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 146 | openEntitlementPoolsEditor( |
| 147 | dispatch, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 148 | { entitlementPool, licenseModelId, version, isReadOnlyMode } = {} |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 149 | ) { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 150 | if (licenseModelId && version && entitlementPool) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 151 | this.fetchLimits(dispatch, { |
| 152 | licenseModelId, |
| 153 | version, |
| 154 | entitlementPool |
| 155 | }); |
| 156 | } |
| 157 | dispatch({ |
| 158 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN, |
| 159 | entitlementPool |
| 160 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 161 | dispatch({ |
| 162 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 163 | data: { |
| 164 | modalComponentName: modalContentMapper.EP_EDITOR, |
| 165 | modalComponentProps: { |
| 166 | version, |
| 167 | licenseModelId, |
| 168 | isReadOnlyMode, |
| 169 | size: modalSizes.LARGE |
| 170 | }, |
| 171 | title: |
| 172 | licenseModelId && version && entitlementPool |
| 173 | ? i18n('Edit Entitlement Pool') |
| 174 | : i18n('Create New Entitlement Pool') |
| 175 | } |
| 176 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 177 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 178 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 179 | async deleteEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 180 | dispatch, |
| 181 | { licenseModelId, entitlementPoolId, version } |
| 182 | ) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 183 | await deleteEntitlementPool(licenseModelId, entitlementPoolId, version); |
| 184 | |
| 185 | await ItemsHelper.checkItemStatus(dispatch, { |
| 186 | itemId: licenseModelId, |
| 187 | versionId: version.id |
| 188 | }); |
| 189 | |
| 190 | await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 191 | licenseModelId, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 192 | version |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 193 | }); |
| 194 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 195 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 196 | entitlementPoolsEditorDataChanged(dispatch, { deltaData }) { |
| 197 | dispatch({ |
| 198 | type: |
| 199 | entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED, |
| 200 | deltaData |
| 201 | }); |
| 202 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 203 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 204 | closeEntitlementPoolsEditor(dispatch) { |
| 205 | dispatch({ |
| 206 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE |
| 207 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 208 | dispatch({ |
| 209 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 210 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 211 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 212 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 213 | async saveEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 214 | dispatch, |
| 215 | { licenseModelId, previousEntitlementPool, entitlementPool, version } |
| 216 | ) { |
| 217 | if (previousEntitlementPool) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 218 | await putEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 219 | licenseModelId, |
| 220 | previousEntitlementPool, |
| 221 | entitlementPool, |
| 222 | version |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 223 | ); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 224 | } else { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 225 | await postEntitlementPool(licenseModelId, entitlementPool, version); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 226 | } |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 227 | await ItemsHelper.checkItemStatus(dispatch, { |
| 228 | itemId: licenseModelId, |
| 229 | versionId: version.id |
| 230 | }); |
| 231 | await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
| 232 | licenseModelId, |
| 233 | version |
| 234 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 235 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 236 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 237 | hideDeleteConfirm(dispatch) { |
| 238 | dispatch({ |
| 239 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 240 | entitlementPoolToDelete: false |
| 241 | }); |
| 242 | }, |
| 243 | openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) { |
| 244 | dispatch({ |
| 245 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 246 | entitlementPoolToDelete: entitlementPool |
| 247 | }); |
| 248 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 249 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 250 | fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) { |
| 251 | return fetchLimitsList( |
| 252 | licenseModelId, |
| 253 | entitlementPool.id, |
| 254 | version |
| 255 | ).then(response => { |
| 256 | dispatch({ |
| 257 | type: |
| 258 | entitlementPoolsActionTypes.entitlementPoolsEditor |
| 259 | .LIMITS_LIST_LOADED, |
| 260 | response |
| 261 | }); |
| 262 | }); |
| 263 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 264 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 265 | submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 266 | const propmise = limit.id |
| 267 | ? putLimit(licenseModelId, entitlementPool.id, version, limit) |
| 268 | : postLimit(licenseModelId, entitlementPool.id, version, limit); |
| 269 | return propmise.then(() => { |
| 270 | dispatch({ |
| 271 | type: limitEditorActions.CLOSE |
| 272 | }); |
| 273 | this.fetchLimits(dispatch, { |
| 274 | licenseModelId, |
| 275 | version, |
| 276 | entitlementPool |
| 277 | }); |
| 278 | return ItemsHelper.checkItemStatus(dispatch, { |
| 279 | itemId: licenseModelId, |
| 280 | versionId: version.id |
| 281 | }); |
| 282 | }); |
| 283 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 284 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 285 | deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 286 | return deleteLimit( |
| 287 | licenseModelId, |
| 288 | entitlementPool.id, |
| 289 | version, |
| 290 | limit.id |
| 291 | ).then(() => { |
| 292 | this.fetchLimits(dispatch, { |
| 293 | licenseModelId, |
| 294 | version, |
| 295 | entitlementPool |
| 296 | }); |
| 297 | return ItemsHelper.checkItemStatus(dispatch, { |
| 298 | itemId: licenseModelId, |
| 299 | versionId: version.id |
| 300 | }); |
| 301 | }); |
| 302 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 303 | }; |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 304 | |
| 305 | export default EntitlementPoolsActionHelper; |