]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_rest_bucket.cc
430a8ef33392050557d47133340268eaaa98b61b
[ceph.git] / ceph / src / rgw / rgw_rest_bucket.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "rgw_op.h"
5 #include "rgw_bucket.h"
6 #include "rgw_rest_bucket.h"
7
8 #include "include/str_list.h"
9
10 #define dout_subsys ceph_subsys_rgw
11
12 class RGWOp_Bucket_Info : public RGWRESTOp {
13
14 public:
15 RGWOp_Bucket_Info() {}
16
17 int check_caps(RGWUserCaps& caps) override {
18 return caps.check_cap("buckets", RGW_CAP_READ);
19 }
20
21 void execute() override;
22
23 const string name() override { return "get_bucket_info"; }
24 };
25
26 void RGWOp_Bucket_Info::execute()
27 {
28 RGWBucketAdminOpState op_state;
29
30 bool fetch_stats;
31
32 std::string bucket;
33
34 string uid_str;
35
36 RESTArgs::get_string(s, "uid", uid_str, &uid_str);
37 rgw_user uid(uid_str);
38
39 RESTArgs::get_string(s, "bucket", bucket, &bucket);
40 RESTArgs::get_bool(s, "stats", false, &fetch_stats);
41
42 op_state.set_user_id(uid);
43 op_state.set_bucket_name(bucket);
44 op_state.set_fetch_stats(fetch_stats);
45
46 http_ret = RGWBucketAdminOp::info(store, op_state, flusher);
47 }
48
49 class RGWOp_Get_Policy : public RGWRESTOp {
50
51 public:
52 RGWOp_Get_Policy() {}
53
54 int check_caps(RGWUserCaps& caps) override {
55 return caps.check_cap("buckets", RGW_CAP_READ);
56 }
57
58 void execute() override;
59
60 const string name() override { return "get_policy"; }
61 };
62
63 void RGWOp_Get_Policy::execute()
64 {
65 RGWBucketAdminOpState op_state;
66
67 std::string bucket;
68 std::string object;
69
70 RESTArgs::get_string(s, "bucket", bucket, &bucket);
71 RESTArgs::get_string(s, "object", object, &object);
72
73 op_state.set_bucket_name(bucket);
74 op_state.set_object(object);
75
76 http_ret = RGWBucketAdminOp::get_policy(store, op_state, flusher);
77 }
78
79 class RGWOp_Check_Bucket_Index : public RGWRESTOp {
80
81 public:
82 RGWOp_Check_Bucket_Index() {}
83
84 int check_caps(RGWUserCaps& caps) override {
85 return caps.check_cap("buckets", RGW_CAP_WRITE);
86 }
87
88 void execute() override;
89
90 const string name() override { return "check_bucket_index"; }
91 };
92
93 void RGWOp_Check_Bucket_Index::execute()
94 {
95 std::string bucket;
96
97 bool fix_index;
98 bool check_objects;
99
100 RGWBucketAdminOpState op_state;
101
102 RESTArgs::get_string(s, "bucket", bucket, &bucket);
103 RESTArgs::get_bool(s, "fix", false, &fix_index);
104 RESTArgs::get_bool(s, "check-objects", false, &check_objects);
105
106 op_state.set_bucket_name(bucket);
107 op_state.set_fix_index(fix_index);
108 op_state.set_check_objects(check_objects);
109
110 http_ret = RGWBucketAdminOp::check_index(store, op_state, flusher);
111 }
112
113 class RGWOp_Bucket_Link : public RGWRESTOp {
114
115 public:
116 RGWOp_Bucket_Link() {}
117
118 int check_caps(RGWUserCaps& caps) override {
119 return caps.check_cap("buckets", RGW_CAP_WRITE);
120 }
121
122 void execute() override;
123
124 const string name() override { return "link_bucket"; }
125 };
126
127 void RGWOp_Bucket_Link::execute()
128 {
129 std::string uid_str;
130 std::string bucket;
131 std::string bucket_id;
132
133 RGWBucketAdminOpState op_state;
134
135 RESTArgs::get_string(s, "uid", uid_str, &uid_str);
136 RESTArgs::get_string(s, "bucket", bucket, &bucket);
137 RESTArgs::get_string(s, "bucket-id", bucket_id, &bucket_id);
138
139 rgw_user uid(uid_str);
140 op_state.set_user_id(uid);
141 op_state.set_bucket_name(bucket);
142 op_state.set_bucket_id(bucket_id);
143
144 http_ret = RGWBucketAdminOp::link(store, op_state);
145 }
146
147 class RGWOp_Bucket_Unlink : public RGWRESTOp {
148
149 public:
150 RGWOp_Bucket_Unlink() {}
151
152 int check_caps(RGWUserCaps& caps) override {
153 return caps.check_cap("buckets", RGW_CAP_WRITE);
154 }
155
156 void execute() override;
157
158 const string name() override { return "unlink_bucket"; }
159 };
160
161 void RGWOp_Bucket_Unlink::execute()
162 {
163 std::string uid_str;
164 std::string bucket;
165
166 RGWBucketAdminOpState op_state;
167
168 RESTArgs::get_string(s, "uid", uid_str, &uid_str);
169 rgw_user uid(uid_str);
170
171 RESTArgs::get_string(s, "bucket", bucket, &bucket);
172
173 op_state.set_user_id(uid);
174 op_state.set_bucket_name(bucket);
175
176 http_ret = RGWBucketAdminOp::unlink(store, op_state);
177 }
178
179 class RGWOp_Bucket_Remove : public RGWRESTOp {
180
181 public:
182 RGWOp_Bucket_Remove() {}
183
184 int check_caps(RGWUserCaps& caps) override {
185 return caps.check_cap("buckets", RGW_CAP_WRITE);
186 }
187
188 void execute() override;
189
190 const string name() override { return "remove_bucket"; }
191 };
192
193 void RGWOp_Bucket_Remove::execute()
194 {
195 std::string bucket;
196 bool delete_children;
197
198 RGWBucketAdminOpState op_state;
199
200 RESTArgs::get_string(s, "bucket", bucket, &bucket);
201 RESTArgs::get_bool(s, "purge-objects", false, &delete_children);
202
203 op_state.set_bucket_name(bucket);
204 op_state.set_delete_children(delete_children);
205
206 http_ret = RGWBucketAdminOp::remove_bucket(store, op_state);
207 }
208
209 class RGWOp_Set_Bucket_Quota : public RGWRESTOp {
210
211 public:
212 RGWOp_Set_Bucket_Quota() {}
213
214 int check_caps(RGWUserCaps& caps) {
215 return caps.check_cap("buckets", RGW_CAP_WRITE);
216 }
217
218 void execute();
219
220 virtual const string name() { return "set_bucket_quota"; }
221 };
222
223 #define QUOTA_INPUT_MAX_LEN 1024
224
225 void RGWOp_Set_Bucket_Quota::execute()
226 {
227 bool uid_arg_existed = false;
228 std::string uid_str;
229 RESTArgs::get_string(s, "uid", uid_str, &uid_str, &uid_arg_existed);
230 if (! uid_arg_existed) {
231 http_ret = -EINVAL;
232 return;
233 }
234 rgw_user uid(uid_str);
235 bool bucket_arg_existed = false;
236 std::string bucket;
237 RESTArgs::get_string(s, "bucket", bucket, &bucket, &bucket_arg_existed);
238 if (! bucket_arg_existed) {
239 http_ret = -EINVAL;
240 return;
241 }
242
243 bool use_http_params;
244
245 if (s->content_length > 0) {
246 use_http_params = false;
247 } else {
248 const char *encoding = s->info.env->get("HTTP_TRANSFER_ENCODING");
249 use_http_params = (!encoding || strcmp(encoding, "chunked") != 0);
250 }
251 RGWQuotaInfo quota;
252 if (!use_http_params) {
253 bool empty;
254 http_ret = rgw_rest_get_json_input(store->ctx(), s, quota, QUOTA_INPUT_MAX_LEN, &empty);
255 if (http_ret < 0) {
256 if (!empty)
257 return;
258 /* was probably chunked input, but no content provided, configure via http params */
259 use_http_params = true;
260 }
261 }
262 if (use_http_params) {
263 RGWBucketInfo bucket_info;
264 map<string, bufferlist> attrs;
265 RGWObjectCtx obj_ctx(store);
266 http_ret = store->get_bucket_info(obj_ctx, uid.tenant, bucket, bucket_info, NULL, &attrs);
267 if (http_ret < 0) {
268 return;
269 }
270 RGWQuotaInfo *old_quota = &bucket_info.quota;
271 int64_t old_max_size_kb = rgw_rounded_kb(old_quota->max_size);
272 int64_t max_size_kb;
273 RESTArgs::get_int64(s, "max-objects", old_quota->max_objects, &quota.max_objects);
274 RESTArgs::get_int64(s, "max-size-kb", old_max_size_kb, &max_size_kb);
275 quota.max_size = max_size_kb * 1024;
276 RESTArgs::get_bool(s, "enabled", old_quota->enabled, &quota.enabled);
277 }
278
279 RGWBucketAdminOpState op_state;
280 op_state.set_user_id(uid);
281 op_state.set_bucket_name(bucket);
282 op_state.set_quota(quota);
283
284 http_ret = RGWBucketAdminOp::set_quota(store, op_state);
285 }
286
287 class RGWOp_Object_Remove: public RGWRESTOp {
288
289 public:
290 RGWOp_Object_Remove() {}
291
292 int check_caps(RGWUserCaps& caps) override {
293 return caps.check_cap("buckets", RGW_CAP_WRITE);
294 }
295
296 void execute() override;
297
298 const string name() override { return "remove_object"; }
299 };
300
301 void RGWOp_Object_Remove::execute()
302 {
303 std::string bucket;
304 std::string object;
305
306 RGWBucketAdminOpState op_state;
307
308 RESTArgs::get_string(s, "bucket", bucket, &bucket);
309 RESTArgs::get_string(s, "object", object, &object);
310
311 op_state.set_bucket_name(bucket);
312 op_state.set_object(object);
313
314 http_ret = RGWBucketAdminOp::remove_object(store, op_state);
315 }
316
317 RGWOp *RGWHandler_Bucket::op_get()
318 {
319
320 if (s->info.args.sub_resource_exists("policy"))
321 return new RGWOp_Get_Policy;
322
323 if (s->info.args.sub_resource_exists("index"))
324 return new RGWOp_Check_Bucket_Index;
325
326 return new RGWOp_Bucket_Info;
327 }
328
329 RGWOp *RGWHandler_Bucket::op_put()
330 {
331 if (s->info.args.sub_resource_exists("quota"))
332 return new RGWOp_Set_Bucket_Quota;
333 return new RGWOp_Bucket_Link;
334 }
335
336 RGWOp *RGWHandler_Bucket::op_post()
337 {
338 return new RGWOp_Bucket_Unlink;
339 }
340
341 RGWOp *RGWHandler_Bucket::op_delete()
342 {
343 if (s->info.args.sub_resource_exists("object"))
344 return new RGWOp_Object_Remove;
345
346 return new RGWOp_Bucket_Remove;
347 }
348