From 7c2b89bb050c3ebda639c78efa19cbf4b269193d Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Wed, 21 Aug 2024 14:05:27 +0200 Subject: [PATCH] Various volumes CLI fixes * Adopt to backend api changes for S3Credentials * better formatting for volume size --- lib/backend-api/schema.graphql | 16 ++++- lib/backend-api/src/query.rs | 58 ++++--------------- lib/backend-api/src/types.rs | 22 ++++++- lib/cli/Cargo.toml | 2 - .../commands/app/volumes/s3_credentials.rs | 4 +- lib/cli/src/types.rs | 23 +++++--- 6 files changed, 64 insertions(+), 61 deletions(-) diff --git a/lib/backend-api/schema.graphql b/lib/backend-api/schema.graphql index e972ff48fa5..3af8f4574fc 100644 --- a/lib/backend-api/schema.graphql +++ b/lib/backend-api/schema.graphql @@ -33,6 +33,7 @@ type User implements Node & PackageOwner & Owner { """Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.""" username: String! + registerIntent: String isEmailValidated: Boolean! bio: String location: String @@ -950,6 +951,7 @@ type DeployApp implements Node & Owner { secrets(offset: Int, before: String, after: String, first: Int, last: Int): SecretConnection! usageMetrics(forRange: MetricRange!, variant: MetricType!): [UsageMetric]! s3Url: URL + s3Credentials: S3Credentials deleted: Boolean! favicon: URL screenshot(viewportSize: AppScreenshotViewportSize, appearance: AppScreenshotAppearance): URL @@ -1090,6 +1092,12 @@ character sequences. """ scalar URL +type S3Credentials { + accessKey: String! + secretKey: String! + endpoint: String! +} + enum AppScreenshotViewportSize { MOBILE DESKTOP @@ -1124,15 +1132,17 @@ enum LogStream { } type AppVersionVolume { + id: ID! name: String! + s3Url: String! mountPaths: [AppVersionVolumeMountPath]! size: Int - usedSize: Int + usedSize: BigInt } type AppVersionVolumeMountPath { path: String! - subpath: String! + subpath: String } type PackageDistribution { @@ -3570,6 +3580,7 @@ input RegisterUserInput { username: CaseInsensitiveString! password: String! acceptedTos: Boolean + intent: String clientMutationId: String } @@ -3583,6 +3594,7 @@ input SocialAuthJWTInput { provider: String! accessToken: String! register: Boolean = false + registerIntent: String clientMutationId: String } diff --git a/lib/backend-api/src/query.rs b/lib/backend-api/src/query.rs index de0d32877cb..29ae874192c 100644 --- a/lib/backend-api/src/query.rs +++ b/lib/backend-api/src/query.rs @@ -209,63 +209,29 @@ pub async fn get_app_volumes( Ok(volumes) } -/// S3 credentials for an app. -/// -/// Retrieved with [`get_app_s3_credentials`]. -#[derive(Clone)] -pub struct AppS3Credentials { - pub url: String, - pub access_key: String, - pub secret_key: String, -} - /// Load the S3 credentials. /// /// S3 can be used to get access to an apps volumes. pub async fn get_app_s3_credentials( client: &WasmerClient, app_id: impl Into, -) -> Result { - const ACCESS_KEY_NAME: &str = "WASMER_APP_S3_ACCESS_KEY"; - const SECRET_KEY_NAME: &str = "WASMER_APP_S3_SECRET_KEY"; - +) -> Result { let app_id = app_id.into(); // Firt load the app to get the s3 url. - let app = get_app_by_id(client, app_id.clone()).await?; - let url = app.s3_url.context("app has no volumes")?; - - // Load the secrets. - let secrets = - get_all_app_secrets_filtered(client, app_id, [ACCESS_KEY_NAME, SECRET_KEY_NAME]).await?; + let app1 = get_app_by_id(client, app_id.clone()).await?; - let access_key_id = secrets - .iter() - .find(|s| s.name == ACCESS_KEY_NAME) - .context("missing access key")? - .id - .clone(); - - let secret_key_id = secrets - .iter() - .find(|s| s.name == SECRET_KEY_NAME) - .context("missing secret key")? - .id - .clone(); - - let access_key = get_app_secret_value_by_id(client, access_key_id.into_inner()) - .await? - .with_context(|| format!("No value found for secret with name '{}'", ACCESS_KEY_NAME))?; - - let secret_key = get_app_secret_value_by_id(client, secret_key_id.into_inner()) + let vars = types::GetDeployAppVars { + owner: app1.owner.global_name, + name: app1.name, + }; + client + .run_graphql_strict(types::GetDeployAppS3Credentials::build(vars)) .await? - .with_context(|| format!("No value found for secret with name '{}'", SECRET_KEY_NAME))?; - - Ok(AppS3Credentials { - url: url.0, - access_key, - secret_key, - }) + .get_deploy_app + .context("app not found")? + .s3_credentials + .context("app does not have S3 credentials") } /// Load all available regions. diff --git a/lib/backend-api/src/types.rs b/lib/backend-api/src/types.rs index 0d66847984d..f528be0461f 100644 --- a/lib/backend-api/src/types.rs +++ b/lib/backend-api/src/types.rs @@ -690,6 +690,26 @@ mod queries { pub get_deploy_app: Option, } + #[derive(cynic::QueryFragment, Debug)] + #[cynic(graphql_type = "Query", variables = "GetDeployAppVars")] + pub struct GetDeployAppS3Credentials { + #[arguments(owner: $owner, name: $name)] + pub get_deploy_app: Option, + } + + #[derive(cynic::QueryFragment, Debug)] + #[cynic(graphql_type = "DeployApp", variables = "GetDeployAppVars")] + pub struct AppWithS3Credentials { + pub s3_credentials: Option, + } + + #[derive(cynic::QueryFragment, Debug)] + pub struct S3Credentials { + pub access_key: String, + pub secret_key: String, + pub endpoint: String, + } + #[derive(cynic::QueryVariables, Debug, Clone)] pub struct PaginationVars { pub offset: Option, @@ -804,7 +824,7 @@ mod queries { pub struct AppVersionVolume { pub name: String, pub size: Option, - pub used_size: Option, + pub used_size: Option, } #[derive(cynic::QueryFragment, Debug)] diff --git a/lib/cli/Cargo.toml b/lib/cli/Cargo.toml index 18d3beecc9e..79d8327a5e4 100644 --- a/lib/cli/Cargo.toml +++ b/lib/cli/Cargo.toml @@ -164,8 +164,6 @@ http.workspace = true is-terminal = "0.4.7" colored = "2.0" anyhow = "1.0" - -# For the inspect subcommand bytesize = "1.0" cfg-if = "1.0" tempfile = "3.6.0" diff --git a/lib/cli/src/commands/app/volumes/s3_credentials.rs b/lib/cli/src/commands/app/volumes/s3_credentials.rs index abfd378924d..92b22d055f3 100644 --- a/lib/cli/src/commands/app/volumes/s3_credentials.rs +++ b/lib/cli/src/commands/app/volumes/s3_credentials.rs @@ -57,13 +57,13 @@ endpoint = {endpoint} app_name = app.name, access_key = creds.access_key, secret_key = creds.secret_key, - endpoint = creds.url, + endpoint = creds.endpoint, ); println!("{}", rclone_config); } else { println!("S3 credentials for app {}:\n", app.name); - println!(" S3 URL: {}", creds.url); + println!(" S3 URL: {}", creds.endpoint); println!(" Access key: {}", creds.access_key); println!(" Secret key: {}", creds.secret_key); println!(); diff --git a/lib/cli/src/types.rs b/lib/cli/src/types.rs index 27fc3349ff3..0b391d036d1 100644 --- a/lib/cli/src/types.rs +++ b/lib/cli/src/types.rs @@ -164,7 +164,7 @@ impl CliRender for wasmer_api::types::AppVersionVolume { vec!["Name".to_string(), self.name.clone()], vec![ "Used size".to_string(), - format_disk_size_opt(self.used_size), + format_disk_size_opt(self.used_size.clone()), ], ]); table.to_string() @@ -173,18 +173,25 @@ impl CliRender for wasmer_api::types::AppVersionVolume { fn render_list_table(items: &[Self]) -> String { let mut table = Table::new(); table.set_header(vec!["Name".to_string(), "Used size".to_string()]); - table.add_rows( - items - .iter() - .map(|vol| vec![vol.name.clone(), format_disk_size_opt(vol.used_size)]), - ); + table.add_rows(items.iter().map(|vol| { + vec![ + vol.name.clone(), + format_disk_size_opt(vol.used_size.clone()), + ] + })); table.to_string() } } -fn format_disk_size_opt(value: Option) -> String { +fn format_disk_size_opt(value: Option) -> String { + let value = value.and_then(|x| { + let y: Option = x.0.try_into().ok(); + y + }); + if let Some(v) = value { - format!("{}Mb", v) + let s = bytesize::ByteSize(v); + s.to_string() } else { "n/a".to_string() }