Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional URL for config build #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 23 additions & 31 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ pub enum Command {
},
/// Update the configuration by introspecting the database, using the configuration options.
Update,
// /// Upgrade the configuration to the latest version. This does not involve the database.
// Upgrade {
// #[arg(long)]
// dir_from: PathBuf,
// #[arg(long)]
// dir_to: PathBuf,
// },
}

/// The set of errors that can go wrong _in addition to_ generic I/O or parsing errors.
Expand All @@ -54,7 +47,6 @@ pub async fn run(command: Command, context: Context<impl Environment>) -> anyhow
match command {
Command::Initialize { with_metadata } => initialize(with_metadata, context).await?,
Command::Update => update(context).await?,
// Command::Upgrade { dir_from, dir_to } => upgrade(dir_from, dir_to).await?,
};
Ok(())
}
Expand Down Expand Up @@ -95,31 +87,31 @@ async fn initialize(with_metadata: bool, context: Context<impl Environment>) ->
},
),
supported_environment_variables: vec![
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_ACCESS_KEY_ID".to_string(),
description: "The AWS DynamoDB access key ID".to_string(),
default_value: Some("dynamodbql://read_only_user:[email protected]:5432/v3-docs-sample-app".to_string()),
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_ACCESS_KEY_ID".to_string(),
description: "The AWS DynamoDB access key ID".to_string(),
default_value: None,
required: true,
},
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_SECRET_ACCESS_KEY".to_string(),
description: "The AWS DynamoDB secret access key".to_string(),
default_value: Some(String::new()),
required: true
},
// metadata::EnvironmentVariableDefinition {
// name: "HASURA_DYNAMODB_AWS_PROVIDER_NAME".to_string(),
// description: "The AWS DynamoDB provider name".to_string(),
// default_value: Some(String::new()),
// required: true,
// },
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_REGION".to_string(),
description: "The AWS DynamoDB region".to_string(),
default_value: Some(String::new()),
},
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_SECRET_ACCESS_KEY".to_string(),
description: "The AWS DynamoDB secret access key".to_string(),
default_value: None,
required: true,
},
],
},
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_URL".to_string(),
description: "The AWS DynamoDB URL".to_string(),
default_value: Some(String::new()),
required: false,
},
metadata::EnvironmentVariableDefinition {
name: "HASURA_DYNAMODB_AWS_REGION".to_string(),
description: "The AWS DynamoDB region".to_string(),
default_value: None,
required: true,
},
],
commands: metadata::Commands {
update: Some("hasura-ndc-dynamodb update".to_string()),
watch: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/configuration/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Configuration {
pub metadata: metadata::Metadata,
pub access_key_id: String,
pub secret_access_key: String,
// pub provider_name: String,
pub url: Option<String>,
pub region: String,
// pub mutations_version: Option<metadata::mutations::MutationsVersion>,
}
12 changes: 6 additions & 6 deletions crates/configuration/src/connection_settings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Database connection settings.

use crate::values::{AccessKeyId, Region, Secret, SecretAccessKey};
use crate::values::{connection_info::Url, AccessKeyId, Region, Secret, SecretAccessKey};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub const DEFAULT_ACCESS_KEY_ID_VARIABLE: &str = "HASURA_DYNAMODB_AWS_ACCESS_KEY_ID";
pub const DEFAULT_SECRET_ACCESS_KEY_VARIABLE: &str = "HASURA_DYNAMODB_AWS_SECRET_ACCESS_KEY";
pub const DEFAULT_PROVIDER_NAME: &str = "HASURA_DYNAMODB_AWS_PROVIDER_NAME";
pub const DEFAULT_URL_VARIABLE: &str = "HASURA_DYNAMODB_URL";
pub const DEFAULT_REGION_VARIABLE: &str = "HASURA_DYNAMODB_AWS_REGION";

/// Database connection settings.
Expand All @@ -15,7 +15,7 @@ pub const DEFAULT_REGION_VARIABLE: &str = "HASURA_DYNAMODB_AWS_REGION";
pub struct DatabaseConnectionSettings {
pub access_key_id: AccessKeyId,
pub secret_access_key: SecretAccessKey,
// pub provider_name: ProviderName,
pub url: Url,
pub region: Region,
}

Expand All @@ -28,9 +28,9 @@ impl DatabaseConnectionSettings {
secret_access_key: SecretAccessKey(Secret::FromEnvironment {
variable: DEFAULT_SECRET_ACCESS_KEY_VARIABLE.into(),
}),
// provider_name: ProviderName(Secret::FromEnvironment {
// variable: DEFAULT_PROVIDER_NAME.into(),
// }),
url: Url(Secret::FromEnvironment {
variable: DEFAULT_URL_VARIABLE.into(),
}),
region: Region(Secret::FromEnvironment {
variable: DEFAULT_REGION_VARIABLE.into(),
}),
Expand Down
13 changes: 7 additions & 6 deletions crates/configuration/src/to_runtime_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! That can be used by the connector at runtime.

use super::version1::ParsedConfiguration;
use crate::environment::Environment;
use crate::environment::{Environment, Variable};
use crate::error::MakeRuntimeConfigurationError;
use crate::values::{AccessKeyId, Region, Secret, SecretAccessKey};
use query_engine_metadata::{self, metadata};
Expand Down Expand Up @@ -47,14 +47,17 @@ pub fn make_runtime_configuration(
})
}
}?;

let url = environment
.read(&Variable::from("HASURA_DYNAMODB_URL"))
.ok();

