Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history


Change-Id: I1419e103db913b14687b6a223c775bebe7d2ddb1
  • Loading branch information
mtlljm committed Nov 3, 2023
2 parents d7fb33b + 3d85621 commit 126fa98
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 98 deletions.
2 changes: 1 addition & 1 deletion docker/dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM grafana/grafana:10.0.4-ubuntu
FROM grafana/grafana:9.5.2-ubuntu

USER root
WORKDIR /root
Expand Down
180 changes: 83 additions & 97 deletions src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,97 @@
SPDX-License-Identifier: Apache-2.0
*/

import React, {PureComponent} from 'react';
import {Checkbox, InlineLabel, Select, VerticalGroup, HorizontalGroup} from '@grafana/ui';
import React, {useEffect, useMemo, useRef} from 'react';
import {Checkbox, HorizontalGroup, InlineLabel, Select, VerticalGroup} from '@grafana/ui';
import {DataSourcePluginOptionsEditorProps, SelectableValue} from '@grafana/data';
import { EspDataSourceOptions } from '../types';
import {EspDataSourceOptions} from '../types';

interface Props extends DataSourcePluginOptionsEditorProps<EspDataSourceOptions> {}
ConfigEditor.DISCOVERY_DEFAULT_OPTIONS = [
{label: 'SAS Event Stream Manager', value: 'http://sas-event-stream-manager-app/SASEventStreamManager'},
{label: 'SAS Event Stream Processing Studio', value: 'http://sas-event-stream-processing-studio-app/SASEventStreamProcessingStudio'},
];

interface State {
selectedOption: SelectableValue<string> | undefined;
customOptions: Array<SelectableValue<string>>;
}
ConfigEditor.DISCOVERY_DEFAULT_VIYA_OPTIONS = [
{label: 'SAS Event Stream Manager', value: 'https://sas-event-stream-manager-app/SASEventStreamManager'},
{label: 'SAS Event Stream Processing Studio', value: 'https://sas-event-stream-processing-studio-app/SASEventStreamProcessingStudio'},
];

export function ConfigEditor({options, onOptionsChange}: DataSourcePluginOptionsEditorProps<EspDataSourceOptions>) {
const {jsonData} = options;

const getDiscoveryOptions = (isViya: boolean) => isViya ? ConfigEditor.DISCOVERY_DEFAULT_VIYA_OPTIONS : ConfigEditor.DISCOVERY_DEFAULT_OPTIONS;

const deriveSelectedOptionFromUrl = (discoveryServiceUrl: string | null, discoveryOptions: Array<SelectableValue<string>>) => {
if (!discoveryServiceUrl) {
return null;
}

const matchingOption = discoveryOptions.find(option => option.value === discoveryServiceUrl);
return matchingOption ? matchingOption : {value: discoveryServiceUrl, label: discoveryServiceUrl};
}

const changePropOptions = (change: Object) => {
const newOptions = {...options, ...change};
onOptionsChange(newOptions);
}

const changePropOptionsJsonData = (change: Object) => {
const newJsonData = {...jsonData, ...change};
changePropOptions({jsonData: newJsonData});
}

export class ConfigEditor extends PureComponent<Props> {
private static DISCOVERY_URL_DEFAULT_STUDIO = 'http://sas-event-stream-processing-studio-app/SASEventStreamProcessingStudio';
private static DISCOVERY_URL_DEFAULT_ESM = 'http://sas-event-stream-manager-app/SASEventStreamManager';
private static DISCOVERY_DEFAULT_OPTIONS = [
{label: 'SAS Event Stream Manager', value: ConfigEditor.DISCOVERY_URL_DEFAULT_ESM},
{label: 'SAS Event Stream Processing Studio', value: ConfigEditor.DISCOVERY_URL_DEFAULT_STUDIO},
];

state: State;

constructor(props: Props) {
super(props);

this.props.options.jsonData.oauthPassThru = true;

this.state = {
selectedOption: undefined,
customOptions: [],
};

let discoveryServiceUrl = this.getDiscoveryServiceUrlFromProps();
if (!discoveryServiceUrl) {
this.state.selectedOption = ConfigEditor.DISCOVERY_DEFAULT_OPTIONS[0];
} else {
const matchingOption = ConfigEditor.DISCOVERY_DEFAULT_OPTIONS.find(option => option.value === discoveryServiceUrl);
if (matchingOption) {
this.state.selectedOption = matchingOption;
} else {
const customOption: SelectableValue<string> = {value: discoveryServiceUrl, label: discoveryServiceUrl};
this.state.customOptions = [...this.state.customOptions, customOption];
this.state.selectedOption = customOption;
}
const handleDiscoveryServiceProviderChange = (selectedOption: SelectableValue<string>) => {
changePropOptions({url: selectedOption.value});
}
this.setDiscoveryServiceUrlInProps(this.state.selectedOption.value ?? "");

this.handleDiscoveryServiceProviderChange = this.handleDiscoveryServiceProviderChange.bind(this);
}

private handleDiscoveryServiceProviderChange(selectedOption: SelectableValue<string>) {
this.setState({selectedOption: selectedOption});
this.setDiscoveryServiceUrlInProps(selectedOption.value ?? "");
}

private setDiscoveryServiceUrlInProps(newUrl: string) {
this.props.options.url = newUrl;
this.props.onOptionsChange(this.props.options);
}

private getDiscoveryServiceUrlFromProps(): string | undefined {
return this.props.options.url;
}

private handleTlsSkipVerifyCheckboxChange(e: React.ChangeEvent<HTMLInputElement>) {
this.changePropOptionsJsonData({tlsSkipVerify: e.currentTarget.checked});
}

private changePropOptionsJsonData(change: Object) {
const newJsonData = {
...this.props.options.jsonData,
...change
};

this.props.onOptionsChange({
...this.props.options,
jsonData: newJsonData
});
}

render() {

const handleTlsSkipVerifyCheckboxChange = (checked: boolean) => {
changePropOptionsJsonData({tlsSkipVerify: checked});
}

const handleViyaCheckboxChange = (checked: boolean) => {
const isViya = checked;
// Grafana will ignore attempts to reset datasource URLs and will revert to the previously saved value upon a future save, rather than persist a falsy URL.
// To prevent this unwanted behaviour, a default URL is chosen to override the existing URL if possible.
const defaultUrl = getDiscoveryOptions(isViya)?.at(0)?.value;
changePropOptions({
url: defaultUrl ?? "",
jsonData: {...jsonData, isViya: isViya}
});
}

const mountEffectRefIsViya = useRef(jsonData.isViya);
const mountEffectRefChangePropOptionsJsonData = useRef(changePropOptionsJsonData);
useEffect(() => {
const isViya = mountEffectRefIsViya.current;
const changePropOptionsJsonData = mountEffectRefChangePropOptionsJsonData.current;

changePropOptionsJsonData({oauthPassThru: true});

if (isViya == null) {
(async () => {
let isViya: boolean = await fetch(`${window.location.origin}/SASLogon/`).then((response) => response.ok, () => false);
changePropOptionsJsonData({isViya: isViya});
})();
}
}, []);

const discoveryOptions = getDiscoveryOptions(jsonData.isViya);
const selectedDiscoveryOption = useMemo(() => deriveSelectedOptionFromUrl(options.url, discoveryOptions), [options.url, discoveryOptions]);

return (
<VerticalGroup>
<HorizontalGroup>
<InlineLabel width="auto">
Discovery service provider
</InlineLabel>
<Select
options={[...ConfigEditor.DISCOVERY_DEFAULT_OPTIONS, ...this.state.customOptions]}
value={this.state.selectedOption}
allowCustomValue
onCreateOption={customValue => {
const customOption: SelectableValue<string> = {value: customValue, label: customValue};
this.setState( (prevState: State) => {
return {
customOptions: [...prevState.customOptions, customOption],
};
});
this.handleDiscoveryServiceProviderChange(customOption);
}}
onChange={this.handleDiscoveryServiceProviderChange}
<HorizontalGroup>
<InlineLabel width="auto">Discovery service provider</InlineLabel>
<Checkbox label="Viya" value={jsonData.isViya ?? false} onChange={e => handleViyaCheckboxChange(e.currentTarget.checked)}/>
<Select key={`${jsonData.isViya}`}
options={discoveryOptions} value={selectedDiscoveryOption}
allowCustomValue onCreateOption={customValue => handleDiscoveryServiceProviderChange({value: customValue, label: customValue})}
onChange={handleDiscoveryServiceProviderChange}
/>
</HorizontalGroup>
<Checkbox label="(Insecure) Skip TLS certificate verification" value={jsonData.tlsSkipVerify ?? false}
onChange={e => handleTlsSkipVerifyCheckboxChange(e.currentTarget.checked)}
/>
</HorizontalGroup>
<Checkbox label="(Insecure) Skip TLS certificate verification" value={this.props.options.jsonData.tlsSkipVerify} onChange={this.handleTlsSkipVerifyCheckboxChange}/>
</VerticalGroup>
);
}
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ export function isField(espObject: EspObject | undefined): espObject is Field {
export interface EspDataSourceOptions extends DataSourceJsonData {
oauthPassThru: boolean;
tlsSkipVerify: boolean;
isViya: boolean;
}

0 comments on commit 126fa98

Please sign in to comment.