Skip to content

Commit 567b86d

Browse files
committed
improve
1 parent b0fc8b0 commit 567b86d

5 files changed

Lines changed: 40 additions & 91 deletions

File tree

src/api/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,18 @@ impl AuthenticatedApi<'_> {
989989
self.post(&path, body)
990990
}
991991

992-
/// Fetches preprod retention settings for the given organization.
993-
pub fn fetch_preprod_retention(&self, org: &str) -> ApiResult<PreprodRetention> {
994-
let path = format!("/api/0/organizations/{}/preprod/retention/", PathArg(org));
995-
self.get(&path)?
996-
.convert_rnf(ApiErrorKind::OrganizationNotFound)
992+
/// Fetches upload options for snapshots.
993+
pub fn fetch_snapshots_upload_options(
994+
&self,
995+
org: &str,
996+
project: &str,
997+
) -> ApiResult<SnapshotUploadOptions> {
998+
let path = format!(
999+
"/api/0/projects/{}/{}/preprodartifacts/snapshots/upload-options/",
1000+
PathArg(org),
1001+
PathArg(project)
1002+
);
1003+
self.get(&path)?.convert()
9971004
}
9981005
}
9991006

@@ -1980,9 +1987,18 @@ pub struct LogEntry {
19801987
pub message: Option<String>,
19811988
}
19821989

1983-
/// Preprod retention settings for an organization.
1990+
/// Upload options returned by the snapshots upload-options endpoint.
19841991
#[derive(Debug, Deserialize)]
1985-
pub struct PreprodRetention {
1986-
/// Retention period for snapshots, in days.
1987-
pub snapshots: u64,
1992+
#[serde(rename_all = "camelCase")]
1993+
pub struct SnapshotUploadOptions {
1994+
pub objectstore: ObjectstoreUploadOptions,
1995+
}
1996+
1997+
/// Objectstore configuration for file uploads.
1998+
#[derive(Debug, Deserialize)]
1999+
#[serde(rename_all = "camelCase")]
2000+
pub struct ObjectstoreUploadOptions {
2001+
pub url: String,
2002+
pub scopes: Vec<(String, String)>,
2003+
pub expiration_policy: String,
19882004
}

src/commands/build/snapshots.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::fs;
33
use std::path::Path;
4-
use std::time::Duration;
4+
use std::str::FromStr as _;
55

66
use anyhow::{Context as _, Result};
77
use clap::{Arg, ArgMatches, Command};
@@ -16,9 +16,7 @@ use walkdir::WalkDir;
1616

1717
use crate::api::Api;
1818
use crate::config::{Auth, Config};
19-
use crate::utils::api::get_org_project_id;
2019
use crate::utils::args::ArgExt as _;
21-
use crate::utils::objectstore::get_objectstore_url;
2220
use http::{self, HeaderValue};
2321

2422
const EXPERIMENTAL_WARNING: &str =
@@ -294,35 +292,26 @@ fn read_jpeg_dimensions(data: &[u8]) -> Option<(u32, u32)> {
294292
}
295293

296294
fn upload_images(images: &[ImageInfo], org: &str, project: &str) -> Result<()> {
297-
let config = Config::current();
298-
let auth = config
299-
.get_auth()
300-
.ok_or_else(|| anyhow::anyhow!("Authentication required"))?;
301-
let token = match auth {
302-
Auth::Token(token) => token.raw().expose_secret(),
303-
};
304-
305295
let api = Api::current();
306296
let authenticated_api = api.authenticated()?;
307-
let retention = authenticated_api.fetch_preprod_retention(org)?;
308-
let expiration =
309-
ExpirationPolicy::TimeToLive(Duration::from_secs(retention.snapshots * 24 * 60 * 60));
310-
311-
let url = get_objectstore_url(&authenticated_api, org)?;
312-
let header_value = HeaderValue::from_str(&format!("Bearer {token}"))
313-
.context("Auth token contains invalid characters for HTTP header")?;
314-
let client = ClientBuilder::new(url)
297+
let options = authenticated_api.fetch_snapshots_upload_options(org, project)?;
298+
299+
let expiration = ExpirationPolicy::from_str(&options.objectstore.expiration_policy)
300+
.context("Failed to parse expiration policy from upload options")?;
301+
302+
let client = ClientBuilder::new(options.objectstore.url)
315303
.configure_reqwest(move |r| {
316304
let mut headers = http::HeaderMap::new();
317-
headers.insert(AUTHORIZATION, header_value);
305+
headers.insert(AUTHORIZATION, HeaderValue::from_static("placeholder")); // TODO: get token from upload options endpoint
318306
r.default_headers(headers)
319307
})
320308
.build()?;
321309

322-
let (org_id, project_id) = get_org_project_id(Api::current(), org, project)?;
323-
let session = Usecase::new("preprod")
324-
.for_project(org_id, project_id)
325-
.session(&client)?;
310+
let mut scope = Usecase::new("preprod").scope();
311+
for (key, value) in &options.objectstore.scopes {
312+
scope = scope.push(key, value);
313+
}
314+
let session = scope.session(&client)?;
326315

327316
let runtime = tokio::runtime::Builder::new_current_thread()
328317
.enable_all()
@@ -337,14 +326,12 @@ fn upload_images(images: &[ImageInfo], org: &str, project: &str) -> Result<()> {
337326
let contents = fs::read(&image.path)
338327
.with_context(|| format!("Failed to read image: {}", image.path.display()))?;
339328

340-
let obj_key = format!("{org_id}/{project_id}/{}", image.hash);
341-
342-
info!("Queueing {} as {obj_key}", image.path.display());
329+
info!("Queueing {} as {}", image.path.display(), image.hash);
343330

344331
many_builder = many_builder.push(
345332
session
346333
.put(contents)
347-
.key(&obj_key)
334+
.key(&image.hash)
348335
.expiration_policy(expiration),
349336
);
350337
}

src/utils/api/mod.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/utils/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Various utility functionality.
22
pub mod android;
3-
pub mod api;
43
pub mod args;
54
pub mod auth_token;
65
pub mod build;
@@ -17,7 +16,6 @@ pub mod fs;
1716
pub mod http;
1817
pub mod logging;
1918
pub mod non_empty;
20-
pub mod objectstore;
2119
pub mod progress;
2220
pub mod proguard;
2321
pub mod releases;

src/utils/objectstore.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)