Skip to content

Commit 5894ac4

Browse files
authored
Merge pull request #60 from hasura/paritosh/engine-1552
add native toolchain definition to connector metadata
2 parents 52f9a8c + e655b84 commit 5894ac4

9 files changed

+329
-0
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,10 @@ tokio = { workspace = true, features = ["full"] }
2020
[build-dependencies]
2121
build-data = { workspace = true }
2222

23+
[dev-dependencies]
24+
insta = { workspace = true }
25+
serde_json = { workspace = true }
26+
tempfile = { workspace = true }
27+
2328
[package.metadata.cargo-machete]
2429
ignored = ["build_data", "build-data"] # apparently cargo-machete doesn't find dependencies used by build scripts

crates/cli/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,18 @@ async fn initialize(with_metadata: bool, context: Context<impl Environment>) ->
133133
action: metadata::DockerComposeWatchAction::SyncAndRestart,
134134
ignore: vec![],
135135
}],
136+
native_toolchain_definition: Some(metadata::NativeToolchainDefinition {
137+
commands: vec![
138+
("start".to_string(), metadata::CommandDefinition::ShellScript {
139+
bash: "#!/usr/bin/env bash\nset -eu -o pipefail\nHASURA_CONFIGURATION_DIRECTORY=\"$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH\" \"$HASURA_DDN_NATIVE_CONNECTOR_DIR/ndc-bigquery\" serve".to_string(),
140+
powershell: "$ErrorActionPreference = \"Stop\"\n$env:HASURA_CONFIGURATION_DIRECTORY=\"$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH\"; & \"$env:HASURA_DDN_NATIVE_CONNECTOR_DIR\\ndc-bigquery.exe\" serve".to_string(),
141+
}),
142+
("update".to_string(), metadata::CommandDefinition::ShellScript {
143+
bash: "#!/usr/bin/env bash\nset -eu -o pipefail\n\"$HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR/hasura-ndc-bigquery\" update".to_string(),
144+
powershell: "$ErrorActionPreference = \"Stop\"\n& \"$env:HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR\\hasura-ndc-bigquery.exe\" update".to_string(),
145+
}),
146+
].into_iter().collect(),
147+
})
136148
};
137149

138150
fs::write(metadata_file, serde_yaml::to_string(&metadata)?).await?;

crates/cli/src/metadata.rs

+22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct ConnectorMetadataDefinition {
1414
pub cli_plugin: Option<CliPluginDefinition>,
1515
#[serde(skip_serializing_if = "Vec::is_empty")]
1616
pub docker_compose_watch: DockerComposeWatch,
17+
#[serde(skip_serializing_if = "Option::is_none")]
18+
pub native_toolchain_definition: Option<NativeToolchainDefinition>,
1719
}
1820

1921
#[derive(Debug, Serialize, Deserialize)]
@@ -75,3 +77,23 @@ pub enum DockerComposeWatchAction {
7577
#[serde(rename = "sync+restart")]
7678
SyncAndRestart,
7779
}
80+
81+
#[derive(Debug, Serialize, Deserialize)]
82+
#[serde(rename_all = "camelCase")]
83+
pub struct NativeToolchainDefinition {
84+
pub commands: std::collections::BTreeMap<CommandName, CommandDefinition>,
85+
}
86+
87+
pub type CommandName = String;
88+
89+
#[derive(Debug, Serialize, Deserialize)]
90+
#[serde(rename_all = "PascalCase", tag = "type")]
91+
pub enum CommandDefinition {
92+
#[serde(rename_all = "camelCase")]
93+
ShellScript { bash: String, powershell: String },
94+
#[serde(rename_all = "camelCase")]
95+
DockerizedCommand {
96+
docker_image: String,
97+
command_args: Vec<String>,
98+
},
99+
}

crates/cli/tests/common/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![allow(dead_code)] // required because this is included multiple times
2+
3+
use std::path::Path;
4+
5+
use tokio::fs;
6+
7+
pub async fn assert_file_ends_with_newline(path: impl AsRef<Path>) -> anyhow::Result<()> {
8+
let contents = fs::read_to_string(path).await?;
9+
assert_ends_with_newline(&contents);
10+
Ok(())
11+
}
12+
13+
pub fn assert_ends_with_newline(contents: &str) {
14+
assert_eq!(contents.chars().last(), Some('\n'));
15+
}

