xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2019 AT&T Intellectual Property. All rights |
| 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'; |
| 32 | |
| 33 | const ModalStyled = styled(Modal)` |
| 34 | background-color: transparent; |
| 35 | ` |
| 36 | const FormStyled = styled(Form.Group)` |
| 37 | padding: .25rem 1.5rem; |
| 38 | ` |
sebdet | 2e9ae12 | 2019-11-15 16:28:55 +0100 | [diff] [blame] | 39 | export default class DeployLoopModal extends React.Component { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 40 | state = { |
| 41 | loopCache: this.props.loopCache, |
| 42 | temporaryPropertiesJson: JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())), |
| 43 | show: true |
| 44 | }; |
| 45 | constructor(props, context) { |
| 46 | super(props, context); |
| 47 | |
| 48 | this.handleSave = this.handleSave.bind(this); |
| 49 | this.handleClose = this.handleClose.bind(this); |
| 50 | this.handleChange = this.handleChange.bind(this); |
| 51 | this.refreshStatus = this.refreshStatus.bind(this); |
| 52 | this.renderDeployParam = this.renderDeployParam.bind(this); |
| 53 | } |
| 54 | componentWillReceiveProps(newProps) { |
| 55 | this.setState({ |
| 56 | loopName: newProps.loopCache.getLoopName(), |
| 57 | show: true |
| 58 | }); |
| 59 | } |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 60 | componentDidMount() { |
| 61 | const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; |
| 62 | Object.keys(deployJsonList) |
| 63 | .filter((obj) => Object.keys(deployJsonList).indexOf(obj) === 0) |
| 64 | .map(obj => |
| 65 | this.setState({key: obj}) |
| 66 | ); |
| 67 | } |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 68 | handleClose(){ |
| 69 | this.props.history.push('/'); |
| 70 | } |
xuegao | 841ddaa | 2019-09-19 14:23:14 +0200 | [diff] [blame] | 71 | handleSave() { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 72 | const loopName = this.props.loopCache.getLoopName(); |
| 73 | // save the global propserties |
| 74 | LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => { |
| 75 | this.setState({ show: false }); |
| 76 | |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 77 | LoopActionService.performAction(loopName, "deploy").then(pars => { |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 78 | this.props.showAlert("Action deploy successfully performed"); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 79 | // refresh status and update loop logs |
| 80 | this.refreshStatus(loopName); |
| 81 | }) |
| 82 | .catch(error => { |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 83 | this.props.showAlert("Action deploy failed"); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 84 | // refresh status and update loop logs |
| 85 | this.refreshStatus(loopName); |
| 86 | }); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | refreshStatus(loopName) { |
| 91 | LoopActionService.refreshStatus(loopName).then(data => { |
| 92 | this.props.updateLoopFunction(data); |
| 93 | this.props.history.push('/'); |
| 94 | }) |
| 95 | .catch(error => { |
xuegao | 0efeb6b | 2019-10-07 14:36:34 +0200 | [diff] [blame] | 96 | this.props.showAlert("Refresh status failed"); |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 97 | this.props.history.push('/'); |
| 98 | }); |
| 99 | } |
| 100 | handleChange(event) { |
| 101 | let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 102 | deploymentParam[this.state.key][event.target.name] = event.target.value; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 103 | |
| 104 | this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}}); |
| 105 | } |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 106 | renderDeployParamTabs() { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 107 | if (typeof (this.state.temporaryPropertiesJson) === "undefined") { |
| 108 | return ""; |
| 109 | } |
| 110 | |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 111 | const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; |
| 112 | var indents = []; |
| 113 | Object.keys(deployJsonList).map((item,key) => |
| 114 | indents.push(<Tab eventKey={item} title={item}> |
| 115 | {this.renderDeployParam(deployJsonList[item])} |
| 116 | </Tab>) |
| 117 | ); |
| 118 | return indents; |
| 119 | } |
| 120 | |
| 121 | renderDeployParam(deployJson) { |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 122 | var indents = []; |
| 123 | Object.keys(deployJson).map((item,key) => |
| 124 | indents.push(<FormStyled> |
| 125 | <Form.Label>{item}</Form.Label> |
| 126 | <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control> |
| 127 | </FormStyled>)); |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 128 | return indents; |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 129 | } |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 130 | render() { |
| 131 | return ( |
| 132 | <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} > |
| 133 | <Modal.Header closeButton> |
| 134 | <Modal.Title>Deployment parameters</Modal.Title> |
| 135 | </Modal.Header> |
xuegao | 9047def | 2019-12-13 11:50:24 +0100 | [diff] [blame^] | 136 | <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}> |
| 137 | {this.renderDeployParamTabs()} |
| 138 | </Tabs> |
xuegao | 691e2b7 | 2019-08-16 11:07:24 +0200 | [diff] [blame] | 139 | <Modal.Footer> |
| 140 | <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> |
| 141 | <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button> |
| 142 | </Modal.Footer> |
| 143 | </ModalStyled> |
| 144 | ); |
| 145 | } |
| 146 | } |