Skip to content

Commit 6b6278e

Browse files
committed
feat(sdk): undeploy
1 parent f5fc024 commit 6b6278e

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

meta-cli/src/com/server.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ lazy_static! {
2525
}
2626

2727
#[derive(Serialize)]
28-
struct ResponseSuccess {
28+
struct CLIResponseSuccess {
2929
data: Value,
3030
}
3131

3232
#[derive(Serialize)]
33-
struct ResponseError {
33+
struct CLIResponseError {
3434
error: String,
3535
}
3636

@@ -57,11 +57,11 @@ async fn config() -> impl Responder {
5757

5858
HttpResponse::Ok()
5959
.status(StatusCode::OK)
60-
.json(ResponseSuccess { data })
60+
.json(CLIResponseSuccess { data })
6161
}
6262
None => HttpResponse::Ok()
6363
.status(StatusCode::INTERNAL_SERVER_ERROR)
64-
.json(ResponseError {
64+
.json(CLIResponseError {
6565
error: "Could not get config".to_string(),
6666
}),
6767
}
@@ -72,12 +72,12 @@ async fn command() -> impl Responder {
7272
match ServerStore::get_command() {
7373
Some(command) => HttpResponse::Ok()
7474
.status(StatusCode::OK)
75-
.json(ResponseSuccess {
75+
.json(CLIResponseSuccess {
7676
data: serde_json::to_value(command).unwrap(),
7777
}),
7878
None => HttpResponse::Ok()
7979
.status(StatusCode::INTERNAL_SERVER_ERROR)
80-
.json(ResponseError {
80+
.json(CLIResponseError {
8181
error: "Could not get command".to_string(),
8282
}),
8383
}

typegraph/node/sdk/src/tg_manage.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MPL-2.0
33

44
import { ArtifactResolutionConfig } from "./gen/interfaces/metatype-typegraph-core.js";
5+
import { BasicAuth, tgRemove } from "./tg_deploy.js";
56
import { TypegraphOutput } from "./typegraph.js";
67
import { getEnvVariable } from "./utils/func_utils.js";
78

@@ -10,21 +11,37 @@ const PORT_SOURCE = "META_CLI_SERVER_PORT";
1011

1112
type Command = "serialize" | "deploy" | "undeploy" | "unpack_migration";
1213

14+
// Types for CLI => SDK
1315
type CLIServerResponse = {
1416
command: Command;
1517
config: CLIConfigRequest;
1618
};
1719

1820
type CLIConfigRequest = {
19-
typegates: Record<string, string>;
21+
typegate: {
22+
endpoint: string;
23+
auth: {
24+
username: string;
25+
password: string;
26+
};
27+
};
2028
secrets: Record<string, string>;
2129
artifactsConfig: ArtifactResolutionConfig;
2230
};
2331

24-
type SDKSuccess<T> = {
32+
type CLISuccess<T> = {
2533
data: T;
2634
};
2735

36+
// Types for SDK => CLi
37+
type SDKResponse<T> = {
38+
command: Command;
39+
data: T;
40+
} | {
41+
command: Command;
42+
error: T;
43+
};
44+
2845
export class Manager {
2946
#port: number;
3047
#typegraph: TypegraphOutput;
@@ -60,7 +77,8 @@ export class Manager {
6077
this.#deploy(config);
6178
break;
6279
case "undeploy":
63-
this.#undeploy();
80+
await this.#undeploy(config);
81+
break;
6482
case "unpack_migration":
6583
this.#unpackMigration(config);
6684
break;
@@ -71,37 +89,59 @@ export class Manager {
7189

7290
async #requestCommands(): Promise<CLIServerResponse> {
7391
const { data: config } = await this.#requestConfig();
74-
7592
const { data: command } =
7693
await (await fetch(new URL("command", this.#endpoint)))
77-
.json() as SDKSuccess<Command>;
94+
.json() as CLISuccess<Command>;
7895

7996
console.error("Command", command);
8097

8198
return { command, config };
8299
}
83100

84-
async #requestConfig(): Promise<SDKSuccess<CLIConfigRequest>> {
101+
async #requestConfig(): Promise<CLISuccess<CLIConfigRequest>> {
85102
const response = await fetch(new URL("config", this.#endpoint));
86-
return (await response.json()) as SDKSuccess<CLIConfigRequest>;
103+
return (await response.json()) as CLISuccess<CLIConfigRequest>;
87104
}
88105

89106
#serialize(config: CLIConfigRequest): void {
90107
const ret = this.#typegraph.serialize(config.artifactsConfig);
91-
// TODO:
92-
// send back through http
93108
console.log(ret);
94109
}
95110

96111
#deploy(config: CLIConfigRequest): void {
97112
// TODO
98113
}
99114

100-
#undeploy(): void {
101-
// TODO
115+
async #undeploy({ typegate }: CLIConfigRequest): Promise<void> {
116+
const { endpoint, auth: { username, password } } = typegate;
117+
await this.#relayResultToCLI(
118+
"undeploy",
119+
async () =>
120+
await tgRemove(this.#typegraph, {
121+
baseUrl: endpoint,
122+
auth: new BasicAuth(username, password),
123+
}),
124+
);
102125
}
103126

104127
#unpackMigration(config: CLIConfigRequest): void {
105128
// TODO
106129
}
130+
131+
async #relayResultToCLI<T>(initiator: Command, fn: () => Promise<T>) {
132+
let response: SDKResponse<any>;
133+
try {
134+
const data = await fn();
135+
response = { command: initiator, data };
136+
} catch (err) {
137+
const msg = err instanceof Error ? err.message : err;
138+
response = { command: initiator, error: msg };
139+
}
140+
141+
await fetch(new URL("response", this.#endpoint), {
142+
method: "POST",
143+
headers: { "Content-Type": "application/json" },
144+
body: JSON.stringify(response),
145+
});
146+
}
107147
}

0 commit comments

Comments
 (0)