awsLambdaCloudformation.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import debounce from 'lodash/debounce';
  4. import * as qs from 'query-string';
  5. import {addErrorMessage, addLoadingMessage} from 'app/actionCreators/indicator';
  6. import Button from 'app/components/actions/button';
  7. import List from 'app/components/list';
  8. import ListItem from 'app/components/list/listItem';
  9. import {t} from 'app/locale';
  10. import {Organization} from 'app/types';
  11. import {uniqueId} from 'app/utils/guid';
  12. import {trackIntegrationEvent} from 'app/utils/integrationUtil';
  13. import SelectField from 'app/views/settings/components/forms/selectField';
  14. import TextField from 'app/views/settings/components/forms/textField';
  15. import FooterWithButtons from './components/footerWithButtons';
  16. import HeaderWithHelp from './components/headerWithHelp';
  17. // let the browser generate and store the external ID
  18. // this way the same user always has the same external ID if they restart the pipeline
  19. const ID_NAME = 'AWS_EXTERNAL_ID';
  20. const getAwsExternalId = () => {
  21. let awsExternalId = window.localStorage.getItem(ID_NAME);
  22. if (!awsExternalId) {
  23. awsExternalId = uniqueId();
  24. window.localStorage.setItem(ID_NAME, awsExternalId);
  25. }
  26. return awsExternalId;
  27. };
  28. const accountNumberRegex = /^\d{12}$/;
  29. const testAccountNumber = (arn: string) => accountNumberRegex.test(arn);
  30. type Props = {
  31. baseCloudformationUrl: string;
  32. templateUrl: string;
  33. stackName: string;
  34. regionList: string[];
  35. initialStepNumber: number;
  36. organization: Organization;
  37. accountNumber?: string;
  38. region?: string;
  39. error?: string;
  40. awsExternalId?: string;
  41. };
  42. type State = {
  43. awsExternalId?: string;
  44. accountNumber?: string;
  45. region?: string;
  46. accountNumberError?: string;
  47. submitting?: boolean;
  48. showInputs?: boolean;
  49. };
  50. export default class AwsLambdaCloudformation extends React.Component<Props, State> {
  51. state: State = {
  52. accountNumber: this.props.accountNumber,
  53. region: this.props.region,
  54. awsExternalId: this.props.awsExternalId ?? getAwsExternalId(),
  55. showInputs: !!this.props.awsExternalId,
  56. };
  57. componentDidMount() {
  58. // show the error if we have it
  59. const {error} = this.props;
  60. if (error) {
  61. addErrorMessage(error, {duration: 10000});
  62. }
  63. }
  64. get initialData() {
  65. const {region, accountNumber} = this.props;
  66. const {awsExternalId} = this.state;
  67. return {
  68. awsExternalId,
  69. region,
  70. accountNumber,
  71. };
  72. }
  73. get cloudformationUrl() {
  74. // generarate the cloudformation URL using the params we get from the server
  75. // and the external id we generate
  76. const {baseCloudformationUrl, templateUrl, stackName} = this.props;
  77. //always us the generated AWS External ID in local storage
  78. const awsExternalId = getAwsExternalId();
  79. const query = qs.stringify({
  80. templateURL: templateUrl,
  81. stackName,
  82. param_ExternalId: awsExternalId,
  83. });
  84. return `${baseCloudformationUrl}?${query}`;
  85. }
  86. get regionOptions() {
  87. return this.props.regionList.map(region => ({value: region, label: region}));
  88. }
  89. handleSubmit = (e: React.MouseEvent) => {
  90. this.setState({submitting: true});
  91. e.preventDefault();
  92. //use the external ID from the form on on the submission
  93. const {accountNumber, region, awsExternalId} = this.state;
  94. const data = {
  95. accountNumber,
  96. region,
  97. awsExternalId,
  98. };
  99. addLoadingMessage(t('Submitting\u2026'));
  100. const {
  101. location: {origin},
  102. } = window;
  103. // redirect to the extensions endpoint with the form fields as query params
  104. // this is needed so we don't restart the pipeline loading from the original
  105. // OrganizationIntegrationSetupView route
  106. const newUrl = `${origin}/extensions/aws_lambda/setup/?${qs.stringify(data)}`;
  107. window.location.assign(newUrl);
  108. };
  109. validateAccountNumber = (value: string) => {
  110. // validate the account number
  111. let accountNumberError = '';
  112. if (!value) {
  113. accountNumberError = t('Account number required');
  114. } else if (!testAccountNumber(value)) {
  115. accountNumberError = t('Invalid account number');
  116. }
  117. this.setState({accountNumberError});
  118. };
  119. handleChangeArn = (accountNumber: string) => {
  120. this.debouncedTrackValueChanged('accountNumber');
  121. // reset the error if we ever get a valid account number
  122. if (testAccountNumber(accountNumber)) {
  123. this.setState({accountNumberError: ''});
  124. }
  125. this.setState({accountNumber});
  126. };
  127. hanldeChangeRegion = (region: string) => {
  128. this.debouncedTrackValueChanged('region');
  129. this.setState({region});
  130. };
  131. handleChangeExternalId = (awsExternalId: string) => {
  132. this.debouncedTrackValueChanged('awsExternalId');
  133. awsExternalId = awsExternalId.trim();
  134. this.setState({awsExternalId});
  135. };
  136. handleChangeShowInputs = () => {
  137. this.setState({showInputs: true});
  138. trackIntegrationEvent(
  139. 'integrations.installation_input_value_changed',
  140. {
  141. integration: 'aws_lambda',
  142. integration_type: 'first_party',
  143. field_name: 'showInputs',
  144. },
  145. this.props.organization
  146. );
  147. };
  148. get formValid() {
  149. const {accountNumber, region, awsExternalId} = this.state;
  150. return !!region && testAccountNumber(accountNumber || '') && !!awsExternalId;
  151. }
  152. //debounce so we don't send a request on every input change
  153. debouncedTrackValueChanged = debounce((fieldName: string) => {
  154. trackIntegrationEvent(
  155. 'integrations.installation_input_value_changed',
  156. {
  157. integration: 'aws_lambda',
  158. integration_type: 'first_party',
  159. field_name: fieldName,
  160. },
  161. this.props.organization
  162. );
  163. }, 200);
  164. trackOpenCloudFormation = () => {
  165. trackIntegrationEvent(
  166. 'integrations.cloudformation_link_clicked',
  167. {
  168. integration: 'aws_lambda',
  169. integration_type: 'first_party',
  170. },
  171. this.props.organization
  172. );
  173. };
  174. render = () => {
  175. const {initialStepNumber} = this.props;
  176. const {
  177. accountNumber,
  178. region,
  179. accountNumberError,
  180. submitting,
  181. awsExternalId,
  182. showInputs,
  183. } = this.state;
  184. return (
  185. <React.Fragment>
  186. <HeaderWithHelp docsUrl="https://docs.sentry.io/product/integrations/aws-lambda/" />
  187. <StyledList symbol="colored-numeric" initialCounterValue={initialStepNumber}>
  188. <ListItem>
  189. <h3>{t("Add Sentry's CloudFormation")}</h3>
  190. <StyledButton
  191. priority="primary"
  192. onClick={this.trackOpenCloudFormation}
  193. external
  194. href={this.cloudformationUrl}
  195. >
  196. {t('Go to AWS')}
  197. </StyledButton>
  198. {!showInputs && (
  199. <React.Fragment>
  200. <p>
  201. {t(
  202. "Once you've created Sentry's CloudFormation stack (or if you already have one) press the button below to continue."
  203. )}
  204. </p>
  205. <Button name="showInputs" onClick={this.handleChangeShowInputs}>
  206. {t("I've created the stack")}
  207. </Button>
  208. </React.Fragment>
  209. )}
  210. </ListItem>
  211. {showInputs ? (
  212. <ListItem>
  213. <h3>{t('Add AWS Account Information')}</h3>
  214. <TextField
  215. name="accountNumber"
  216. value={accountNumber}
  217. onChange={this.handleChangeArn}
  218. onBlur={this.validateAccountNumber}
  219. error={accountNumberError}
  220. inline={false}
  221. stacked
  222. label={t('AWS Account Number')}
  223. showHelpInTooltip
  224. help={t(
  225. 'Your account number can be found on the right side of the header in AWS'
  226. )}
  227. />
  228. <SelectField
  229. name="region"
  230. value={region}
  231. onChange={this.hanldeChangeRegion}
  232. options={this.regionOptions}
  233. allowClear={false}
  234. inline={false}
  235. stacked
  236. label={t('AWS Region')}
  237. showHelpInTooltip
  238. help={t(
  239. 'Your current region can be found on the right side of the header in AWS'
  240. )}
  241. />
  242. <TextField
  243. name="awsExternalId"
  244. value={awsExternalId}
  245. onChange={this.handleChangeExternalId}
  246. inline={false}
  247. stacked
  248. error={awsExternalId ? '' : t('External ID Required')}
  249. label={t('External ID')}
  250. showHelpInTooltip
  251. help={t(
  252. 'Do not edit unless you are copying from a previously created CloudFormation stack'
  253. )}
  254. />
  255. </ListItem>
  256. ) : (
  257. <React.Fragment />
  258. )}
  259. </StyledList>
  260. <FooterWithButtons
  261. buttonText={t('Next')}
  262. onClick={this.handleSubmit}
  263. disabled={submitting || !this.formValid}
  264. />
  265. </React.Fragment>
  266. );
  267. };
  268. }
  269. const StyledList = styled(List)`
  270. padding: 100px 50px 50px 50px;
  271. `;
  272. const StyledButton = styled(Button)`
  273. margin-bottom: 20px;
  274. `;