AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
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 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 17 | import Configuration from 'sdc-app/config/Configuration.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 18 | import { actionTypes as licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.js'; |
| 19 | import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
| 20 | import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 21 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 22 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 24 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | function fetchLicenseKeyGroupsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | } |
| 32 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 33 | function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 34 | return RestAPIUtil.destroy( |
| 35 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}` |
| 36 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | } |
| 38 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 39 | function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 41 | name: licenseKeyGroup.name, |
| 42 | description: licenseKeyGroup.description, |
| 43 | operationalScope: getValue(licenseKeyGroup.operationalScope), |
| 44 | type: licenseKeyGroup.type, |
| 45 | increments: licenseKeyGroup.increments, |
| 46 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 47 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 48 | startDate: licenseKeyGroup.startDate, |
| 49 | expiryDate: licenseKeyGroup.expiryDate |
| 50 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 51 | } |
| 52 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 54 | return RestAPIUtil.put( |
| 55 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, |
| 56 | { |
| 57 | name: licenseKeyGroup.name, |
| 58 | description: licenseKeyGroup.description, |
| 59 | operationalScope: getValue(licenseKeyGroup.operationalScope), |
| 60 | type: licenseKeyGroup.type, |
| 61 | increments: licenseKeyGroup.increments, |
| 62 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 63 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 64 | startDate: licenseKeyGroup.startDate, |
| 65 | expiryDate: licenseKeyGroup.expiryDate |
| 66 | } |
| 67 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 70 | function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 71 | return RestAPIUtil.fetch( |
| 72 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits` |
| 73 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 77 | return RestAPIUtil.destroy( |
| 78 | `${baseUrl( |
| 79 | licenseModelId, |
| 80 | version |
| 81 | )}/${licenseKeyGroupId}/limits/${limitId}` |
| 82 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | function postLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 86 | return RestAPIUtil.post( |
| 87 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, |
| 88 | { |
| 89 | name: limit.name, |
| 90 | type: limit.type, |
| 91 | description: limit.description, |
| 92 | metric: getStrValue(limit.metric), |
| 93 | value: limit.value, |
| 94 | unit: getStrValue(limit.unit), |
| 95 | aggregationFunction: getValue(limit.aggregationFunction), |
| 96 | time: getValue(limit.time) |
| 97 | } |
| 98 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | function putLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 102 | return RestAPIUtil.put( |
| 103 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${ |
| 104 | limit.id |
| 105 | }`, |
| 106 | { |
| 107 | name: limit.name, |
| 108 | type: limit.type, |
| 109 | description: limit.description, |
| 110 | metric: getStrValue(limit.metric), |
| 111 | value: limit.value, |
| 112 | unit: getStrValue(limit.unit), |
| 113 | aggregationFunction: getValue(limit.aggregationFunction), |
| 114 | time: getValue(limit.time) |
| 115 | } |
| 116 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 117 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 118 | |
| 119 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 120 | fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) { |
| 121 | return fetchLicenseKeyGroupsList(licenseModelId, version).then( |
| 122 | response => |
| 123 | dispatch({ |
| 124 | type: |
| 125 | licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED, |
| 126 | response |
| 127 | }) |
| 128 | ); |
| 129 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 130 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 131 | openLicenseKeyGroupsEditor( |
| 132 | dispatch, |
| 133 | { licenseKeyGroup, licenseModelId, version } = {} |
| 134 | ) { |
| 135 | if (licenseModelId && version) { |
| 136 | this.fetchLimits(dispatch, { |
| 137 | licenseModelId, |
| 138 | version, |
| 139 | licenseKeyGroup |
| 140 | }); |
| 141 | } |
| 142 | dispatch({ |
| 143 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN, |
| 144 | licenseKeyGroup |
| 145 | }); |
| 146 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 147 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 148 | closeLicenseKeyGroupEditor(dispatch) { |
| 149 | dispatch({ |
| 150 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE |
| 151 | }); |
| 152 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 153 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 154 | saveLicenseKeyGroup( |
| 155 | dispatch, |
| 156 | { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version } |
| 157 | ) { |
| 158 | if (previousLicenseKeyGroup) { |
| 159 | return putLicenseKeyGroup( |
| 160 | licenseModelId, |
| 161 | licenseKeyGroup, |
| 162 | version |
| 163 | ).then(() => { |
| 164 | dispatch({ |
| 165 | type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP, |
| 166 | licenseKeyGroup |
| 167 | }); |
| 168 | return ItemsHelper.checkItemStatus(dispatch, { |
| 169 | itemId: licenseModelId, |
| 170 | versionId: version.id |
| 171 | }); |
| 172 | }); |
| 173 | } else { |
| 174 | return postLicenseKeyGroup( |
| 175 | licenseModelId, |
| 176 | licenseKeyGroup, |
| 177 | version |
| 178 | ).then(response => { |
| 179 | dispatch({ |
| 180 | type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP, |
| 181 | licenseKeyGroup: { |
| 182 | ...licenseKeyGroup, |
| 183 | referencingFeatureGroups: [], |
| 184 | id: response.value |
| 185 | } |
| 186 | }); |
| 187 | return ItemsHelper.checkItemStatus(dispatch, { |
| 188 | itemId: licenseModelId, |
| 189 | versionId: version.id |
| 190 | }); |
| 191 | }); |
| 192 | } |
| 193 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 194 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 195 | deleteLicenseKeyGroup( |
| 196 | dispatch, |
| 197 | { licenseModelId, licenseKeyGroupId, version } |
| 198 | ) { |
| 199 | return deleteLicenseKeyGroup( |
| 200 | licenseModelId, |
| 201 | licenseKeyGroupId, |
| 202 | version |
| 203 | ).then(() => { |
| 204 | dispatch({ |
| 205 | type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP, |
| 206 | licenseKeyGroupId |
| 207 | }); |
| 208 | return ItemsHelper.checkItemStatus(dispatch, { |
| 209 | itemId: licenseModelId, |
| 210 | versionId: version.id |
| 211 | }); |
| 212 | }); |
| 213 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 214 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 215 | hideDeleteConfirm(dispatch) { |
| 216 | dispatch({ |
| 217 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 218 | licenseKeyGroupToDelete: false |
| 219 | }); |
| 220 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 221 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 222 | openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) { |
| 223 | dispatch({ |
| 224 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 225 | licenseKeyGroupToDelete: licenseKeyGroup |
| 226 | }); |
| 227 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 228 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 229 | fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) { |
| 230 | return fetchLimitsList( |
| 231 | licenseModelId, |
| 232 | licenseKeyGroup.id, |
| 233 | version |
| 234 | ).then(response => { |
| 235 | dispatch({ |
| 236 | type: |
| 237 | licenseKeyGroupsConstants.licenseKeyGroupsEditor |
| 238 | .LIMITS_LIST_LOADED, |
| 239 | response |
| 240 | }); |
| 241 | }); |
| 242 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 243 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 244 | submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) { |
| 245 | const promise = limit.id |
| 246 | ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit) |
| 247 | : postLimit(licenseModelId, licenseKeyGroup.id, version, limit); |
| 248 | return promise.then(() => { |
| 249 | dispatch({ |
| 250 | type: limitEditorActions.CLOSE |
| 251 | }); |
| 252 | this.fetchLimits(dispatch, { |
| 253 | licenseModelId, |
| 254 | version, |
| 255 | licenseKeyGroup |
| 256 | }); |
| 257 | return ItemsHelper.checkItemStatus(dispatch, { |
| 258 | itemId: licenseModelId, |
| 259 | versionId: version.id |
| 260 | }); |
| 261 | }); |
| 262 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 263 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 264 | deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) { |
| 265 | return deleteLimit( |
| 266 | licenseModelId, |
| 267 | licenseKeyGroup.id, |
| 268 | version, |
| 269 | limit.id |
| 270 | ).then(() => { |
| 271 | this.fetchLimits(dispatch, { |
| 272 | licenseModelId, |
| 273 | version, |
| 274 | licenseKeyGroup |
| 275 | }); |
| 276 | return ItemsHelper.checkItemStatus(dispatch, { |
| 277 | itemId: licenseModelId, |
| 278 | versionId: version.id |
| 279 | }); |
| 280 | }); |
| 281 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 282 | }; |