Skip to content

Commit 49d202a

Browse files
committed
refac
1 parent 09d4ccc commit 49d202a

1 file changed

Lines changed: 62 additions & 7 deletions

File tree

src/lib/components/AddTerminalServerModal.svelte

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
verifyTerminalServerConnection,
1818
putOrchestratorPolicy,
1919
putOrchestratorLifecycle,
20+
getOrchestratorPolicy,
21+
getOrchestratorLifecycle,
2022
refreshOrchestratorTerminals
2123
} from '$lib/apis/configs';
2224
import { getTerminalConfig } from '$lib/apis/terminal';
@@ -56,6 +58,8 @@
5658
let lifecycleJson = '{}';
5759
let refreshOnlyIdle = true;
5860
let refreshReset = false;
61+
let loadingPolicy = false;
62+
let policyLoadError = '';
5963
6064
const stringifyJson = (value: object | null | undefined) => {
6165
return JSON.stringify(value && Object.keys(value).length ? value : {}, null, 2);
@@ -73,10 +77,10 @@
7377
accessGrants = connection?.config?.access_grants ?? [];
7478
7579
// Restore policy state
76-
serverType = connection?.server_type ?? null;
80+
serverType = connection?.server_type ?? (connection?.policy_id ? 'orchestrator' : null);
7781
policyId = connection?.policy_id ?? '';
7882
79-
const p = connection?.policy ?? {};
83+
const p: Record<string, any> = {};
8084
policyImage = p.image ?? '';
8185
policyIdleTimeout = p.idle_timeout_minutes ?? 30;
8286
policyStorage = p.storage ? 'persistent' : 'ephemeral';
@@ -89,9 +93,11 @@
8993
// Restore resources
9094
policyCpu = p.cpu_limit ?? '1';
9195
policyMemory = p.memory_limit ?? '1Gi';
92-
lifecycleJson = stringifyJson(connection?.lifecycle);
96+
lifecycleJson = stringifyJson({});
9397
refreshOnlyIdle = true;
9498
refreshReset = false;
99+
loadingPolicy = false;
100+
policyLoadError = '';
95101
} else {
96102
id = '';
97103
url = '';
@@ -114,11 +120,44 @@
114120
lifecycleJson = '{}';
115121
refreshOnlyIdle = true;
116122
refreshReset = false;
123+
loadingPolicy = false;
124+
policyLoadError = '';
125+
}
126+
};
127+
128+
const loadPolicy = async () => {
129+
if (!connection || serverType !== 'orchestrator' || !policyId || direct) return;
130+
131+
loadingPolicy = true;
132+
policyLoadError = '';
133+
try {
134+
let policy: any = null;
135+
try {
136+
policy = await getOrchestratorPolicy(localStorage.token, url, key, policyId, auth_type);
137+
} catch (error: any) {
138+
if (error?.status !== 404) throw error;
139+
}
140+
141+
const lifecycle = await getOrchestratorLifecycle(localStorage.token, url, key, policyId, auth_type);
142+
const data = policy?.data ?? {};
143+
policyImage = data.image ?? '';
144+
policyIdleTimeout = data.idle_timeout_minutes ?? 30;
145+
policyStorage = data.storage ? 'persistent' : 'ephemeral';
146+
policyStorageSize = data.storage ?? '5Gi';
147+
policyEnvPairs = Object.entries(data.env ?? {}).map(([key, value]) => ({ key, value: String(value) }));
148+
policyCpu = data.cpu_limit ?? '1';
149+
policyMemory = data.memory_limit ?? '1Gi';
150+
lifecycleJson = stringifyJson(lifecycle?.data);
151+
} catch (error: any) {
152+
policyLoadError = error?.message || String(error);
153+
} finally {
154+
loadingPolicy = false;
117155
}
118156
};
119157
120158
$: if (show) {
121159
init();
160+
void loadPolicy();
122161
}
123162
124163
const verifyHandler = async () => {
@@ -262,6 +301,14 @@
262301
url = url.replace(/\/$/, '');
263302
// Bearer key whitespace breaks the terminal WebSocket auth (HTTP headers strip it, JSON doesn't)
264303
key = key.trim();
304+
if (loadingPolicy) {
305+
toast.error($i18n.t('Policy is still loading'));
306+
return;
307+
}
308+
if (policyLoadError) {
309+
toast.error($i18n.t('Failed to load policy: {{error}}', { error: policyLoadError }));
310+
return;
311+
}
265312
266313
// Save policy to orchestrator if applicable
267314
let policyData = {};
@@ -301,8 +348,7 @@
301348
},
302349
// Policy fields
303350
...(serverType ? { server_type: serverType } : {}),
304-
...(serverType === 'orchestrator' && policyId ? { policy_id: policyId } : {}),
305-
...(serverType === 'orchestrator' ? { policy: policyData, lifecycle: lifecycleData } : {})
351+
...(serverType === 'orchestrator' && policyId ? { policy_id: policyId } : {})
306352
};
307353
308354
onSubmit(result);
@@ -478,6 +524,11 @@
478524
</div>
479525
</div>
480526
</div>
527+
{#if loadingPolicy}
528+
<div class="mt-2 text-xs text-gray-500">{$i18n.t('Loading policy...')}</div>
529+
{:else if policyLoadError}
530+
<div class="mt-2 text-xs text-red-600 dark:text-red-400">{$i18n.t('Failed to load policy: {{error}}', { error: policyLoadError })}</div>
531+
{/if}
481532

482533
<div class="flex gap-2 mt-2">
483534
<div class="flex flex-col w-full">
@@ -664,7 +715,7 @@
664715
</div>
665716
</div>
666717

667-
<div class="flex flex-wrap items-center justify-between gap-2 mt-2">
718+
<div class="flex flex-wrap items-center justify-between gap-2 mt-2">
668719
<div class="flex items-center gap-3 text-xs text-gray-500 dark:text-gray-400">
669720
<label class="flex items-center gap-1.5">
670721
<input type="checkbox" bind:checked={refreshOnlyIdle} />
@@ -675,6 +726,9 @@
675726
<span>{$i18n.t('Reset persisted files')}</span>
676727
</label>
677728
</div>
729+
<div class="mt-2 text-xs text-gray-500 dark:text-gray-400">
730+
{$i18n.t('Policy changes apply to newly provisioned terminals. Refresh matching terminals to apply them to existing terminals.')}
731+
</div>
678732
<button
679733
type="button"
680734
class="px-2 py-1 text-xs font-medium rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-850 dark:hover:bg-gray-800 transition"
@@ -843,8 +897,9 @@
843897
</div>
844898

845899
<button
846-
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
900+
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 disabled:opacity-50 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
847901
type="submit"
902+
disabled={loadingPolicy || !!policyLoadError}
848903
>
849904
{$i18n.t('Save')}
850905
</button>

0 commit comments

Comments
 (0)