Skip to content

Commit cff0dd4

Browse files
authored
CLI Panel respects disabled CLI option for Data Source (#87)
1 parent e88bce5 commit cff0dd4

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

src/redis-cli-panel/components/redis-cli-panel/redis-cli-panel.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,17 @@ const dataSourceMock = {
6868
),
6969
name: 'datasource',
7070
};
71+
const dataSourceInstanceSettingsMock = {
72+
jsonData: { cliDisabled: false },
73+
};
7174

7275
const dataSourceSrvGetMock = jest.fn().mockImplementation(() => Promise.resolve(dataSourceMock));
76+
const dataSourceSrvGetInstanceSettingsMock = jest.fn().mockImplementation(() => dataSourceInstanceSettingsMock);
7377

7478
jest.mock('@grafana/runtime', () => ({
7579
getDataSourceSrv: () => ({
7680
get: dataSourceSrvGetMock,
81+
getInstanceSettings: dataSourceSrvGetInstanceSettingsMock,
7782
}),
7883
}));
7984

@@ -552,6 +557,31 @@ describe('RedisCLIPanel', () => {
552557
});
553558
});
554559

560+
it('If CLI disabled should not display buttons', () => {
561+
const options = getOptions({
562+
output: 'custom-output',
563+
});
564+
const overrideData = {
565+
...data,
566+
request: { targets: [{ datasource: 'datasource/id' }] },
567+
};
568+
569+
dataSourceInstanceSettingsMock.jsonData.cliDisabled = true;
570+
const wrapper = shallow(
571+
<RedisCLIPanel
572+
{...additionalProps}
573+
width={width}
574+
height={height}
575+
data={overrideData}
576+
onOptionsChange={onOptionsChangeMock}
577+
replaceVariables={replaceVariablesMock}
578+
options={options}
579+
/>
580+
);
581+
const button = wrapper.find(Button);
582+
expect(button.exists()).toBeFalsy();
583+
});
584+
555585
afterAll(() => {
556586
jest.resetAllMocks();
557587
});

src/redis-cli-panel/components/redis-cli-panel/redis-cli-panel.tsx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { ChangeEvent } from 'react';
22
import { Observable } from 'rxjs';
33
import { map as map$, switchMap as switchMap$ } from 'rxjs/operators';
4+
import { RedisDataSourceOptions } from 'types';
45
import { css, cx } from '@emotion/css';
56
import { DataFrame, DataQueryRequest, DataQueryResponse, PanelProps } from '@grafana/data';
67
import { getDataSourceSrv } from '@grafana/runtime';
@@ -24,6 +25,15 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
2425
const { query, output, help } = options;
2526
const styles = Styles(useTheme2());
2627

28+
/**
29+
* Get configured Data Source Id
30+
*/
31+
const targets = data.request?.targets;
32+
let datasource = '';
33+
if (targets && targets.length && targets[0].datasource) {
34+
datasource = targets[0].datasource;
35+
}
36+
2737
/**
2838
* Run Query
2939
*
@@ -38,28 +48,19 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
3848
}
3949

4050
/**
41-
* Get configured Data Source Id
51+
* Check Data Source
4252
*/
43-
const targets = data.request?.targets;
44-
let datasource = '';
45-
if (targets && targets.length && targets[0].datasource) {
46-
datasource = targets[0].datasource;
47-
} else {
53+
if (!datasource) {
4854
return onOptionsChange({ ...options, output: 'Unknown Data Source' });
4955
}
5056

51-
/**
52-
* Get Data Source
53-
*/
54-
const ds = await getDataSourceSrv().get(datasource);
55-
5657
// Response Error
5758
let error = '';
5859

5960
/**
6061
* Run Query
6162
*/
62-
63+
const ds = await getDataSourceSrv().get(datasource);
6364
const dsQuery = ds.query({
6465
targets: [{ refId: 'A', query: replaceVariables(query), cli: !options.raw }],
6566
} as DataQueryRequest<RedisQuery>) as unknown;
@@ -142,6 +143,31 @@ export const RedisCLIPanel: React.FC<PanelProps<PanelOptions>> = ({
142143
onOptionsChange({ ...options, query: event.target.value, help });
143144
};
144145

146+
/**
147+
* Check if CLI disabled
148+
*/
149+
const jsonData = getDataSourceSrv().getInstanceSettings(datasource)?.jsonData as RedisDataSourceOptions;
150+
const cliDisabled = jsonData?.cliDisabled;
151+
152+
/**
153+
* CLI disabled
154+
*/
155+
if (cliDisabled) {
156+
return (
157+
<div
158+
className={cx(
159+
styles.wrapper,
160+
css`
161+
width: ${width}px;
162+
height: ${height}px;
163+
`
164+
)}
165+
>
166+
CLI is disabled for this Data Source.
167+
</div>
168+
);
169+
}
170+
145171
/**
146172
* Return
147173
*/

src/redis-cli-panel/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export enum ResponseMode {
3737
*/
3838
export const ResponseModeOptions = [
3939
{
40-
label: 'CLI',
40+
label: ResponseMode.CLI,
4141
value: ResponseMode.CLI,
4242
},
4343
{
44-
label: 'Raw',
44+
label: ResponseMode.RAW,
4545
value: ResponseMode.RAW,
4646
},
4747
];

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ export interface RedisDataSourceOptions extends DataSourceJsonData {
106106
*/
107107
acl: boolean;
108108

109+
/**
110+
* CLI disabled
111+
*
112+
* @type {boolean}
113+
*/
114+
cliDisabled: boolean;
115+
109116
/**
110117
* ACL Username
111118
*

0 commit comments

Comments
 (0)