mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-10 05:24:20 +00:00
228 lines
6.0 KiB
TypeScript
228 lines
6.0 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
|
|
import { auth } from "@/auth.config";
|
|
import { parseStringify } from "@/lib";
|
|
|
|
// Credentials for basic auth
|
|
const username = process.env.API_USERNAME;
|
|
const password = process.env.API_PASSWORD;
|
|
|
|
export const getProviders = async ({
|
|
page = 1,
|
|
query = "",
|
|
sort = "",
|
|
filters = {},
|
|
}) => {
|
|
const session = await auth();
|
|
const tenantId = session?.user.tenantId;
|
|
|
|
if (isNaN(Number(page)) || page < 1) redirect("/providers");
|
|
|
|
const keyServer = process.env.API_BASE_URL;
|
|
const url = new URL(`${keyServer}/providers?sort=-inserted_at`);
|
|
|
|
if (page) url.searchParams.append("page[number]", page.toString());
|
|
if (query) url.searchParams.append("filter[search]", query);
|
|
if (sort) url.searchParams.append("sort", sort);
|
|
|
|
// Handle multiple filters
|
|
Object.entries(filters).forEach(([key, value]) => {
|
|
if (key !== "filter[search]") {
|
|
url.searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
|
|
try {
|
|
const providers = await fetch(url.toString(), {
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
},
|
|
});
|
|
const data = await providers.json();
|
|
const parsedData = parseStringify(data);
|
|
revalidatePath("/providers");
|
|
return parsedData;
|
|
} catch (error) {
|
|
console.error("Error fetching providers:", error);
|
|
return undefined;
|
|
}
|
|
};
|
|
|
|
export const getProvider = async (formData: FormData) => {
|
|
const session = await auth();
|
|
const tenantId = session?.user.tenantId;
|
|
const providerId = formData.get("id");
|
|
|
|
const keyServer = process.env.API_BASE_URL;
|
|
const url = new URL(`${keyServer}/providers/${providerId}`);
|
|
|
|
try {
|
|
const providers = await fetch(url.toString(), {
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
},
|
|
});
|
|
const data = await providers.json();
|
|
const parsedData = parseStringify(data);
|
|
return parsedData;
|
|
} catch (error) {
|
|
return {
|
|
error: getErrorMessage(error),
|
|
};
|
|
}
|
|
};
|
|
|
|
export const updateProvider = async (formData: FormData) => {
|
|
const session = await auth();
|
|
const keyServer = process.env.API_BASE_URL;
|
|
|
|
const tenantId = session?.user.tenantId;
|
|
const providerId = formData.get("providerId");
|
|
const providerAlias = formData.get("alias");
|
|
|
|
const url = new URL(`${keyServer}/providers/${providerId}`);
|
|
|
|
try {
|
|
const response = await fetch(url.toString(), {
|
|
method: "PATCH",
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
"Content-Type": "application/vnd.api+json",
|
|
},
|
|
body: JSON.stringify({
|
|
data: {
|
|
type: "Provider",
|
|
id: providerId,
|
|
attributes: {
|
|
alias: providerAlias,
|
|
},
|
|
},
|
|
}),
|
|
});
|
|
const data = await response.json();
|
|
revalidatePath("/providers");
|
|
return parseStringify(data);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return {
|
|
error: getErrorMessage(error),
|
|
};
|
|
}
|
|
};
|
|
|
|
export const addProvider = async (formData: FormData) => {
|
|
const session = await auth();
|
|
const keyServer = process.env.API_BASE_URL;
|
|
|
|
const tenantId = session?.user.tenantId;
|
|
|
|
const providerType = formData.get("providerType");
|
|
const providerId = formData.get("providerId");
|
|
const providerAlias = formData.get("providerAlias");
|
|
|
|
const url = new URL(`${keyServer}/providers`);
|
|
|
|
try {
|
|
const response = await fetch(url.toString(), {
|
|
method: "POST",
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
"Content-Type": "application/vnd.api+json",
|
|
},
|
|
body: JSON.stringify({
|
|
data: {
|
|
type: "Provider",
|
|
attributes: {
|
|
provider: providerType,
|
|
uid: providerId,
|
|
alias: providerAlias,
|
|
},
|
|
},
|
|
}),
|
|
});
|
|
const data = await response.json();
|
|
revalidatePath("/providers");
|
|
return parseStringify(data);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return {
|
|
error: getErrorMessage(error),
|
|
};
|
|
}
|
|
};
|
|
|
|
export const checkConnectionProvider = async (formData: FormData) => {
|
|
const session = await auth();
|
|
const keyServer = process.env.API_BASE_URL;
|
|
const tenantId = session?.user.tenantId;
|
|
|
|
const providerId = formData.get("id");
|
|
|
|
const url = new URL(`${keyServer}/providers/${providerId}/connection`);
|
|
|
|
try {
|
|
const response = await fetch(url.toString(), {
|
|
method: "POST",
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
},
|
|
});
|
|
const data = await response.json();
|
|
revalidatePath("/providers");
|
|
return parseStringify(data);
|
|
} catch (error) {
|
|
return {
|
|
error: getErrorMessage(error),
|
|
};
|
|
}
|
|
};
|
|
|
|
export const deleteProvider = async (formData: FormData) => {
|
|
const session = await auth();
|
|
const keyServer = process.env.API_BASE_URL;
|
|
const tenantId = session?.user.tenantId;
|
|
|
|
const providerId = formData.get("id");
|
|
const url = new URL(`${keyServer}/providers/${providerId}`);
|
|
|
|
try {
|
|
const response = await fetch(url.toString(), {
|
|
method: "DELETE",
|
|
headers: {
|
|
"X-Tenant-ID": `${tenantId}`,
|
|
Authorization: "Basic " + btoa(`${username}:${password}`),
|
|
},
|
|
});
|
|
const data = await response.json();
|
|
revalidatePath("/providers");
|
|
return parseStringify(data);
|
|
} catch (error) {
|
|
return {
|
|
error: getErrorMessage(error),
|
|
};
|
|
}
|
|
};
|
|
|
|
export const getErrorMessage = (error: unknown): string => {
|
|
let message: string;
|
|
|
|
if (error instanceof Error) {
|
|
message = error.message;
|
|
} else if (error && typeof error === "object" && "message" in error) {
|
|
message = String(error.message);
|
|
} else if (typeof error === "string") {
|
|
message = error;
|
|
} else {
|
|
message = "Oops! Something went wrong.";
|
|
}
|
|
return message;
|
|
};
|