Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from influxdata/fix/panic-validate
Browse files Browse the repository at this point in the history
fix: resolve panic where no metadata was entered
  • Loading branch information
helenosheaa authored Mar 15, 2023
2 parents 27b3f5b + 77d9314 commit 0fa82cc
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: CI
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ The plugin requires the user to run Grafana ^9.2.5.
Download the latest release:

```sh
$ curl -L https://github.com/influxdata/grafana-flightsql-datasource/releases/download/v0.1.5/influxdata-flightsql-datasource-0.1.5.zip
$ curl -L https://github.com/influxdata/grafana-flightsql-datasource/releases/download/v0.1.6/influxdata-flightsql-datasource-0.1.6.zip
```

Unzip the release into your Grafana plugins directory:

```
$ unzip influxdata-flightsql-datasource-0.1.5.zip -d grafana-plugins/
$ unzip influxdata-flightsql-datasource-0.1.6.zip -d grafana-plugins/
```

### Configuration
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flightsql-datasource",
"version": "0.1.5",
"version": "0.1.6",
"description": "",
"scripts": {
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
Expand Down
7 changes: 5 additions & 2 deletions pkg/flightsql/flightsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (cfg config) validate() error {
noToken := len(cfg.Token) == 0
noUserPass := len(cfg.Username) == 0 || len(cfg.Password) == 0

if noToken && noUserPass {
// if not secure don't make users supply a token
if noToken && noUserPass && cfg.Secure {
return fmt.Errorf("token or username/password are required")
}

Expand Down Expand Up @@ -99,7 +100,9 @@ func NewDatasource(settings backend.DataSourceInstanceSettings) (instancemgmt.In
}
authMD, _ := metadata.FromOutgoingContext(ctx)
md = metadata.Join(md, authMD)
} else {
}

if cfg.Token != "" {
md.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.Token))
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/flightsql/query_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func (d *FlightSQLDatasource) query(ctx context.Context, query sqlutil.Query) (r
}
}()

ctx = metadata.NewOutgoingContext(ctx, d.md)
if d.md.Len() != 0 {
ctx = metadata.NewOutgoingContext(ctx, d.md)
}

info, err := d.client.Execute(ctx, query.RawSQL)
if err != nil {
return backend.ErrDataResponse(backend.StatusInternal, fmt.Sprintf("flightsql: %s", err))
Expand Down
8 changes: 7 additions & 1 deletion src/components/BuilderView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ beforeAll(() => {
if (/Warning.*not wrapped in act/.test(args[0])) {
return
}
if (/Each child in a list should have a unique "key" prop/.test(args[0])) {
return
}
originalError.call(console, ...args)
}
})
Expand All @@ -20,7 +23,10 @@ afterAll(() => {
const setup = async (optionOverrides?: object) => {
const props = {
query: '',
datasource: '',
datasource: {
getTables: () => {},
getColumns: () => {}
},
onChange: () => {},
fromRawSql: true,
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/BuilderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ export function BuilderView({query, datasource, onChange, fromRawSql}: any) {
;(async () => {
const res = await datasource.getTables()

const dbSchemaArr = res.frames[0].data.values[1].map((t: string) => ({
const dbSchemaArr = res?.frames[0].data.values[1].map((t: string) => ({
dbSchema: t,
}))

const tableArr = res.frames[0].data.values[2].map((t: string) => ({
const tableArr = res?.frames[0].data.values[2].map((t: string) => ({
label: t,
value: t,
}))

const mergedArr = dbSchemaArr.map((obj: any, index: string | number) => ({
const mergedArr = dbSchemaArr?.map((obj: any, index: string | number) => ({
...obj,
...tableArr[index],
}))
Expand Down
21 changes: 12 additions & 9 deletions src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL
value: jsonData?.selectedAuthType,
label: jsonData?.selectedAuthType,
})
const existingMetastate = jsonData?.metadata?.map((m: any) => ({key: Object.keys(m)[0], value: Object.values(m)[0]}))
const existingMetastate = jsonData?.metadata?.length && jsonData?.metadata?.map((m: any) => ({key: Object.keys(m)[0], value: Object.values(m)[0]}))
const [metaDataArr, setMetaData] = useState(existingMetastate || [{key: '', value: ''}])
useEffect(() => {
onAuthTypeChange(selectedAuthType, options, onOptionsChange)
Expand All @@ -35,13 +35,16 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL

useEffect(() => {
const {onOptionsChange, options} = props
const mapData = metaDataArr?.map((m: any) => ({[m.key]: m.value}))
const jsonData = {
...options.jsonData,
metadata: mapData,
secure: true,
let mapData
let jsonData
if (metaDataArr[0]?.key !== '') {
mapData = metaDataArr?.map((m: any) => ({[m.key]: m.value}))
jsonData = {
...options.jsonData,
metadata: mapData,
}
onOptionsChange({...options, jsonData})
}
onOptionsChange({...options, jsonData})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [metaDataArr])

Expand Down Expand Up @@ -128,7 +131,7 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL
width={40}
name="key"
type="text"
value={metaDataArr[i].key || ''}
value={metaDataArr[i]?.key || ''}
placeholder="key"
onChange={(e) => onKeyChange(e, metaDataArr, i, setMetaData)}
></Input>
Expand All @@ -139,7 +142,7 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL
width={40}
name="value"
type="text"
value={metaDataArr[i].value || ''}
value={metaDataArr[i]?.value || ''}
placeholder="value"
onChange={(e) => onValueChange(e, metaDataArr, i, setMetaData)}
></Input>
Expand Down
2 changes: 1 addition & 1 deletion src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function QueryEditor(props: QueryEditorProps<FlightSQLDataSource, SQLQuer

const getTables = useCallback(async () => {
const res = await datasource.getTables()
return res.frames[0].data.values[2].map((t: string) => ({
return res?.frames[0].data.values[2].map((t: string) => ({
name: checkCasing(t),
}))
}, [datasource])
Expand Down

0 comments on commit 0fa82cc

Please sign in to comment.