DeployCardRender.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. import React, {Component} from 'react';
  2. import {FormattedMessage} from 'react-intl';
  3. import {Input, Radio, Collapse, Button, Icon, Tooltip} from 'antd';
  4. import {idGen} from "../../../../../../func";
  5. import {graphqlUrl, manageUsers} from "../../../../../../config";
  6. import {
  7. ADD_DEPLOY,
  8. UPDATE_DEPLOY,
  9. UPDATE_PROJECT_DEPLOY_AND_CLOUD,
  10. UPDATE_PROJECT_ONLY_STATUS
  11. } from "../../../../../../gql";
  12. import {request} from 'graphql-request'
  13. import {removePrefix, shiftPrefix, pushPostfix} from "../../../../../../func";
  14. const Panel = Collapse.Panel;
  15. // 如需改变显示,请在此处 value 处更改
  16. // 如需添加中文,请在此处 value 处添加
  17. // eg: 'xxxx': <F.. id='xxx'/>
  18. const valueToKey = {
  19. 'functionName': 'functionName',
  20. 'cosBucketName': 'cosBucketName',
  21. 'cosObjectName': 'cosObjectName',
  22. 'cosBucketRegion': 'cosBucketRegion',
  23. 'serviceName': 'serviceName',
  24. 'subnetId': 'subnetId',
  25. 'vpcId': 'vpcId',
  26. 'region': 'region',
  27. 'description': 'description',
  28. };
  29. const toolTipTitle = {
  30. 'functionName': 'its funcname',
  31. 'cosBucketName': 'its cosBucketName',
  32. 'cosObjectName': 'its cosObjectName',
  33. 'cosBucketRegion': 'its cosBucketRegion',
  34. 'serviceName': 'its serviceName',
  35. 'subnetId': 'its subnetId',
  36. 'vpcId': 'its vpcId',
  37. 'region': 'its region',
  38. 'description': 'its description',
  39. };
  40. const youMustFill = {
  41. 'functionName': true,
  42. 'cosBucketName': true,
  43. 'cosObjectName': true,
  44. 'cosBucketRegion': true,
  45. 'serviceName': true,
  46. 'subnetId': false,
  47. 'vpcId': false,
  48. 'region': true,
  49. 'description': false,
  50. };
  51. class DeployCardRender extends Component {
  52. constructor(props) {
  53. super(props);
  54. if(props.deploy !== '' && props.deploy !== null) {
  55. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = props.deploy;
  56. this.state = {
  57. configs: ['description', 'vpcId', 'subnetId'],
  58. description,
  59. showOK: false,
  60. functionName,
  61. region,
  62. cosBucketName,
  63. cosObjectName: removePrefix('.jar', cosObjectName),
  64. cosBucketRegion,
  65. serviceName,
  66. vpcId,
  67. subnetId
  68. }
  69. } else {
  70. this.state = {
  71. configs: ['description', 'vpcId', 'subnetId'],
  72. description: '',
  73. showOK: false,
  74. functionName: props.defalutName,
  75. region: props.region === '' ? 'ap-beijing' : props.region,
  76. cosBucketName: 'graphqlfc',
  77. cosObjectName: props.defalutName,
  78. cosBucketRegion: props.region === '' ? 'ap-beijing' : props.region,
  79. serviceName: '',
  80. vpcId: '',
  81. subnetId: '',
  82. };
  83. }
  84. }
  85. componentWillReceiveProps(next) {
  86. if (next.deploy !== '' && next.deploy !== null) {
  87. let {functionName, cosBucketName, cosObjectName, cosBucketRegion, serviceName, vpcId, subnetId, region, description} = next.deploy;
  88. this.setState({
  89. description,
  90. functionName,
  91. region: next.region === '' ? region : next.region,
  92. cosBucketName,
  93. cosObjectName: removePrefix('.jar', cosObjectName),
  94. cosBucketRegion: next.region === '' ? cosBucketRegion : next.region,
  95. serviceName,
  96. vpcId,
  97. subnetId
  98. })
  99. } else {
  100. this.setState({
  101. functionName: next.defalutName,
  102. region: next.region === '' ? 'ap-beijing' : next.region,
  103. cosBucketName: 'graphqlfc',
  104. cosObjectName: next.defalutName,
  105. cosBucketRegion: next.region === '' ? 'ap-beijing' : next.region,
  106. serviceName: '',
  107. vpcId: '',
  108. subnetId: ''
  109. });
  110. }
  111. };
  112. switchConfig = (label) => {
  113. return (e) => {
  114. this.setState({
  115. [label]: e.target.value
  116. })
  117. };
  118. };
  119. ok = (id, userID, projectID, cloudID, deploy, projectStatus) => {
  120. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  121. let varObj = {
  122. id,
  123. cloud_id: cloudID,
  124. user_id: userID,
  125. cosObjectName: pushPostfix('.jar', cosObjectName),
  126. region: shiftPrefix('ap-', region),
  127. cosBucketRegion: shiftPrefix('ap-', cosBucketRegion),
  128. description,
  129. cosBucketName,
  130. vpcId,
  131. subnetId,
  132. functionName,
  133. memorySize: 512,
  134. timeout: 300,
  135. handler: 'tencent_graphql.Bridge::handler',
  136. serviceName: "",
  137. fc_id: '',
  138. createdAt: new Date().getTime(),
  139. updatedAt: ''
  140. };
  141. if (deploy === '' || deploy === null) {
  142. request(graphqlUrl, ADD_DEPLOY, varObj).then(
  143. data => {
  144. if (data.create_deploy !== null) {
  145. this.setState({
  146. showOK: true
  147. });
  148. // 写回 project
  149. request(graphqlUrl, UPDATE_PROJECT_DEPLOY_AND_CLOUD, {
  150. id: projectID,
  151. updatedAt: new Date().getTime(),
  152. deploy_id: id,
  153. cloud_id: cloudID,
  154. projectStatus: 'functioned'
  155. }).then(data => {
  156. if(data.update_project !== null)
  157. this.props.stepByStep(1);
  158. })
  159. }
  160. setTimeout(() => {
  161. this.setState({
  162. showOK: false
  163. })
  164. }, 1500)
  165. }
  166. )
  167. } else {
  168. let {description, cosBucketName, subnetId, cosObjectName, region, vpcId, cosBucketRegion, functionName} = this.state;
  169. let varObj = {
  170. id: deploy.id,
  171. cosObjectName: pushPostfix('.jar', cosObjectName),
  172. region: shiftPrefix('ap-', region),
  173. cosBucketRegion: shiftPrefix('ap-', cosBucketRegion),
  174. description,
  175. subnetId,
  176. cosBucketName,
  177. vpcId,
  178. functionName,
  179. updatedAt: new Date().getTime()
  180. };
  181. request(graphqlUrl, UPDATE_DEPLOY, varObj).then(
  182. data => {
  183. if (data.update_deploy !== null) {
  184. this.setState({
  185. showOK: true
  186. });
  187. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  188. id: projectID,
  189. updatedAt: new Date().getTime(),
  190. projectStatus: projectStatus === 'deployed' ? 'updated': 'functioned'
  191. }).then(data => {
  192. if(data.update_project !== null)
  193. this.props.stepByStep(1);
  194. })
  195. }
  196. setTimeout(() => {
  197. this.setState({
  198. showOK: false
  199. })
  200. }, 1500)
  201. }
  202. )
  203. }
  204. };
  205. render() {
  206. const customPanelStyle = {
  207. background: '#f7f7f7',
  208. borderRadius: 4,
  209. marginBottom: 24,
  210. border: 0,
  211. overflow: 'hidden',
  212. };
  213. const ioobotCloudID = ['tencent_CloudID', 'aliyun_CloudID'];
  214. const disable = {
  215. 'subnetId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
  216. 'vpcId': ioobotCloudID.includes(this.props.cloudID) ? true : '',
  217. };
  218. return (
  219. <div>
  220. <div style={{marginBottom: 10}}>
  221. <span className='vice-title'>
  222. {
  223. youMustFill['functionName']?
  224. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  225. :
  226. ''
  227. }
  228. <FormattedMessage id={valueToKey['functionName']}/>
  229. &nbsp;
  230. <Tooltip placement="top" title={toolTipTitle['functionName']}>
  231. <Icon type="question-circle"/>
  232. </Tooltip>
  233. </span>
  234. <Input value={this.state.functionName} style={{width: 400}}
  235. onChange={this.switchConfig('functionName')}/>
  236. </div>
  237. <div style={{marginBottom: 10}}>
  238. <span className='vice-title'>
  239. {
  240. youMustFill['region']?
  241. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  242. :
  243. ''
  244. }
  245. <FormattedMessage id={valueToKey['region']}/>
  246. &nbsp;
  247. <Tooltip placement="top" title={toolTipTitle['region']}>
  248. <Icon type="question-circle"/>
  249. </Tooltip>
  250. </span>
  251. <Radio.Group onChange={(e) => {
  252. this.props.switchRegion(e);
  253. }} defaultValue={this.state.region} value={removePrefix('ap-', this.state.region)}
  254. buttonStyle="solid">
  255. <Radio.Button value="guangzhou"><FormattedMessage id="Guangzhou"/></Radio.Button>
  256. <Radio.Button value="shanghai"><FormattedMessage id="Shanghai"/></Radio.Button>
  257. <Radio.Button value="beijing"><FormattedMessage id="Beijing"/></Radio.Button>
  258. <Radio.Button value="chengdu"><FormattedMessage id="Chengdu"/></Radio.Button>
  259. </Radio.Group>
  260. </div>
  261. <div style={{marginBottom: 10}}>
  262. <span className='vice-title'>
  263. {
  264. youMustFill['cosBucketRegion']?
  265. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  266. :
  267. ''
  268. }
  269. <FormattedMessage id={valueToKey['cosBucketRegion']}/>
  270. &nbsp;
  271. <Tooltip placement="top" title={toolTipTitle['cosBucketRegion']}>
  272. <Icon type="question-circle"/>
  273. </Tooltip>
  274. </span>
  275. <Radio.Group onChange={(e) => {
  276. // 如果不想一变所有都变,就将 onchange 改成
  277. // onChange={this.switchConfig('cosBucketRegion')}
  278. this.props.switchRegion(e);
  279. }} defaultValue={this.state.region}
  280. value={removePrefix('ap-', this.state.cosBucketRegion)} buttonStyle="solid">
  281. <Radio.Button value="guangzhou"><FormattedMessage id="Guangzhou"/></Radio.Button>
  282. <Radio.Button value="shanghai"><FormattedMessage id="Shanghai"/></Radio.Button>
  283. <Radio.Button value="beijing"><FormattedMessage id="Beijing"/></Radio.Button>
  284. <Radio.Button value="chengdu"><FormattedMessage id="Chengdu"/></Radio.Button>
  285. </Radio.Group>
  286. </div>
  287. <div key={'cosBucketName'} style={{marginBottom: 10}}>
  288. <span className='vice-title'>
  289. {
  290. youMustFill['cosBucketName']?
  291. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  292. :
  293. ''
  294. }
  295. <FormattedMessage id={valueToKey['cosBucketName']}/>
  296. &nbsp;
  297. <Tooltip placement="top" title={toolTipTitle['cosBucketName']}>
  298. <Icon type="question-circle"/>
  299. </Tooltip>
  300. </span>
  301. <Input value={this.state.cosBucketName} style={{width: 400}}
  302. disabled={disable['cosBucketName'] === true}
  303. onChange={this.switchConfig('cosBucketName')}/>
  304. </div>
  305. <div key={'cosObjectName'} style={{marginBottom: 10}}>
  306. <span className='vice-title'>
  307. {
  308. youMustFill['cosObjectName']?
  309. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  310. :
  311. ''
  312. }
  313. <FormattedMessage id={valueToKey['cosObjectName']}/>
  314. &nbsp;
  315. <Tooltip placement="top" title={toolTipTitle['cosObjectName']}>
  316. <Icon type="question-circle"/>
  317. </Tooltip>
  318. </span>
  319. <Input value={this.state.cosObjectName} style={{width: 400}}
  320. disabled={disable['cosObjectName'] === true} addonAfter=".jar"
  321. onChange={this.switchConfig('cosObjectName')}/>
  322. </div>
  323. <Collapse bordered={false}>
  324. <Panel header={<FormattedMessage id="Want more options?"/>} style={customPanelStyle}>
  325. {/*腾讯云为空字符串,不显示,这里未作区分*/}
  326. {/*<div style={{marginBottom: 10}}>*/}
  327. {/*<span className='vice-title'>{valueToKey['serviceName']}*/}
  328. {/*<span> </span>*/}
  329. {/*<Tooltip placement="top" title={toolTipTitle['serviceName']}>*/}
  330. {/*<Icon type="question-circle"/>*/}
  331. {/*</Tooltip>*/}
  332. {/*</span>*/}
  333. {/*<Input value={this.state.serviceName} style={{width: 400}} disabled*/}
  334. {/*onChange={this.switchConfig('serviceName')}/>*/}
  335. {/*</div>*/}
  336. {
  337. this.state.configs.map(config => (
  338. <div key={config} style={{marginBottom: 10}}>
  339. <span className='vice-title'>
  340. {
  341. youMustFill[config]?
  342. <span style={{color: 'red', display: 'inline', marginRight: 10}}>*</span>
  343. :
  344. ''
  345. }
  346. <FormattedMessage id={valueToKey[config]}/>
  347. &nbsp;
  348. <Tooltip placement="top" title={toolTipTitle[config]}>
  349. <Icon type="question-circle"/>
  350. </Tooltip>
  351. </span>
  352. <Input value={this.state[config]} style={{width: 400}}
  353. disabled={disable[config] === true}
  354. onChange={this.switchConfig(config)}/>
  355. </div>
  356. ))
  357. }
  358. </Panel>
  359. </Collapse>
  360. <div>
  361. {
  362. manageUsers.includes(this.props.userID) ?
  363. <div>
  364. <Button onClick={() => {
  365. const id = idGen('deploy');
  366. this.ok(id, this.props.userID, this.props.projectID, this.props.cloudID, this.props.deploy, this.props.projectStatus);
  367. }} type='primary'><FormattedMessage id="save"/></Button>
  368. {
  369. this.state.showOK === true ?
  370. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  371. :
  372. ''
  373. }
  374. </div>
  375. :
  376. this.props.trialcase ?
  377. ''
  378. :
  379. <div>
  380. <Button onClick={() => {
  381. const id = idGen('deploy');
  382. this.ok(id, this.props.userID, this.props.projectID, this.props.cloudID, this.props.deploy, this.props.projectStatus);
  383. }} type='primary'><FormattedMessage id="save"/></Button>
  384. {
  385. this.state.showOK === true ?
  386. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  387. :
  388. ''
  389. }
  390. </div>
  391. }
  392. </div>
  393. </div>
  394. )
  395. }
  396. }
  397. export default DeployCardRender;