1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "rgw_bucket.h"
6 #include "rgw_rest_bucket.h"
8 #include "include/str_list.h"
10 #define dout_subsys ceph_subsys_rgw
12 class RGWOp_Bucket_Info
: public RGWRESTOp
{
15 RGWOp_Bucket_Info() {}
17 int check_caps(RGWUserCaps
& caps
) override
{
18 return caps
.check_cap("buckets", RGW_CAP_READ
);
21 void execute() override
;
23 const string
name() override
{ return "get_bucket_info"; }
26 void RGWOp_Bucket_Info::execute()
28 RGWBucketAdminOpState op_state
;
36 RESTArgs::get_string(s
, "uid", uid_str
, &uid_str
);
37 rgw_user
uid(uid_str
);
39 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
);
40 RESTArgs::get_bool(s
, "stats", false, &fetch_stats
);
42 op_state
.set_user_id(uid
);
43 op_state
.set_bucket_name(bucket
);
44 op_state
.set_fetch_stats(fetch_stats
);
46 http_ret
= RGWBucketAdminOp::info(store
, op_state
, flusher
);
49 class RGWOp_Get_Policy
: public RGWRESTOp
{
54 int check_caps(RGWUserCaps
& caps
) override
{
55 return caps
.check_cap("buckets", RGW_CAP_READ
);
58 void execute() override
;
60 const string
name() override
{ return "get_policy"; }
63 void RGWOp_Get_Policy::execute()
65 RGWBucketAdminOpState op_state
;
70 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
);
71 RESTArgs::get_string(s
, "object", object
, &object
);
73 op_state
.set_bucket_name(bucket
);
74 op_state
.set_object(object
);
76 http_ret
= RGWBucketAdminOp::get_policy(store
, op_state
, flusher
);
79 class RGWOp_Check_Bucket_Index
: public RGWRESTOp
{
82 RGWOp_Check_Bucket_Index() {}
84 int check_caps(RGWUserCaps
& caps
) override
{
85 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
88 void execute() override
;
90 const string
name() override
{ return "check_bucket_index"; }
93 void RGWOp_Check_Bucket_Index::execute()
100 RGWBucketAdminOpState op_state
;
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
);
106 op_state
.set_bucket_name(bucket
);
107 op_state
.set_fix_index(fix_index
);
108 op_state
.set_check_objects(check_objects
);
110 http_ret
= RGWBucketAdminOp::check_index(store
, op_state
, flusher
);
113 class RGWOp_Bucket_Link
: public RGWRESTOp
{
116 RGWOp_Bucket_Link() {}
118 int check_caps(RGWUserCaps
& caps
) override
{
119 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
122 void execute() override
;
124 const string
name() override
{ return "link_bucket"; }
127 void RGWOp_Bucket_Link::execute()
131 std::string bucket_id
;
133 RGWBucketAdminOpState op_state
;
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
);
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
);
144 http_ret
= RGWBucketAdminOp::link(store
, op_state
);
147 class RGWOp_Bucket_Unlink
: public RGWRESTOp
{
150 RGWOp_Bucket_Unlink() {}
152 int check_caps(RGWUserCaps
& caps
) override
{
153 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
156 void execute() override
;
158 const string
name() override
{ return "unlink_bucket"; }
161 void RGWOp_Bucket_Unlink::execute()
166 RGWBucketAdminOpState op_state
;
168 RESTArgs::get_string(s
, "uid", uid_str
, &uid_str
);
169 rgw_user
uid(uid_str
);
171 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
);
173 op_state
.set_user_id(uid
);
174 op_state
.set_bucket_name(bucket
);
176 http_ret
= RGWBucketAdminOp::unlink(store
, op_state
);
179 class RGWOp_Bucket_Remove
: public RGWRESTOp
{
182 RGWOp_Bucket_Remove() {}
184 int check_caps(RGWUserCaps
& caps
) override
{
185 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
188 void execute() override
;
190 const string
name() override
{ return "remove_bucket"; }
193 void RGWOp_Bucket_Remove::execute()
196 bool delete_children
;
198 RGWBucketAdminOpState op_state
;
200 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
);
201 RESTArgs::get_bool(s
, "purge-objects", false, &delete_children
);
203 op_state
.set_bucket_name(bucket
);
204 op_state
.set_delete_children(delete_children
);
206 http_ret
= RGWBucketAdminOp::remove_bucket(store
, op_state
);
209 class RGWOp_Set_Bucket_Quota
: public RGWRESTOp
{
212 RGWOp_Set_Bucket_Quota() {}
214 int check_caps(RGWUserCaps
& caps
) {
215 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
220 virtual const string
name() { return "set_bucket_quota"; }
223 #define QUOTA_INPUT_MAX_LEN 1024
225 void RGWOp_Set_Bucket_Quota::execute()
227 bool uid_arg_existed
= false;
229 RESTArgs::get_string(s
, "uid", uid_str
, &uid_str
, &uid_arg_existed
);
230 if (! uid_arg_existed
) {
234 rgw_user
uid(uid_str
);
235 bool bucket_arg_existed
= false;
237 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
, &bucket_arg_existed
);
238 if (! bucket_arg_existed
) {
243 bool use_http_params
;
245 if (s
->content_length
> 0) {
246 use_http_params
= false;
248 const char *encoding
= s
->info
.env
->get("HTTP_TRANSFER_ENCODING");
249 use_http_params
= (!encoding
|| strcmp(encoding
, "chunked") != 0);
252 if (!use_http_params
) {
254 http_ret
= rgw_rest_get_json_input(store
->ctx(), s
, quota
, QUOTA_INPUT_MAX_LEN
, &empty
);
258 /* was probably chunked input, but no content provided, configure via http params */
259 use_http_params
= true;
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
);
270 RGWQuotaInfo
*old_quota
= &bucket_info
.quota
;
271 int64_t old_max_size_kb
= rgw_rounded_kb(old_quota
->max_size
);
273 RESTArgs::get_int64(s
, "max-objects", old_quota
->max_objects
, "a
.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
, "a
.enabled
);
279 RGWBucketAdminOpState op_state
;
280 op_state
.set_user_id(uid
);
281 op_state
.set_bucket_name(bucket
);
282 op_state
.set_quota(quota
);
284 http_ret
= RGWBucketAdminOp::set_quota(store
, op_state
);
287 class RGWOp_Object_Remove
: public RGWRESTOp
{
290 RGWOp_Object_Remove() {}
292 int check_caps(RGWUserCaps
& caps
) override
{
293 return caps
.check_cap("buckets", RGW_CAP_WRITE
);
296 void execute() override
;
298 const string
name() override
{ return "remove_object"; }
301 void RGWOp_Object_Remove::execute()
306 RGWBucketAdminOpState op_state
;
308 RESTArgs::get_string(s
, "bucket", bucket
, &bucket
);
309 RESTArgs::get_string(s
, "object", object
, &object
);
311 op_state
.set_bucket_name(bucket
);
312 op_state
.set_object(object
);
314 http_ret
= RGWBucketAdminOp::remove_object(store
, op_state
);
317 RGWOp
*RGWHandler_Bucket::op_get()
320 if (s
->info
.args
.sub_resource_exists("policy"))
321 return new RGWOp_Get_Policy
;
323 if (s
->info
.args
.sub_resource_exists("index"))
324 return new RGWOp_Check_Bucket_Index
;
326 return new RGWOp_Bucket_Info
;
329 RGWOp
*RGWHandler_Bucket::op_put()
331 if (s
->info
.args
.sub_resource_exists("quota"))
332 return new RGWOp_Set_Bucket_Quota
;
333 return new RGWOp_Bucket_Link
;
336 RGWOp
*RGWHandler_Bucket::op_post()
338 return new RGWOp_Bucket_Unlink
;
341 RGWOp
*RGWHandler_Bucket::op_delete()
343 if (s
->info
.args
.sub_resource_exists("object"))
344 return new RGWOp_Object_Remove
;
346 return new RGWOp_Bucket_Remove
;