|
| 1 | +// MinIO Rust Library for Amazon S3 Compatible Cloud Storage |
| 2 | +// Copyright 2025 MinIO, Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +use crate::s3::builders::SegmentedBytes; |
| 17 | +use crate::s3::error::Error; |
| 18 | +use crate::s3::response::SetBucketVersioningResponse; |
| 19 | +use crate::s3::types::{S3Api, S3Request, ToS3Request}; |
| 20 | +use crate::s3::utils::{check_bucket_name, merge, Multimap}; |
| 21 | +use crate::s3::Client; |
| 22 | +use bytes::Bytes; |
| 23 | +use http::Method; |
| 24 | + |
| 25 | +/// Argument builder for [set_bucket_encryption()](Client::set_bucket_encryption) API |
| 26 | +#[derive(Clone, Debug, Default)] |
| 27 | +pub struct SetBucketVersioning { |
| 28 | + pub(crate) client: Option<Client>, |
| 29 | + |
| 30 | + pub(crate) extra_headers: Option<Multimap>, |
| 31 | + pub(crate) extra_query_params: Option<Multimap>, |
| 32 | + pub(crate) region: Option<String>, |
| 33 | + pub(crate) bucket: String, |
| 34 | + |
| 35 | + pub(crate) status: bool, |
| 36 | + pub(crate) mfa_delete: Option<bool>, |
| 37 | +} |
| 38 | + |
| 39 | +impl SetBucketVersioning { |
| 40 | + pub fn new(bucket: &str) -> Self { |
| 41 | + Self { |
| 42 | + bucket: bucket.to_owned(), |
| 43 | + ..Default::default() |
| 44 | + } |
| 45 | + } |
| 46 | + pub fn client(mut self, client: &Client) -> Self { |
| 47 | + self.client = Some(client.clone()); |
| 48 | + self |
| 49 | + } |
| 50 | + |
| 51 | + pub fn extra_headers(mut self, extra_headers: Option<Multimap>) -> Self { |
| 52 | + self.extra_headers = extra_headers; |
| 53 | + self |
| 54 | + } |
| 55 | + |
| 56 | + pub fn extra_query_params(mut self, extra_query_params: Option<Multimap>) -> Self { |
| 57 | + self.extra_query_params = extra_query_params; |
| 58 | + self |
| 59 | + } |
| 60 | + |
| 61 | + pub fn region(mut self, region: Option<String>) -> Self { |
| 62 | + self.region = region; |
| 63 | + self |
| 64 | + } |
| 65 | + |
| 66 | + pub fn status(mut self, status: bool) -> Self { |
| 67 | + self.status = status; |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + pub fn mfa_delete(mut self, mfa_delete: Option<bool>) -> Self { |
| 72 | + self.mfa_delete = mfa_delete; |
| 73 | + self |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl S3Api for SetBucketVersioning { |
| 78 | + type S3Response = SetBucketVersioningResponse; |
| 79 | +} |
| 80 | + |
| 81 | +impl ToS3Request for SetBucketVersioning { |
| 82 | + fn to_s3request(&self) -> Result<S3Request, Error> { |
| 83 | + check_bucket_name(&self.bucket, true)?; |
| 84 | + let mut headers = Multimap::new(); |
| 85 | + if let Some(v) = &self.extra_headers { |
| 86 | + merge(&mut headers, v); |
| 87 | + } |
| 88 | + |
| 89 | + let mut query_params = Multimap::new(); |
| 90 | + if let Some(v) = &self.extra_query_params { |
| 91 | + merge(&mut query_params, v); |
| 92 | + } |
| 93 | + query_params.insert(String::from("versioning"), String::new()); |
| 94 | + |
| 95 | + let mut data = String::from("<VersioningConfiguration>"); |
| 96 | + data.push_str("<Status>"); |
| 97 | + data.push_str(match self.status { |
| 98 | + true => "Enabled", |
| 99 | + false => "Suspended", |
| 100 | + }); |
| 101 | + data.push_str("</Status>"); |
| 102 | + if let Some(v) = self.mfa_delete { |
| 103 | + data.push_str("<MFADelete>"); |
| 104 | + data.push_str(match v { |
| 105 | + true => "Enabled", |
| 106 | + false => "Disabled", |
| 107 | + }); |
| 108 | + data.push_str("</MFADelete>"); |
| 109 | + } |
| 110 | + data.push_str("</VersioningConfiguration>"); |
| 111 | + |
| 112 | + let bytes: Bytes = data.into(); |
| 113 | + let body: Option<SegmentedBytes> = Some(SegmentedBytes::from(bytes)); |
| 114 | + |
| 115 | + let req = S3Request::new( |
| 116 | + self.client.as_ref().ok_or(Error::NoClientProvided)?, |
| 117 | + Method::GET, |
| 118 | + ) |
| 119 | + .region(self.region.as_deref()) |
| 120 | + .bucket(Some(&self.bucket)) |
| 121 | + .query_params(query_params) |
| 122 | + .headers(headers) |
| 123 | + .body(body); |
| 124 | + |
| 125 | + Ok(req) |
| 126 | + } |
| 127 | +} |
0 commit comments