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

blob_storage: Make Snapshot type consistent. #1008

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 46 additions & 5 deletions sdk/storage_blobs/src/blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ use azure_core::{
content_type, date,
headers::{self, Headers},
parsing::from_azure_time,
Etag, LeaseDuration, LeaseState, LeaseStatus,
AppendToUrlQuery, Etag, LeaseDuration, LeaseState, LeaseStatus,
};
use azure_storage::{ConsistencyCRC64, ConsistencyMD5, CopyId, CopyProgress};
use std::borrow::Cow;
use std::collections::HashMap;
use std::str::FromStr;
use time::OffsetDateTime;

#[cfg(feature = "azurite_workaround")]
Expand Down Expand Up @@ -59,7 +61,10 @@ create_enum!(RehydratePriority, (High, "High"), (Standard, "Standard"));

create_enum!(PageWriteType, (Update, "update"), (Clear, "clear"));

use azure_core::error::Error;
use azure_core::headers::HeaderName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these use statements be added in the existing azure_core entry above?

use serde::{self, Deserialize, Deserializer};

fn deserialize_crc64_optional<'de, D>(deserializer: D) -> Result<Option<ConsistencyCRC64>, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -88,7 +93,7 @@ where
#[serde(rename_all = "PascalCase")]
pub struct Blob {
pub name: String,
pub snapshot: Option<OffsetDateTime>,
pub snapshot: Option<Snapshot>,
pub version_id: Option<String>,
pub is_current_version: Option<bool>,
pub deleted: Option<bool>,
Expand Down Expand Up @@ -230,9 +235,7 @@ impl Blob {

let tags = h.get_optional_as(&headers::TAGS)?;

// TODO: Retrieve the snapshot time from
// the headers
let snapshot = None;
let snapshot = h.get_optional_as(&SNAPSHOT)?;

Ok(Blob {
name: blob_name.into(),
Expand Down Expand Up @@ -287,3 +290,41 @@ impl Blob {
pub(crate) fn copy_status_from_headers(headers: &Headers) -> azure_core::Result<CopyStatus> {
headers.get_as(&headers::COPY_STATUS)
}

pub const SNAPSHOT: HeaderName = HeaderName::from_static("x-ms-snapshot");

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Snapshot(Cow<'static, str>);

impl Snapshot {
pub fn new<S: Into<Cow<'static, str>>>(s: S) -> Self {
Self(s.into())
}

pub const fn from_static(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
}

impl<S> From<S> for Snapshot
where
S: Into<Cow<'static, str>>,
{
fn from(f: S) -> Self {
Self::new(f)
}
}

impl FromStr for Snapshot {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(s.to_string()))
}
}

impl AppendToUrlQuery for Snapshot {
fn append_to_url_query(&self, url: &mut url::Url) {
url.query_pairs_mut().append_pair("snapshot", &self.0);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::blob::Snapshot;
use crate::{blob::operations::DeleteBlobResponse, prelude::*};
use azure_core::{headers::Headers, prelude::*};

Expand Down
5 changes: 2 additions & 3 deletions sdk/storage_blobs/src/blob/operations/snapshot_blob.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::blob::{Snapshot, SNAPSHOT};
use crate::prelude::*;
use azure_core::headers::{etag_from_headers, HeaderName};
use azure_core::headers::etag_from_headers;
use azure_core::{
headers::{date_from_headers, last_modified_from_headers, request_id_from_headers, Headers},
prelude::*,
Expand Down Expand Up @@ -64,5 +65,3 @@ impl TryFrom<&Headers> for SnapshotBlobResponse {
})
}
}

pub const SNAPSHOT: HeaderName = HeaderName::from_static("x-ms-snapshot");
1 change: 1 addition & 0 deletions sdk/storage_blobs/src/clients/blob_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::blob::Snapshot;
use crate::{
blob::operations::*,
options::{BA512Range, Tags},
Expand Down
3 changes: 2 additions & 1 deletion sdk/storage_blobs/src/options/blob_versioning.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Snapshot, VersionId};
use super::VersionId;
use crate::blob::Snapshot;
use azure_core::AppendToUrlQuery;

#[derive(Debug, Clone)]
Expand Down
8 changes: 0 additions & 8 deletions sdk/storage_blobs/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,3 @@ request_query!(
VersionId,
"version_id"
);

request_query!(
/// This type could also be a DateTime but the docs clearly states to treat is as opaque so we do not convert it in any way.
///
/// See: <https://docs.microsoft.com/rest/api/storageservices/get-blob>"]
Snapshot,
"snapshot"
);