blob: 2155977f61bcd751b316bf0d97b8651e816cd921 [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';
xuegaoed700372020-04-08 08:46:25 +020032import Spinner from 'react-bootstrap/Spinner'
33
34const StyledSpinnerDiv = styled.div`
35 justify-content: center !important;
36 display: flex !important;
37`;
xuegao691e2b72019-08-16 11:07:24 +020038
39const ModalStyled = styled(Modal)`
40 background-color: transparent;
41`
42const FormStyled = styled(Form.Group)`
43 padding: .25rem 1.5rem;
44`
sebdet2e9ae122019-11-15 16:28:55 +010045export default class DeployLoopModal extends React.Component {
xuegao8c2ce602020-01-13 10:27:54 +010046
47
48
xuegao691e2b72019-08-16 11:07:24 +020049 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);
xuegaoed700372020-04-08 08:46:25 +020057 this.renderSpinner = this.renderSpinner.bind(this);
xuegao8c2ce602020-01-13 10:27:54 +010058
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"];
69 let initialKey;
70 Object.keys(deployJsonList)
71 .filter((obj) => Object.keys(deployJsonList).indexOf(obj) === 0)
72 .map(obj =>
73 initialKey = obj
74 );
75 return initialKey;
xuegao691e2b72019-08-16 11:07:24 +020076 }
77 componentWillReceiveProps(newProps) {
78 this.setState({
79 loopName: newProps.loopCache.getLoopName(),
80 show: true
81 });
82 }
xuegao8c2ce602020-01-13 10:27:54 +010083
xuegao691e2b72019-08-16 11:07:24 +020084 handleClose(){
xuegaobe9a2a52020-03-24 16:38:01 +010085 this.setState({ show: false });
xuegao691e2b72019-08-16 11:07:24 +020086 this.props.history.push('/');
87 }
xuegaobe9a2a52020-03-24 16:38:01 +010088
xuegaoed700372020-04-08 08:46:25 +020089 renderSpinner() {
90 if (this.state.deploying) {
91 return (
92 <StyledSpinnerDiv>
93 <Spinner animation="border" role="status">
94 <span className="sr-only">Loading...</span>
95 </Spinner>
96 </StyledSpinnerDiv>
97 );
98 } else {
99 return (<div></div>);
100 }
101 }
102
xuegao841ddaa2019-09-19 14:23:14 +0200103 handleSave() {
xuegao691e2b72019-08-16 11:07:24 +0200104 const loopName = this.props.loopCache.getLoopName();
105 // save the global propserties
xuegaoed700372020-04-08 08:46:25 +0200106 this.setState({ deploying: true });
xuegao691e2b72019-08-16 11:07:24 +0200107 LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => {
xuegao691e2b72019-08-16 11:07:24 +0200108 LoopActionService.performAction(loopName, "deploy").then(pars => {
xuegaocc5fe512020-04-06 13:13:52 +0200109 this.props.showSucAlert("Action deploy successfully performed");
xuegao691e2b72019-08-16 11:07:24 +0200110 // refresh status and update loop logs
111 this.refreshStatus(loopName);
112 })
113 .catch(error => {
xuegaocc5fe512020-04-06 13:13:52 +0200114 this.props.showFailAlert("Action deploy failed");
xuegao691e2b72019-08-16 11:07:24 +0200115 // refresh status and update loop logs
116 this.refreshStatus(loopName);
117 });
118 });
119 }
120
121 refreshStatus(loopName) {
122 LoopActionService.refreshStatus(loopName).then(data => {
123 this.props.updateLoopFunction(data);
xuegaoed700372020-04-08 08:46:25 +0200124 this.setState({ show: false, deploying: false });
125 this.props.history.push('/');
xuegao691e2b72019-08-16 11:07:24 +0200126 })
127 .catch(error => {
xuegaocc5fe512020-04-06 13:13:52 +0200128 this.props.showFailAlert("Refresh status failed");
xuegaoed700372020-04-08 08:46:25 +0200129 this.setState({ show: false, deploying: false });
130 this.props.history.push('/');
xuegao691e2b72019-08-16 11:07:24 +0200131 });
132 }
133 handleChange(event) {
134 let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
xuegao9047def2019-12-13 11:50:24 +0100135 deploymentParam[this.state.key][event.target.name] = event.target.value;
xuegao691e2b72019-08-16 11:07:24 +0200136
137 this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}});
138 }
xuegao9047def2019-12-13 11:50:24 +0100139 renderDeployParamTabs() {
xuegao691e2b72019-08-16 11:07:24 +0200140 if (typeof (this.state.temporaryPropertiesJson) === "undefined") {
141 return "";
142 }
143
xuegao9047def2019-12-13 11:50:24 +0100144 const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"];
145 var indents = [];
146 Object.keys(deployJsonList).map((item,key) =>
147 indents.push(<Tab eventKey={item} title={item}>
148 {this.renderDeployParam(deployJsonList[item])}
149 </Tab>)
150 );
151 return indents;
152 }
xuegao9047def2019-12-13 11:50:24 +0100153 renderDeployParam(deployJson) {
xuegao691e2b72019-08-16 11:07:24 +0200154 var indents = [];
155 Object.keys(deployJson).map((item,key) =>
156 indents.push(<FormStyled>
157 <Form.Label>{item}</Form.Label>
158 <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control>
159 </FormStyled>));
xuegao9047def2019-12-13 11:50:24 +0100160 return indents;
xuegao691e2b72019-08-16 11:07:24 +0200161 }
xuegao691e2b72019-08-16 11:07:24 +0200162 render() {
163 return (
Ted Humphreydf68db22020-04-16 17:36:17 +0000164 <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
xuegao691e2b72019-08-16 11:07:24 +0200165 <Modal.Header closeButton>
166 <Modal.Title>Deployment parameters</Modal.Title>
167 </Modal.Header>
xuegao9047def2019-12-13 11:50:24 +0100168 <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}>
169 {this.renderDeployParamTabs()}
170 </Tabs>
xuegaoed700372020-04-08 08:46:25 +0200171 {this.renderSpinner()}
xuegao691e2b72019-08-16 11:07:24 +0200172 <Modal.Footer>
173 <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>
174 <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button>
175 </Modal.Footer>
176 </ModalStyled>
177 );
178 }
179}