From 15c01895fbfbe251445f750b795c2b918573bac4 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Fri, 10 Jan 2014 15:11:08 -0800 Subject: [PATCH] radosgw-admin: user quota interface also add needed functionality in RGWUser Signed-off-by: Yehuda Sadeh --- src/rgw/rgw_admin.cc | 27 ++++++++++++++++++++++++--- src/rgw/rgw_common.h | 7 ++++++- src/rgw/rgw_json_enc.cc | 2 ++ src/rgw/rgw_user.cc | 6 ++++++ src/rgw/rgw_user.h | 13 +++++++++++-- 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 986b2ea0ad105..0bef8864fc06a 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -722,6 +722,24 @@ int set_user_bucket_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_st return 0; } +int set_user_quota(int opt_cmd, RGWUser& user, RGWUserAdminOpState& op_state, int64_t max_size, int64_t max_objects, + bool have_max_size, bool have_max_objects) +{ + RGWUserInfo& user_info = op_state.get_user_info(); + + set_quota_info(user_info.user_quota, opt_cmd, max_size, max_objects, have_max_size, have_max_objects); + + op_state.set_user_quota(user_info.user_quota); + + string err; + int r = user.modify(op_state, &err); + if (r < 0) { + cerr << "ERROR: failed updating user info: " << cpp_strerror(-r) << ": " << err << std::endl; + return -r; + } + return 0; +} + static int sync_bucket_stats(RGWRados *store, string& bucket_name) { RGWBucketInfo bucket_info; @@ -2412,11 +2430,14 @@ next: } set_bucket_quota(store, opt_cmd, bucket_name, max_size, max_objects, have_max_size, have_max_objects); } else if (!user_id.empty()) { - if (quota_scope != "bucket") { - cerr << "ERROR: only bucket-level user quota can be handled. Please specify --quota-scope=bucket" << std::endl; + if (quota_scope == "bucket") { + set_user_bucket_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects); + } else if (quota_scope == "user") { + set_user_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects); + } else { + cerr << "ERROR: invalid quota scope specification. Please specify either --quota-scope=bucket, or --quota-scope=user" << std::endl; return EINVAL; } - set_user_bucket_quota(opt_cmd, user, user_op, max_size, max_objects, have_max_size, have_max_objects); } } diff --git a/src/rgw/rgw_common.h b/src/rgw/rgw_common.h index d6a1286429c58..3e4f0f4823f35 100644 --- a/src/rgw/rgw_common.h +++ b/src/rgw/rgw_common.h @@ -432,11 +432,12 @@ struct RGWUserInfo string default_placement; list placement_tags; RGWQuotaInfo bucket_quota; + RGWQuotaInfo user_quota; RGWUserInfo() : auid(0), suspended(0), max_buckets(RGW_DEFAULT_MAX_BUCKETS), op_mask(RGW_OP_TYPE_ALL), system(0) {} void encode(bufferlist& bl) const { - ENCODE_START(14, 9, bl); + ENCODE_START(15, 9, bl); ::encode(auid, bl); string access_key; string secret_key; @@ -472,6 +473,7 @@ struct RGWUserInfo ::encode(default_placement, bl); ::encode(placement_tags, bl); ::encode(bucket_quota, bl); + ::encode(user_quota, bl); ENCODE_FINISH(bl); } void decode(bufferlist::iterator& bl) { @@ -531,6 +533,9 @@ struct RGWUserInfo if (struct_v >= 14) { ::decode(bucket_quota, bl); } + if (struct_v >= 15) { + ::decode(user_quota, bl); + } DECODE_FINISH(bl); } void dump(Formatter *f) const; diff --git a/src/rgw/rgw_json_enc.cc b/src/rgw/rgw_json_enc.cc index 4d6b25374b9a0..89c0238605db3 100644 --- a/src/rgw/rgw_json_enc.cc +++ b/src/rgw/rgw_json_enc.cc @@ -397,6 +397,7 @@ void RGWUserInfo::dump(Formatter *f) const encode_json("default_placement", default_placement, f); encode_json("placement_tags", placement_tags, f); encode_json("bucket_quota", bucket_quota, f); + encode_json("user_quota", user_quota, f); } @@ -448,6 +449,7 @@ void RGWUserInfo::decode_json(JSONObj *obj) JSONDecoder::decode_json("default_placement", default_placement, obj); JSONDecoder::decode_json("placement_tags", placement_tags, obj); JSONDecoder::decode_json("bucket_quota", bucket_quota, obj); + JSONDecoder::decode_json("user_quota", user_quota, obj); } void RGWQuotaInfo::dump(Formatter *f) const diff --git a/src/rgw/rgw_user.cc b/src/rgw/rgw_user.cc index d8bbafabfb55b..7e769332cc3f7 100644 --- a/src/rgw/rgw_user.cc +++ b/src/rgw/rgw_user.cc @@ -1685,6 +1685,9 @@ int RGWUser::execute_add(RGWUserAdminOpState& op_state, std::string *err_msg) if (op_state.has_bucket_quota()) user_info.bucket_quota = op_state.get_bucket_quota(); + if (op_state.has_user_quota()) + user_info.user_quota = op_state.get_user_quota(); + // update the request op_state.set_user_info(user_info); op_state.set_populated(); @@ -1890,6 +1893,9 @@ int RGWUser::execute_modify(RGWUserAdminOpState& op_state, std::string *err_msg) if (op_state.has_bucket_quota()) user_info.bucket_quota = op_state.get_bucket_quota(); + if (op_state.has_user_quota()) + user_info.user_quota = op_state.get_user_quota(); + if (op_state.has_suspension_op()) { __u8 suspended = op_state.get_suspension_status(); user_info.suspended = suspended; diff --git a/src/rgw/rgw_user.h b/src/rgw/rgw_user.h index 2749e6c2d51d3..9d4315bdd6650 100644 --- a/src/rgw/rgw_user.h +++ b/src/rgw/rgw_user.h @@ -173,8 +173,10 @@ struct RGWUserAdminOpState { bool user_params_checked; bool bucket_quota_specified; + bool user_quota_specified; RGWQuotaInfo bucket_quota; + RGWQuotaInfo user_quota; void set_access_key(std::string& access_key) { if (access_key.empty()) @@ -289,12 +291,16 @@ struct RGWUserAdminOpState { key_op = true; } - void set_bucket_quota(RGWQuotaInfo& quota) - { + void set_bucket_quota(RGWQuotaInfo& quota) { bucket_quota = quota; bucket_quota_specified = true; } + void set_user_quota(RGWQuotaInfo& quota) { + user_quota = quota; + user_quota_specified = true; + } + bool is_populated() { return populated; }; bool is_initialized() { return initialized; }; bool has_existing_user() { return existing_user; }; @@ -314,6 +320,7 @@ struct RGWUserAdminOpState { bool will_purge_data() { return purge_data; }; bool will_generate_subuser() { return gen_subuser; }; bool has_bucket_quota() { return bucket_quota_specified; } + bool has_user_quota() { return user_quota_specified; } void set_populated() { populated = true; }; void clear_populated() { populated = false; }; void set_initialized() { initialized = true; }; @@ -329,6 +336,7 @@ struct RGWUserAdminOpState { uint32_t get_max_buckets() { return max_buckets; }; uint32_t get_op_mask() { return op_mask; }; RGWQuotaInfo& get_bucket_quota() { return bucket_quota; } + RGWQuotaInfo& get_user_quota() { return user_quota; } std::string get_user_id() { return user_id; }; std::string get_subuser() { return subuser; }; @@ -417,6 +425,7 @@ struct RGWUserAdminOpState { subuser_params_checked = false; user_params_checked = false; bucket_quota_specified = false; + user_quota_specified = false; } }; -- 2.39.5