/
libirods_rule_engine_plugin-policy_engine-testing_policy.cpp
195 lines (166 loc) · 6.53 KB
/
libirods_rule_engine_plugin-policy_engine-testing_policy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <irods/policy_composition_framework_policy_engine.hpp>
#include <irods/policy_composition_framework_parameter_capture.hpp>
#include <irods/policy_composition_framework_configuration_manager.hpp>
#include <irods/rsModAVUMetadata.hpp>
#define IRODS_METADATA_ENABLE_SERVER_SIDE_API
#include <irods/metadata.hpp>
#include <irods/policy_composition_framework_event_handler.hpp>
namespace {
// clang-format off
namespace pc = irods::policy_composition;
namespace kw = irods::policy_composition::keywords;
namespace pe = irods::policy_composition::policy_engine;
namespace fs = irods::experimental::filesystem;
namespace fsvr = irods::experimental::filesystem::server;
using json = nlohmann::json;
// clang-format on
const std::map<std::string, std::string> type_to_token {
{kw::collection, "-C"},
{kw::data_object, "-d"},
{kw::user, "-u"},
{kw::resource, "-R"}
};
auto type_to_entity(const std::string& _type, json _params)
{
const std::map<std::string, std::string> type_to_index {
{kw::collection, kw::logical_path},
{kw::data_object, kw::logical_path},
{kw::user, kw::user_name},
{kw::resource, kw::source_resource}
};
auto idx = type_to_index.at(_type);
return _params.at(idx).get<std::string>();
} // type_to_entity
irods::error testing_policy(const pe::context& ctx, pe::arg_type out)
{
std::string user_name{}, logical_path{}, source_resource{}, destination_resource{};
rodsLog(LOG_NOTICE, "PARAMETERS\n--------\n%s\n--------", ctx.parameters.dump(4).c_str());
if(ctx.parameters.contains("query_results")) {
using fsp = irods::experimental::filesystem::path;
std::string tmp_coll_name{}, tmp_data_name{};
auto query_results = ctx.parameters.at("query_results").get<std::vector<std::string>>();
//user_name = query_results[0];
tmp_coll_name = query_results[1];
tmp_data_name = query_results[2];
//source_resource = query_results[3];
logical_path = (fsp{tmp_coll_name} / fsp{tmp_data_name}).string();
}
else {
std::tie(user_name, logical_path, source_resource, destination_resource) =
capture_parameters(ctx.parameters, tag_first_resc);
}
auto comm = ctx.rei->rsComm;
std::string event{"unspecified"};
if(ctx.parameters.contains(kw::event)) {
event = ctx.parameters.at(kw::event);
}
std::string entity_type, option, target;
modAVUMetadataInp_t set_op{};
if("METADATA" == event) {
if(ctx.parameters.contains(kw::conditional)) {
if(ctx.parameters.contains(kw::metadata)) {
if(ctx.parameters.at(kw::conditional).at(kw::metadata).contains(kw::entity_type)) {
entity_type = ctx.parameters.at(kw::conditional).at(kw::metadata).at(kw::entity_type);
}
}
}
else if(ctx.parameters.contains(kw::metadata)) {
if(ctx.parameters.at(kw::metadata).contains(kw::entity_type)) {
entity_type = ctx.parameters.at(kw::metadata).at(kw::entity_type);
}
}
else {
return ERROR(
SYS_INVALID_INPUT_PARAM,
"testing_policy :: missing 'entity_type' in 'metadata'");
}
option = type_to_token.at(entity_type);
target = type_to_entity(entity_type, ctx.parameters);
}
else if(!logical_path.empty()) {
target = logical_path;
if(!fsvr::exists(*comm, target)) {
target = fs::path(target).parent_path();
}
option = fsvr::is_data_object(*comm, target) ? "-d" : "-C";
}
else if(!source_resource.empty()) {
target = (event == "REMOVE") ? "demoResc" : source_resource;
option = "-R";
}
else if(!user_name.empty()) {
target = (event == "REMOVE") ? "rods" : user_name;
option = "-u";
}
set_op.arg0 = "add";
set_op.arg1 = const_cast<char*>(option.c_str());
set_op.arg2 = const_cast<char*>(target.c_str());
set_op.arg3 = "irods_policy_testing_policy";
set_op.arg4 = const_cast<char*>(event.c_str());
auto status = rsModAVUMetadata(comm, &set_op);
if(status < 0) {
return ERROR(
status,
boost::format("Failed to invoke test_policy for [%s] with metadata [%s] [%s]")
% target
% "irods_testing_policy"
% event);
}
return SUCCESS();
} // testing_policy
} // namespace
const char usage[] = R"(
{
"id": "file:///var/lib/irods/configuration_schemas/v3/policy_engine_usage.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "This is a test, this is only a test."
"input_interfaces": [
{
"name" : "event_handler-collection_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-data_object_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-metadata_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-user_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "event_handler-resource_modified",
"description" : "",
"json_schema" : ""
},
{
"name" : "direct_invocation",
"description" : "",
"json_schema" : ""
},
{
"name" : "query_results"
"description" : "",
"json_schema" : ""
},
],
"output_json_for_validation" : ""
}
)";
extern "C"
pe::plugin_pointer_type plugin_factory(
const std::string& _plugin_name
, const std::string&) {
return pe::make(
_plugin_name
, "irods_policy_testing_policy"
, usage
, testing_policy);
} // plugin_factory