DeployCard.js 14 KB

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