Skip to content

Commit bf7cd7d

Browse files
committed
feat(cli): prepare undeploy
1 parent 6b6278e commit bf7cd7d

File tree

7 files changed

+106
-21
lines changed

7 files changed

+106
-21
lines changed

libs/common/src/node.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
use anyhow::{Context, Result};
55
use indoc::indoc;
66
use reqwest::{Client, IntoUrl, RequestBuilder, Url};
7+
use serde::Serialize;
78
use std::{collections::HashMap, time::Duration};
89

910
use crate::{
1011
graphql::{self, Query},
1112
typegraph::Typegraph,
1213
};
1314

14-
#[derive(Debug, Clone)]
15+
#[derive(Debug, Serialize, Clone)]
1516
pub struct BasicAuth {
1617
pub username: String,
1718
pub password: String,
@@ -21,7 +22,7 @@ pub struct BasicAuth {
2122
pub struct Node {
2223
pub base_url: Url,
2324
pub prefix: Option<String>,
24-
auth: Option<BasicAuth>,
25+
pub auth: Option<BasicAuth>,
2526
pub env: HashMap<String, String>,
2627
}
2728

meta-cli/src/cli/undeploy.rs

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

44
use crate::{
5-
com::store::{Command, ServerStore},
5+
com::store::{Command, Endpoint, ServerStore},
66
config::Config,
77
};
88
use anyhow::Result;
@@ -36,6 +36,11 @@ impl Action for Undeploy {
3636
// Hint the server what state we are globally in
3737
ServerStore::with(Some(Command::Undeploy), Some(config.clone()));
3838

39+
ServerStore::set_endpoint(Endpoint {
40+
typegate: node.base_url.clone().into(),
41+
auth: node.auth.clone(),
42+
});
43+
3944
node.try_undeploy(&self.typegraphs).await?;
4045
Ok(())
4146
}

meta-cli/src/com/server.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
55

66
use crate::com::store::ServerStore;
7-
use actix_web::{get, post, App, HttpResponse, HttpServer, Responder};
7+
use actix_web::{get, post, web::Query, App, HttpRequest, HttpResponse, HttpServer, Responder};
88
use lazy_static::lazy_static;
99
use reqwest::StatusCode;
10-
use serde::Serialize;
10+
use serde::{Deserialize, Serialize};
1111
use serde_json::{json, Value};
1212

1313
pub fn random_free_port() -> u16 {
@@ -34,18 +34,31 @@ struct CLIResponseError {
3434
error: String,
3535
}
3636

37+
#[derive(Debug, Deserialize)]
38+
struct QueryConfigParams {
39+
typegraph: String,
40+
}
41+
3742
#[get("/config")]
38-
async fn config() -> impl Responder {
43+
async fn config(req: HttpRequest) -> impl Responder {
44+
let parsed = Query::<QueryConfigParams>::from_query(req.query_string());
45+
let folder = match parsed {
46+
Ok(p) => p.typegraph.to_owned(),
47+
Err(_) => "".to_string(),
48+
};
49+
let endpoint = ServerStore::get_endpoint();
3950
match ServerStore::get_config() {
4051
Some(config) => {
4152
let data = json!({
42-
"typegates": config.typegates,
53+
"typegate": {
54+
"endpoint": endpoint.typegate,
55+
"auth": endpoint.auth
56+
},
4357
"secrets": ServerStore::get_secrets(),
4458
"artifactsConfig": json!({
4559
"dir": config.base_dir.display().to_string(),
4660
"prismaMigration": {
47-
// TODO:
48-
"migrationDir": ".",
61+
"migrationDir": config.prisma_migrations_dir(&folder),
4962
"action": {
5063
// TODO:
5164
"create": true,

meta-cli/src/com/store.rs

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

44
use crate::config::Config;
5+
use common::node::BasicAuth;
56
use lazy_static::lazy_static;
67
use serde::Serialize;
78
use std::{
@@ -34,11 +35,18 @@ pub enum Command {
3435
Serialize,
3536
}
3637

38+
#[derive(Default, Clone, Debug)]
39+
pub struct Endpoint {
40+
pub typegate: String,
41+
pub auth: Option<BasicAuth>,
42+
}
43+
3744
#[derive(Default, Clone, Debug)]
3845
pub struct ServerStore {
3946
config: Option<Config>,
4047
command: Option<Command>,
4148
secrets: HashMap<String, String>,
49+
endpoint: Endpoint,
4250
}
4351

4452
#[allow(dead_code)]
@@ -69,4 +77,12 @@ impl ServerStore {
6977
pub fn get_secrets() -> HashMap<String, String> {
7078
with_store(|s| s.secrets.clone())
7179
}
80+
81+
pub fn set_endpoint(endpoint: Endpoint) {
82+
with_store_mut(|s| s.endpoint = endpoint)
83+
}
84+
85+
pub fn get_endpoint() -> Endpoint {
86+
with_store(|s| s.endpoint.clone())
87+
}
7288
}

meta-cli/src/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ impl NodeConfig {
100100
self.env.clone(),
101101
)
102102
}
103+
104+
pub fn get_auth_pair(&self) -> (Option<String>, Option<String>) {
105+
(self.username.to_owned(), self.password.to_owned())
106+
}
103107
}
104108

105109
#[derive(Deserialize, Debug, Default, Clone)]
@@ -239,6 +243,19 @@ impl Config {
239243
.get(&module_type)
240244
.unwrap_or(&DEFAULT_LOADER_CONFIG)
241245
}
246+
247+
pub fn prisma_migrations_dir(&self, typegraph: &str) -> PathBuf {
248+
let mut path = self.base_dir.join(
249+
self.typegraphs
250+
.materializers
251+
.prisma
252+
.migrations_path
253+
.as_deref()
254+
.unwrap_or_else(|| Path::new("prisma/migrations")),
255+
);
256+
path.push(typegraph);
257+
path
258+
}
242259
}
243260

244261
#[cfg(test)]

typegraph/node/sdk/src/tg_manage.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type CLIServerResponse = {
2020
type CLIConfigRequest = {
2121
typegate: {
2222
endpoint: string;
23-
auth: {
23+
auth?: {
2424
username: string;
2525
password: string;
2626
};
@@ -33,14 +33,11 @@ type CLISuccess<T> = {
3333
data: T;
3434
};
3535

36-
// Types for SDK => CLi
36+
// Types for SDK => CLi (typically forwarding the response from typegate)
3737
type SDKResponse<T> = {
3838
command: Command;
39-
data: T;
40-
} | {
41-
command: Command;
42-
error: T;
43-
};
39+
typegraphName: string;
40+
} & ({ error: T } | { data: T });
4441

4542
export class Manager {
4643
#port: number;
@@ -89,6 +86,7 @@ export class Manager {
8986

9087
async #requestCommands(): Promise<CLIServerResponse> {
9188
const { data: config } = await this.#requestConfig();
89+
console.error("CONFIG", config);
9290
const { data: command } =
9391
await (await fetch(new URL("command", this.#endpoint)))
9492
.json() as CLISuccess<Command>;
@@ -99,7 +97,10 @@ export class Manager {
9997
}
10098

10199
async #requestConfig(): Promise<CLISuccess<CLIConfigRequest>> {
102-
const response = await fetch(new URL("config", this.#endpoint));
100+
const params = new URLSearchParams({
101+
typegraph: this.#typegraph.name,
102+
});
103+
const response = await fetch(new URL("config?" + params, this.#endpoint));
103104
return (await response.json()) as CLISuccess<CLIConfigRequest>;
104105
}
105106

@@ -113,13 +114,18 @@ export class Manager {
113114
}
114115

115116
async #undeploy({ typegate }: CLIConfigRequest): Promise<void> {
116-
const { endpoint, auth: { username, password } } = typegate;
117+
const { endpoint, auth } = typegate;
118+
if (!auth) {
119+
throw new Error(
120+
`"${this.#typegraph.name}" received null or undefined "auth" field on the configuration`,
121+
);
122+
}
117123
await this.#relayResultToCLI(
118124
"undeploy",
119125
async () =>
120126
await tgRemove(this.#typegraph, {
121127
baseUrl: endpoint,
122-
auth: new BasicAuth(username, password),
128+
auth: new BasicAuth(auth.username, auth.password),
123129
}),
124130
);
125131
}
@@ -129,13 +135,14 @@ export class Manager {
129135
}
130136

131137
async #relayResultToCLI<T>(initiator: Command, fn: () => Promise<T>) {
138+
const typegraphName = this.#typegraph.name;
132139
let response: SDKResponse<any>;
133140
try {
134141
const data = await fn();
135-
response = { command: initiator, data };
142+
response = { command: initiator, typegraphName, data };
136143
} catch (err) {
137144
const msg = err instanceof Error ? err.message : err;
138-
response = { command: initiator, error: msg };
145+
response = { command: initiator, typegraphName, error: msg };
139146
}
140147

141148
await fetch(new URL("response", this.#endpoint), {

website/static/specs/0.0.3.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,24 @@
12411241
"$ref": "#/definitions/InjectionData_for_String"
12421242
}
12431243
}
1244+
},
1245+
{
1246+
"type": "object",
1247+
"required": [
1248+
"data",
1249+
"source"
1250+
],
1251+
"properties": {
1252+
"source": {
1253+
"type": "string",
1254+
"enum": [
1255+
"random"
1256+
]
1257+
},
1258+
"data": {
1259+
"$ref": "#/definitions/InjectionData_for_String"
1260+
}
1261+
}
12441262
}
12451263
]
12461264
},
@@ -2363,6 +2381,14 @@
23632381
},
23642382
"version": {
23652383
"type": "string"
2384+
},
2385+
"random_seed": {
2386+
"type": [
2387+
"integer",
2388+
"null"
2389+
],
2390+
"format": "uint32",
2391+
"minimum": 0.0
23662392
}
23672393
}
23682394
},

0 commit comments

Comments
 (0)