|
| 1 | +import axios from "axios"; |
| 2 | +import { message } from "antd"; |
| 3 | +import { ManagedOrg } from "../types/enterprise.types"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Fetch workspaces for a specific environment |
| 7 | + * @param apiServiceUrl - API service URL for the environment |
| 8 | + * @param environmentId - ID of the environment |
| 9 | + * |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +export async function getManagedWorkspaces( |
| 14 | + environmentId: string, |
| 15 | + apiServiceUrl: string |
| 16 | +): Promise<ManagedOrg[]> { |
| 17 | + if (!environmentId || !apiServiceUrl) { |
| 18 | + throw new Error("Missing environmentId or apiServiceUrl"); |
| 19 | + } |
| 20 | + |
| 21 | + try { |
| 22 | + const res = await axios.get(`${apiServiceUrl}/api/plugins/enterprise/org`); |
| 23 | + const all: ManagedOrg[] = res.data; |
| 24 | + return all.filter(org => org.environmentId === environmentId); |
| 25 | + } catch (err) { |
| 26 | + const errorMsg = err instanceof Error ? err.message : "Failed to fetch managed workspaces"; |
| 27 | + message.error(errorMsg); |
| 28 | + throw err; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Fetch workspaces for a specific environment |
| 35 | + * @param apiServiceUrl - API service URL for the environment |
| 36 | + * @param environmentId - ID of the environment |
| 37 | + * @param orgName - Name of the workspace |
| 38 | + * @param orgTags - Tags of the workspace |
| 39 | + * |
| 40 | + */ |
| 41 | + |
| 42 | +export async function connectManagedWorkspace( |
| 43 | + environmentId: string, |
| 44 | + apiServiceUrl: string, |
| 45 | + orgName: string, |
| 46 | + orgTags: string[] = [] |
| 47 | +) { |
| 48 | + if (!environmentId || !apiServiceUrl || !orgName) { |
| 49 | + throw new Error("Missing required params to connect org"); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const payload = { |
| 54 | + environment_id: environmentId, |
| 55 | + org_name: orgName, |
| 56 | + org_tags: orgTags, |
| 57 | + }; |
| 58 | + |
| 59 | + const res = await axios.post(`${apiServiceUrl}/api/plugins/enterprise/org`, payload); |
| 60 | + return res.data; |
| 61 | + } catch (err) { |
| 62 | + const errorMsg = err instanceof Error ? err.message : "Failed to connect org"; |
| 63 | + message.error(errorMsg); |
| 64 | + throw err; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +/** |
| 74 | + * Fetch workspaces for a specific environment |
| 75 | + * @param apiServiceUrl - API service URL for the environment |
| 76 | + * @param orgId - ID of the workspace |
| 77 | + * |
| 78 | + */ |
| 79 | +export async function unconnectManagedWorkspace( |
| 80 | + apiServiceUrl: string, |
| 81 | + orgId: string |
| 82 | +) { |
| 83 | + if (!apiServiceUrl || !orgId) { |
| 84 | + throw new Error("Missing apiServiceUrl or orgId"); |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + await axios.delete(`${apiServiceUrl}/api/plugins/enterprise/org/${orgId}`); |
| 89 | + } catch (err) { |
| 90 | + const errorMsg = err instanceof Error ? err.message : "Failed to unconnect org"; |
| 91 | + message.error(errorMsg); |
| 92 | + throw err; |
| 93 | + } |
| 94 | +} |
0 commit comments