From 9b09b34a08596891098214fc636257ee89b8a04a Mon Sep 17 00:00:00 2001 From: py Date: Tue, 3 Dec 2024 23:16:16 +0530 Subject: [PATCH] add optional url --- crates/cli/src/lib.rs | 54 +++++++--------- crates/configuration/src/configuration.rs | 2 +- .../configuration/src/connection_settings.rs | 12 ++-- .../src/to_runtime_configuration.rs | 13 ++-- .../src/values/connection_info.rs | 15 +++++ crates/configuration/src/version1.rs | 61 +++++++++++-------- crates/ndc-dynamodb/src/state.rs | 18 +++++- 7 files changed, 102 insertions(+), 73 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 690f75b..1d70c52 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -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. @@ -54,7 +47,6 @@ pub async fn run(command: Command, context: Context) -> 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(()) } @@ -95,31 +87,31 @@ async fn initialize(with_metadata: bool, context: Context) -> }, ), 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:readonlyuser@35.236.11.122: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, diff --git a/crates/configuration/src/configuration.rs b/crates/configuration/src/configuration.rs index 3548be3..499e6f7 100644 --- a/crates/configuration/src/configuration.rs +++ b/crates/configuration/src/configuration.rs @@ -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, pub region: String, // pub mutations_version: Option, } diff --git a/crates/configuration/src/connection_settings.rs b/crates/configuration/src/connection_settings.rs index fd5743e..cd9cc64 100644 --- a/crates/configuration/src/connection_settings.rs +++ b/crates/configuration/src/connection_settings.rs @@ -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. @@ -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, } @@ -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(), }), diff --git a/crates/configuration/src/to_runtime_configuration.rs b/crates/configuration/src/to_runtime_configuration.rs index ebe9efa..5802d0a 100644 --- a/crates/configuration/src/to_runtime_configuration.rs +++ b/crates/configuration/src/to_runtime_configuration.rs @@ -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}; @@ -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), }) } @@ -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), } } diff --git a/crates/configuration/src/values/connection_info.rs b/crates/configuration/src/values/connection_info.rs index ba71cca..eb6de57 100644 --- a/crates/configuration/src/values/connection_info.rs +++ b/crates/configuration/src/values/connection_info.rs @@ -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 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()) + } +} diff --git a/crates/configuration/src/version1.rs b/crates/configuration/src/version1.rs index 584a4dc..d243e35 100644 --- a/crates/configuration/src/version1.rs +++ b/crates/configuration/src/version1.rs @@ -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}; @@ -79,21 +79,16 @@ 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(), @@ -101,23 +96,37 @@ pub async fn introspect( 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; @@ -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 { diff --git a/crates/ndc-dynamodb/src/state.rs b/crates/ndc-dynamodb/src/state.rs index 3ca5b21..862ac53 100644 --- a/crates/ndc-dynamodb/src/state.rs +++ b/crates/ndc-dynamodb/src/state.rs @@ -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, @@ -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);