crates/cli/tests/initialize_tests.rs

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
mod common;
2+
3+
use tokio::fs;
4+
5+
use ndc_bigquery_cli::*;
6+
use ndc_bigquery_configuration as configuration;
7+
use ndc_bigquery_configuration::ParsedConfiguration;
8+
9+
#[tokio::test]
10+
async fn test_initialize_directory() -> anyhow::Result<()> {
11+
let dir = tempfile::tempdir()?;
12+
13+
let context = Context {
14+
context_path: dir.path().to_owned(),
15+
environment: configuration::environment::EmptyEnvironment,
16+
release_version: None,
17+
};
18+
run(
19+
Command::Initialize {
20+
with_metadata: false,
21+
},
22+
context,
23+
)
24+
.await?;
25+
26+
let configuration_schema_file_path = dir.path().join("schema.json");
27+
assert!(configuration_schema_file_path.exists());
28+
common::assert_file_ends_with_newline(&configuration_schema_file_path).await?;
29+
30+
let configuration_file_path = dir.path().join("configuration.json");
31+
assert!(configuration_file_path.exists());
32+
let contents = fs::read_to_string(configuration_file_path).await?;
33+
common::assert_ends_with_newline(&contents);
34+
let _: ParsedConfiguration = configuration::parse_configuration(&dir).await?;
35+
36+
let metadata_file_path = dir
37+
.path()
38+
.join(".hasura-connector")
39+
.join("connector-metadata.yaml");
40+
assert!(!metadata_file_path.exists());
41+
42+
Ok(())
43+
}
44+
45+
#[tokio::test]
46+
async fn test_initialize_version_is_unchanged() -> anyhow::Result<()> {
47+
let dir = tempfile::tempdir()?;
48+
49+
let context = Context {
50+
context_path: dir.path().to_owned(),
51+
environment: configuration::environment::EmptyEnvironment,
52+
release_version: None,
53+
};
54+
run(
55+
Command::Initialize {
56+
with_metadata: false,
57+
},
58+
context,
59+
)
60+
.await?;
61+
62+
let configuration_file_path = dir.path().join("configuration.json");
63+
assert!(configuration_file_path.exists());
64+
let configuration_value: serde_json::Value =
65+
serde_json::from_str(fs::read_to_string(configuration_file_path).await?.as_str())?;
66+
67+
let version = configuration_value
68+
.as_object()
69+
.unwrap()
70+
.get("version")
71+
.unwrap();
72+
73+
insta::assert_snapshot!(version.to_string());
74+
Ok(())
75+
}
76+
77+
#[tokio::test]
78+
async fn test_do_not_initialize_when_files_already_exist() -> anyhow::Result<()> {
79+
let dir = tempfile::tempdir()?;
80+
fs::write(
81+
dir.path().join("random.file"),
82+
"this directory is no longer empty",
83+
)
84+
.await?;
85+
86+
let context = Context {
87+
context_path: dir.path().to_owned(),
88+
environment: configuration::environment::EmptyEnvironment,
89+
release_version: None,
90+
};
91+
match run(
92+
Command::Initialize {
93+
with_metadata: false,
94+
},
95+
context,
96+
)
97+
.await
98+
{
99+
Ok(()) => panic!("Expected the command to fail."),
100+
Err(error) => match error.downcast::<Error>() {
101+
Err(input) => panic!("Expected a CLI error, but got {input}"),
102+
Ok(cli_error) => assert_eq!(cli_error, Error::DirectoryIsNotEmpty),
103+
},
104+
}
105+
106+
Ok(())
107+
}
108+
109+
#[tokio::test]
110+
async fn test_initialize_directory_with_metadata() -> anyhow::Result<()> {
111+
let dir = tempfile::tempdir()?;
112+
113+
let context = Context {
114+
context_path: dir.path().to_owned(),
115+
environment: configuration::environment::EmptyEnvironment,
116+
release_version: None,
117+
};
118+
run(
119+
Command::Initialize {
120+
with_metadata: true,
121+
},
122+
context,
123+
)
124+
.await?;
125+
126+
let configuration_schema_file_path = dir.path().join("schema.json");
127+
assert!(configuration_schema_file_path.exists());
128+
common::assert_file_ends_with_newline(&configuration_schema_file_path).await?;
129+
130+
let configuration_file_path = dir.path().join("configuration.json");
131+
assert!(configuration_file_path.exists());
132+
common::assert_file_ends_with_newline(&configuration_file_path).await?;
133+
134+
let metadata_file_path = dir
135+
.path()
136+
.join(".hasura-connector")
137+
.join("connector-metadata.yaml");
138+
assert!(metadata_file_path.exists());
139+
let contents = fs::read_to_string(metadata_file_path).await?;
140+
common::assert_ends_with_newline(&contents);
141+
insta::assert_snapshot!(contents);
142+
143+
Ok(())
144+
}
145+
146+
#[tokio::test]
147+
async fn test_initialize_directory_with_metadata_and_release_version() -> anyhow::Result<()> {
148+
let dir = tempfile::tempdir()?;
149+
150+
let context = Context {
151+
context_path: dir.path().to_owned(),
152+
environment: configuration::environment::EmptyEnvironment,
153+
release_version: Some("v1.2.3"),
154+
};
155+
run(
156+
Command::Initialize {
157+
with_metadata: true,
158+
},
159+
context,
160+
)
161+
.await?;
162+
163+
let configuration_schema_file_path = dir.path().join("schema.json");
164+
assert!(configuration_schema_file_path.exists());
165+
common::assert_file_ends_with_newline(&configuration_schema_file_path).await?;
166+
167+
let configuration_file_path = dir.path().join("configuration.json");
168+
assert!(configuration_file_path.exists());
169+
common::assert_file_ends_with_newline(&configuration_file_path).await?;
170+
171+
let metadata_file_path = dir
172+
.path()
173+
.join(".hasura-connector")
174+
.join("connector-metadata.yaml");
175+
assert!(metadata_file_path.exists());
176+
let contents = fs::read_to_string(metadata_file_path).await?;
177+
common::assert_ends_with_newline(&contents);
178+
insta::assert_snapshot!(contents);
179+
180+
Ok(())
181+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: crates/cli/tests/initialize_tests.rs
3+
expression: contents
4+
---
5+
packagingDefinition:
6+
type: PrebuiltDockerImage
7+
dockerImage: ghcr.io/hasura/ndc-bigquery:latest
8+
supportedEnvironmentVariables:
9+
- name: HASURA_BIGQUERY_SERVICE_KEY
10+
description: The BigQuery service key
11+
- name: HASURA_BIGQUERY_PROJECT_ID
12+
description: The BigQuery project ID/name
13+
- name: HASURA_BIGQUERY_DATASET_ID
14+
description: The BigQuery dataset ID/name
15+
commands:
16+
update: hasura-ndc-bigquery update
17+
cliPlugin:
18+
name: ndc-bigquery
19+
version: latest
20+
dockerComposeWatch:
21+
- path: ./
22+
target: /etc/connector
23+
action: sync+restart
24+
nativeToolchainDefinition:
25+
commands:
26+
start:
27+
type: ShellScript
28+
bash: |-
29+
#!/usr/bin/env bash
30+
set -eu -o pipefail
31+
HASURA_CONFIGURATION_DIRECTORY="$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH" "$HASURA_DDN_NATIVE_CONNECTOR_DIR/ndc-bigquery" serve
32+
powershell: |-
33+
$ErrorActionPreference = "Stop"
34+
$env:HASURA_CONFIGURATION_DIRECTORY="$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH"; & "$env:HASURA_DDN_NATIVE_CONNECTOR_DIR\ndc-bigquery.exe" serve
35+
update:
36+
type: ShellScript
37+
bash: |-
38+
#!/usr/bin/env bash
39+
set -eu -o pipefail
40+
"$HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR/hasura-ndc-bigquery" update
41+
powershell: |-
42+
$ErrorActionPreference = "Stop"
43+
& "$env:HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR\hasura-ndc-bigquery.exe" update
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: crates/cli/tests/initialize_tests.rs
3+
expression: contents
4+
---
5+
packagingDefinition:
6+
type: PrebuiltDockerImage
7+
dockerImage: ghcr.io/hasura/ndc-bigquery:v1.2.3
8+
supportedEnvironmentVariables:
9+
- name: HASURA_BIGQUERY_SERVICE_KEY
10+
description: The BigQuery service key
11+
- name: HASURA_BIGQUERY_PROJECT_ID
12+
description: The BigQuery project ID/name
13+
- name: HASURA_BIGQUERY_DATASET_ID
14+
description: The BigQuery dataset ID/name
15+
commands:
16+
update: hasura-ndc-bigquery update
17+
cliPlugin:
18+
name: ndc-bigquery
19+
version: v1.2.3
20+
dockerComposeWatch:
21+
- path: ./
22+
target: /etc/connector
23+
action: sync+restart
24+
nativeToolchainDefinition:
25+
commands:
26+
start:
27+
type: ShellScript
28+
bash: |-
29+
#!/usr/bin/env bash
30+
set -eu -o pipefail
31+
HASURA_CONFIGURATION_DIRECTORY="$HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH" "$HASURA_DDN_NATIVE_CONNECTOR_DIR/ndc-bigquery" serve
32+
powershell: |-
33+
$ErrorActionPreference = "Stop"
34+
$env:HASURA_CONFIGURATION_DIRECTORY="$env:HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH"; & "$env:HASURA_DDN_NATIVE_CONNECTOR_DIR\ndc-bigquery.exe" serve
35+
update:
36+
type: ShellScript
37+
bash: |-
38+
#!/usr/bin/env bash
39+
set -eu -o pipefail
40+
"$HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR/hasura-ndc-bigquery" update
41+
powershell: |-
42+
$ErrorActionPreference = "Stop"
43+
& "$env:HASURA_DDN_NATIVE_CONNECTOR_PLUGIN_DIR\hasura-ndc-bigquery.exe" update
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: crates/cli/tests/initialize_tests.rs
3+
expression: version.to_string()
4+
---
5+
1

0 commit comments

Comments
 (0)