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