From 9f199eb7318be6975cd20cc265836fb627a4267b Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 17 Feb 2021 17:53:17 +0100 Subject: Add code in UI to create new policy version Add code to call the policy create backend endpoint + alert box to show status of the creation + Fix backend + Issue-ID: POLICY-2928 Signed-off-by: sebdet Change-Id: Ia8f2506ecc692ad68111ebe7a55a92579b951908 (cherry picked from commit c427e6459d066859e6da068bf50c78a0d120080a) --- ui-react/src/api/PolicyService.js | 23 ++++++ .../components/dialogs/Policy/ViewAllPolicies.js | 95 +++++++++++++++++++--- 2 files changed, 105 insertions(+), 13 deletions(-) (limited to 'ui-react') diff --git a/ui-react/src/api/PolicyService.js b/ui-react/src/api/PolicyService.js index fdbb5d5ea..16cc1f322 100644 --- a/ui-react/src/api/PolicyService.js +++ b/ui-react/src/api/PolicyService.js @@ -40,4 +40,27 @@ export default class PolicyService { return {}; }); } + static createNewPolicy(policyModelType, policyModelVersion, policyJson) { + return fetch(window.location.pathname + 'restservices/clds/v2/policies/' + policyModelType + '/' + policyModelVersion, { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(policyJson) + }) + .then(function (response) { + console.debug("createNewPolicy response received: ", response.status); + if (response.ok) { + return response.text; + } else { + console.error("createNewPolicy query failed"); + return ""; + } + }) + .catch(function (error) { + console.error("createNewPolicy error received", error); + throw new Error(error) + }); + } } diff --git a/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js b/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js index a1cf9d5c1..dfebc51d8 100644 --- a/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js +++ b/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js @@ -47,6 +47,12 @@ import PolicyService from '../../../api/PolicyService'; import PolicyToscaService from '../../../api/PolicyToscaService'; import Select from 'react-select'; import JSONEditor from '@json-editor/json-editor'; +import OnapUtils from '../../../utils/OnapUtils'; +import Alert from 'react-bootstrap/Alert'; + +const DivWhiteSpaceStyled = styled.div` + white-space: pre; +` const ModalStyled = styled(Modal)` @media (min-width: 1200px) { @@ -76,9 +82,11 @@ export default class ViewAllPolicies extends React.Component { state = { show: true, content: 'Please select a policy to display it', - selectedRow: -1, + selectedRowId: -1, policiesListData: [], prefixGrouping: false, + showSuccessAlert: false, + showFailAlert: false, policyColumnsDefinition: [ { title: "Policy Name", field: "name", @@ -148,7 +156,8 @@ export default class ViewAllPolicies extends React.Component { this.handlePrefixGrouping = this.handlePrefixGrouping.bind(this); this.handleDeletePolicy = this.handleDeletePolicy.bind(this); this.handleUpdatePolicy = this.handleUpdatePolicy.bind(this); - this.handleCreateNewVersion = this.handleCreateNewVersion(this); + this.handleCreateNewVersion = this.handleCreateNewVersion.bind(this); + this.disableAlert = this.disableAlert.bind(this); this.getAllPolicies(); } @@ -159,10 +168,10 @@ export default class ViewAllPolicies extends React.Component { let selectedSubPdpGroup = pdpSplit[1]; if (typeof selectedSubPdpGroup !== "undefined") { let temp = this.state.policiesListData; - temp[this.state.selectedRow]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup}; + temp[this.state.selectedRowId]["pdpGroupInfo"] = {"pdpGroup":selectedPdpGroup,"pdpSubGroup":selectedSubPdpGroup}; this.setState({policiesListData: temp}); } else { - delete this.state.policiesListData[this.state.selectedRow]["pdpGroupInfo"]; + delete this.state.policiesListData[this.state.selectedRowId]["pdpGroupInfo"]; } } @@ -237,9 +246,9 @@ export default class ViewAllPolicies extends React.Component { handleOnRowClick(rowData) { PolicyToscaService.getToscaPolicyModel(rowData["type"], rowData["type_version"]).then(respJsonPolicyTosca => { this.setState({ - selectedRow: rowData.tableData.id, - selectedRowJsonSchema: respJsonPolicyTosca, - selectedRowPolicyProperties: rowData["properties"], + selectedRowId: rowData.tableData.id, + selectedRowIdJsonSchema: respJsonPolicyTosca, + selectedRowIdPolicyProperties: rowData["properties"], jsonEditorForPolicy: this.createJsonEditor(respJsonPolicyTosca, rowData["properties"]) }); }); @@ -253,8 +262,52 @@ export default class ViewAllPolicies extends React.Component { return null; } - handleCreateNewVersion(event,rowData) { - return null; + customValidation(editorData) { + // method for sub-classes to override with customized validation + return []; + } + + handleCreateNewVersion() { + var editorData = this.state.jsonEditorForPolicy.getValue(); + var errors = this.state.jsonEditorForPolicy.validate(); + errors = errors.concat(this.customValidation(editorData)); + + if (errors.length !== 0) { + console.error("Errors detected during policy data validation ", errors); + this.setState({ + showFailAlert: true, + showMessage: 'Errors detected during policy data validation:\n' + OnapUtils.jsonEditorErrorFormatter(errors) + }); + return; + } else { + console.info("NO validation errors found in policy data"); + let newPolicy = JSON.parse(JSON.stringify(this.state.policiesListData[this.state.selectedRowId])); + newPolicy["properties"] = editorData; + let newVersion = this.bumpVersion(newPolicy["version"]); + newPolicy["version"] = newVersion; + newPolicy["metadata"]["policy-version"] = newVersion; + // Remove stuff added by UI + delete newPolicy["tableData"]; + PolicyService.createNewPolicy(newPolicy["type"], newPolicy["type_version"], newPolicy).then(respPolicyCreation => { + if (respPolicyCreation === "") { + //it indicates a failure + this.setState({ + showFailAlert: true, + showMessage: 'Policy Creation Failure' + }); + } else { + this.setState({ + showSuccessAlert: true, + showMessage: 'Policy in version ' + newVersion + ' created successfully' + }); + } + }) + } + } + + bumpVersion(versionToBump) { + let semVer = versionToBump.split("."); + return parseInt(semVer[0])+1 + "." + semVer[1] + "." + semVer[2]; } handleUpdatePolicy() { @@ -262,11 +315,16 @@ export default class ViewAllPolicies extends React.Component { this.props.history.push('/') } + disableAlert() { + this.setState ({ showSuccessAlert: false, showFailAlert: false }); + } + render() { return ( + } @@ -283,7 +341,7 @@ export default class ViewAllPolicies extends React.Component { exportButton: true, headerStyle:rowHeaderStyle, rowStyle: rowData => ({ - backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' + backgroundColor: (this.state.selectedRowId !== -1 && this.state.selectedRowId === rowData.tableData.id) ? '#EEE' : '#FFF' }) }} actions={[ @@ -293,14 +351,25 @@ export default class ViewAllPolicies extends React.Component { onClick: (event, rowData) => this.handleDeletePolicy(event, rowData) } ]} - />
Policy Properties Editor
- - + + + + + {this.state.showMessage} + + + + + {this.state.showMessage} + + -- cgit 1.2.3-korg