Ok(crate::Configuration {
metadata: convert_metadata(parsed_config.metadata),
access_key_id,
secret_access_key,
// provider_name,
url,
region,
// pool_settings: parsed_config.pool_settings,
// mutations_version: convert_mutations_version(parsed_config.mutations_version),
})
}

Expand All @@ -64,8 +67,6 @@ pub fn convert_metadata(metadata: metadata::Metadata) -> query_engine_metadata::
query_engine_metadata::metadata::Metadata {
tables: convert_tables(metadata.tables),
scalar_types: convert_scalar_types(metadata.scalar_types),
// composite_types: convert_composite_types(metadata.composite_types),
// native_operations: convert_native_operations(metadata.native_operations),
}
}

Expand Down
15 changes: 15 additions & 0 deletions crates/configuration/src/values/connection_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ impl From<&str> for Region {
Self::from(value.to_string())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
pub struct Url(pub Secret);

impl From<String> for Url {
fn from(value: String) -> Self {
Self(value.into())
}
}

impl From<&str> for Url {
fn from(value: &str) -> Self {
Self::from(value.to_string())
}
}
61 changes: 35 additions & 26 deletions crates/configuration/src/version1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Internal Configuration and state for our connector.

use crate::environment::Environment;
use crate::environment::{Environment, Variable};
use crate::error::WriteParsedConfigurationError;
use crate::values::Secret;
use crate::{connection_settings, AccessKeyId, SecretAccessKey};
Expand Down Expand Up @@ -79,45 +79,54 @@ pub async fn introspect(
Cow::Owned(environment.read(variable)?)
}
};
// let provider_name = match &args.connection_settings.provider_name {
// ProviderName(Secret::Plain(value)) => Cow::Borrowed(value),
// ProviderName(Secret::FromEnvironment { variable }) => Cow::Owned(environment.read(variable)?),
// };
let url = environment
.read(&Variable::from("HASURA_DYNAMODB_URL"))
.ok();
let region = match &args.connection_settings.region {
crate::Region(Secret::Plain(value)) => Cow::Borrowed(value),
crate::Region(Secret::FromEnvironment { variable }) => {
Cow::Owned(environment.read(variable)?)
}
};
// let access_key_id = args.connection_settings.access_key_id.clone();
// let secret_access_key = args.connection_settings.secret_access_key.clone();
// let session_token = args.connection_settings.session_token.clone();
// let region = args.connection_settings.region.clone();
// let config = aws_config::load_from_env().await;

let credentials = aws_sdk_dynamodb::config::Credentials::new(
access_key_id.to_string(),
secret_access_key.to_string(),
None, // Optional session token
None, // Expiration (None for non-expiring)
"my-provider", // Provider name
);

// Configure AWS SDK with explicit credentials
let config = Config::builder()
let config_builder = Config::builder()
.region(aws_config::Region::new(region.to_string()))
.credentials_provider(credentials)
.behavior_version_latest()
.build();

// To use localhost url
// let config = aws_config::defaults(aws_config::BehaviorVersion::latest())
// .test_credentials()
// .region(aws_config::Region::new("us-west-2"))
// // DynamoDB run locally uses port 8000 by default.
// .endpoint_url("http://localhost:8085")
// .load()
// .await;
// let dynamodb_local_config = aws_sdk_dynamodb::config::Builder::from(&config).build();
.behavior_version_latest();

let config = match url {
Some(aws_url) => config_builder.endpoint_url(aws_url).build(),
None => config_builder.build(),
};

// let config = if url.is_empty() {
// Config::builder()
// .region(aws_config::Region::new(region.to_string()))
// .credentials_provider(credentials.clone())
// .behavior_version_latest()
// .build()
// } else {
// Config::builder()
// .region(aws_config::Region::new(region.to_string()))
// .credentials_provider(credentials.clone())
// .behavior_version_latest()
// .endpoint_url(url.to_string())
// .build()
// };

// let config = Config::builder()
// .region(aws_config::Region::new(region.to_string()))
// .credentials_provider(credentials)
// .behavior_version_latest()
// .endpoint_url(url.to_string())
// .build();

let client = aws_sdk_dynamodb::Client::from_conf(config);
let tables_result = client.list_tables().send().await;
Expand Down Expand Up @@ -293,7 +302,7 @@ pub async fn introspect(
connection_settings: connection_settings::DatabaseConnectionSettings {
access_key_id: args.connection_settings.access_key_id.clone(),
secret_access_key: args.connection_settings.secret_access_key.clone(),
// provider_name: args.connection_settings.provider_name.clone(),
url: args.connection_settings.url.clone(),
region: args.connection_settings.region.clone(),
},
metadata: metadata::Metadata {
Expand Down
18 changes: 15 additions & 3 deletions crates/ndc-dynamodb/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub async fn create_state(
let access_key_id = configuration.access_key_id.clone();
let secret_access_key = configuration.secret_access_key.clone();
let region = configuration.region.clone();
let url = configuration.url.clone();

let credentials = aws_sdk_dynamodb::config::Credentials::new(
access_key_id,
Expand All @@ -40,11 +41,22 @@ pub async fn create_state(
"my-provider", // Provider name
);

let config = Config::builder()
// let config = Config::builder()
// .region(aws_config::Region::new(region.to_string()))
// .credentials_provider(credentials)
// .behavior_version_latest()
// .endpoint_url(url.to_string())
// .build();

let config_builder = Config::builder()
.region(aws_config::Region::new(region))
.credentials_provider(credentials)
.behavior_version_latest()
.build();
.behavior_version_latest();

let config = match url {
Some(aws_url) => config_builder.endpoint_url(aws_url).build(),
None => config_builder.build(),
};

let client = aws_sdk_dynamodb::Client::from_conf(config);

Expand Down
Loading