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