From 6a80455c6c605fa3997297450ec49e408f4d8d03 Mon Sep 17 00:00:00 2001 From: Jarod Liu Date: Mon, 18 Nov 2024 10:51:51 +0800 Subject: [PATCH] fix: byteplus access provider --- migrations/1731872250_add_byteplus.go | 107 ++++++++++ .../certimate/AccessByteplusForm.tsx | 194 ++++++++++++++++++ .../components/certimate/AccessEditDialog.tsx | 12 ++ 3 files changed, 313 insertions(+) create mode 100644 migrations/1731872250_add_byteplus.go create mode 100644 ui/src/components/certimate/AccessByteplusForm.tsx diff --git a/migrations/1731872250_add_byteplus.go b/migrations/1731872250_add_byteplus.go new file mode 100644 index 00000000..63daae39 --- /dev/null +++ b/migrations/1731872250_add_byteplus.go @@ -0,0 +1,107 @@ +package migrations + +import ( + "encoding/json" + + "github.com/pocketbase/dbx" + "github.com/pocketbase/pocketbase/daos" + m "github.com/pocketbase/pocketbase/migrations" + "github.com/pocketbase/pocketbase/models/schema" +) + +func init() { + m.Register(func(db dbx.Builder) error { + dao := daos.New(db) + + collection, err := dao.FindCollectionByNameOrId("4yzbv8urny5ja1e") + if err != nil { + return err + } + + // update + edit_configType := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "hwy7m03o", + "name": "configType", + "type": "select", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSelect": 1, + "values": [ + "aliyun", + "tencent", + "huaweicloud", + "qiniu", + "aws", + "cloudflare", + "namesilo", + "godaddy", + "pdns", + "httpreq", + "local", + "ssh", + "webhook", + "k8s", + "baiducloud", + "dogecloud", + "volcengine", + "byteplus" + ] + } + }`), edit_configType); err != nil { + return err + } + collection.Schema.AddField(edit_configType) + + return dao.SaveCollection(collection) + }, func(db dbx.Builder) error { + dao := daos.New(db) + + collection, err := dao.FindCollectionByNameOrId("4yzbv8urny5ja1e") + if err != nil { + return err + } + + // update + edit_configType := &schema.SchemaField{} + if err := json.Unmarshal([]byte(`{ + "system": false, + "id": "hwy7m03o", + "name": "configType", + "type": "select", + "required": false, + "presentable": false, + "unique": false, + "options": { + "maxSelect": 1, + "values": [ + "aliyun", + "tencent", + "huaweicloud", + "qiniu", + "aws", + "cloudflare", + "namesilo", + "godaddy", + "pdns", + "httpreq", + "local", + "ssh", + "webhook", + "k8s", + "baiducloud", + "dogecloud", + "volcengine" + ] + } + }`), edit_configType); err != nil { + return err + } + collection.Schema.AddField(edit_configType) + + return dao.SaveCollection(collection) + }) +} diff --git a/ui/src/components/certimate/AccessByteplusForm.tsx b/ui/src/components/certimate/AccessByteplusForm.tsx new file mode 100644 index 00000000..82b3dbad --- /dev/null +++ b/ui/src/components/certimate/AccessByteplusForm.tsx @@ -0,0 +1,194 @@ +import { useForm } from "react-hook-form"; +import { useTranslation } from "react-i18next"; +import z from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { ClientResponseError } from "pocketbase"; + +import { Input } from "@/components/ui/input"; +import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; +import { Button } from "@/components/ui/button"; +import { PbErrorData } from "@/domain/base"; +import { accessProvidersMap, accessTypeFormSchema, type Access, type ByteplusConfig } from "@/domain/access"; +import { save } from "@/repository/access"; +import { useConfigContext } from "@/providers/config"; + +type AccessByteplusFormProps = { + op: "add" | "edit" | "copy"; + data?: Access; + onAfterReq: () => void; +}; + +const AccessByteplusForm = ({ data, op, onAfterReq }: AccessByteplusFormProps) => { + const { addAccess, updateAccess } = useConfigContext(); + const { t } = useTranslation(); + const formSchema = z.object({ + id: z.string().optional(), + name: z + .string() + .min(1, "access.authorization.form.name.placeholder") + .max(64, t("common.errmsg.string_max", { max: 64 })), + configType: accessTypeFormSchema, + accessKey: z + .string() + .min(1, "access.authorization.form.access_key.placeholder") + .max(64, t("common.errmsg.string_max", { max: 64 })), + secretKey: z + .string() + .min(1, "access.authorization.form.secret_key.placeholder") + .max(64, t("common.errmsg.string_max", { max: 64 })), + }); + + let config: ByteplusConfig = { + accessKey: "", + secretKey: "", + }; + if (data) config = data.config as ByteplusConfig; + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + id: data?.id, + name: data?.name || "", + configType: "byteplus", + accessKey: config.accessKey, + secretKey: config.secretKey, + }, + }); + + const onSubmit = async (data: z.infer) => { + const req: Access = { + id: data.id as string, + name: data.name, + configType: data.configType, + usage: accessProvidersMap.get(data.configType)!.usage, + config: { + accessKey: data.accessKey, + secretKey: data.secretKey, + }, + }; + + try { + req.id = op == "copy" ? "" : req.id; + const rs = await save(req); + + onAfterReq(); + + req.id = rs.id; + req.created = rs.created; + req.updated = rs.updated; + if (data.id && op == "edit") { + updateAccess(req); + return; + } + addAccess(req); + } catch (e) { + const err = e as ClientResponseError; + + Object.entries(err.response.data as PbErrorData).forEach(([key, value]) => { + form.setError(key as keyof z.infer, { + type: "manual", + message: value.message, + }); + }); + + return; + } + }; + + return ( + <> +
+ { + e.stopPropagation(); + form.handleSubmit(onSubmit)(e); + }} + className="space-y-8" + > + ( + + {t("access.authorization.form.name.label")} + + + + + + + )} + /> + + ( + + {t("access.authorization.form.config.label")} + + + + + + + )} + /> + + ( + + {t("access.authorization.form.config.label")} + + + + + + + )} + /> + + ( + + {t("access.authorization.form.access_key.label")} + + + + + + + )} + /> + + ( + + {t("access.authorization.form.secret_key.label")} + + + + + + + )} + /> + + + +
+ +
+ + + + ); +}; + +export default AccessByteplusForm; diff --git a/ui/src/components/certimate/AccessEditDialog.tsx b/ui/src/components/certimate/AccessEditDialog.tsx index 7c888530..e339edbd 100644 --- a/ui/src/components/certimate/AccessEditDialog.tsx +++ b/ui/src/components/certimate/AccessEditDialog.tsx @@ -22,6 +22,7 @@ import AccessSSHForm from "./AccessSSHForm"; import AccessWebhookForm from "./AccessWebhookForm"; import AccessKubernetesForm from "./AccessKubernetesForm"; import AccessVolcengineForm from "./AccessVolcengineForm"; +import AccessByteplusForm from "./AccessByteplusForm"; import { Access } from "@/domain/access"; import { AccessTypeSelect } from "./AccessTypeSelect"; @@ -228,6 +229,17 @@ const AccessEditDialog = ({ trigger, op, data, className }: AccessEditProps) => /> ); break; + case "byteplus": + childComponent = ( + { + setOpen(false); + }} + /> + ); + break; } return (