1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::{
    bucket::{IamPolicy, TestIamPermission},
    error::GoogleResponse,
    object::percent_encode,
    resources::common::ListResponse,
    Bucket, NewBucket,
};

/// Operations on [`Bucket`]()s.
#[derive(Debug)]
pub struct BucketClient<'a>(pub(super) &'a super::Client);

impl<'a> BucketClient<'a> {
    /// Creates a new `Bucket`. There are many options that you can provide for creating a new
    /// bucket, so the `NewBucket` resource contains all of them. Note that `NewBucket` implements
    /// `Default`, so you don't have to specify the fields you're not using. And error is returned
    /// if that bucket name is already taken.
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::bucket::{Bucket, NewBucket};
    /// use cloud_storage::bucket::{Location, MultiRegion};
    ///
    /// let client = Client::default();
    /// let new_bucket = NewBucket {
    ///    name: "cloud-storage-rs-doc-1".to_string(), // this is the only mandatory field
    ///    location: Location::Multi(MultiRegion::Eu),
    ///    ..Default::default()
    /// };
    /// let bucket = client.bucket().create(&new_bucket).await?;
    /// # client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(&self, new_bucket: &NewBucket) -> crate::Result<Bucket> {
        let url = format!("{}/b/", crate::BASE_URL);
        let project = &crate::SERVICE_ACCOUNT.project_id;
        let query = [("project", project)];
        let result: GoogleResponse<Bucket> = self
            .0
            .client
            .post(&url)
            .headers(self.0.get_headers().await?)
            .query(&query)
            .json(new_bucket)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Returns all `Bucket`s within this project.
    ///
    /// ### Note
    /// When using incorrect permissions, this function fails silently and returns an empty list.
    ///
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    ///
    /// let client = Client::default();
    /// let buckets = client.bucket().list().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn list(&self) -> crate::Result<Vec<Bucket>> {
        let url = dbg!(format!("{}/b/", crate::BASE_URL));
        let project = &crate::SERVICE_ACCOUNT.project_id;
        let query = [("project", project)];
        dbg!(
            self.0
                .client
                .get(&url)
                .headers(self.0.get_headers().await?)
                .query(&query)
                .send()
                .await?
                .text()
                .await
        )?;
        let result: GoogleResponse<ListResponse<Bucket>> = self
            .0
            .client
            .get(&url)
            .headers(self.0.get_headers().await?)
            .query(&query)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s.items),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Returns a single `Bucket` by its name. If the Bucket does not exist, an error is returned.
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    ///
    /// let client = Client::default();
    /// # use cloud_storage::bucket::NewBucket;
    /// # let new_bucket = NewBucket {
    /// #   name: "cloud-storage-rs-doc-2".to_string(),
    /// #    ..Default::default()
    /// # };
    /// # let _ = client.bucket().create(&new_bucket).await?;
    ///
    /// let bucket = client.bucket().read("cloud-storage-rs-doc-2").await?;
    /// # client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn read(&self, name: &str) -> crate::Result<Bucket> {
        let url = format!("{}/b/{}", crate::BASE_URL, percent_encode(name),);
        let result: GoogleResponse<Bucket> = self
            .0
            .client
            .get(&url)
            .headers(self.0.get_headers().await?)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Update an existing `Bucket`. If you declare you bucket as mutable, you can edit its fields.
    /// You can then flush your changes to Google Cloud Storage using this method.
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::bucket::{Bucket, RetentionPolicy};
    ///
    /// let client = Client::default();
    /// # use cloud_storage::bucket::NewBucket;
    /// # let new_bucket = NewBucket {
    /// #   name: "cloud-storage-rs-doc-3".to_string(),
    /// #    ..Default::default()
    /// # };
    /// # let _ = client.bucket().create(&new_bucket).await?;
    ///
    /// let mut bucket = client.bucket().read("cloud-storage-rs-doc-3").await?;
    /// bucket.retention_policy = Some(RetentionPolicy {
    ///     retention_period: 50,
    ///     effective_time: chrono::Utc::now() + chrono::Duration::seconds(50),
    ///     is_locked: Some(false),
    /// });
    /// client.bucket().update(&bucket).await?;
    /// # client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn update(&self, bucket: &Bucket) -> crate::Result<Bucket> {
        let url = format!("{}/b/{}", crate::BASE_URL, percent_encode(&bucket.name),);
        let result: GoogleResponse<Bucket> = self
            .0
            .client
            .put(&url)
            .headers(self.0.get_headers().await?)
            .json(bucket)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Delete an existing `Bucket`. This permanently removes a bucket from Google Cloud Storage.
    /// An error is returned when you don't have sufficient permissions, or when the
    /// `retention_policy` prevents you from deleting your Bucket.
    /// ### Example
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    ///
    /// let client = Client::default();
    /// # use cloud_storage::bucket::NewBucket;
    /// # let new_bucket = NewBucket {
    /// #   name: "unnecessary-bucket".to_string(),
    /// #    ..Default::default()
    /// # };
    /// # let _ = client.bucket().create(&new_bucket).await?;
    ///
    /// let bucket = client.bucket().read("unnecessary-bucket").await?;
    /// client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn delete(&self, bucket: Bucket) -> crate::Result<()> {
        let url = format!("{}/b/{}", crate::BASE_URL, percent_encode(&bucket.name));
        let response = self
            .0
            .client
            .delete(&url)
            .headers(self.0.get_headers().await?)
            .send()
            .await?;
        if response.status().is_success() {
            Ok(())
        } else {
            Err(crate::Error::Google(response.json().await?))
        }
    }

    /// Returns the [IAM Policy](https://cloud.google.com/iam/docs/) for this bucket.
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    ///
    /// let client = Client::default();
    /// # use cloud_storage::bucket::NewBucket;
    /// # let new_bucket = NewBucket {
    /// #   name: "cloud-storage-rs-doc-4".to_string(),
    /// #    ..Default::default()
    /// # };
    /// # let _ = client.bucket().create(&new_bucket).await?;
    ///
    /// let bucket = client.bucket().read("cloud-storage-rs-doc-4").await?;
    /// let policy = client.bucket().get_iam_policy(&bucket).await?;
    /// # client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_iam_policy(&self, bucket: &Bucket) -> crate::Result<IamPolicy> {
        let url = format!("{}/b/{}/iam", crate::BASE_URL, percent_encode(&bucket.name));
        let result: GoogleResponse<IamPolicy> = self
            .0
            .client
            .get(&url)
            .headers(self.0.get_headers().await?)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Updates the [IAM Policy](https://cloud.google.com/iam/docs/) for this bucket.
    /// ### Example
    /// ```
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    /// use cloud_storage::bucket::{IamPolicy, Binding, IamRole, StandardIamRole, Entity};
    ///
    /// let client = Client::default();
    /// # use cloud_storage::bucket::NewBucket;
    /// # let new_bucket = NewBucket {
    /// #   name: "cloud-storage-rs-doc-5".to_string(),
    /// #    ..Default::default()
    /// # };
    /// # let _ = client.bucket().create(&new_bucket).await?;
    ///
    /// let bucket = client.bucket().read("cloud-storage-rs-doc-5").await?;
    /// let iam_policy = IamPolicy {
    ///     version: 1,
    ///     bindings: vec![
    ///         Binding {
    ///             role: IamRole::Standard(StandardIamRole::ObjectViewer),
    ///             members: vec!["allUsers".to_string()],
    ///             condition: None,
    ///         }
    ///     ],
    ///     ..Default::default()
    /// };
    /// let policy = client.bucket().set_iam_policy(&bucket, &iam_policy).await?;
    /// # client.bucket().delete(bucket).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn set_iam_policy(
        &self,
        bucket: &Bucket,
        iam: &IamPolicy,
    ) -> crate::Result<IamPolicy> {
        let url = format!("{}/b/{}/iam", crate::BASE_URL, percent_encode(&bucket.name));
        let result: GoogleResponse<IamPolicy> = self
            .0
            .client
            .put(&url)
            .headers(self.0.get_headers().await?)
            .json(iam)
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }

    /// Checks whether the user provided in the service account has this permission.
    /// ### Example
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use cloud_storage::Client;
    /// use cloud_storage::Bucket;
    ///
    /// let client = Client::default();
    /// let bucket = client.bucket().read("my-bucket").await?;
    /// client.bucket().test_iam_permission(&bucket, "storage.buckets.get").await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn test_iam_permission(
        &self,
        bucket: &Bucket,
        permission: &str,
    ) -> crate::Result<TestIamPermission> {
        if permission == "storage.buckets.list" || permission == "storage.buckets.create" {
            return Err(crate::Error::new(
                "tested permission must not be `storage.buckets.list` or `storage.buckets.create`",
            ));
        }
        let url = format!(
            "{}/b/{}/iam/testPermissions",
            crate::BASE_URL,
            percent_encode(&bucket.name)
        );
        let result: GoogleResponse<TestIamPermission> = self
            .0
            .client
            .get(&url)
            .headers(self.0.get_headers().await?)
            .query(&[("permissions", permission)])
            .send()
            .await?
            .json()
            .await?;
        match result {
            GoogleResponse::Success(s) => Ok(s),
            GoogleResponse::Error(e) => Err(e.into()),
        }
    }
}