diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_monitor/azure_monitor_datasource.ts b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_monitor/azure_monitor_datasource.ts index ae37a4a6c3a..3a201a468c8 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_monitor/azure_monitor_datasource.ts +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/azure_monitor/azure_monitor_datasource.ts @@ -56,7 +56,7 @@ export default class AzureMonitorDatasource extends DataSourceWithBackend = (props: Props) => { const primaryCredentials = useMemo(() => getCredentials(props.options), [props.options]); const logAnalyticsCredentials = useMemo(() => getLogAnalyticsCredentials(props.options), [props.options]); const subscriptionId = logAnalyticsCredentials - ? props.options.jsonData.logAnalyticsSubscriptionId - : props.options.jsonData.subscriptionId; + ? logAnalyticsCredentials.defaultSubscriptionId + : primaryCredentials.defaultSubscriptionId; const credentialsEnabled = primaryCredentials.authType === 'clientsecret'; @@ -99,18 +99,6 @@ export const AnalyticsConfig: FunctionComponent = (props: Props) => { setSameAsSwitched(true); }; - const onLogAnalyticsDefaultSubscriptionChange = (subscriptionId: string | undefined) => { - updateOptions((options) => { - return { - ...options, - jsonData: { - ...options.jsonData, - logAnalyticsSubscriptionId: subscriptionId || '', - }, - }; - }); - }; - const onDefaultWorkspaceChange = (selected: SelectableValue) => { updateOptions((options) => { return { @@ -157,9 +145,7 @@ export const AnalyticsConfig: FunctionComponent = (props: Props) => { )} diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.test.tsx b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.test.tsx index 7dc0dfa5975..52a403a246b 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.test.tsx +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.test.tsx @@ -11,8 +11,8 @@ const setup = (propsFunc?: (props: Props) => Props) => { tenantId: 'e7f3f661-a933-3h3f-0294-31c4f962ec48', clientId: '34509fad-c0r9-45df-9e25-f1ee34af6900', clientSecret: undefined, + defaultSubscriptionId: '44987801-6nn6-49he-9b2d-9106972f9789', }, - defaultSubscription: '44987801-6nn6-49he-9b2d-9106972f9789', azureCloudOptions: [ { value: 'azuremonitor', label: 'Azure' }, { value: 'govazuremonitor', label: 'Azure US Government' }, @@ -20,7 +20,6 @@ const setup = (propsFunc?: (props: Props) => Props) => { { value: 'chinaazuremonitor', label: 'Azure China' }, ], onCredentialsChange: jest.fn(), - onDefaultSubscriptionChange: jest.fn(), getSubscriptions: jest.fn(), }; diff --git a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.tsx b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.tsx index 6f07dcb3824..3ff9b32cc35 100644 --- a/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.tsx +++ b/public/app/plugins/datasource/grafana-azure-monitor-datasource/components/AzureCredentialsForm.tsx @@ -8,10 +8,8 @@ const { Select, Input } = LegacyForms; export interface Props { managedIdentityEnabled: boolean; credentials: AzureCredentials; - defaultSubscription?: string; azureCloudOptions?: SelectableValue[]; onCredentialsChange: (updatedCredentials: AzureCredentials) => void; - onDefaultSubscriptionChange?: (subscriptionId: string | undefined) => void; getSubscriptions?: () => Promise; } @@ -27,14 +25,7 @@ const authTypeOptions: Array> = [ ]; export const AzureCredentialsForm: FunctionComponent = (props: Props) => { - const { - credentials, - defaultSubscription, - azureCloudOptions, - onCredentialsChange, - onDefaultSubscriptionChange, - getSubscriptions, - } = props; + const { credentials, azureCloudOptions, onCredentialsChange, getSubscriptions } = props; const hasRequiredFields = isCredentialsComplete(credentials); const [subscriptions, setSubscriptions] = useState>>([]); @@ -59,15 +50,15 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const updateSubscriptions = (received: Array>) => { setSubscriptions(received); - if (onDefaultSubscriptionChange) { - if (!defaultSubscription && received.length > 0) { + if (getSubscriptions) { + if (!credentials.defaultSubscriptionId && received.length > 0) { // Setting the default subscription if subscriptions received but no default subscription selected - onDefaultSubscriptionChange(received[0].value); - } else if (defaultSubscription) { - const found = received.find((opt) => opt.value === defaultSubscription); + onSubscriptionChange(received[0]); + } else if (credentials.defaultSubscriptionId) { + const found = received.find((opt) => opt.value === credentials.defaultSubscriptionId); if (!found) { // Unsetting the default found if it isn't found among the received subscriptions - onDefaultSubscriptionChange(undefined); + onSubscriptionChange(undefined); } } } @@ -75,9 +66,11 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onAuthTypeChange = (selected: SelectableValue) => { if (onCredentialsChange) { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, authType: selected.value || 'msi', + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } @@ -85,9 +78,11 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onAzureCloudChange = (selected: SelectableValue) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, azureCloud: selected.value, + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } @@ -95,9 +90,11 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onTenantIdChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, tenantId: event.target.value, + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } @@ -105,9 +102,11 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onClientIdChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientId: event.target.value, + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } @@ -115,9 +114,11 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onClientSecretChange = (event: ChangeEvent) => { if (onCredentialsChange && credentials.authType === 'clientsecret') { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientSecret: event.target.value, + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } @@ -125,17 +126,23 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => const onClientSecretReset = () => { if (onCredentialsChange && credentials.authType === 'clientsecret') { + setSubscriptions([]); const updated: AzureCredentials = { ...credentials, clientSecret: '', + defaultSubscriptionId: undefined, }; onCredentialsChange(updated); } }; - const onSubscriptionChange = (selected: SelectableValue) => { - if (onDefaultSubscriptionChange) { - onDefaultSubscriptionChange(selected?.value); + const onSubscriptionChange = (selected: SelectableValue | undefined) => { + if (onCredentialsChange) { + const updated: AzureCredentials = { + ...credentials, + defaultSubscriptionId: selected?.value, + }; + onCredentialsChange(updated); } }; @@ -230,14 +237,18 @@ export const AzureCredentialsForm: FunctionComponent = (props: Props) => )} )} - {getSubscriptions && onDefaultSubscriptionChange && ( + {getSubscriptions && ( <>
Default Subscription