feat: toggle able schedule
This commit is contained in:
parent
ca12d80aa8
commit
00efbdc6a2
1 changed files with 61 additions and 60 deletions
|
|
@ -34,6 +34,14 @@ const BACKUP_MINIO_ACCESS_KEY = getEnvVar("BACKUP_MINIO_ACCESS_KEY");
|
||||||
const BACKUP_MINIO_SECRET_KEY = getEnvVar("BACKUP_MINIO_SECRET_KEY");
|
const BACKUP_MINIO_SECRET_KEY = getEnvVar("BACKUP_MINIO_SECRET_KEY");
|
||||||
const BACKUP_MINIO_BUCKET = getEnvVar("BACKUP_MINIO_BUCKET");
|
const BACKUP_MINIO_BUCKET = getEnvVar("BACKUP_MINIO_BUCKET");
|
||||||
|
|
||||||
|
function jsonParseOrPlainText(str: string) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(str);
|
||||||
|
} catch (_) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Route("/api/v1/backup")
|
@Route("/api/v1/backup")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
export class BackupController extends Controller {
|
export class BackupController extends Controller {
|
||||||
|
|
@ -100,14 +108,7 @@ export class BackupController extends Controller {
|
||||||
{
|
{
|
||||||
headers: { Authorization: `Bearer ${WINDMILL_API_KEY}` },
|
headers: { Authorization: `Bearer ${WINDMILL_API_KEY}` },
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
const data = await r.json();
|
|
||||||
if (typeof data === "object" && "error" in data) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Cannot get status.");
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("create")
|
@Post("create")
|
||||||
|
|
@ -156,14 +157,7 @@ export class BackupController extends Controller {
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
const data = await r.text();
|
|
||||||
if (data.includes("error")) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Backup Error");
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post("restore")
|
@Post("restore")
|
||||||
|
|
@ -205,14 +199,7 @@ export class BackupController extends Controller {
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
const data = await r.text();
|
|
||||||
if (data.includes("error")) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Backup Error");
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("delete")
|
@Delete("delete")
|
||||||
|
|
@ -234,12 +221,7 @@ export class BackupController extends Controller {
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => {
|
||||||
const data = await r.text();
|
return jsonParseOrPlainText(await r.text());
|
||||||
if (data.includes("error")) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Error delete backup.");
|
|
||||||
}
|
|
||||||
return data;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -265,6 +247,7 @@ export class BackupController extends Controller {
|
||||||
id: v.path.replace("f/backup_schedule/", ""),
|
id: v.path.replace("f/backup_schedule/", ""),
|
||||||
name: v.summary,
|
name: v.summary,
|
||||||
schedule: v.schedule,
|
schedule: v.schedule,
|
||||||
|
enabled: v.enabled,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -313,21 +296,13 @@ export class BackupController extends Controller {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}).then(async (r) => {
|
}).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
const data = await r.text();
|
|
||||||
if (data.includes("error")) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Error create schedule.");
|
|
||||||
}
|
|
||||||
console.log(data);
|
|
||||||
return data;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("schedule/{id}")
|
@Put("schedule/{id}")
|
||||||
async updateSchedule(
|
async updateSchedule(
|
||||||
@Path() id: string,
|
@Path() id: string,
|
||||||
@Body() body: { name: string; schedule: string; timezone?: string },
|
@Body() body: { name: string; schedule: string; enabled?: boolean; timezone?: string },
|
||||||
) {
|
) {
|
||||||
if (!/^[a-zA-Z0-9\-]+$/.test(body.name)) {
|
if (!/^[a-zA-Z0-9\-]+$/.test(body.name)) {
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Invalid name.");
|
throw new HttpError(HttpStatus.BAD_REQUEST, "Invalid name.");
|
||||||
|
|
@ -372,17 +347,53 @@ export class BackupController extends Controller {
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => {
|
||||||
if (r.status >= 400) {
|
const body = jsonParseOrPlainText(await r.text());
|
||||||
console.log(await r.json());
|
|
||||||
throw new Error("Error trying to run script/flow.");
|
|
||||||
}
|
|
||||||
const data = await r.text();
|
|
||||||
|
|
||||||
if (data.includes("error")) {
|
if (r.status === 200) {
|
||||||
console.error(data);
|
await fetch(
|
||||||
throw new Error("Error create schedule.");
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/setenabled/f/backup_schedule/${id}`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${WINDMILL_API_KEY}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ enabled: body.enabled }),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return body;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("schedule/{id}/toggle")
|
||||||
|
async toggleSchedule(@Path() id: string) {
|
||||||
|
return await fetch(
|
||||||
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/get/f/backup_schedule/${id}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${WINDMILL_API_KEY}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).then(async (r) => {
|
||||||
|
const body = jsonParseOrPlainText(await r.text());
|
||||||
|
|
||||||
|
if (typeof body === "object") {
|
||||||
|
const result = await fetch(
|
||||||
|
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/setenabled/f/backup_schedule/${id}`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${WINDMILL_API_KEY}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ enabled: !body.enabled }),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return jsonParseOrPlainText(await result.text());
|
||||||
}
|
}
|
||||||
return data;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -397,16 +408,6 @@ export class BackupController extends Controller {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
).then(async (r) => {
|
).then(async (r) => jsonParseOrPlainText(await r.text()));
|
||||||
if (r.status === 404) {
|
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Schedule not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await r.text();
|
|
||||||
if (data.includes("error")) {
|
|
||||||
console.error(data);
|
|
||||||
throw new Error("Error create schedule.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue