Skip to content

Commit

Permalink
Use @grafana/runtime instead of grafanaBootData (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
idastambuk authored Jul 13, 2023
1 parent 3b2a509 commit cd94e70
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 74 deletions.
2 changes: 1 addition & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default [
plugins: [
externals({
deps: true,
include: ['react', '@grafana/data', '@grafana/ui', 'lodash'],
include: ['react', '@grafana/data', '@grafana/ui', '@grafana/runtime', 'lodash'],
packagePath,
}),
resolve(),
Expand Down
70 changes: 17 additions & 53 deletions src/ConnectionConfig.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { AwsAuthDataSourceJsonData, AwsAuthDataSourceSecureJsonData, AwsAuthType } from './types';
import { ConnectionConfig, ConnectionConfigProps } from './ConnectionConfig';
import { config } from '@grafana/runtime';

const getProps = (propOverrides?: object) => {
const props: ConnectionConfigProps<AwsAuthDataSourceJsonData, AwsAuthDataSourceSecureJsonData> = {
Expand Down Expand Up @@ -51,19 +52,19 @@ const getProps = (propOverrides?: object) => {
return props;
};

const resetWindow = () => {
(window as any).grafanaBootData = {
settings: {
awsAllowedAuthProviders: [AwsAuthType.EC2IAMRole, AwsAuthType.Keys],
awsAssumeRoleEnabled: false,
},
};
};
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
config: {
awsAllowedAuthProviders: [AwsAuthType.EC2IAMRole, AwsAuthType.Keys],
awsAssumeRoleEnabled: false
},
}));

describe('ConnectionConfig', () => {
beforeEach(() => resetWindow());
afterEach(() => resetWindow());

beforeEach(() => {
config.awsAllowedAuthProviders = [AwsAuthType.EC2IAMRole, AwsAuthType.Keys];
config.awsAssumeRoleEnabled = false;
});
it('should use auth type from props if its set', async () => {
const onOptionsChange = jest.fn();
const props = getProps({ onOptionsChange, options: { jsonData: { authType: AwsAuthType.Keys } } });
Expand All @@ -79,11 +80,11 @@ describe('ConnectionConfig', () => {
render(<ConnectionConfig {...props} />);
await waitFor(() => expect(screen.getByTestId('connection-config')).toBeInTheDocument());

const config = props.options;
const optionsConfig = props.options;
expect(onOptionsChange).toHaveBeenCalledWith({
...config,
...optionsConfig,
jsonData: {
...config.jsonData,
...optionsConfig.jsonData,
authType: AwsAuthType.EC2IAMRole,
},
});
Expand Down Expand Up @@ -140,53 +141,16 @@ describe('ConnectionConfig', () => {
expect(screen.queryByText('Connection Details')).not.toBeInTheDocument();
});

it('should use default auth if awsAllowedAuthProviders was not found on window obj', async () => {
(window as any).grafanaBootData = {
settings: {},
};
const onOptionsChange = jest.fn();
const props = getProps({ onOptionsChange });
render(<ConnectionConfig {...props} />);
await waitFor(() => expect(screen.getByTestId('connection-config')).toBeInTheDocument());

const config = props.options;
expect(onOptionsChange).toHaveBeenCalledWith({
...config,
jsonData: {
...config.jsonData,
authType: AwsAuthType.Default,
},
});
});

it('should render assume role if awsAssumeRoleEnabled was not found on window obj', async () => {
(window as any).grafanaBootData = {
settings: {},
};
const props = getProps();
render(<ConnectionConfig {...props} />);
await waitFor(() => expect(screen.getByTestId('connection-config')).toBeInTheDocument());
expect(screen.queryByText('Assume Role ARN')).toBeInTheDocument();
});

it('should not render assume role if awsAssumeRoleEnabled was set to false', async () => {
(window as any).grafanaBootData = {
settings: {
awsAssumeRoleEnabled: false,
},
};
config.awsAssumeRoleEnabled = false;
const props = getProps();
render(<ConnectionConfig {...props} />);
await waitFor(() => expect(screen.getByTestId('connection-config')).toBeInTheDocument());
expect(screen.queryByText('Assume Role ARN')).not.toBeInTheDocument();
});

it('should render assume role if awsAssumeRoleEnabled was set to true', async () => {
(window as any).grafanaBootData = {
settings: {
awsAssumeRoleEnabled: true,
},
};
config.awsAssumeRoleEnabled = true;
const props = getProps();
render(<ConnectionConfig {...props} />);
await waitFor(() => expect(screen.getByTestId('connection-config')).toBeInTheDocument());
Expand Down
15 changes: 8 additions & 7 deletions src/ConnectionConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
onUpdateDatasourceJsonDataOption,
onUpdateDatasourceSecureJsonDataOption,
} from '@grafana/data';

import { config } from '@grafana/runtime';
import { standardRegions } from './regions';
import { AwsAuthDataSourceJsonData, AwsAuthDataSourceSecureJsonData, AwsAuthType } from './types';
import { awsAuthProviderOptions } from './providers';

export const DEFAULT_LABEL_WIDTH = 28;
const toOption = (value: string) => ({ value, label: value });

const isAwsAuthType = (value: any): value is AwsAuthType => {
return typeof value === 'string' && awsAuthProviderOptions.some((opt) => opt.value === value);
}
export interface ConnectionConfigProps<
J extends AwsAuthDataSourceJsonData = AwsAuthDataSourceJsonData,
S = AwsAuthDataSourceSecureJsonData
Expand All @@ -36,13 +38,12 @@ export const ConnectionConfig: FC<ConnectionConfigProps> = (props: ConnectionCon
if (profile === undefined) {
profile = options.database;
}

const settings = (window as any).grafanaBootData.settings;
const awsAssumeRoleEnabled = config.awsAssumeRoleEnabled
const awsAllowedAuthProviders = useMemo(
() => settings.awsAllowedAuthProviders ?? [AwsAuthType.Default, AwsAuthType.Keys, AwsAuthType.Credentials],
[settings.awsAllowedAuthProviders]
() => config.awsAllowedAuthProviders.filter(isAwsAuthType),
[]
);
const awsAssumeRoleEnabled = settings.awsAssumeRoleEnabled ?? true;

const currentProvider = awsAuthProviderOptions.find((p) => p.value === options.jsonData.authType);

Expand Down
27 changes: 14 additions & 13 deletions src/SIGV4ConnectionConfig.test.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
import React from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import { SIGV4ConnectionConfig } from 'SIGV4ConnectionConfig';
import { AwsAuthType } from 'types';
import { AwsAuthType } from './types';
import { SIGV4ConnectionConfig } from './SIGV4ConnectionConfig';
import {config} from '@grafana/runtime'

const resetWindow = () => {
(window as any).grafanaBootData = {
settings: {
awsAllowedAuthProviders: [AwsAuthType.Credentials, AwsAuthType.Keys],
awsAssumeRoleEnabled: true,
},
};
};
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
config: {
awsAllowedAuthProviders: [AwsAuthType.Credentials, AwsAuthType.Keys],
awsAssumeRoleEnabled: true,
},
}));

describe('SIGV4ConnectionConfig', () => {
beforeEach(() => {
config.awsAllowedAuthProviders = [AwsAuthType.Credentials, AwsAuthType.Keys];
config.awsAssumeRoleEnabled = true;
});
const setup = (onOptionsChange?: () => {}) => {
const props: DataSourcePluginOptionsEditorProps<any, any> = {
options: {
Expand Down Expand Up @@ -56,9 +60,6 @@ describe('SIGV4ConnectionConfig', () => {
render(<SIGV4ConnectionConfig {...props} />);
};

beforeEach(() => resetWindow());
afterEach(() => resetWindow());

it('should map incoming props correctly', () => {
setup();
expect(screen.getByText('Credentials file')).toBeInTheDocument();
Expand Down

0 comments on commit cd94e70

Please sign in to comment.