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