blob: 9c9a16feb901fa4d9bd32f12def6c4390437e715 [file] [log] [blame]
xuegao691e2b72019-08-16 11:07:24 +02001/*-
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 */
23import React from 'react';
sebdet2e9ae122019-11-15 16:28:55 +010024import LoopActionService from '../../../api/LoopActionService';
25import LoopService from '../../../api/LoopService';
xuegao691e2b72019-08-16 11:07:24 +020026import Button from 'react-bootstrap/Button';
27import Modal from 'react-bootstrap/Modal';
28import Form from 'react-bootstrap/Form';
xuegao9047def2019-12-13 11:50:24 +010029import Tabs from 'react-bootstrap/Tabs';
30import Tab from 'react-bootstrap/Tab';
xuegao691e2b72019-08-16 11:07:24 +020031import styled from 'styled-components';
32
33const ModalStyled = styled(Modal)`
34 background-color: transparent;
35`
36const FormStyled = styled(Form.Group)`
37 padding: .25rem 1.5rem;
38`
sebdet2e9ae122019-11-15 16:28:55 +010039export default class DeployLoopModal extends React.Component {
xuegao691e2b72019-08-16 11:07:24 +020040 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 }
xuegao9047def2019-12-13 11:50:24 +010060 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 }
xuegao691e2b72019-08-16 11:07:24 +020068 handleClose(){
69 this.props.history.push('/');
70 }
xuegao841ddaa2019-09-19 14:23:14 +020071 handleSave() {
xuegao691e2b72019-08-16 11:07:24 +020072 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
xuegao691e2b72019-08-16 11:07:24 +020077 LoopActionService.performAction(loopName, "deploy").then(pars => {
xuegao0efeb6b2019-10-07 14:36:34 +020078 this.props.showAlert("Action deploy successfully performed");
xuegao691e2b72019-08-16 11:07:24 +020079 // refresh status and update loop logs
80 this.refreshStatus(loopName);
81 })
82 .catch(error => {
xuegao0efeb6b2019-10-07 14:36:34 +020083 this.props.showAlert("Action deploy failed");
xuegao691e2b72019-08-16 11:07:24 +020084 // 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 => {
xuegao0efeb6b2019-10-07 14:36:34 +020096 this.props.showAlert("Refresh status failed");
xuegao691e2b72019-08-16 11:07:24 +020097 this.props.history.push('/');
98 });
99 }
100 handleChange(event) {
101 let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
xuegao9047def2019-12-13 11:50:24 +0100102 deploymentParam[this.state.key][event.target.name] = event.target.value;
xuegao691e2b72019-08-16 11:07:24 +0200103
104 this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}});
105 }
xuegao9047def2019-12-13 11:50:24 +0100106 renderDeployParamTabs() {
xuegao691e2b72019-08-16 11:07:24 +0200107 if (typeof (this.state.temporaryPropertiesJson) === "undefined") {
108 return "";
109 }
110
xuegao9047def2019-12-13 11:50:24 +0100111 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) {
xuegao691e2b72019-08-16 11:07:24 +0200122 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>));
xuegao9047def2019-12-13 11:50:24 +0100128 return indents;
xuegao691e2b72019-08-16 11:07:24 +0200129 }
xuegao691e2b72019-08-16 11:07:24 +0200130 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>
xuegao9047def2019-12-13 11:50:24 +0100136 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}>
137 {this.renderDeployParamTabs()}
138 </Tabs>
xuegao691e2b72019-08-16 11:07:24 +0200139 <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}