]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_otp.cc
import ceph quincy 17.2.6
[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, const DoutPrefixProvider *dpp) 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 dpp);
109 if (ret < 0) {
110 return ret;
111 }
112
113 mdo->objv = objv_tracker.read_version;
114
115 *obj = mdo.release();
116
117 return 0;
118 }
119
120 int do_put(RGWSI_MetaBackend_Handler::Op *op, string& entry,
121 RGWMetadataObject *_obj, RGWObjVersionTracker& objv_tracker,
122 optional_yield y,
123 const DoutPrefixProvider *dpp,
124 RGWMDLogSyncType type, bool from_remote_zone) override {
125 RGWOTPMetadataObject *obj = static_cast<RGWOTPMetadataObject *>(_obj);
126
127 RGWSI_OTP_BE_Ctx be_ctx(op->ctx());
128
129 int ret = svc.otp->store_all(dpp, be_ctx,
130 entry,
131 obj->devices,
132 obj->mtime,
133 &objv_tracker,
134 y);
135 if (ret < 0) {
136 return ret;
137 }
138
139 return STATUS_APPLIED;
140 }
141
142 int do_remove(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWObjVersionTracker& objv_tracker,
143 optional_yield y, const DoutPrefixProvider *dpp) override {
144 RGWSI_MBOTP_RemoveParams params;
145
146 RGWSI_OTP_BE_Ctx be_ctx(op->ctx());
147
148 return svc.otp->remove_all(dpp, be_ctx,
149 entry,
150 &objv_tracker,
151 y);
152 }
153
154 public:
155 RGWOTPMetadataHandler() {}
156
157 string get_type() override { return "otp"; }
158 };
159
160
161 RGWOTPCtl::RGWOTPCtl(RGWSI_Zone *zone_svc,
162 RGWSI_OTP *otp_svc)
163 {
164 svc.zone = zone_svc;
165 svc.otp = otp_svc;
166 }
167
168
169 void RGWOTPCtl::init(RGWOTPMetadataHandler *_meta_handler)
170 {
171 meta_handler = _meta_handler;
172 be_handler = meta_handler->get_be_handler();
173 }
174
175 int RGWOTPCtl::read_all(const rgw_user& uid,
176 RGWOTPInfo *info,
177 optional_yield y,
178 const DoutPrefixProvider *dpp,
179 const GetParams& params)
180 {
181 info->uid = uid;
182 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
183 return svc.otp->read_all(ctx, uid, &info->devices, params.mtime, params.objv_tracker, y, dpp);
184 });
185 }
186
187 int RGWOTPCtl::store_all(const DoutPrefixProvider *dpp,
188 const RGWOTPInfo& info,
189 optional_yield y,
190 const PutParams& params)
191 {
192 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
193 return svc.otp->store_all(dpp, ctx, info.uid, info.devices, params.mtime, params.objv_tracker, y);
194 });
195 }
196
197 int RGWOTPCtl::remove_all(const DoutPrefixProvider *dpp,
198 const rgw_user& uid,
199 optional_yield y,
200 const RemoveParams& params)
201 {
202 return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
203 return svc.otp->remove_all(dpp, ctx, uid, params.objv_tracker, y);
204 });
205 }
206
207
208 RGWMetadataHandler *RGWOTPMetaHandlerAllocator::alloc()
209 {
210 return new RGWOTPMetadataHandler();
211 }