xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
sebdet | 4b6371f | 2021-04-15 19:43:27 +0200 | [diff] [blame] | 3 | * ONAP POLICY-CLAMP |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 4 | * ================================================================================ |
sebdet | 4b6371f | 2021-04-15 19:43:27 +0200 | [diff] [blame] | 5 | * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 6 | * reserved. |
| 7 | * ================================================================================ |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * ============LICENSE_END============================================ |
| 20 | * =================================================================== |
| 21 | * |
| 22 | */ |
| 23 | import React from 'react'; |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 24 | import LoopActionService from '../../../api/LoopActionService'; |
| 25 | import LoopService from '../../../api/LoopService'; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 26 | import Button from 'react-bootstrap/Button'; |
| 27 | import Modal from 'react-bootstrap/Modal'; |
| 28 | import Form from 'react-bootstrap/Form'; |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 29 | import Tabs from 'react-bootstrap/Tabs'; |
| 30 | import Tab from 'react-bootstrap/Tab'; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 31 | import styled from 'styled-components'; |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 32 | import Spinner from 'react-bootstrap/Spinner' |
| 33 | |
| 34 | const StyledSpinnerDiv = styled.div` |
| 35 | justify-content: center !important; |
| 36 | display: flex !important; |
| 37 | `; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 38 | |
| 39 | const ModalStyled = styled(Modal)` |
| 40 | background-color: transparent; |
| 41 | ` |
| 42 | const FormStyled = styled(Form.Group)` |
| 43 | padding: .25rem 1.5rem; |
| 44 | ` |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 45 | export default class DeployLoopModal extends React.Component { |
xuegao | 8c2ce60 | 2020-01-13 10:27:54 +0100 | [diff] [blame] | 46 | |
| 47 | |
| 48 | |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 49 | constructor(props, context) { |
| 50 | super(props, context); |
| 51 | |
| 52 | this.handleSave = this.handleSave.bind(this); |
| 53 | this.handleClose = this.handleClose.bind(this); |
| 54 | this.handleChange = this.handleChange.bind(this); |
| 55 | this.refreshStatus = this.refreshStatus.bind(this); |
| 56 | this.renderDeployParam = this.renderDeployParam.bind(this); |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 57 | this.renderSpinner = this.renderSpinner.bind(this); |
xuegao | 8c2ce60 | 2020-01-13 10:27:54 +0100 | [diff] [blame] | 58 | |
| 59 | const propertiesJson = JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())); |
| 60 | this.state = { |
| 61 | loopCache: this.props.loopCache, |
| 62 | temporaryPropertiesJson: propertiesJson, |
| 63 | show: true, |
| 64 | key: this.getInitialKeyValue(propertiesJson) |
| 65 | }; |
| 66 | } |
| 67 | getInitialKeyValue(temporaryPropertiesJson) { |
| 68 | const deployJsonList = temporaryPropertiesJson["dcaeDeployParameters"]; |
sebdet | 4b6371f | 2021-04-15 19:43:27 +0200 | [diff] [blame] | 69 | return Object.keys(deployJsonList).find((obj) => Object.keys(deployJsonList).indexOf(obj) === 0); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 70 | } |
| 71 | componentWillReceiveProps(newProps) { |
| 72 | this.setState({ |
| 73 | loopName: newProps.loopCache.getLoopName(), |
| 74 | show: true |
| 75 | }); |
| 76 | } |
xuegao | 8c2ce60 | 2020-01-13 10:27:54 +0100 | [diff] [blame] | 77 | |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 78 | handleClose(){ |
xuegao | be9a2a5 | 2020-03-24 16:38:01 +0100 | [diff] [blame] | 79 | this.setState({ show: false }); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 80 | this.props.history.push('/'); |
| 81 | } |
xuegao | be9a2a5 | 2020-03-24 16:38:01 +0100 | [diff] [blame] | 82 | |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 83 | renderSpinner() { |
| 84 | if (this.state.deploying) { |
| 85 | return ( |
| 86 | <StyledSpinnerDiv> |
| 87 | <Spinner animation="border" role="status"> |
| 88 | <span className="sr-only">Loading...</span> |
| 89 | </Spinner> |
| 90 | </StyledSpinnerDiv> |
| 91 | ); |
| 92 | } else { |
| 93 | return (<div></div>); |
| 94 | } |
| 95 | } |
| 96 | |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 97 | handleSave() { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 98 | const loopName = this.props.loopCache.getLoopName(); |
| 99 | // save the global propserties |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 100 | this.setState({ deploying: true }); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 101 | LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 102 | LoopActionService.performAction(loopName, "deploy").then(pars => { |
xuegao | cc5fe51 | 2020-04-06 13:13:52 +0200 | [diff] [blame] | 103 | this.props.showSucAlert("Action deploy successfully performed"); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 104 | // refresh status and update loop logs |
| 105 | this.refreshStatus(loopName); |
| 106 | }) |
| 107 | .catch(error => { |
xuegao | cc5fe51 | 2020-04-06 13:13:52 +0200 | [diff] [blame] | 108 | this.props.showFailAlert("Action deploy failed"); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 109 | // refresh status and update loop logs |
| 110 | this.refreshStatus(loopName); |
| 111 | }); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | refreshStatus(loopName) { |
| 116 | LoopActionService.refreshStatus(loopName).then(data => { |
| 117 | this.props.updateLoopFunction(data); |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 118 | this.setState({ show: false, deploying: false }); |
| 119 | this.props.history.push('/'); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 120 | }) |
| 121 | .catch(error => { |
xuegao | cc5fe51 | 2020-04-06 13:13:52 +0200 | [diff] [blame] | 122 | this.props.showFailAlert("Refresh status failed"); |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 123 | this.setState({ show: false, deploying: false }); |
| 124 | this.props.history.push('/'); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 125 | }); |
| 126 | } |
| 127 | handleChange(event) { |
| 128 | let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 129 | deploymentParam[this.state.key][event.target.name] = event.target.value; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 130 | |
| 131 | this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}}); |
| 132 | } |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 133 | renderDeployParamTabs() { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 134 | if (typeof (this.state.temporaryPropertiesJson) === "undefined") { |
| 135 | return ""; |
| 136 | } |
| 137 | |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 138 | const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; |
| 139 | var indents = []; |
sebdet | 4b6371f | 2021-04-15 19:43:27 +0200 | [diff] [blame] | 140 | Object.keys(deployJsonList).forEach(item => |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 141 | indents.push(<Tab eventKey={item} title={item}> |
| 142 | {this.renderDeployParam(deployJsonList[item])} |
| 143 | </Tab>) |
| 144 | ); |
| 145 | return indents; |
| 146 | } |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 147 | renderDeployParam(deployJson) { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 148 | var indents = []; |
sebdet | 4b6371f | 2021-04-15 19:43:27 +0200 | [diff] [blame] | 149 | Object.keys(deployJson).forEach(item => |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 150 | indents.push(<FormStyled> |
| 151 | <Form.Label>{item}</Form.Label> |
| 152 | <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control> |
| 153 | </FormStyled>)); |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 154 | return indents; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 155 | } |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 156 | render() { |
| 157 | return ( |
Ted Humphrey | df68db2 | 2020-04-16 17:36:17 +0000 | [diff] [blame] | 158 | <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 159 | <Modal.Header closeButton> |
| 160 | <Modal.Title>Deployment parameters</Modal.Title> |
| 161 | </Modal.Header> |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame] | 162 | <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}> |
| 163 | {this.renderDeployParamTabs()} |
| 164 | </Tabs> |
xuegao | ed70037 | 2020-04-08 08:46:25 +0200 | [diff] [blame] | 165 | {this.renderSpinner()} |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 166 | <Modal.Footer> |
| 167 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 168 | <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button> |
| 169 | </Modal.Footer> |
| 170 | </ModalStyled> |
| 171 | ); |
| 172 | } |
| 173 | } |