From 1756b749352e949a4c933ec382e8f54cce4f17c0 Mon Sep 17 00:00:00 2001 From: Daniel Gryniewicz Date: Thu, 14 Jul 2016 12:39:53 -0400 Subject: [PATCH] Add op to get Lifecycle configuration Signed-off-by: Daniel Gryniewicz --- src/rgw/rgw_lc.cc | 16 ++-------------- src/rgw/rgw_lc.h | 17 +++++++---------- src/rgw/rgw_lc_s3.cc | 19 ++++++++++++++++--- src/rgw/rgw_lc_s3.h | 39 ++++++++++++++++++++++++++++----------- src/rgw/rgw_op.cc | 16 ++++++++++++++++ src/rgw/rgw_op.h | 18 ++++++++++++++++++ src/rgw/rgw_rest.h | 6 ++++++ src/rgw/rgw_rest_s3.cc | 40 ++++++++++++++++++++++++++++++++++++++++ src/rgw/rgw_rest_s3.h | 12 ++++++++++++ 9 files changed, 145 insertions(+), 38 deletions(-) diff --git a/src/rgw/rgw_lc.cc b/src/rgw/rgw_lc.cc index 3d110e829fe..0e8df5ed333 100644 --- a/src/rgw/rgw_lc.cc +++ b/src/rgw/rgw_lc.cc @@ -40,19 +40,7 @@ void RGWLifecycleConfiguration::add_rule(LCRule *rule) void RGWLifecycleConfiguration::_add_rule(LCRule *rule) { - string prefix; - LCExpiration expiration; - int days; - if (!rule->get_prefix(prefix)) { - ldout(cct, 5) << "ERROR: rule->get_prefix() failed" << dendl; - } - if (!rule->get_expiration(expiration)) { - ldout(cct, 5) << "ERROR: rule->get_expiration() failed" << dendl; - } - if (!expiration.get_days(&days)) { - ldout(cct, 5) << "ERROR: expiration->get_days() failed" << dendl; - } - prefix_map[prefix] = days; + prefix_map[rule->get_prefix()] = rule->get_expiration().get_days(); } void *RGWLC::LCWorker::entry() { @@ -481,7 +469,7 @@ exit: void RGWLC::start_processor() { worker = new LCWorker(cct, this); - worker->create("lifecycle_thread"); + worker->create("lifecycle_thr"); } void RGWLC::stop_processor() diff --git a/src/rgw/rgw_lc.h b/src/rgw/rgw_lc.h index cdc5ff2f6ba..0c4126c1ece 100644 --- a/src/rgw/rgw_lc.h +++ b/src/rgw/rgw_lc.h @@ -53,7 +53,7 @@ public: void dump(Formatter *f) const; // static void generate_test_instances(list& o); void set_days(const string& _days) { days = _days; } - bool get_days(int* _days) {*_days = atoi(days.c_str()); return true; } + int get_days() {return atoi(days.c_str()); } }; WRITE_CLASS_ENCODER(LCExpiration) @@ -75,19 +75,16 @@ public: return true; } - bool get_status(string& _status) { - _status = status; - return true; + string& get_status() { + return status; } - bool get_prefix(string& _prefix) { - _prefix = prefix; - return true; + string& get_prefix() { + return prefix; } - bool get_expiration(LCExpiration& _expriation) { - _expriation = expiration; - return true; + LCExpiration& get_expiration() { + return expiration; } void set_id(string*_id) { diff --git a/src/rgw/rgw_lc_s3.cc b/src/rgw/rgw_lc_s3.cc index cee710bb9a7..cc05a2a3590 100644 --- a/src/rgw/rgw_lc_s3.cc +++ b/src/rgw/rgw_lc_s3.cc @@ -5,6 +5,7 @@ #include "include/types.h" +#include "rgw_user.h" #include "rgw_lc_s3.h" @@ -41,7 +42,7 @@ bool LCRule_S3::xml_end(const char *el) { id.clear(); prefix.clear(); status.clear(); - + lc_id = static_cast(find_first("ID")); if (!lc_id) return false; @@ -56,7 +57,7 @@ bool LCRule_S3::xml_end(const char *el) { if (!lc_status) return false; status = lc_status->get_data(); - + lc_expiration = static_cast(find_first("Expiration")); if (!lc_expiration) return false; @@ -87,7 +88,19 @@ int RGWLifecycleConfiguration_S3::rebuild(RGWRados *store, RGWLifecycleConfigura } } - return 0; + return 0; +} + +void RGWLifecycleConfiguration_S3::dump_xml(Formatter *f) const +{ + f->open_object_section_in_ns("LifecycleConfiguration", XMLNS_AWS_S3); + + for (auto iter = rule_map.begin(); iter != rule_map.end(); ++iter) { + const LCRule_S3& rule = static_cast(iter->second); + rule.dump_xml(f); + } + + f->close_section(); // Lifecycle } XMLObj *RGWLCXMLParser_S3::alloc_obj(const char *el) diff --git a/src/rgw/rgw_lc_s3.h b/src/rgw/rgw_lc_s3.h index 1de47d5fb60..01f3b324f9d 100644 --- a/src/rgw/rgw_lc_s3.h +++ b/src/rgw/rgw_lc_s3.h @@ -16,17 +16,6 @@ using namespace std; -class LCRule_S3 : public LCRule, public XMLObj -{ -public: - LCRule_S3() {} - ~LCRule_S3() {} - - void to_xml(CephContext *cct, ostream& out); - bool xml_end(const char *el); - bool xml_start(const char *el, const char **attr); -}; - class LCID_S3 : public XMLObj { public: @@ -69,6 +58,32 @@ public: void to_xml(ostream& out) { out << "" << "" << days << ""<< ""; } + void dump_xml(Formatter *f) const { + f->open_object_section("Expiration"); + encode_xml("Days", days, f); + f->close_section(); // Expiration + } +}; + +class LCRule_S3 : public LCRule, public XMLObj +{ +public: + LCRule_S3() {} + ~LCRule_S3() {} + + void to_xml(CephContext *cct, ostream& out); + bool xml_end(const char *el); + bool xml_start(const char *el, const char **attr); + void dump_xml(Formatter *f) const { + const LCExpiration_S3& expir = static_cast(expiration); + + f->open_object_section("Rule"); + encode_xml("ID", id, f); + encode_xml("Prefix", prefix, f); + encode_xml("Status", status, f); + expir.dump_xml(f); + f->close_section(); // Rule + } }; class RGWLCXMLParser_S3 : public RGWXMLParser @@ -84,6 +99,7 @@ class RGWLifecycleConfiguration_S3 : public RGWLifecycleConfiguration, public XM { public: RGWLifecycleConfiguration_S3(CephContext *_cct) : RGWLifecycleConfiguration(_cct) {} + RGWLifecycleConfiguration_S3() : RGWLifecycleConfiguration(NULL) {} ~RGWLifecycleConfiguration_S3() {} bool xml_end(const char *el); @@ -98,6 +114,7 @@ public: out << ""; } int rebuild(RGWRados *store, RGWLifecycleConfiguration& dest); + void dump_xml(Formatter *f) const; }; diff --git a/src/rgw/rgw_op.cc b/src/rgw/rgw_op.cc index 6bd66a03e86..48813d6522c 100644 --- a/src/rgw/rgw_op.cc +++ b/src/rgw/rgw_op.cc @@ -3669,6 +3669,16 @@ int RGWPutACLs::verify_permission() return 0; } +int RGWGetLC::verify_permission() +{ + bool perm; + perm = verify_bucket_permission(s, RGW_PERM_WRITE_ACP); + if (!perm) + return -EACCES; + + return 0; +} + int RGWPutLC::verify_permission() { bool perm; @@ -3694,6 +3704,11 @@ void RGWPutACLs::pre_exec() rgw_bucket_object_pre_exec(s); } +void RGWGetLC::pre_exec() +{ + rgw_bucket_object_pre_exec(s); +} + void RGWPutLC::pre_exec() { rgw_bucket_object_pre_exec(s); @@ -3806,6 +3821,7 @@ static void get_lc_oid(struct req_state *s, string& oid) oid.append(buf); return; } + void RGWPutLC::execute() { bufferlist bl; diff --git a/src/rgw/rgw_op.h b/src/rgw/rgw_op.h index 1fbb3483243..578b1377311 100644 --- a/src/rgw/rgw_op.h +++ b/src/rgw/rgw_op.h @@ -34,6 +34,7 @@ #include "rgw_acl.h" #include "rgw_cors.h" #include "rgw_quota.h" +#include "rgw_lc.h" #include "include/assert.h" @@ -1005,6 +1006,23 @@ public: virtual uint32_t op_mask() { return RGW_OP_TYPE_WRITE; } }; +class RGWGetLC : public RGWOp { +protected: + int ret; + +public: + RGWGetLC() : ret(0) { } + virtual ~RGWGetLC() { } + + int verify_permission(); + void pre_exec(); + virtual void execute() = 0; + + virtual void send_response() = 0; + virtual const string name() { return "get_lifecycle"; } + virtual uint32_t op_mask() { return RGW_OP_TYPE_WRITE; } +}; + class RGWPutLC : public RGWOp { protected: int ret; diff --git a/src/rgw/rgw_rest.h b/src/rgw/rgw_rest.h index 831b214df75..928a7a10aaa 100644 --- a/src/rgw/rgw_rest.h +++ b/src/rgw/rgw_rest.h @@ -278,6 +278,12 @@ public: virtual int get_params(); }; +class RGWGetLC_ObjStore : public RGWGetLC { +public: + RGWGetLC_ObjStore() {} + ~RGWGetLC_ObjStore() {} +}; + class RGWPutLC_ObjStore : public RGWPutLC { public: RGWPutLC_ObjStore() {} diff --git a/src/rgw/rgw_rest_s3.cc b/src/rgw/rgw_rest_s3.cc index 679d423568d..b3ed5c25784 100644 --- a/src/rgw/rgw_rest_s3.cc +++ b/src/rgw/rgw_rest_s3.cc @@ -2260,6 +2260,44 @@ void RGWPutACLs_ObjStore_S3::send_response() dump_start(s); } +void RGWGetLC_ObjStore_S3::execute() +{ + map bucket_attrs; + + config.set_ctx(s->cct); + + RGWObjectCtx& obj_ctx = *static_cast(s->obj_ctx); + int ret = store->get_bucket_info(obj_ctx, s->bucket_tenant, s->bucket_name, s->bucket_info, NULL, &bucket_attrs); + if (ret < 0) { + ldout(s->cct, 0) << "LC:get_bucket_info failed" << s->bucket_name << dendl; + return; + } + + map::iterator aiter = bucket_attrs.find(RGW_ATTR_LC); + if (aiter == bucket_attrs.end()) + return; + + bufferlist::iterator iter(&aiter->second); + try { + config.decode(iter); + } catch (const buffer::error& e) { + ldout(s->cct, 0) << __func__ << "decode life cycle config failed" << dendl; + return; + } +} + +void RGWGetLC_ObjStore_S3::send_response() +{ + if (ret) + set_req_state_err(s, ret); + dump_errno(s); + end_header(s, this, "application/xml"); + dump_start(s); + + config.dump_xml(s->formatter); + rgw_flush_formatter_and_reset(s, s->formatter); +} + void RGWPutLC_ObjStore_S3::send_response() { if (ret) @@ -2833,6 +2871,8 @@ RGWOp *RGWHandler_REST_Bucket_S3::op_get() return new RGWGetRequestPayment_ObjStore_S3; } else if (s->info.args.exists("uploads")) { return new RGWListBucketMultiparts_ObjStore_S3; + } else if(is_lc_op()) { + return new RGWGetLC_ObjStore_S3; } return get_obj_op(true); } diff --git a/src/rgw/rgw_rest_s3.h b/src/rgw/rgw_rest_s3.h index 43c254e3825..c48b2eb1f69 100644 --- a/src/rgw/rgw_rest_s3.h +++ b/src/rgw/rgw_rest_s3.h @@ -12,6 +12,7 @@ #include "rgw_http_errors.h" #include "rgw_acl_s3.h" #include "rgw_policy_s3.h" +#include "rgw_lc_s3.h" #include "rgw_keystone.h" #include "rgw_rest_conn.h" #include "rgw_ldap.h" @@ -257,6 +258,17 @@ public: int get_params(); }; +class RGWGetLC_ObjStore_S3 : public RGWGetLC_ObjStore { +protected: + RGWLifecycleConfiguration_S3 config; +public: + RGWGetLC_ObjStore_S3() {} + ~RGWGetLC_ObjStore_S3() {} + virtual void execute(); + + void send_response(); +}; + class RGWPutLC_ObjStore_S3 : public RGWPutLC_ObjStore { public: RGWPutLC_ObjStore_S3() {}