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'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 24 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | const restPrefix = Configuration.get('restPrefix'); |
| 26 | const { id: versionId } = version; |
| 27 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 28 | } |
| 29 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 30 | function fetchEntitlementPoolsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 32 | } |
| 33 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 34 | function postEntitlementPool(licenseModelId, entitlementPool, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 36 | name: entitlementPool.name, |
| 37 | description: entitlementPool.description, |
| 38 | thresholdValue: entitlementPool.thresholdValue, |
| 39 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 40 | increments: entitlementPool.increments, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 41 | time: entitlementPool.time, |
| 42 | startDate: entitlementPool.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 43 | expiryDate: entitlementPool.expiryDate, |
| 44 | manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 45 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | function putEntitlementPool( |
| 49 | licenseModelId, |
| 50 | previousEntitlementPool, |
| 51 | entitlementPool, |
| 52 | version |
| 53 | ) { |
| 54 | return RestAPIUtil.put( |
| 55 | `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, |
| 56 | { |
| 57 | name: entitlementPool.name, |
| 58 | description: entitlementPool.description, |
| 59 | thresholdValue: entitlementPool.thresholdValue, |
| 60 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 61 | increments: entitlementPool.increments, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 62 | time: entitlementPool.time, |
| 63 | startDate: entitlementPool.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 64 | expiryDate: entitlementPool.expiryDate, |
| 65 | manufacturerReferenceNumber: |
| 66 | entitlementPool.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 67 | } |
| 68 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 69 | } |
| 70 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 71 | function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 72 | return RestAPIUtil.destroy( |
| 73 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}` |
| 74 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 75 | } |
| 76 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 77 | function fetchLimitsList(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 78 | return RestAPIUtil.fetch( |
| 79 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits` |
| 80 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 81 | } |
| 82 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 83 | function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | return RestAPIUtil.destroy( |
| 85 | `${baseUrl( |
| 86 | licenseModelId, |
| 87 | version |
| 88 | )}/${entitlementPoolId}/limits/${limitId}` |
| 89 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 90 | } |
| 91 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 92 | function postLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 93 | return RestAPIUtil.post( |
| 94 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/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 Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | function putLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 109 | return RestAPIUtil.put( |
| 110 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/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 Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 124 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 125 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 126 | const EntitlementPoolsActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 127 | fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) { |
| 128 | return fetchEntitlementPoolsList(licenseModelId, version).then( |
| 129 | response => |
| 130 | dispatch({ |
| 131 | type: |
| 132 | entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED, |
| 133 | response |
| 134 | }) |
| 135 | ); |
| 136 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 137 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 138 | openEntitlementPoolsEditor( |
| 139 | dispatch, |
| 140 | { entitlementPool, licenseModelId, version } = {} |
| 141 | ) { |
| 142 | if (licenseModelId && version) { |
| 143 | this.fetchLimits(dispatch, { |
| 144 | licenseModelId, |
| 145 | version, |
| 146 | entitlementPool |
| 147 | }); |
| 148 | } |
| 149 | dispatch({ |
| 150 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN, |
| 151 | entitlementPool |
| 152 | }); |
| 153 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 154 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 155 | async deleteEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 156 | dispatch, |
| 157 | { licenseModelId, entitlementPoolId, version } |
| 158 | ) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 159 | await deleteEntitlementPool(licenseModelId, entitlementPoolId, version); |
| 160 | |
| 161 | await ItemsHelper.checkItemStatus(dispatch, { |
| 162 | itemId: licenseModelId, |
| 163 | versionId: version.id |
| 164 | }); |
| 165 | |
| 166 | await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 167 | licenseModelId, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 168 | version |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 169 | }); |
| 170 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 171 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 172 | entitlementPoolsEditorDataChanged(dispatch, { deltaData }) { |
| 173 | dispatch({ |
| 174 | type: |
| 175 | entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED, |
| 176 | deltaData |
| 177 | }); |
| 178 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 179 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 180 | closeEntitlementPoolsEditor(dispatch) { |
| 181 | dispatch({ |
| 182 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE |
| 183 | }); |
| 184 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 185 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 186 | async saveEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 187 | dispatch, |
| 188 | { licenseModelId, previousEntitlementPool, entitlementPool, version } |
| 189 | ) { |
| 190 | if (previousEntitlementPool) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 191 | await putEntitlementPool( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 192 | licenseModelId, |
| 193 | previousEntitlementPool, |
| 194 | entitlementPool, |
| 195 | version |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 196 | ); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 197 | } else { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 198 | await postEntitlementPool(licenseModelId, entitlementPool, version); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 199 | } |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 200 | await ItemsHelper.checkItemStatus(dispatch, { |
| 201 | itemId: licenseModelId, |
| 202 | versionId: version.id |
| 203 | }); |
| 204 | await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
| 205 | licenseModelId, |
| 206 | version |
| 207 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 208 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 209 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 210 | hideDeleteConfirm(dispatch) { |
| 211 | dispatch({ |
| 212 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 213 | entitlementPoolToDelete: false |
| 214 | }); |
| 215 | }, |
| 216 | openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) { |
| 217 | dispatch({ |
| 218 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 219 | entitlementPoolToDelete: entitlementPool |
| 220 | }); |
| 221 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 222 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 223 | fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) { |
| 224 | return fetchLimitsList( |
| 225 | licenseModelId, |
| 226 | entitlementPool.id, |
| 227 | version |
| 228 | ).then(response => { |
| 229 | dispatch({ |
| 230 | type: |
| 231 | entitlementPoolsActionTypes.entitlementPoolsEditor |
| 232 | .LIMITS_LIST_LOADED, |
| 233 | response |
| 234 | }); |
| 235 | }); |
| 236 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 237 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 238 | submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 239 | const propmise = limit.id |
| 240 | ? putLimit(licenseModelId, entitlementPool.id, version, limit) |
| 241 | : postLimit(licenseModelId, entitlementPool.id, version, limit); |
| 242 | return propmise.then(() => { |
| 243 | dispatch({ |
| 244 | type: limitEditorActions.CLOSE |
| 245 | }); |
| 246 | this.fetchLimits(dispatch, { |
| 247 | licenseModelId, |
| 248 | version, |
| 249 | entitlementPool |
| 250 | }); |
| 251 | return ItemsHelper.checkItemStatus(dispatch, { |
| 252 | itemId: licenseModelId, |
| 253 | versionId: version.id |
| 254 | }); |
| 255 | }); |
| 256 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 257 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 258 | deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 259 | return deleteLimit( |
| 260 | licenseModelId, |
| 261 | entitlementPool.id, |
| 262 | version, |
| 263 | limit.id |
| 264 | ).then(() => { |
| 265 | this.fetchLimits(dispatch, { |
| 266 | licenseModelId, |
| 267 | version, |
| 268 | entitlementPool |
| 269 | }); |
| 270 | return ItemsHelper.checkItemStatus(dispatch, { |
| 271 | itemId: licenseModelId, |
| 272 | versionId: version.id |
| 273 | }); |
| 274 | }); |
| 275 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 276 | }; |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame^] | 277 | |
| 278 | export default EntitlementPoolsActionHelper; |