| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  | import { css } from '@emotion/css'; | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  | import React, { useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { GrafanaTheme2 } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  | import { Alert, Button, ConfirmModal, useStyles2 } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | interface Props { | 
					
						
							|  |  |  |   onMigrate: () => void; | 
					
						
							| 
									
										
										
										
											2023-03-04 00:12:34 +08:00
										 |  |  |   apikeysCount: number; | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  |   disabled?: boolean; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-04 00:12:34 +08:00
										 |  |  | export const MigrateToServiceAccountsCard = ({ onMigrate, apikeysCount, disabled }: Props): JSX.Element => { | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  |   const [isModalOpen, setIsModalOpen] = useState(false); | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  |   const styles = useStyles2(getStyles); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  |   const docsLink = ( | 
					
						
							|  |  |  |     <a | 
					
						
							|  |  |  |       className="external-link" | 
					
						
							| 
									
										
										
										
											2023-03-01 23:34:53 +08:00
										 |  |  |       href="https://grafana.com/docs/grafana/latest/administration/api-keys/#migrate-api-keys-to-grafana-service-accounts" | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  |       target="_blank" | 
					
						
							|  |  |  |       rel="noopener noreferrer" | 
					
						
							|  |  |  |     > | 
					
						
							| 
									
										
										
										
											2023-03-01 23:34:53 +08:00
										 |  |  |       Find out more about the migration here. | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  |     </a> | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2023-03-04 00:12:34 +08:00
										 |  |  |   const migrationBoxDesc = ( | 
					
						
							|  |  |  |     <span> | 
					
						
							|  |  |  |       Are you sure you want to migrate all API keys to service accounts? {docsLink} | 
					
						
							|  |  |  |       <br>This hides the API keys tab.</br> | 
					
						
							|  |  |  |     </span> | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2022-07-22 00:56:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2023-03-04 00:12:34 +08:00
										 |  |  |     <> | 
					
						
							|  |  |  |       {apikeysCount > 0 && ( | 
					
						
							|  |  |  |         <Alert title="Switch from API keys to service accounts" severity="warning"> | 
					
						
							|  |  |  |           <div className={styles.text}> | 
					
						
							|  |  |  |             We will soon deprecate API keys. Each API key will be migrated into a service account with a token and will | 
					
						
							|  |  |  |             continue to work as they were. We encourage you to migrate your API keys to service accounts now. {docsLink} | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |           <div className={styles.actionRow}> | 
					
						
							|  |  |  |             <Button className={styles.actionButton} onClick={() => setIsModalOpen(true)}> | 
					
						
							|  |  |  |               Migrate all service accounts | 
					
						
							|  |  |  |             </Button> | 
					
						
							|  |  |  |             <ConfirmModal | 
					
						
							|  |  |  |               title={'Migrate API keys to service accounts'} | 
					
						
							|  |  |  |               isOpen={isModalOpen} | 
					
						
							|  |  |  |               body={migrationBoxDesc} | 
					
						
							|  |  |  |               confirmText={'Yes, migrate now'} | 
					
						
							|  |  |  |               onConfirm={onMigrate} | 
					
						
							|  |  |  |               onDismiss={() => setIsModalOpen(false)} | 
					
						
							|  |  |  |               confirmVariant="primary" | 
					
						
							|  |  |  |               confirmButtonVariant="primary" | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </Alert> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  |       {apikeysCount === 0 && ( | 
					
						
							|  |  |  |         <> | 
					
						
							|  |  |  |           <Alert title="No API keys found" severity="warning"> | 
					
						
							|  |  |  |             <div className={styles.text}> | 
					
						
							|  |  |  |               No API keys were found. If you reload the browser, this tab will be not available. If any API keys are | 
					
						
							|  |  |  |               created, this tab will appear again. | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </Alert> | 
					
						
							|  |  |  |         </> | 
					
						
							|  |  |  |       )} | 
					
						
							|  |  |  |     </> | 
					
						
							| 
									
										
										
										
											2022-06-15 20:59:40 +08:00
										 |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getStyles = (theme: GrafanaTheme2) => ({ | 
					
						
							|  |  |  |   text: css`
 | 
					
						
							|  |  |  |     margin-bottom: ${theme.spacing(2)}; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   actionRow: css`
 | 
					
						
							|  |  |  |     display: flex; | 
					
						
							|  |  |  |     align-items: center; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   actionButton: css`
 | 
					
						
							|  |  |  |     margin-right: ${theme.spacing(2)}; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  | }); |