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'; |
| 18 | import {actionTypes as licenseKeyGroupsConstants} from './LicenseKeyGroupsConstants.js'; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 19 | import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 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) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 24 | const restPrefix = Configuration.get('restPrefix'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [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) { |
| 34 | return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 35 | } |
| 36 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 37 | function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
| 38 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 39 | name: licenseKeyGroup.name, |
| 40 | description: licenseKeyGroup.description, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 41 | operationalScope: getValue(licenseKeyGroup.operationalScope), |
| 42 | type: licenseKeyGroup.type, |
| 43 | increments: licenseKeyGroup.increments, |
| 44 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 45 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 46 | startDate: licenseKeyGroup.startDate, |
| 47 | expiryDate: licenseKeyGroup.expiryDate |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 48 | }); |
| 49 | } |
| 50 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 51 | function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
| 52 | return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | name: licenseKeyGroup.name, |
| 54 | description: licenseKeyGroup.description, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 55 | operationalScope: getValue(licenseKeyGroup.operationalScope), |
| 56 | type: licenseKeyGroup.type, |
| 57 | increments: licenseKeyGroup.increments, |
| 58 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 59 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 60 | startDate: licenseKeyGroup.startDate, |
| 61 | expiryDate: licenseKeyGroup.expiryDate |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 62 | }); |
| 63 | } |
| 64 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 65 | function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) { |
| 66 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`); |
| 67 | } |
| 68 | |
| 69 | function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) { |
| 70 | return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limitId}`); |
| 71 | } |
| 72 | |
| 73 | function postLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
| 74 | return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, { |
| 75 | name: limit.name, |
| 76 | type: limit.type, |
| 77 | description: limit.description, |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 78 | metric: getStrValue(limit.metric), |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 79 | value: limit.value, |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 80 | unit: getStrValue(limit.unit), |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 81 | aggregationFunction: getValue(limit.aggregationFunction), |
| 82 | time: getValue(limit.time) |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | function putLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
| 87 | |
| 88 | return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limit.id}`, { |
| 89 | name: limit.name, |
| 90 | type: limit.type, |
| 91 | description: limit.description, |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 92 | metric: getStrValue(limit.metric), |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 93 | value: limit.value, |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 94 | unit: getStrValue(limit.unit), |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 95 | aggregationFunction: getValue(limit.aggregationFunction), |
| 96 | time: getValue(limit.time) |
| 97 | }); |
| 98 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | |
| 100 | export default { |
| 101 | fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) { |
| 102 | return fetchLicenseKeyGroupsList(licenseModelId, version).then(response => dispatch({ |
| 103 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED, |
| 104 | response |
| 105 | })); |
| 106 | }, |
| 107 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 108 | openLicenseKeyGroupsEditor(dispatch, {licenseKeyGroup, licenseModelId, version} = {}) { |
| 109 | if (licenseModelId && version) { |
| 110 | this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}); |
| 111 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 112 | dispatch({ |
| 113 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN, |
| 114 | licenseKeyGroup |
| 115 | }); |
| 116 | }, |
| 117 | |
| 118 | closeLicenseKeyGroupEditor(dispatch){ |
| 119 | dispatch({ |
| 120 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE |
| 121 | }); |
| 122 | }, |
| 123 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 124 | saveLicenseKeyGroup(dispatch, {licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version}) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 125 | if (previousLicenseKeyGroup) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 126 | return putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(() => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 127 | dispatch({ |
| 128 | type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP, |
| 129 | licenseKeyGroup |
| 130 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 131 | ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 132 | }); |
| 133 | } |
| 134 | else { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 135 | return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 136 | dispatch({ |
| 137 | type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP, |
| 138 | licenseKeyGroup: { |
| 139 | ...licenseKeyGroup, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 140 | referencingFeatureGroups: [], |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 141 | id: response.value |
| 142 | } |
| 143 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 144 | ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 145 | }); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | }, |
| 150 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 151 | deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){ |
| 152 | return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 153 | dispatch({ |
| 154 | type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP, |
| 155 | licenseKeyGroupId |
| 156 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 157 | ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 158 | }); |
| 159 | }, |
| 160 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 161 | hideDeleteConfirm(dispatch) { |
| 162 | dispatch({ |
| 163 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 164 | licenseKeyGroupToDelete: false |
| 165 | }); |
| 166 | }, |
| 167 | |
| 168 | openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) { |
| 169 | dispatch({ |
| 170 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 171 | licenseKeyGroupToDelete: licenseKeyGroup |
| 172 | }); |
| 173 | }, |
| 174 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 175 | |
| 176 | fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}) { |
| 177 | return fetchLimitsList(licenseModelId, licenseKeyGroup.id, version).then(response => { |
| 178 | dispatch({ |
| 179 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.LIMITS_LIST_LOADED, |
| 180 | response |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 181 | }); |
| 182 | }); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 183 | }, |
| 184 | |
| 185 | submitLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) { |
| 186 | const promise = limit.id ? putLimit(licenseModelId,licenseKeyGroup.id, version, limit) : |
| 187 | postLimit(licenseModelId,licenseKeyGroup.id, version, limit); |
| 188 | return promise.then(() => { |
| 189 | dispatch({ |
| 190 | type: limitEditorActions.CLOSE |
| 191 | }); |
| 192 | this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 193 | ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 194 | }); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 195 | }, |
| 196 | |
| 197 | deleteLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) { |
| 198 | return deleteLimit(licenseModelId,licenseKeyGroup.id, version, limit.id).then(() => { |
| 199 | this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame^] | 200 | ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 201 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 202 | } |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 203 | |
| 204 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 205 | }; |