blob: 6468e9f9e4b0acc7dcb0d853883462ee12c525df [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 {
xuegao8c2ce602020-01-13 10:27:54 +010040
41
42
xuegao691e2b72019-08-16 11:07:24 +020043 constructor(props, context) {
44 super(props, context);
45
46 this.handleSave = this.handleSave.bind(this);
47 this.handleClose = this.handleClose.bind(this);
48 this.handleChange = this.handleChange.bind(this);
49 this.refreshStatus = this.refreshStatus.bind(this);
50 this.renderDeployParam = this.renderDeployParam.bind(this);
xuegao8c2ce602020-01-13 10:27:54 +010051
52 const propertiesJson = JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties()));
53 this.state = {
54 loopCache: this.props.loopCache,
55 temporaryPropertiesJson: propertiesJson,
56 show: true,
57 key: this.getInitialKeyValue(propertiesJson)
58 };
59 }
60 getInitialKeyValue(temporaryPropertiesJson) {
61 const deployJsonList = temporaryPropertiesJson["dcaeDeployParameters"];
62 let initialKey;
63 Object.keys(deployJsonList)
64 .filter((obj) => Object.keys(deployJsonList).indexOf(obj) === 0)
65 .map(obj =>
66 initialKey = obj
67 );
68 return initialKey;
xuegao691e2b72019-08-16 11:07:24 +020069 }
70 componentWillReceiveProps(newProps) {
71 this.setState({
72 loopName: newProps.loopCache.getLoopName(),
73 show: true
74 });
75 }
xuegao8c2ce602020-01-13 10:27:54 +010076
xuegao691e2b72019-08-16 11:07:24 +020077 handleClose(){
78 this.props.history.push('/');
79 }
xuegao841ddaa2019-09-19 14:23:14 +020080 handleSave() {
xuegao691e2b72019-08-16 11:07:24 +020081 const loopName = this.props.loopCache.getLoopName();
82 // save the global propserties
83 LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => {
84 this.setState({ show: false });
85
xuegao691e2b72019-08-16 11:07:24 +020086 LoopActionService.performAction(loopName, "deploy").then(pars => {
xuegao0efeb6b2019-10-07 14:36:34 +020087 this.props.showAlert("Action deploy successfully performed");
xuegao691e2b72019-08-16 11:07:24 +020088 // refresh status and update loop logs
89 this.refreshStatus(loopName);
90 })
91 .catch(error => {
xuegao0efeb6b2019-10-07 14:36:34 +020092 this.props.showAlert("Action deploy failed");
xuegao691e2b72019-08-16 11:07:24 +020093 // refresh status and update loop logs
94 this.refreshStatus(loopName);
95 });
96 });
97 }
98
99 refreshStatus(loopName) {
100 LoopActionService.refreshStatus(loopName).then(data => {
101 this.props.updateLoopFunction(data);
102 this.props.history.push('/');
103 })
104 .catch(error => {
xuegao0efeb6b2019-10-07 14:36:34 +0200105 this.props.showAlert("Refresh status failed");
xuegao691e2b72019-08-16 11:07:24 +0200106 this.props.history.push('/');
107 });
108 }
109 handleChange(event) {
110 let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
xuegao9047def2019-12-13 11:50:24 +0100111 deploymentParam[this.state.key][event.target.name] = event.target.value;
xuegao691e2b72019-08-16 11:07:24 +0200112
113 this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}});
114 }
xuegao9047def2019-12-13 11:50:24 +0100115 renderDeployParamTabs() {
xuegao691e2b72019-08-16 11:07:24 +0200116 if (typeof (this.state.temporaryPropertiesJson) === "undefined") {
117 return "";
118 }
119
xuegao9047def2019-12-13 11:50:24 +0100120 const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
121 var indents = [];
122 Object.keys(deployJsonList).map((item,key) =>
123 indents.push(<Tab eventKey={item} title={item}>
124 {this.renderDeployParam(deployJsonList[item])}
125 </Tab>)
126 );
127 return indents;
128 }
xuegao9047def2019-12-13 11:50:24 +0100129 renderDeployParam(deployJson) {
xuegao691e2b72019-08-16 11:07:24 +0200130 var indents = [];
131 Object.keys(deployJson).map((item,key) =>
132 indents.push(<FormStyled>
133 <Form.Label>{item}</Form.Label>
134 <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control>
135 </FormStyled>));
xuegao9047def2019-12-13 11:50:24 +0100136 return indents;
xuegao691e2b72019-08-16 11:07:24 +0200137 }
xuegao691e2b72019-08-16 11:07:24 +0200138 render() {
139 return (
140 <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} >
141 <Modal.Header closeButton>
142 <Modal.Title>Deployment parameters</Modal.Title>
143 </Modal.Header>
xuegao9047def2019-12-13 11:50:24 +0100144 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}>
145 {this.renderDeployParamTabs()}
146 </Tabs>
xuegao691e2b72019-08-16 11:07:24 +0200147 <Modal.Footer>
148 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
149 <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button>
150 </Modal.Footer>
151 </ModalStyled>
152 );
153 }
154}