Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: extract instance connection info into a separate InstanceConnectionForm (#270)

tianzhou0634a77115e4
diff --git a/frontend/src/components/InstanceConnectionForm.vue b/frontend/src/components/InstanceConnectionForm.vue
new file mode 100644
index 0000000000..908640d818
--- /dev/null
+++ b/frontend/src/components/InstanceConnectionForm.vue
@@ -0,0 +1,325 @@
+<template>
+ <div class="pt-4">
+ <div class="flex justify-between">
+ <div>
+ <h3 class="text-lg leading-6 font-medium text-gray-900">{{ $t("instance.connection-info") }}</h3>
+ <p class="mt-1 text-sm text-gray-500" :class="create ? 'max-w-xl' : ''">
+ {{ $t("instance.sentence.create-user") }}
+ <span
+ v-if="!create"
+ class="normal-link"
+ @click="toggleCreateUserExample"
+ >{{ $t("instance.show-how-to-create") }}</span>
+ </p>
+ <!-- Specify the fixed width so the create instance dialog width won't shift when switching engine types-->
+ <div v-if="state.showCreateUserExample" class="mt-2 text-sm text-main w-208">
+ <template
+ v-if="
+ instance.engine == 'MYSQL' ||
+ instance.engine == 'TIDB'
+ "
+ >
+ <i18n-t tag="p" keypath="instance.sentence.create-user-example.mysql.template">
+ <template #user>{{ $t("instance.sentence.create-user-example.mysql.user") }}</template>
+ <template #password>
+ <span class="text-red-600">
+ {{
+ $t(
+ "instance.sentence.create-user-example.mysql.password"
+ )
+ }}
+ </span>
+ </template>
+ </i18n-t>
+ </template>
+ <template v-else-if="instance.engine == 'CLICKHOUSE'">
+ <i18n-t tag="p" keypath="instance.sentence.create-user-example.clickhouse.template">
+ <template #password>
+ <span class="text-red-600">YOUR_DB_PWD</span>
+ </template>
+ <template #link>
+ <a
+ class="normal-link"
+ href="https://clickhouse.com/docs/en/operations/access-rights/#access-control-usage"
+ target="__blank"
+ >
+ {{
+ $t(
+ "instance.sentence.create-user-example.clickhouse.sql-driven-workflow"
+ )
+ }}
+ </a>
+ </template>
+ </i18n-t>
+ </template>
+ <template v-else-if="instance.engine == 'POSTGRES'">
+ <BBAttention
+ class="mb-1"
+ :style="'WARN'"
+ :title="
+ $t('instance.sentence.create-user-example.postgres.warn')
+ "
+ />
+ <i18n-t tag="p" keypath="instance.sentence.create-user-example.postgres.template">
+ <template #password>
+ <span class="text-red-600">YOUR_DB_PWD</span>
+ </template>
+ </i18n-t>
+ </template>
+ <template v-else-if="instance.engine == 'SNOWFLAKE'">
+ <i18n-t tag="p" keypath="instance.sentence.create-user-example.snowflake.template">
+ <template #password>
+ <span class="text-red-600">YOUR_DB_PWD</span>
+ </template>
+ <template #warehouse>
+ <span class="text-red-600">YOUR_COMPUTE_WAREHOUSE</span>
+ </template>
+ </i18n-t>
+ </template>
+ <div class="mt-2 flex flex-row">
+ <span
+ class="flex-1 min-w-0 w-full inline-flex items-center px-3 py-2 border border-r border-control-border bg-gray-50 sm:text-sm whitespace-pre"
+ >{{ grantStatement(instance.engine) }}</span>
+ <button
+ tabindex="-1"
+ class="-ml-px px-2 py-2 border border-gray-300 text-sm font-medium text-control-light disabled:text-gray-300 bg-gray-50 hover:bg-gray-100 disabled:bg-gray-50 focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-1 disabled:cursor-not-allowed"
+ @click.prevent="copyGrantStatement"
+ >
+ <heroicons-outline:clipboard class="w-6 h-6" />
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="pt-4 grid grid-cols-1 gap-y-4 gap-x-4 sm:grid-cols-3">
+ <div class="sm:col-span-1 sm:col-start-1">
+ <label for="username" class="textlabel block">{{ $t("common.username") }}</label>
+ <!-- For mysql, username can be empty indicating anonymous user.
+ But it's a very bad practice to use anonymous user for admin operation,
+ thus we make it REQUIRED here.-->
+ <input
+ id="username"
+ name="username"
+ type="text"
+ class="textfield mt-1 w-full"
+ :disabled="!allowEdit"
+ :placeholder="
+ instance.engine == 'CLICKHOUSE'
+ ? $t('common.default')
+ : ''
+ "
+ :value="instance.username"
+ @input="$emit('update-username', $event.target.value)"
+ />
+ </div>
+
+ <div class="sm:col-span-1 sm:col-start-1">
+ <div class="flex flex-row items-center space-x-2">
+ <label for="password" class="textlabel block">
+ {{
+ $t("common.password")
+ }}
+ </label>
+ <!-- In create mode, user can leave the password field empty and create the instance,
+ so there is no need to show the checkbox.-->
+ <BBCheckbox
+ v-if="!create"
+ :title="$t('common.empty')"
+ :value="state.useEmptyPassword"
+ @toggle="
+ (on) => {
+ state.useEmptyPassword = on;
+ $emit('toggle-empty-password', on);
+ }
+ "
+ />
+ </div>
+ <input
+ id="password"
+ name="password"
+ type="text"
+ class="textfield mt-1 w-full"
+ autocomplete="off"
+ :placeholder="
+ state.useEmptyPassword
+ ? $t('instance.no-password')
+ : $t('instance.password-write-only')
+ "
+ :disabled="!allowEdit || state.useEmptyPassword"
+ :value="
+ create
+ ? state.useEmptyPassword
+ ? ''
+ : instance.password
+ : state.useEmptyPassword
+ ? ''
+ : state.updatedPassword
+ "
+ @input="(e) => {
+ state.updatedPassword = e.target.value
+ $emit('update-password', e.target.value)
+ }"
+ />
+ </div>
+ </div>
+ <div v-if="showTestConnection" class="pt-8 space-y-2">
+ <div class="flex flex-row space-x-2">
+ <button
+ type="button"
+ class="btn-normal whitespace-nowrap items-center"
+ :disabled="!instance.host"
+ @click.prevent="testConnection"
+ >{{ $t("instance.test-connection") }}</button>
+ </div>
+ </div>
+ </div>
+</template>
+
+<script lang="ts">
+import { computed, reactive, watch, PropType, ComputedRef } from "vue";
+import { useStore } from "vuex";
+import { toClipboard } from "@soerenmartius/vue3-clipboard";
+import { isDBAOrOwner } from "../utils";
+import {
+ Instance,
+ Principal,
+ ConnectionInfo,
+ SqlResultSet,
+ EngineType,
+} from "../types";
+import isEmpty from "lodash-es/isEmpty";
+import { useI18n } from "vue-i18n";
+
+interface LocalState {
+ // Only used in non-create case.
+ updatedPassword: string;
+ useEmptyPassword: boolean;
+ showCreateUserExample: boolean;
+}
+
+export default {
+ name: "InstanceConnectionForm",
+ components: {},
+ props: {
+ create: {
+ default: false,
+ type: Boolean,
+ },
+ allowEdit: {
+ required: true,
+ type: Boolean,
+ },
+ instance: {
+ // Can be false when create is true
+ required: false,
+ type: Object as PropType<Instance>,
+ },
+ },
+ emits: ["update-username", "update-password", "toggle-empty-password"],
+ setup(props, { }) {
+ const store = useStore();
+ const { t } = useI18n();
+
+ const currentUser: ComputedRef<Principal> = computed(() =>
+ store.getters["auth/currentUser"]()
+ );
+
+ const state = reactive<LocalState>({
+ updatedPassword: "",
+ useEmptyPassword: false,
+ showCreateUserExample: props.create,
+ });
+
+ watch(
+ () => props.instance,
+ () => {
+ state.updatedPassword = "";
+ state.useEmptyPassword = false;
+ }
+ );
+
+ const showTestConnection = computed(() => {
+ return (
+ props.create ||
+ (props.instance.rowStatus == "NORMAL" &&
+ isDBAOrOwner(currentUser.value.role))
+ );
+ });
+
+ const grantStatement = (type: EngineType): string => {
+ switch (type) {
+ case "CLICKHOUSE":
+ return "CREATE USER bytebase IDENTIFIED BY 'YOUR_DB_PWD';\n\nGRANT ALL ON *.* TO bytebase WITH GRANT OPTION;";
+ case "SNOWFLAKE":
+ return "CREATE OR REPLACE USER bytebase PASSWORD = 'YOUR_DB_PWD'\nDEFAULT_ROLE = \"ACCOUNTADMIN\"\nDEFAULT_WAREHOUSE = 'YOUR_COMPUTE_WAREHOUSE';\n\nGRANT ROLE \"ACCOUNTADMIN\" TO USER bytebase;";
+ case "MYSQL":
+ case "TIDB":
+ return "CREATE USER bytebase@'%' IDENTIFIED BY 'YOUR_DB_PWD';\n\nGRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, \nDELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, PROCESS, REFERENCES, \nSELECT, SHOW DATABASES, SHOW VIEW, TRIGGER, UPDATE, USAGE \nON *.* to bytebase@'%';";
+ case "POSTGRES":
+ return "CREATE USER bytebase WITH ENCRYPTED PASSWORD 'YOUR_DB_PWD';\n\nALTER USER bytebase WITH SUPERUSER;";
+ }
+ };
+
+ const toggleCreateUserExample = () => {
+ state.showCreateUserExample = !state.showCreateUserExample;
+ };
+
+ const copyGrantStatement = () => {
+ toClipboard(grantStatement(props.instance.engine)).then(() => {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "INFO",
+ title: t("instance.copy-grant-statement"),
+ });
+ });
+ };
+
+ const testConnection = () => {
+ const connectionInfo: ConnectionInfo = {
+ engine: props.instance.engine,
+ username: props.instance.username,
+ password: props.create
+ ? state.useEmptyPassword
+ ? ""
+ : props.instance.password
+ : state.useEmptyPassword
+ ? ""
+ : state.updatedPassword,
+ useEmptyPassword: state.useEmptyPassword,
+ host: props.instance.host,
+ port: props.instance.port,
+ instanceId: props.create ? undefined : (props.instance as Instance).id,
+ };
+ store
+ .dispatch("sql/ping", connectionInfo)
+ .then((resultSet: SqlResultSet) => {
+ if (isEmpty(resultSet.error)) {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("instance.successfully-connected-instance"),
+ });
+ } else {
+ store.dispatch("notification/pushNotification", {
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("instance.failed-to-connect-instance"),
+ description: resultSet.error,
+ // Manual hide, because user may need time to inspect the error
+ manualHide: true,
+ });
+ }
+ });
+ };
+
+ return {
+ state,
+ showTestConnection,
+ grantStatement,
+ toggleCreateUserExample,
+ copyGrantStatement,
+ testConnection,
+ };
+ },
+};
+</script>
\ No newline at end of file
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index 46d1798a31..33357594eb 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -18,57 +18,37 @@
>
<div class="flex flex-col items-center">
<!-- This awkward code is author couldn't figure out proper way to use dynamic src under vite
- https://github.com/vitejs/vite/issues/1265 -->
+ https://github.com/vitejs/vite/issues/1265-->
<template v-if="engine == 'MYSQL'">
- <img class="h-8 w-auto" src="../assets/db-mysql.png" alt="" />
+ <img class="h-8 w-auto" src="../assets/db-mysql.png" alt />
</template>
<template v-else-if="engine == 'POSTGRES'">
- <img
- class="h-8 w-auto"
- src="../assets/db-postgres.png"
- alt=""
- />
+ <img class="h-8 w-auto" src="../assets/db-postgres.png" alt />
</template>
<template v-else-if="engine == 'TIDB'">
<img class="h-8 w-auto" src="../assets/db-tidb.png" />
</template>
<template v-else-if="engine == 'SNOWFLAKE'">
- <img
- class="h-8 w-auto"
- src="../assets/db-snowflake.png"
- alt=""
- />
+ <img class="h-8 w-auto" src="../assets/db-snowflake.png" alt />
</template>
<template v-else-if="engine == 'CLICKHOUSE'">
- <img
- class="h-8 w-auto"
- src="../assets/db-clickhouse.png"
- alt=""
- />
+ <img class="h-8 w-auto" src="../assets/db-clickhouse.png" alt />
</template>
- <p class="mt-1 text-center textlabel">
- {{ engineName(engine) }}
- </p>
+ <p class="mt-1 text-center textlabel">{{ engineName(engine) }}</p>
<div class="mt-3 radio text-sm">
- <input
- type="radio"
- class="btn"
- :checked="state.instance.engine == engine"
- />
+ <input type="radio" class="btn" :checked="state.instance.engine == engine" />
</div>
</div>
</div>
</template>
</div>
<!-- Instance Name -->
- <div
- class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4"
- :class="create ? 'pt-4' : ''"
- >
+ <div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4" :class="create ? 'pt-4' : ''">
<div class="sm:col-span-2 sm:col-start-1">
<label for="name" class="textlabel flex flex-row items-center">
{{ $t("instance.instance-name") }}
- &nbsp;<span style="color: red">*</span>
+ &nbsp;
+ <span style="color: red">*</span>
<template v-if="!create">
<InstanceEngineIcon class="ml-1" :instance="state.instance" />
<span class="ml-1">{{ state.instance.engineVersion }}</span>
@@ -144,15 +124,11 @@
<div
v-if="state.instance.engine == 'SNOWFLAKE'"
class="mt-2 textinfolabel"
- >
- {{ $t("instance.sentence.proxy.snowflake") }}
- </div>
+ >{{ $t("instance.sentence.proxy.snowflake") }}</div>
</div>
<div class="sm:col-span-1">
- <label for="port" class="textlabel block">
- {{ $t("instance.port") }}
- </label>
+ <label for="port" class="textlabel block">{{ $t("instance.port") }}</label>
<input
id="port"
type="number"
@@ -168,7 +144,7 @@
<!--Do not show external link on create to reduce cognitive load-->
<div v-if="!create" class="sm:col-span-3 sm:col-start-1">
<label for="externallink" class="textlabel inline-flex">
- <span class="">
+ <span class>
{{
state.instance.engine == "SNOWFLAKE"
? $t("instance.snowflake-web-console")
@@ -197,9 +173,7 @@
/>
</template>
<template v-else>
- <div class="mt-1 textinfolabel">
- {{ $t("instance.sentence.console.snowflake") }}
- </div>
+ <div class="mt-1 textinfolabel">{{ $t("instance.sentence.console.snowflake") }}</div>
<input
id="externallink"
required
@@ -215,218 +189,21 @@
</div>
</div>
<!-- Read/Write Connection Info -->
- <div class="pt-4">
- <div class="flex justify-between">
- <div>
- <h3 class="text-lg leading-6 font-medium text-gray-900">
- {{ $t("instance.connection-info") }}
- </h3>
- <p
- class="mt-1 text-sm text-gray-500"
- :class="create ? 'max-w-xl' : ''"
- >
- {{ $t("instance.sentence.create-user") }}
- <span
- v-if="!create"
- class="normal-link"
- @click="toggleCreateUserExample"
- >
- {{ $t("instance.show-how-to-create") }}
- </span>
- </p>
- <!-- Specify the fixed width so the create instance dialog width won't shift when switching engine types-->
- <div
- v-if="state.showCreateUserExample"
- class="mt-2 text-sm text-main w-208"
- >
- <template
- v-if="
- state.instance.engine == 'MYSQL' ||
- state.instance.engine == 'TIDB'
- "
- >
- <i18n-t
- tag="p"
- keypath="instance.sentence.create-user-example.mysql.template"
- >
- <template #user>
- {{ $t("instance.sentence.create-user-example.mysql.user") }}
- </template>
- <template #password>
- <span class="text-red-600">
- {{
- $t(
- "instance.sentence.create-user-example.mysql.password"
- )
- }}
- </span>
- </template>
- </i18n-t>
- </template>
- <template v-else-if="state.instance.engine == 'CLICKHOUSE'">
- <i18n-t
- tag="p"
- keypath="instance.sentence.create-user-example.clickhouse.template"
- >
- <template #password>
- <span class="text-red-600"> YOUR_DB_PWD </span>
- </template>
- <template #link>
- <a
- class="normal-link"
- href="https://clickhouse.com/docs/en/operations/access-rights/#access-control-usage"
- target="__blank"
- >
- {{
- $t(
- "instance.sentence.create-user-example.clickhouse.sql-driven-workflow"
- )
- }}
- </a>
- </template>
- </i18n-t>
- </template>
- <template v-else-if="state.instance.engine == 'POSTGRES'">
- <BBAttention
- class="mb-1"
- :style="'WARN'"
- :title="
- $t('instance.sentence.create-user-example.postgres.warn')
- "
- />
- <i18n-t
- tag="p"
- keypath="instance.sentence.create-user-example.postgres.template"
- >
- <template #password>
- <span class="text-red-600"> YOUR_DB_PWD </span>
- </template>
- </i18n-t>
- </template>
- <template v-else-if="state.instance.engine == 'SNOWFLAKE'">
- <i18n-t
- tag="p"
- keypath="instance.sentence.create-user-example.snowflake.template"
- >
- <template #password>
- <span class="text-red-600"> YOUR_DB_PWD </span>
- </template>
- <template #warehouse>
- <span class="text-red-600"> YOUR_COMPUTE_WAREHOUSE </span>
- </template>
- </i18n-t>
- </template>
- <div class="mt-2 flex flex-row">
- <span
- class="flex-1 min-w-0 w-full inline-flex items-center px-3 py-2 border border-r border-control-border bg-gray-50 sm:text-sm whitespace-pre"
- >
- {{ grantStatement(state.instance.engine) }}
- </span>
- <button
- tabindex="-1"
- class="-ml-px px-2 py-2 border border-gray-300 text-sm font-medium text-control-light disabled:text-gray-300 bg-gray-50 hover:bg-gray-100 disabled:bg-gray-50 focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-1 disabled:cursor-not-allowed"
- @click.prevent="copyGrantStatement"
- >
- <heroicons-outline:clipboard class="w-6 h-6" />
- </button>
- </div>
- </div>
- </div>
- </div>
- <div class="pt-4 grid grid-cols-1 gap-y-4 gap-x-4 sm:grid-cols-3">
- <div class="sm:col-span-1 sm:col-start-1">
- <label for="username" class="textlabel block">
- {{ $t("common.username") }}
- </label>
- <!-- For mysql, username can be empty indicating anonymous user.
- But it's a very bad practice to use anonymous user for admin operation,
- thus we make it REQUIRED here. -->
- <input
- id="username"
- name="username"
- type="text"
- class="textfield mt-1 w-full"
- :disabled="!allowEdit"
- :placeholder="
- state.instance.engine == 'CLICKHOUSE'
- ? $t('common.default')
- : ''
- "
- :value="state.instance.username"
- @input="state.instance.username = $event.target.value"
- />
- </div>
-
- <div class="sm:col-span-1 sm:col-start-1">
- <div class="flex flex-row items-center space-x-2">
- <label for="password" class="textlabel block">{{
- $t("common.password")
- }}</label>
- <!-- In create mode, user can leave the password field empty and create the instance,
- so there is no need to show the checkbox. -->
- <BBCheckbox
- v-if="!create"
- :title="$t('common.empty')"
- :value="state.useEmptyPassword"
- @toggle="
- (on) => {
- state.useEmptyPassword = on;
- }
- "
- />
- </div>
- <input
- id="password"
- name="password"
- type="text"
- class="textfield mt-1 w-full"
- autocomplete="off"
- :placeholder="
- state.useEmptyPassword
- ? $t('instance.no-password')
- : $t('instance.password-write-only')
- "
- :disabled="!allowEdit || state.useEmptyPassword"
- :value="
- create
- ? state.useEmptyPassword
- ? ''
- : state.instance.password
- : state.useEmptyPassword
- ? ''
- : state.updatedPassword
- "
- @input="
- create
- ? (state.instance.password = $event.target.value)
- : (state.updatedPassword = $event.target.value)
- "
- />
- </div>
- </div>
- <div v-if="showTestConnection" class="pt-8 space-y-2">
- <div class="flex flex-row space-x-2">
- <button
- type="button"
- class="btn-normal whitespace-nowrap items-center"
- :disabled="!state.instance.host"
- @click.prevent="testConnection"
- >
- {{ $t("instance.test-connection") }}
- </button>
- </div>
- </div>
- </div>
+ <InstanceConnectionForm
+ :create="create"
+ :allowEdit="allowEdit"
+ :instance="state.instance"
+ @update-username="updateUsername"
+ @update-password="updatePassword"
+ @toggle-empty-password="toggleEmptyPassword"
+ />
</div>
<!-- Action Button Group -->
<div class="pt-4">
<!-- Create button group -->
<div v-if="create" class="flex justify-end items-center">
<div>
- <BBSpin
- v-if="state.creatingOrUpdating"
- :title="$t('common.creating')"
- />
+ <BBSpin v-if="state.creatingOrUpdating" :title="$t('common.creating')" />
</div>
<div class="ml-2">
<button
@@ -434,26 +211,19 @@
class="btn-normal py-2 px-4"
:disabled="state.creatingOrUpdating"
@click.prevent="cancel"
- >
- {{ $t("common.cancel") }}
- </button>
+ >{{ $t("common.cancel") }}</button>
<button
type="button"
class="btn-primary ml-3 inline-flex justify-center py-2 px-4"
:disabled="!allowCreate || state.creatingOrUpdating"
@click.prevent="tryCreate"
- >
- {{ $t("common.create") }}
- </button>
+ >{{ $t("common.create") }}</button>
</div>
</div>
<!-- Update button group -->
<div v-else class="flex justify-end items-center">
<div>
- <BBSpin
- v-if="state.creatingOrUpdating"
- :title="$t('common.updating')"
- />
+ <BBSpin v-if="state.creatingOrUpdating" :title="$t('common.updating')" />
</div>
<button
v-if="allowEdit"
@@ -461,9 +231,7 @@
class="btn-normal ml-2 inline-flex justify-center py-2 px-4"
:disabled="!valueChanged || state.creatingOrUpdating"
@click.prevent="doUpdate"
- >
- {{ $t("common.update") }}
- </button>
+ >{{ $t("common.update") }}</button>
</div>
</div>
</form>
@@ -480,8 +248,7 @@
}
"
@cancel="state.showCreateInstanceWarningModal = false"
- >
- </BBAlert>
+ ></BBAlert>
</template>
<script lang="ts">
@@ -492,6 +259,7 @@ import cloneDeep from "lodash-es/cloneDeep";
import isEqual from "lodash-es/isEqual";
import { toClipboard } from "@soerenmartius/vue3-clipboard";
import EnvironmentSelect from "../components/EnvironmentSelect.vue";
+import InstanceConnectionForm from "../components/InstanceConnectionForm.vue";
import InstanceEngineIcon from "../components/InstanceEngineIcon.vue";
import { instanceSlug, isDBAOrOwner, isDev } from "../utils";
import {
@@ -515,13 +283,12 @@ interface LocalState {
useEmptyPassword: boolean;
showCreateInstanceWarningModal: boolean;
createInstanceWarning: string;
- showCreateUserExample: boolean;
creatingOrUpdating: boolean;
}
export default {
- name: "DataSourceCreateForm",
- components: { EnvironmentSelect, InstanceEngineIcon },
+ name: "InstanceForm",
+ components: { EnvironmentSelect, InstanceConnectionForm, InstanceEngineIcon },
props: {
create: {
default: false,
@@ -549,19 +316,18 @@ export default {
instance: props.instance
? cloneDeep(props.instance)
: {
- environmentId: UNKNOWN_ID,
- name: t("instance.new-instance"),
- engine: "MYSQL",
- // In dev mode, Bytebase is likely run in naked style and access the local network via 127.0.0.1.
- // In release mode, Bytebase is likely run inside docker and access the local network via host.docker.internal.
- host: isDev() ? "127.0.0.1" : "host.docker.internal",
- username: "",
- },
+ environmentId: UNKNOWN_ID,
+ name: t("instance.new-instance"),
+ engine: "MYSQL",
+ // In dev mode, Bytebase is likely run in naked style and access the local network via 127.0.0.1.
+ // In release mode, Bytebase is likely run inside docker and access the local network via host.docker.internal.
+ host: isDev() ? "127.0.0.1" : "host.docker.internal",
+ username: "",
+ },
updatedPassword: "",
useEmptyPassword: false,
showCreateInstanceWarningModal: false,
createInstanceWarning: "",
- showCreateUserExample: props.create,
creatingOrUpdating: false,
});
@@ -577,14 +343,6 @@ export default {
);
});
- const showTestConnection = computed(() => {
- return (
- props.create ||
- ((state.instance as Instance).rowStatus == "NORMAL" &&
- isDBAOrOwner(currentUser.value.role))
- );
- });
-
const valueChanged = computed(() => {
return (
!isEqual(state.instance, state.originalInstance) ||
@@ -621,26 +379,11 @@ export default {
}
};
- const grantStatement = (type: EngineType): string => {
- switch (type) {
- case "CLICKHOUSE":
- return "CREATE USER bytebase IDENTIFIED BY 'YOUR_DB_PWD';\n\nGRANT ALL ON *.* TO bytebase WITH GRANT OPTION;";
- case "SNOWFLAKE":
- return "CREATE OR REPLACE USER bytebase PASSWORD = 'YOUR_DB_PWD'\nDEFAULT_ROLE = \"ACCOUNTADMIN\"\nDEFAULT_WAREHOUSE = 'YOUR_COMPUTE_WAREHOUSE';\n\nGRANT ROLE \"ACCOUNTADMIN\" TO USER bytebase;";
- case "MYSQL":
- case "TIDB":
- return "CREATE USER bytebase@'%' IDENTIFIED BY 'YOUR_DB_PWD';\n\nGRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, \nDELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, PROCESS, REFERENCES, \nSELECT, SHOW DATABASES, SHOW VIEW, TRIGGER, UPDATE, USAGE \nON *.* to bytebase@'%';";
- case "POSTGRES":
- return "CREATE USER bytebase WITH ENCRYPTED PASSWORD 'YOUR_DB_PWD';\n\nALTER USER bytebase WITH SUPERUSER;";
- }
- };
-
const instanceLink = (instance: Instance): string => {
if (instance.engine == "SNOWFLAKE") {
if (instance.host) {
- return `https://${
- instance.host.split("@")[0]
- }.snowflakecomputing.com/console`;
+ return `https://${instance.host.split("@")[0]
+ }.snowflakecomputing.com/console`;
}
}
return instance.host;
@@ -664,9 +407,21 @@ export default {
state.instance.engine = engine;
};
- const toggleCreateUserExample = () => {
- state.showCreateUserExample = !state.showCreateUserExample;
- };
+ const updateUsername = (username: string) => {
+ state.instance.username = username;
+ }
+
+ const updatePassword = (password: string) => {
+ if (props.create) {
+ state.instance.password = password;
+ } else {
+ state.updatedPassword = password;
+ }
+ }
+
+ const toggleEmptyPassword = (useEmptyPassword: boolean) => {
+ state.useEmptyPassword = useEmptyPassword;
+ }
const updateInstance = (field: string, value: string) => {
(state.instance as any)[field] = value;
@@ -792,73 +547,23 @@ export default {
});
};
- const copyGrantStatement = () => {
- toClipboard(grantStatement(state.instance.engine)).then(() => {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "INFO",
- title: t("instance.copy-grant-statement"),
- });
- });
- };
-
- const testConnection = () => {
- const connectionInfo: ConnectionInfo = {
- engine: state.instance.engine,
- username: state.instance.username,
- password: props.create
- ? state.useEmptyPassword
- ? ""
- : state.instance.password
- : state.useEmptyPassword
- ? ""
- : state.updatedPassword,
- useEmptyPassword: state.useEmptyPassword,
- host: state.instance.host,
- port: state.instance.port,
- instanceId: props.create ? undefined : (state.instance as Instance).id,
- };
- store
- .dispatch("sql/ping", connectionInfo)
- .then((resultSet: SqlResultSet) => {
- if (isEmpty(resultSet.error)) {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "SUCCESS",
- title: t("instance.successfully-connected-instance"),
- });
- } else {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "CRITICAL",
- title: t("instance.failed-to-connect-instance"),
- description: resultSet.error,
- // Manual hide, because user may need time to inspect the error
- manualHide: true,
- });
- }
- });
- };
-
return {
state,
allowCreate,
allowEdit,
- showTestConnection,
valueChanged,
defaultPort,
engineName,
- grantStatement,
instanceLink,
changeInstanceEngine,
- toggleCreateUserExample,
+ updateUsername,
+ updatePassword,
+ toggleEmptyPassword,
updateInstance,
cancel,
tryCreate,
doCreate,
doUpdate,
- copyGrantStatement,
- testConnection,
};
},
};