import React, { ChangeEvent, FunctionComponent, useEffect, useReducer, useState } from 'react'; import { SelectableValue } from '@grafana/data'; import { InlineFormLabel, Button } from '@grafana/ui/src/components'; import { Input } from '@grafana/ui/src/components/Forms/Legacy/Input/Input'; import { Select } from '@grafana/ui/src/components/Forms/Legacy/Select/Select'; import { AzureAuthType, AzureCredentials, isCredentialsComplete } from './AzureCredentials'; export interface Props { managedIdentityEnabled: boolean; credentials: AzureCredentials; azureCloudOptions?: SelectableValue[]; onCredentialsChange: (updatedCredentials: AzureCredentials) => void; getSubscriptions?: () => Promise; } const authTypeOptions: Array> = [ { value: 'msi', label: 'Managed Identity', }, { value: 'clientsecret', label: 'App Registration', }, ]; export const AzureCredentialsForm: FunctionComponent = (props: Props) => { const { credentials, azureCloudOptions, onCredentialsChange, getSubscriptions } = props; const hasRequiredFields = isCredentialsComplete(credentials); const [subscriptions, setSubscriptions] = useState>>([]); const [loadSubscriptionsClicked, onLoadSubscriptions] = useReducer((val) => val + 1, 0); useEffect(() => { if (!getSubscriptions || !hasRequiredFields) { updateSubscriptions([]); return; } let canceled = false; getSubscriptions().then((result) => { if (!canceled) { updateSubscriptions(result, loadSubscriptionsClicked); } }); return () => { canceled = true; }; // This effect is intended to be called only once initially and on Load Subscriptions click // eslint-disable-next-line react-hooks/exhaustive-deps }, [loadSubscriptionsClicked]); const updateSubscriptions = (received: Array>, autoSelect = false) => { setSubscriptions(received); if (getSubscriptions) { if (autoSelect && !credentials.defaultSubscriptionId && received.length > 0) { // Selecting the default subscription if subscriptions received but no default subscription selected onSubscriptionChange(received[0]); } else if (credentials.defaultSubscriptionId) { const found = received.find((opt) => opt.value === credentials.defaultSubscriptionId); if (!found) { // Unselecting the default subscription if it isn't found among the received subscriptions onSubscriptionChange(undefined); } } } }; const onAuthTypeChange = (selected: SelectableValue) => { if (onCredentialsChange) { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, authType: selected.value || 'msi', defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onAzureCloudChange = (selected: SelectableValue) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, azureCloud: selected.value, defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onTenantIdChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, tenantId: event.target.value, defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onClientIdChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientId: event.target.value, defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onClientSecretChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientSecret: event.target.value, defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onClientSecretReset = () => { if (onCredentialsChange && credentials.authType === 'clientsecret') { setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientSecret: '', defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; const onSubscriptionChange = (selected: SelectableValue | undefined) => { if (onCredentialsChange) { const updated: AzureCredentials = { ...credentials, defaultSubscriptionId: selected?.value, }; onCredentialsChange(updated); } }; return (
{props.managedIdentityEnabled && (
Authentication opt.value === credentials.azureCloud)} options={azureCloudOptions} onChange={onAzureCloudChange} />
)}
Directory (tenant) ID
Application (client) ID
{typeof credentials.clientSecret === 'symbol' ? (
Client Secret
) : (
Client Secret
)} )} {getSubscriptions && ( <>
Default Subscription