Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_rest_opstate.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation. See file COPYING.
12  *
13  */
14 #include "common/ceph_json.h"
15 #include "common/strtol.h"
16 #include "rgw_rest.h"
17 #include "rgw_op.h"
18 #include "rgw_rest_s3.h"
19 #include "rgw_rest_opstate.h"
20 #include "rgw_client_io.h"
21 #include "common/errno.h"
22 #include "include/assert.h"
23
24 #define dout_context g_ceph_context
25 #define OPSTATE_LIST_MAX_ENTRIES 1000
26 #define dout_subsys ceph_subsys_rgw
27
28 void RGWOp_Opstate_List::execute() {
29   string client_id = s->info.args.get("client-id"),
30          op_id = s->info.args.get("op-id"),
31          object = s->info.args.get("object"),
32          max_entries_str = s->info.args.get("max-entries");
33   unsigned max_entries = OPSTATE_LIST_MAX_ENTRIES;
34   
35   if (!max_entries_str.empty()) {
36     string err;
37     max_entries = (unsigned)strict_strtol(max_entries_str.c_str(), 10, &err);
38     if (!err.empty()) {
39       dout(5) << "Error parsing max-entries " << max_entries_str << dendl;
40       http_ret = -EINVAL;
41       return;
42     }
43   } 
44   
45   RGWOpState oc = RGWOpState(store);
46   void *handle;
47   bool done;
48   list<cls_statelog_entry> entries;
49
50   oc.init_list_entries(client_id, op_id, object, &handle);
51
52   http_ret = 0;
53   send_response();
54   do {
55     int ret = oc.list_entries(handle, max_entries, entries, &done);
56
57     if (ret < 0) {
58       dout(5) << "oc.list_entries failed with error " << ret << dendl;
59       http_ret = ret;
60       oc.finish_list_entries(handle);
61       return;
62     }
63
64     if (!max_entries_str.empty()) 
65       max_entries -= entries.size();
66
67     send_response(entries);
68   } while (!done && max_entries > 0);
69
70   oc.finish_list_entries(handle);
71   send_response_end();
72 }
73
74 void RGWOp_Opstate_List::send_response() {
75   if (sent_header)
76     return;
77
78   set_req_state_err(s, http_ret);
79   dump_errno(s);
80   end_header(s);
81
82   sent_header = true;
83
84   if (http_ret < 0)
85     return;
86
87   s->formatter->open_array_section("entries");
88 }
89
90 void RGWOp_Opstate_List::send_response(list<cls_statelog_entry> entries) {
91   RGWOpState oc(store);
92   for (list<cls_statelog_entry>::iterator it = entries.begin();
93        it != entries.end(); ++it) {
94     oc.dump_entry(*it, s->formatter);
95     flusher.flush();
96   }
97 }
98
99 void RGWOp_Opstate_List::send_response_end() {
100   s->formatter->close_section();
101   flusher.flush();
102 }
103
104 void RGWOp_Opstate_Set::execute() {
105   string client_id = s->info.args.get("client-id"),
106          op_id = s->info.args.get("op-id"),
107          object = s->info.args.get("object"),
108          state_str = s->info.args.get("state");
109
110   if (client_id.empty() ||
111       op_id.empty() ||
112       object.empty() ||
113       state_str.empty()) {
114     dout(5) << "Error - invalid parameter list" << dendl;
115     http_ret = -EINVAL;
116     return;
117   }
118
119   RGWOpState oc(store);
120   RGWOpState::OpState state;
121
122   http_ret = oc.state_from_str(state_str, &state);
123   if (http_ret < 0) {
124     dout(5) << "Error - invalid state" << dendl;
125     return;
126   }
127
128   http_ret = oc.set_state(client_id, op_id, object, state);
129   if (http_ret < 0) {
130     dout(5) << "Error - Unable to set state" << dendl;
131     return;
132   }
133 }
134
135 void RGWOp_Opstate_Renew::execute() {
136   string client_id = s->info.args.get("client-id"),
137          op_id = s->info.args.get("op-id"),
138          object = s->info.args.get("object"),
139          state_str = s->info.args.get("state");
140
141   if (client_id.empty() ||
142       op_id.empty() ||
143       object.empty() ||
144       state_str.empty()) {
145     dout(5) << "Error - invalid parameter list" << dendl;
146     http_ret = -EINVAL;
147     return;
148   }
149   RGWOpState oc(store);
150   RGWOpState::OpState state;
151
152   http_ret = oc.state_from_str(state_str, &state);
153   if (http_ret < 0) {
154     dout(5) << "Error - invalid state" << dendl;
155     return;
156   }
157
158   http_ret = oc.renew_state(client_id, op_id, object, state);
159   if (http_ret < 0) {
160     dout(5) << "Error - Unable to renew state" << dendl;
161     return;
162   }
163 }
164
165 void RGWOp_Opstate_Delete::execute() {
166   string client_id = s->info.args.get("client-id"),
167          op_id = s->info.args.get("op-id"),
168          object = s->info.args.get("object");
169
170   if (client_id.empty() ||
171       op_id.empty() ||
172       object.empty()) {
173     dout(5) << "Error - invalid parameter list" << dendl;
174     http_ret = -EINVAL;
175     return;
176   }
177   
178   RGWOpState oc(store);
179
180   http_ret = oc.remove_entry(client_id, op_id, object);
181   if (http_ret < 0) {
182     dout(5) << "Error - Unable to remove entry" << dendl;
183   }
184 }
185
186 RGWOp *RGWHandler_Opstate::op_post() {
187   if (s->info.args.exists("renew")) {
188     return new RGWOp_Opstate_Renew;
189   } 
190   return new RGWOp_Opstate_Set;
191 }