Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db-connection-ui): Allow Dynamic Big Query Edits #15185

Merged
merged 7 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,43 @@ interface FieldPropTypes {
sslForced?: boolean;
}

const CredentialsInfo = ({ changeMethods }: FieldPropTypes) => {
const [uploadOption, setUploadOption] = useState<number>(0);
const CredentialsInfo = ({ changeMethods, isEditMode, db }: FieldPropTypes) => {
const [uploadOption, setUploadOption] = useState<number>(
CredentialInfoOptions.copyPaste.valueOf(),
);
const [fileToUpload, setFileToUpload] = useState<string | null | undefined>(
null,
);
return (
<CredentialInfoForm>
<span className="label-select">
How do you want to enter service account credentials?
</span>
<Select
defaultValue={CredentialInfoOptions.jsonUpload}
style={{ width: '100%' }}
onChange={option => setUploadOption(option)}
>
<Select.Option value={CredentialInfoOptions.jsonUpload}>
Upload JSON file
</Select.Option>
<Select.Option value={CredentialInfoOptions.copyPaste}>
Copy and Paste JSON credentials
</Select.Option>
</Select>
{!isEditMode && (
<>
<span className="label-select">
How do you want to enter service account credentials?
</span>
<Select
defaultValue={uploadOption}
style={{ width: '100%' }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think preferred is to use css here.

onChange={option => setUploadOption(option)}
>
<Select.Option value={CredentialInfoOptions.jsonUpload}>
Upload JSON file
</Select.Option>

<Select.Option value={CredentialInfoOptions.copyPaste}>
Copy and Paste JSON credentials
</Select.Option>
</Select>
</>
)}
{uploadOption === CredentialInfoOptions.copyPaste ? (
<div className="input-container" onChange={changeMethods.onChange}>
<span className="label-select">Service Account</span>
<textarea className="input-form" name="encrypted_extra" />
<textarea
className="input-form"
name="encrypted_extra"
value={db?.parameters?.credentials_info}
/>
<span className="label-paste">
Copy and paste the entire service account .json file here
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,21 @@ function dbReducer(
case ActionType.fetched:
// convert all the keys in this payload into strings
// eslint-disable-next-line no-case-declarations
let extra_json = {
...JSON.parse(action.payload.extra || ''),
};
extra_json = {
...extra_json,
metadata_params: JSON.stringify(extra_json.metadata_params),
engine_params: JSON.stringify(extra_json.engine_params),
schemas_allowed_for_csv_upload: JSON.stringify(
extra_json.schemas_allowed_for_csv_upload,
),
};
let deserializeExtraJSON = {};
if (action.payload.extra) {
const extra_json = {
...JSON.parse(action.payload.extra || ''),
} as DatabaseObject['extra_json'];

deserializeExtraJSON = {
...JSON.parse(action.payload.extra || ''),
metadata_params: JSON.stringify(extra_json?.metadata_params),
engine_params: JSON.stringify(extra_json?.engine_params),
schemas_allowed_for_csv_upload: JSON.stringify(
extra_json?.schemas_allowed_for_csv_upload,
),
};
}

if (action.payload?.parameters?.query) {
// convert query into URI params string
Expand All @@ -229,11 +233,18 @@ function dbReducer(
).toString();
}

if (action.payload?.parameters?.credentials_info) {
// deserialize credentials info for big query editting
action.payload.parameters.credentials_info = JSON.stringify(
action.payload?.parameters.credentials_info,
);
}

return {
...action.payload,
engine: trimmedState.engine,
configuration_method: trimmedState.configuration_method,
extra_json,
extra_json: deserializeExtraJSON,
parameters: {
...action.payload.parameters,
query,
Expand Down Expand Up @@ -299,7 +310,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
availableDbs?.databases?.find(
(available: { engine: string | undefined }) =>
// TODO: we need a centralized engine in one place
available.engine === db?.engine || db?.backend,
available.engine === (isEditMode ? db?.backend : db?.engine),
) || {};

// Test Connection logic
Expand Down Expand Up @@ -442,7 +453,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const setDatabaseModel = (engine: string) => {
const isDynamic =
availableDbs?.databases.filter(
(db: DatabaseObject) => db.engine === engine,
(db: DatabaseObject) => db.engine || db.backend === engine,
)[0].parameters !== undefined;
setDB({
type: ActionType.dbSelected,
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/views/CRUD/data/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type DatabaseObject = {
username?: string;
password?: string;
encryption?: boolean;
credentials_info?: string;
query?: string | object;
};
configuration_method: CONFIGURATION_METHOD;
Expand Down Expand Up @@ -112,6 +113,11 @@ export type DatabaseForm = {
nullable: boolean;
type: string;
};
credentials_info: {
description: string;
nullable: boolean;
type: string;
};
};
required: string[];
type: string;
Expand Down
2 changes: 1 addition & 1 deletion superset/db_engine_specs/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

class BigQueryParametersSchema(Schema):
credentials_info = EncryptedField(
required=True, description="Contents of BigQuery JSON credentials.",
required=False, description="Contents of BigQuery JSON credentials.",
)


Expand Down