azure_cosmos_mirror/operations/
replace_collection.rs1use crate::headers::from_headers::*;
2use crate::prelude::*;
3use crate::resources::collection::{IndexingPolicy, PartitionKey};
4use azure_core::headers::{
5 content_type_from_headers, etag_from_headers, session_token_from_headers,
6};
7use azure_core::Response as HttpResponse;
8use time::OffsetDateTime;
9
10operation! {
11 ReplaceCollection,
12 client: CollectionClient,
13 partition_key: PartitionKey,
14 ?indexing_policy: IndexingPolicy,
15 ?consistency_level: ConsistencyLevel
16}
17
18impl ReplaceCollectionBuilder {
19 pub fn into_future(self) -> ReplaceCollection {
20 Box::pin(async move {
21 let mut request = self.client.collection_request(azure_core::Method::Put);
22
23 if let Some(cl) = &self.consistency_level {
24 request.insert_headers(cl);
25 }
26
27 let collection = ReplaceCollectionBody {
28 id: self.client.collection_name(),
29 indexing_policy: &self.indexing_policy,
30 partition_key: &self.partition_key,
31 };
32
33 request.set_body(serde_json::to_vec(&collection)?);
34
35 let response = self
36 .client
37 .pipeline()
38 .send(
39 self.context.clone().insert(ResourceType::Collections),
40 &mut request,
41 )
42 .await?;
43
44 ReplaceCollectionResponse::try_from(response).await
45 })
46 }
47}
48
49#[derive(Serialize, Debug)]
50struct ReplaceCollectionBody<'a> {
51 pub id: &'a str,
52 #[serde(rename = "indexingPolicy", skip_serializing_if = "Option::is_none")]
53 pub indexing_policy: &'a Option<IndexingPolicy>,
54 #[serde(rename = "partitionKey")]
55 pub partition_key: &'a PartitionKey,
56}
57
58#[derive(Debug, Clone)]
59pub struct ReplaceCollectionResponse {
60 pub collection: Collection,
61 pub lsn: u64,
62 pub cosmos_quorum_acked_llsn: u64,
63 pub current_replica_set_size: u64,
64 pub number_of_read_regions: u32,
65 pub etag: String,
66 pub charge: f64,
67 pub current_write_quorum: u64,
68 pub server: String,
69 pub collection_partition_index: u64,
70 pub global_committed_lsn: u64,
71 pub session_token: String,
72 pub cosmos_llsn: u64,
73 pub xp_role: u32,
74 pub gateway_version: String,
75 pub collection_service_index: u64,
76 pub content_type: String,
77 pub transport_request_id: u64,
78 pub alt_content_path: String,
79 pub service_version: String,
80 pub quorum_acked_lsn: u64,
81 pub last_state_change: OffsetDateTime,
82 pub date: OffsetDateTime,
83 pub content_location: Option<String>,
84 pub activity_id: uuid::Uuid,
85 pub schema_version: String,
86}
87
88impl ReplaceCollectionResponse {
89 pub async fn try_from(response: HttpResponse) -> azure_core::Result<Self> {
90 let (_status_code, headers, body) = response.deconstruct();
91 let body = body.collect().await?;
92 Ok(Self {
93 collection: serde_json::from_slice(&body)?,
94 last_state_change: last_state_change_from_headers(&headers)?,
95 etag: etag_from_headers(&headers)?,
96 collection_partition_index: collection_partition_index_from_headers(&headers)?,
97 collection_service_index: collection_service_index_from_headers(&headers)?,
98 schema_version: schema_version_from_headers(&headers)?,
99 alt_content_path: alt_content_path_from_headers(&headers)?,
100 charge: request_charge_from_headers(&headers)?,
101 service_version: service_version_from_headers(&headers)?,
102 activity_id: activity_id_from_headers(&headers)?,
103 session_token: session_token_from_headers(&headers)?,
104 gateway_version: gateway_version_from_headers(&headers)?,
105 global_committed_lsn: global_committed_lsn_from_headers(&headers)?,
106 cosmos_llsn: cosmos_llsn_from_headers(&headers)?,
107 number_of_read_regions: number_of_read_regions_from_headers(&headers)?,
108 quorum_acked_lsn: quorum_acked_lsn_from_headers(&headers)?,
109 current_write_quorum: current_write_quorum_from_headers(&headers)?,
110 current_replica_set_size: current_replica_set_size_from_headers(&headers)?,
111 lsn: lsn_from_headers(&headers)?,
112 cosmos_quorum_acked_llsn: cosmos_quorum_acked_llsn_from_headers(&headers)?,
113 server: server_from_headers(&headers)?,
114 xp_role: role_from_headers(&headers)?,
115 content_type: content_type_from_headers(&headers)?,
116 content_location: content_location_from_headers(&headers)?,
117 transport_request_id: transport_request_id_from_headers(&headers)?,
118 date: date_from_headers(&headers)?,
119 })
120 }
121}