refac
This commit is contained in:
1 parent
05f314bae4
commit
d85b52bfc2
5 files changed
+460
-22
No files matched your search
@@ -229,6 +229,85 @@ export const setTerminalServerConnections = async (token: string, connections: o
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
* Detect whether a terminal server URL points to an Orchestrator or a direct
|
||||
* Open Terminal instance.
|
||||
*
|
||||
* - GET {url}/api/v1/policies → 200 → "orchestrator"
|
||||
* - GET {url}/api/config → 200 → "terminal"
|
||||
* - Neither → null
|
||||
*/
|
||||
export const detectTerminalServerType = async (
|
||||
url: string,
|
||||
key: string
|
||||
): Promise<'orchestrator' | 'terminal' | null> => {
|
||||
const baseUrl = url.replace(/\/$/, '');
|
||||
const headers: Record<string, string> = {};
|
||||
if (key) {
|
||||
headers['Authorization'] = `Bearer ${key}`;
|
||||
}
|
||||
|
||||
// Orchestrators expose a policies API; plain terminals don't.
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}/api/v1/policies`, { headers });
|
||||
if (res.ok) return 'orchestrator';
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
// Fall back to open-terminal config endpoint.
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}/api/config`, { headers });
|
||||
if (res.ok) return 'terminal';
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create or update a policy on the orchestrator.
|
||||
* PUT {url}/api/v1/policies/{policyId}
|
||||
*/
|
||||
export const putOrchestratorPolicy = async (
|
||||
url: string,
|
||||
key: string,
|
||||
policyId: string,
|
||||
policyData: object
|
||||
): Promise<object | null> => {
|
||||
let error = null;
|
||||
|
||||
const baseUrl = url.replace(/\/$/, '');
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
if (key) {
|
||||
headers['Authorization'] = `Bearer ${key}`;
|
||||
}
|
||||
|
||||
const res = await fetch(`${baseUrl}/api/v1/policies/${encodeURIComponent(policyId)}`, {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify(policyData)
|
||||
})
|
||||
.then(async (res) => {
|
||||
if (!res.ok) throw await res.json();
|
||||
return res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
error = err.detail;
|
||||
return null;
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
export const verifyToolServerConnection = async (token: string, connection: object) => {
|
||||
let error = null;
|
||||
|
||||
|
||||
Reference in new issue
Block a user