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
use crate::{
bucket_access_control::Entity,
default_object_access_control::{DefaultObjectAccessControl, NewDefaultObjectAccessControl},
error::GoogleResponse,
object::percent_encode,
resources::common::ListResponse,
};
/// Operations on [`DefaultObjectAccessControl`](DefaultObjectAccessControl)s.
#[derive(Debug)]
pub struct DefaultObjectAccessControlClient<'a>(pub(super) &'a super::Client);
impl<'a> DefaultObjectAccessControlClient<'a> {
/// Create a new `DefaultObjectAccessControl` entry on the specified bucket.
/// ### Important
/// Important: This method fails with a `400 Bad Request` response for buckets with uniform
/// bucket-level access enabled. Use `Bucket::get_iam_policy` and `Bucket::set_iam_policy` to
/// control access instead.
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::default_object_access_control::{
/// DefaultObjectAccessControl, NewDefaultObjectAccessControl, Role, Entity,
/// };
///
/// let client = Client::default();
/// let new_acl = NewDefaultObjectAccessControl {
/// entity: Entity::AllAuthenticatedUsers,
/// role: Role::Reader,
/// };
/// let default_acl = client.default_object_access_control().create("mybucket", &new_acl).await?;
/// # client.default_object_access_control().delete(default_acl).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create(
&self,
bucket: &str,
new_acl: &NewDefaultObjectAccessControl,
) -> crate::Result<DefaultObjectAccessControl> {
let url = format!(
"{}/b/{}/defaultObjectAcl",
crate::BASE_URL,
percent_encode(bucket)
);
let result: GoogleResponse<DefaultObjectAccessControl> = self
.0
.client
.post(&url)
.headers(self.0.get_headers().await?)
.json(new_acl)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(mut s) => {
s.bucket = bucket.to_string();
Ok(s)
}
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Retrieves default object ACL entries on the specified bucket.
/// ### Important
/// Important: This method fails with a `400 Bad Request` response for buckets with uniform
/// bucket-level access enabled. Use `Bucket::get_iam_policy` and `Bucket::set_iam_policy` to
/// control access instead.
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::default_object_access_control::DefaultObjectAccessControl;
///
/// let client = Client::default();
/// let default_acls = client.default_object_access_control().list("mybucket").await?;
/// # Ok(())
/// # }
/// ```
pub async fn list(&self, bucket: &str) -> crate::Result<Vec<DefaultObjectAccessControl>> {
let url = format!(
"{}/b/{}/defaultObjectAcl",
crate::BASE_URL,
percent_encode(bucket)
);
let result: GoogleResponse<ListResponse<DefaultObjectAccessControl>> = self
.0
.client
.get(&url)
.headers(self.0.get_headers().await?)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(s) => Ok(s
.items
.into_iter()
.map(|item| DefaultObjectAccessControl {
bucket: bucket.to_string(),
..item
})
.collect()),
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Read a single `DefaultObjectAccessControl`.
/// The `bucket` argument is the name of the bucket whose `DefaultObjectAccessControl` is to be
/// read, and the `entity` argument is the entity holding the permission. Options are
/// Can be "user-`userId`", "user-`email_address`", "group-`group_id`", "group-`email_address`",
/// "allUsers", or "allAuthenticatedUsers".
/// ### Important
/// Important: This method fails with a `400 Bad Request` response for buckets with uniform
/// bucket-level access enabled. Use `Bucket::get_iam_policy` and `Bucket::set_iam_policy` to
/// control access instead.
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::default_object_access_control::{DefaultObjectAccessControl, Entity};
///
/// let client = Client::default();
/// let default_acl = client.default_object_access_control().read("mybucket", &Entity::AllUsers).await?;
/// # Ok(())
/// # }
/// ```
pub async fn read(
&self,
bucket: &str,
entity: &Entity,
) -> crate::Result<DefaultObjectAccessControl> {
let url = format!(
"{}/b/{}/defaultObjectAcl/{}",
crate::BASE_URL,
percent_encode(bucket),
percent_encode(&entity.to_string()),
);
let result: GoogleResponse<DefaultObjectAccessControl> = self
.0
.client
.get(&url)
.headers(self.0.get_headers().await?)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(mut s) => {
s.bucket = bucket.to_string();
Ok(s)
}
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Update the current `DefaultObjectAccessControl`.
/// ### Important
/// Important: This method fails with a `400 Bad Request` response for buckets with uniform
/// bucket-level access enabled. Use `Bucket::get_iam_policy` and `Bucket::set_iam_policy` to
/// control access instead.
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::default_object_access_control::{DefaultObjectAccessControl, Entity};
///
/// let client = Client::default();
/// let mut default_acl = client.default_object_access_control().read("my_bucket", &Entity::AllUsers).await?;
/// default_acl.entity = Entity::AllAuthenticatedUsers;
/// client.default_object_access_control().update(&default_acl).await?;
/// # Ok(())
/// # }
/// ```
pub async fn update(
&self,
default_object_access_control: &DefaultObjectAccessControl,
) -> crate::Result<DefaultObjectAccessControl> {
let url = format!(
"{}/b/{}/defaultObjectAcl/{}",
crate::BASE_URL,
percent_encode(&default_object_access_control.bucket),
percent_encode(&default_object_access_control.entity.to_string()),
);
let result: GoogleResponse<DefaultObjectAccessControl> = self
.0
.client
.put(&url)
.headers(self.0.get_headers().await?)
.json(default_object_access_control)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(mut s) => {
s.bucket = default_object_access_control.bucket.to_string();
Ok(s)
}
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Delete this 'DefaultObjectAccessControl`.
/// ### Important
/// Important: This method fails with a `400 Bad Request` response for buckets with uniform
/// bucket-level access enabled. Use `Bucket::get_iam_policy` and `Bucket::set_iam_policy` to
/// control access instead.
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::default_object_access_control::{DefaultObjectAccessControl, Entity};
///
/// let client = Client::default();
/// let mut default_acl = client.default_object_access_control().read("my_bucket", &Entity::AllUsers).await?;
/// client.default_object_access_control().delete(default_acl).await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete(
&self,
default_object_access_control: DefaultObjectAccessControl,
) -> Result<(), crate::Error> {
let url = format!(
"{}/b/{}/defaultObjectAcl/{}",
crate::BASE_URL,
percent_encode(&default_object_access_control.bucket),
percent_encode(&default_object_access_control.entity.to_string()),
);
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?))
}
}
}