import { ChangeEvent } from 'react'; import { AzureCredentials, AzureAuthType } from '@grafana/azure-sdk'; import { SelectableValue } from '@grafana/data'; import { Button, Field, Select, Input } from '@grafana/ui/src/components'; export interface Props { managedIdentityEnabled: boolean; azureEntraPasswordCredentialsEnabled: boolean; credentials: AzureCredentials; azureCloudOptions?: SelectableValue[]; onCredentialsChange: (updatedCredentials: AzureCredentials) => void; disabled?: boolean; } export const AzureCredentialsForm = (props: Props) => { const { managedIdentityEnabled, azureEntraPasswordCredentialsEnabled, credentials, azureCloudOptions, onCredentialsChange, disabled, } = props; const onAuthTypeChange = (selected: SelectableValue) => { if (onCredentialsChange) { const updated: AzureCredentials = { ...credentials, authType: selected.value || 'msi', }; onCredentialsChange(updated); } }; const onAzureCloudChange = (selected: SelectableValue) => { if (credentials.authType === 'clientsecret') { const updated: AzureCredentials = { ...credentials, azureCloud: selected.value, }; onCredentialsChange(updated); } }; const onTenantIdChange = (event: ChangeEvent) => { if (credentials.authType === 'clientsecret') { const updated: AzureCredentials = { ...credentials, tenantId: event.target.value, }; onCredentialsChange(updated); } }; const onClientIdChange = (event: ChangeEvent) => { if (credentials.authType === 'clientsecret' || credentials.authType === 'ad-password') { const updated: AzureCredentials = { ...credentials, clientId: event.target.value, }; onCredentialsChange(updated); } }; const onClientSecretChange = (event: ChangeEvent) => { if (credentials.authType === 'clientsecret') { const updated: AzureCredentials = { ...credentials, clientSecret: event.target.value, }; onCredentialsChange(updated); } }; const onClientSecretReset = () => { if (credentials.authType === 'clientsecret') { const updated: AzureCredentials = { ...credentials, clientSecret: '', }; onCredentialsChange(updated); } }; const onUserIdChange = (event: ChangeEvent) => { if (credentials.authType === 'ad-password') { const updated: AzureCredentials = { ...credentials, userId: event.target.value, }; onCredentialsChange(updated); } }; const onPasswordChange = (event: ChangeEvent) => { if (credentials.authType === 'ad-password') { const updated: AzureCredentials = { ...credentials, password: event.target.value, }; onCredentialsChange(updated); } }; const onPasswordReset = () => { if (credentials.authType === 'ad-password') { const updated: AzureCredentials = { ...credentials, password: '', }; onCredentialsChange(updated); } }; const authTypeOptions: Array> = [ { value: 'clientsecret', label: 'App Registration', }, ]; if (managedIdentityEnabled) { authTypeOptions.push({ value: 'msi', label: 'Managed Identity', }); } if (azureEntraPasswordCredentialsEnabled) { authTypeOptions.push({ value: 'ad-password', label: 'Azure Entra Password', }); } return (
opt.value === credentials.azureCloud)} options={azureCloudOptions} onChange={onAzureCloudChange} isDisabled={disabled} inputId="azure-cloud-type" aria-label="Azure Cloud" width={20} /> )} {!disabled && (typeof credentials.clientSecret === 'symbol' ? (
) : ( ))} )} {credentials.authType === 'ad-password' && azureEntraPasswordCredentialsEnabled && ( <> {!disabled && (typeof credentials.password === 'symbol' ? (
) : ( ))} )}
); }; export default AzureCredentialsForm;