]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_otp.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rgw / rgw_otp.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
3
4 #include <errno.h>
5
6 #include <string>
7 #include <map>
8 #include <boost/algorithm/string.hpp>
9
10 #include "common/errno.h"
11 #include "common/Formatter.h"
12 #include "common/ceph_json.h"
13 #include "rgw_otp.h"
14 #include "rgw_zone.h"
15 #include "rgw_metadata.h"
16
17 #include "include/types.h"
18
19 #include "rgw_common.h"
20 #include "rgw_tools.h"
21
22 #include "services/svc_zone.h"
23 #include "services/svc_meta.h"
24 #include "services/svc_meta_be.h"
25 #include "services/svc_meta_be_otp.h"
26 #include "services/svc_otp.h"
27
28 #define dout_subsys ceph_subsys_rgw
29
30 using namespace std;
31
32
33 class RGWOTPMetadataHandler;
34
35 class RGWOTPMetadataObject : public RGWMetadataObject {
36 friend class RGWOTPMetadataHandler;
37
38 otp_devices_list_t devices;
39 public:
40 RGWOTPMetadataObject() {}
41 RGWOTPMetadataObject(otp_devices_list_t&& _devices, const obj_version& v, const real_time m) {
42 devices = std::move(_devices);
43 objv = v;
44 mtime = m;
45 }
46
47 void dump(Formatter *f) const override {
48 encode_json("devices", devices, f);
49 }
50
51 otp_devices_list_t& get_devs() {
52 return devices;
53 }
54 };
55
56
57 class RGWOTPMetadataHandler : public RGWOTPMetadataHandlerBase {
58 friend class RGWOTPCtl;
59
60 struct Svc {
61 RGWSI_Zone *zone;
62 RGWSI_MetaBackend *meta_be;
63 RGWSI_OTP *otp;
64 } svc;
65
66 int init(RGWSI_Zone *zone,
67 RGWSI_MetaBackend *_meta_be,
68 RGWSI_OTP *_otp) {
69 base_init(zone->ctx(), _otp->get_be_handler().get());
70 svc.zone = zone;
71 svc.meta_be = _meta_be;
72 svc.otp = _otp;
73 return 0;
74 }
75
76 int call(std::function<int(RGWSI_OTP_BE_Ctx& ctx)> f) {
77 return be_handler->call([&](RGWSI_MetaBackend_Handler::Op *op) {
78 RGWSI_OTP_BE_Ctx ctx(op->ctx());
79 return f(ctx);
80 });
81 }
82
83 RGWMetadataObject *get_meta_obj(JSONObj *jo, const obj_version& objv, const ceph::real_time& mtime) override {
84 otp_devices_list_t devices;
85 try {
86 JSONDecoder::decode_json("devices", devices, jo);
87 } catch (JSONDecoder::err& e) {
88 return nullptr;
89 }
90
91 return new RGWOTPMetadataObject(std::move(devices), objv, mtime);
92 }
93
94 int do_get(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWMetadataObject **obj, optional_yield y) override {
95 RGWObjVersionTracker objv_tracker;
96
97 std::unique_ptr<RGWOTPMetadataObject> mdo(new RGWOTPMetadataObject);
98
99
100 RGWSI_OTP_BE_Ctx be_ctx(op->ctx());
101
102 int ret = svc.otp->read_all(be_ctx,
103 entry,
104 &mdo->get_devs(),
105 &mdo->get_mtime(),
106 &objv_tracker,
107 y);
108 if (ret < 0) {
109 return ret;
110 }
111
112 mdo->objv = objv_tracker.read_version;
113
114 *obj = mdo.release();
115
116 return 0;
117 }
118
119 int do_put(RGWSI_MetaBackend_Handler::Op *op, string& entry,
120 RGWMetadataObject *_obj, RGWObjVersionTracker& objv_tracker,
121 optional_yield y,
122 RGWMDLogSyncType type, bool from_remote_zone) override {
123 RGWOTPMetadataObject *obj = static_cast<RGWOTPMetadataObject *>(_obj);
124
125 RGWSI_OTP_BE_Ctx be_ctx(op->ctx());
126
127 int ret = svc.otp->store_all(be_ctx,
128 entry,
129 obj->devices,
130 obj->mtime,
131 &objv_tracker,
132 y);
133 if (ret < 0) {
134 return ret;
135 }
136
137 return STATUS_APPLIED;
138 }
139
140 int do_remove(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWObjVersionTracker& objv_tracker,
141 optional_yield y) override {
142 RGWSI_MBOTP_RemoveParams params;
143
144 RGWSI_OTP_BE_Ctx be_ctx(op->ctx());
145
146 return svc.otp->remove_all(be_ctx,
147 entry,
148 &objv_tracker,
149 y);
150 }
151
152 public:
153 RGWOTPMetadataHandler() {}
154
155 string get_type() override { return "otp"; }
156 };
157
158
159 RGWOTPCtl::RGWOTPCtl(RGWSI_Zone *zone_svc,
160 RGWSI_OTP *otp_svc)
161 {
162 svc.zone = zone_svc;
163 svc.otp = otp_svc;
164 }
165
166
167 void RGWOTPCtl::init(RGWOTPMetadataHandler *_meta_handler)
168 {
169 meta_handler = _meta_handler;
170 be_handler = meta_handler->get_be_handler();
171 }
172
173 int RGWOTPCtl::read_all(const rgw_user& uid,
174 RGWOTPInfo *info,
175 optional_yield y,
176 const GetParams& params)
177 {
178 info->uid = uid;
179 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
180 return svc.otp->read_all(ctx, uid, &info->devices, params.mtime, params.objv_tracker, y);
181 });
182 }
183
184 int RGWOTPCtl::store_all(const RGWOTPInfo& info,
185 optional_yield y,
186 const PutParams& params)
187 {
188 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
189 return svc.otp->store_all(ctx, info.uid, info.devices, params.mtime, params.objv_tracker, y);
190 });
191 }
192
193 int RGWOTPCtl::remove_all(const rgw_user& uid,
194 optional_yield y,
195 const RemoveParams& params)
196 {
197 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
198 return svc.otp->remove_all(ctx, uid, params.objv_tracker, y);
199 });
200 }
201
202
203 RGWMetadataHandler *RGWOTPMetaHandlerAllocator::alloc()
204 {
205 return new RGWOTPMetadataHandler();
206 }