CloudConfig.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import React, {Component} from 'react';
  2. import {Button, Input, Spin, Icon} from 'antd';
  3. import {SHOW_CLOUD, ADD_CLOUD, UPDATE_CLOUD} from "../gql";
  4. import {request} from 'graphql-request'
  5. import {getCookie} from '../cookie';
  6. import {idGen} from "../func";
  7. class CloudConfig extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. show: false,
  12. showOK: '',
  13. showNotOK: '',
  14. tencentCLoudID: '',
  15. tenID: '',
  16. tenKey: '',
  17. aliyunCLoudID: '',
  18. aliID: '',
  19. aliKey: '',
  20. awsCLoudID: '',
  21. awsID: '',
  22. awsKey: '',
  23. userID: getCookie('user_id')
  24. };
  25. request('http://123.206.193.98:3000/graphql', SHOW_CLOUD, {user_id: this.state.userID}).then(
  26. data => {
  27. let tencent = data.cloud_by_props.find(cloud => cloud.cloudName === 'tencent');
  28. let aliyun = data.cloud_by_props.find(cloud => cloud.cloudName === 'aliyun');
  29. let amazon = data.cloud_by_props.find(cloud => cloud.cloudName === 'amazon');
  30. this.setState({
  31. show: true,
  32. tencentCLoudID: tencent ? tencent.id : '',
  33. tenID: tencent ? tencent.secretId : '',
  34. tenKey: tencent ? tencent.secretKey : '',
  35. aliyunCLoudID: aliyun ? aliyun.id : '',
  36. aliID: aliyun ? aliyun.secretId : '',
  37. aliKey: aliyun ? aliyun.secretKey : '',
  38. awsCLoudID: amazon ? amazon.id : '',
  39. awsID: amazon ? amazon.secretId : '',
  40. awsKey: amazon ? amazon.secretKey : ''
  41. });
  42. }
  43. );
  44. }
  45. inputChange = (kind) => {
  46. return (e) => {
  47. this.setState({
  48. [kind]: e.target.value
  49. })
  50. }
  51. };
  52. submit = (id, cloudName, secretId, secretKey) => {
  53. return () => {
  54. if (id === '') {
  55. let varObj = {
  56. id: idGen('cloud'),
  57. user_id: this.state.userID,
  58. appId: '',
  59. cloudName,
  60. secretId,
  61. secretKey,
  62. createdAt: new Date().getTime(),
  63. updatedAt: ''
  64. };
  65. request('http://123.206.193.98:3000/graphql', ADD_CLOUD, varObj).then(
  66. data => {
  67. if (data.create_cloud.length !== null) {
  68. this.setState({
  69. showOK: cloudName
  70. });
  71. setTimeout(() => {
  72. this.setState({
  73. showOK: ''
  74. });
  75. }, 1500)
  76. } else {
  77. this.setState({
  78. showNotOK: cloudName
  79. });
  80. setTimeout(() => {
  81. this.setState({
  82. showNotOK: ''
  83. });
  84. }, 1500)
  85. }
  86. }
  87. );
  88. } else {
  89. let varObj = {
  90. id,
  91. secretId,
  92. secretKey,
  93. updatedAt: new Date().getTime()
  94. };
  95. request('http://123.206.193.98:3000/graphql', UPDATE_CLOUD, varObj).then(
  96. data => {
  97. if (data.update_cloud !== null) {
  98. this.setState({
  99. showOK: cloudName
  100. });
  101. setTimeout(() => {
  102. this.setState({
  103. showOK: ''
  104. });
  105. }, 1500)
  106. } else {
  107. this.setState({
  108. showNotOK: cloudName
  109. });
  110. setTimeout(() => {
  111. this.setState({
  112. showNotOK: ''
  113. });
  114. }, 1500)
  115. }
  116. }
  117. );
  118. }
  119. }
  120. };
  121. render() {
  122. return (
  123. this.state.show ?
  124. <div>
  125. <div className="column-menu" onClick={() => {
  126. this.props.history.push({
  127. pathname: '/login',
  128. })
  129. }}>
  130. <Icon type="left" style={{color: '#3187FA'}}/> login
  131. </div>
  132. <div style={{marginTop: 15}}>
  133. <span className='cloud-name'>Tencent: </span>
  134. <div style={{marginBottom: 15}}>
  135. <div>
  136. <div style={{marginBottom: 10}}>
  137. <span className='item-title-cloud'>ID:</span>
  138. <Input style={{width: 250}} value={this.state.aliID} onChange={this.inputChange('tenID')}/>
  139. </div>
  140. <div style={{marginBottom: 10}}>
  141. <span className='item-title-cloud'>Key:</span>
  142. <Input style={{width: 250}} value={this.state.aliKey} onChange={this.inputChange('tenKey')}/>
  143. </div>
  144. {
  145. this.state.showOK === 'tencent' ?
  146. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  147. :
  148. this.state.showNotOK === 'tencent' ?
  149. <span>not ok</span>
  150. :
  151. ""
  152. }
  153. </div>
  154. <Button type='primary'
  155. onClick={this.submit(this.state.tencentCLoudID, 'tencent', this.state.tenID, this.state.tenKey)}>ok</Button>
  156. </div>
  157. </div>
  158. <div>
  159. <span className='cloud-name'>Aliyun: </span>
  160. <div style={{marginBottom: 15}}>
  161. <div>
  162. <div style={{marginBottom: 10}}>
  163. <span className='item-title-cloud'>ID:</span>
  164. <Input style={{width: 250}} value={this.state.aliID} onChange={this.inputChange('aliID')}/>
  165. </div>
  166. <div style={{marginBottom: 10}}>
  167. <span className='item-title-cloud'>Key:</span>
  168. <Input style={{width: 250}} value={this.state.aliKey} onChange={this.inputChange('aliKey')}/>
  169. </div>
  170. {
  171. this.state.showOK === 'aliyun' ?
  172. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  173. :
  174. this.state.showNotOK === 'aliyun' ?
  175. <span>not ok</span>
  176. :
  177. ""
  178. }
  179. </div>
  180. <Button type='primary'
  181. onClick={this.submit(this.state.aliyunCLoudID, 'aliyun', this.state.aliID, this.state.aliKey)}>ok</Button>
  182. </div>
  183. </div>
  184. <div>
  185. <span className='cloud-name'>AWS: </span>
  186. <div style={{marginBottom: 15}}>
  187. <div>
  188. <div style={{marginBottom: 10}}>
  189. <span className='item-title-cloud'>ID:</span>
  190. <Input style={{width: 250}} value={this.state.aliID} onChange={this.inputChange('awsID')}/>
  191. </div>
  192. <div style={{marginBottom: 10}}>
  193. <span className='item-title-cloud'>Key:</span>
  194. <Input style={{width: 250}} value={this.state.aliKey} onChange={this.inputChange('awsKey')}/>
  195. </div>
  196. {
  197. this.state.showOK === 'amazon' ?
  198. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  199. :
  200. this.state.showNotOK === 'amazon' ?
  201. <span>not ok</span>
  202. :
  203. ""
  204. }
  205. </div>
  206. <Button type='primary'
  207. onClick={this.submit(this.state.awsCLoudID, 'amazon', this.state.awsID, this.state.awsKey)}>ok</Button>
  208. </div>
  209. </div>
  210. </div>
  211. :
  212. <Spin style={{marginLeft: 3}}/>
  213. )
  214. }
  215. }
  216. export default CloudConfig;