//native
type Cockroachdb = {
token: string;
};
/**
* Get a formatted generic connection string for a cluster
* Can be used by the following roles assigned at the organization, folder or cluster scope:
- ORG_ADMIN
- CLUSTER_ADMIN
- CLUSTER_OPERATOR_WRITER
- CLUSTER_DEVELOPER
- FOLDER_ADMIN
- FOLDER_MOVER
*/
export async function main(
auth: Cockroachdb,
cluster_id: string,
database: string | undefined,
sql_user: string | undefined,
os: "MAC" | "LINUX" | "WINDOWS" | undefined,
) {
const url = new URL(
`https://cockroachlabs.cloud/api/v1/clusters/${cluster_id}/connection-string`,
);
for (const [k, v] of [
["database", database],
["sql_user", sql_user],
["os", os],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 235 days ago