opendal/services/swift/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::sync::Arc;
21
22use http::Response;
23use http::StatusCode;
24use log::debug;
25
26use super::core::*;
27use super::delete::SwfitDeleter;
28use super::error::parse_error;
29use super::lister::SwiftLister;
30use super::writer::SwiftWriter;
31use crate::raw::*;
32use crate::services::SwiftConfig;
33use crate::*;
34
35impl Configurator for SwiftConfig {
36    type Builder = SwiftBuilder;
37    fn into_builder(self) -> Self::Builder {
38        SwiftBuilder { config: self }
39    }
40}
41
42/// [OpenStack Swift](https://docs.openstack.org/api-ref/object-store/#)'s REST API support.
43/// For more information about swift-compatible services, refer to [Compatible Services](#compatible-services).
44#[doc = include_str!("docs.md")]
45#[doc = include_str!("compatible_services.md")]
46#[derive(Default, Clone)]
47pub struct SwiftBuilder {
48    config: SwiftConfig,
49}
50
51impl Debug for SwiftBuilder {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        let mut d = f.debug_struct("SwiftBuilder");
54        d.field("config", &self.config);
55        d.finish_non_exhaustive()
56    }
57}
58
59impl SwiftBuilder {
60    /// Set the remote address of this backend
61    ///
62    /// Endpoints should be full uri, e.g.
63    ///
64    /// - `http://127.0.0.1:8080/v1/AUTH_test`
65    /// - `http://192.168.66.88:8080/swift/v1`
66    /// - `https://openstack-controller.example.com:8080/v1/ccount`
67    ///
68    /// If user inputs endpoint without scheme, we will
69    /// prepend `https://` to it.
70    pub fn endpoint(mut self, endpoint: &str) -> Self {
71        self.config.endpoint = if endpoint.is_empty() {
72            None
73        } else {
74            Some(endpoint.trim_end_matches('/').to_string())
75        };
76        self
77    }
78
79    /// Set container of this backend.
80    ///
81    /// All operations will happen under this container. It is required. e.g. `snapshots`
82    pub fn container(mut self, container: &str) -> Self {
83        self.config.container = if container.is_empty() {
84            None
85        } else {
86            Some(container.trim_end_matches('/').to_string())
87        };
88        self
89    }
90
91    /// Set root of this backend.
92    ///
93    /// All operations will happen under this root.
94    pub fn root(mut self, root: &str) -> Self {
95        self.config.root = if root.is_empty() {
96            None
97        } else {
98            Some(root.to_string())
99        };
100
101        self
102    }
103
104    /// Set the token of this backend.
105    ///
106    /// Default to empty string.
107    pub fn token(mut self, token: &str) -> Self {
108        if !token.is_empty() {
109            self.config.token = Some(token.to_string());
110        }
111        self
112    }
113}
114
115impl Builder for SwiftBuilder {
116    const SCHEME: Scheme = Scheme::Swift;
117    type Config = SwiftConfig;
118
119    /// Build a SwiftBackend.
120    fn build(self) -> Result<impl Access> {
121        debug!("backend build started: {:?}", &self);
122
123        let root = normalize_root(&self.config.root.unwrap_or_default());
124        debug!("backend use root {}", root);
125
126        let endpoint = match self.config.endpoint {
127            Some(endpoint) => {
128                if endpoint.starts_with("http") {
129                    endpoint
130                } else {
131                    format!("https://{endpoint}")
132                }
133            }
134            None => {
135                return Err(Error::new(
136                    ErrorKind::ConfigInvalid,
137                    "missing endpoint for Swift",
138                ));
139            }
140        };
141        debug!("backend use endpoint: {}", &endpoint);
142
143        let container = match self.config.container {
144            Some(container) => container,
145            None => {
146                return Err(Error::new(
147                    ErrorKind::ConfigInvalid,
148                    "missing container for Swift",
149                ));
150            }
151        };
152
153        let token = self.config.token.unwrap_or_default();
154
155        Ok(SwiftBackend {
156            core: Arc::new(SwiftCore {
157                info: {
158                    let am = AccessorInfo::default();
159                    am.set_scheme(Scheme::Swift)
160                        .set_root(&root)
161                        .set_native_capability(Capability {
162                            stat: true,
163                            stat_has_cache_control: true,
164                            stat_has_content_length: true,
165                            stat_has_content_type: true,
166                            stat_has_content_encoding: true,
167                            stat_has_content_range: true,
168                            stat_has_etag: true,
169                            stat_has_content_md5: true,
170                            stat_has_last_modified: true,
171                            stat_has_content_disposition: true,
172                            stat_has_user_metadata: true,
173                            read: true,
174
175                            write: true,
176                            write_can_empty: true,
177                            write_with_user_metadata: true,
178
179                            delete: true,
180
181                            list: true,
182                            list_with_recursive: true,
183                            list_has_content_length: true,
184                            list_has_content_md5: true,
185                            list_has_content_type: true,
186                            list_has_last_modified: true,
187
188                            shared: true,
189
190                            ..Default::default()
191                        });
192                    am.into()
193                },
194                root,
195                endpoint,
196                container,
197                token,
198            }),
199        })
200    }
201}
202
203/// Backend for Swift service
204#[derive(Debug, Clone)]
205pub struct SwiftBackend {
206    core: Arc<SwiftCore>,
207}
208
209impl Access for SwiftBackend {
210    type Reader = HttpBody;
211    type Writer = oio::OneShotWriter<SwiftWriter>;
212    type Lister = oio::PageLister<SwiftLister>;
213    type Deleter = oio::OneShotDeleter<SwfitDeleter>;
214    type BlockingReader = ();
215    type BlockingWriter = ();
216    type BlockingLister = ();
217    type BlockingDeleter = ();
218
219    fn info(&self) -> Arc<AccessorInfo> {
220        self.core.info.clone()
221    }
222
223    async fn stat(&self, path: &str, _args: OpStat) -> Result<RpStat> {
224        let resp = self.core.swift_get_metadata(path).await?;
225
226        match resp.status() {
227            StatusCode::OK | StatusCode::NO_CONTENT => {
228                let headers = resp.headers();
229                let mut meta = parse_into_metadata(path, headers)?;
230                let user_meta = parse_prefixed_headers(headers, "x-object-meta-");
231                if !user_meta.is_empty() {
232                    meta.with_user_metadata(user_meta);
233                }
234
235                Ok(RpStat::new(meta))
236            }
237            _ => Err(parse_error(resp)),
238        }
239    }
240
241    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
242        let resp = self.core.swift_read(path, args.range(), &args).await?;
243
244        let status = resp.status();
245
246        match status {
247            StatusCode::OK | StatusCode::PARTIAL_CONTENT => Ok((RpRead::new(), resp.into_body())),
248            _ => {
249                let (part, mut body) = resp.into_parts();
250                let buf = body.to_buffer().await?;
251                Err(parse_error(Response::from_parts(part, buf)))
252            }
253        }
254    }
255
256    async fn write(&self, path: &str, args: OpWrite) -> Result<(RpWrite, Self::Writer)> {
257        let writer = SwiftWriter::new(self.core.clone(), args.clone(), path.to_string());
258
259        let w = oio::OneShotWriter::new(writer);
260
261        Ok((RpWrite::default(), w))
262    }
263
264    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
265        Ok((
266            RpDelete::default(),
267            oio::OneShotDeleter::new(SwfitDeleter::new(self.core.clone())),
268        ))
269    }
270
271    async fn list(&self, path: &str, args: OpList) -> Result<(RpList, Self::Lister)> {
272        let l = SwiftLister::new(
273            self.core.clone(),
274            path.to_string(),
275            args.recursive(),
276            args.limit(),
277        );
278
279        Ok((RpList::default(), oio::PageLister::new(l)))
280    }
281
282    async fn copy(&self, from: &str, to: &str, _args: OpCopy) -> Result<RpCopy> {
283        // cannot copy objects larger than 5 GB.
284        // Reference: https://docs.openstack.org/api-ref/object-store/#copy-object
285        let resp = self.core.swift_copy(from, to).await?;
286
287        let status = resp.status();
288
289        match status {
290            StatusCode::CREATED | StatusCode::OK => Ok(RpCopy::default()),
291            _ => Err(parse_error(resp)),
292        }
293    }
294}