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