Skip to content

Commit 497c216

Browse files
committed
MINIFICPP-1448 - Separate json subformats into its own property
1 parent 791db78 commit 497c216

File tree

5 files changed

+58
-56
lines changed

5 files changed

+58
-56
lines changed

extensions/windows-event-log/ConsumeWindowsEventLog.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,18 @@ core::Property ConsumeWindowsEventLog::OutputFormat(
135135
core::PropertyBuilder::createProperty("Output Format")->
136136
isRequired(true)->
137137
withDefaultValue(Both)->
138-
withAllowableValues<std::string>({XML, Plaintext, Both, JSONSimple, JSONFlattened, JSONRaw})->
138+
withAllowableValues<std::string>({XML, Plaintext, Both, JSON})->
139139
withDescription("Set the output format type. In case \'Both\' is selected the processor generates two flow files for every event captured in format XML and Plaintext")->
140140
build());
141141

142+
core::Property ConsumeWindowsEventLog::JSONFormat(
143+
core::PropertyBuilder::createProperty("JSON Format")->
144+
isRequired(true)->
145+
withDefaultValue(JSONSimple)->
146+
withAllowableValues<std::string>({JSONSimple, JSONFlattened, JSONRaw})->
147+
withDescription("Set the json format type. Only applicable if Output Format is set to 'JSON'")->
148+
build());
149+
142150
core::Property ConsumeWindowsEventLog::BatchCommitSize(
143151
core::PropertyBuilder::createProperty("Batch Commit Size")->
144152
isRequired(false)->
@@ -198,7 +206,7 @@ void ConsumeWindowsEventLog::initialize() {
198206
//! Set the supported properties
199207
setSupportedProperties({
200208
Channel, Query, MaxBufferSize, InactiveDurationToReconnect, IdentifierMatcher, IdentifierFunction, ResolveAsAttributes,
201-
EventHeaderDelimiter, EventHeader, OutputFormat, BatchCommitSize, BookmarkRootDirectory, ProcessOldEvents
209+
EventHeaderDelimiter, EventHeader, OutputFormat, JSONFormat, BatchCommitSize, BookmarkRootDirectory, ProcessOldEvents
202210
});
203211

204212
//! Set the supported relationships
@@ -259,12 +267,16 @@ void ConsumeWindowsEventLog::onSchedule(const std::shared_ptr<core::ProcessConte
259267
} else if (mode == Both) {
260268
output_.xml = true;
261269
output_.plaintext = true;
262-
} else if (mode == JSONRaw) {
263-
output_.json.raw = true;
264-
} else if (mode == JSONSimple) {
265-
output_.json.simple = true;
266-
} else if (mode == JSONFlattened) {
267-
output_.json.flattened = true;
270+
} else if (mode == JSON) {
271+
std::string json_format;
272+
context->getProperty(JSONFormat.getName(), json_format);
273+
if (json_format == JSONRaw) {
274+
output_.json.type = JSONType::Raw;
275+
} else if (json_format == JSONSimple) {
276+
output_.json.type = JSONType::Simple;
277+
} else if (json_format == JSONFlattened) {
278+
output_.json.type = JSONType::Flattened;
279+
}
268280
} else {
269281
// in the future this might be considered an error, but for now due to backwards
270282
// compatibility we just fall through and execute the processor outputing nothing
@@ -625,27 +637,17 @@ bool ConsumeWindowsEventLog::createEventRender(EVT_HANDLE hEvent, EventRender& e
625637
logger_->log_trace("Finish writing in XML");
626638
}
627639

628-
if (output_.json.raw) {
640+
if (output_.json.type == JSONType::Raw) {
629641
logger_->log_trace("Writing event in raw JSON");
630-
631-
eventRender.json.raw = wel::jsonToString(wel::toRawJSON(doc));
632-
642+
eventRender.json = wel::jsonToString(wel::toRawJSON(doc));
633643
logger_->log_trace("Finish writing in raw JSON");
634-
}
635-
636-
if (output_.json.simple) {
644+
} else if (output_.json.type == JSONType::Simple) {
637645
logger_->log_trace("Writing event in simple JSON");
638-
639-
eventRender.json.simple = wel::jsonToString(wel::toSimpleJSON(doc));
640-
646+
eventRender.json = wel::jsonToString(wel::toSimpleJSON(doc));
641647
logger_->log_trace("Finish writing in simple JSON");
642-
}
643-
644-
if (output_.json.flattened) {
648+
} else if (output_.json.type == JSONType::Flattened) {
645649
logger_->log_trace("Writing event in flattened JSON");
646-
647-
eventRender.json.flattened = wel::jsonToString(wel::toFlattenedJSON(doc));
648-
650+
eventRender.json = wel::jsonToString(wel::toFlattenedJSON(doc));
649651
logger_->log_trace("Finish writing in flattened JSON");
650652
}
651653

@@ -730,19 +732,15 @@ void ConsumeWindowsEventLog::putEventRenderFlowFileToSession(const EventRender&
730732
commitFlowFile(session.create(), eventRender.plaintext, "text/plain");
731733
}
732734

733-
if (output_.json.raw) {
735+
if (output_.json.type == JSONType::Raw) {
734736
logger_->log_trace("Writing rendered raw JSON to a flow file");
735-
commitFlowFile(session.create(), eventRender.json.raw, "application/json");
736-
}
737-
738-
if (output_.json.simple) {
737+
commitFlowFile(session.create(), eventRender.json, "application/json");
738+
} else if (output_.json.type == JSONType::Simple) {
739739
logger_->log_trace("Writing rendered simple JSON to a flow file");
740-
commitFlowFile(session.create(), eventRender.json.simple, "application/json");
741-
}
742-
743-
if (output_.json.flattened) {
740+
commitFlowFile(session.create(), eventRender.json, "application/json");
741+
} else if (output_.json.type == JSONType::Flattened) {
744742
logger_->log_trace("Writing rendered flattened JSON to a flow file");
745-
commitFlowFile(session.create(), eventRender.json.flattened, "application/json");
743+
commitFlowFile(session.create(), eventRender.json, "application/json");
746744
}
747745
}
748746

extensions/windows-event-log/ConsumeWindowsEventLog.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ struct EventRender {
4646
std::map<std::string, std::string> matched_fields;
4747
std::string xml;
4848
std::string plaintext;
49-
struct {
50-
std::string raw;
51-
std::string simple;
52-
std::string flattened;
53-
} json;
49+
std::string json;
5450
};
5551

5652
class Bookmark;
@@ -82,6 +78,7 @@ class ConsumeWindowsEventLog : public core::Processor {
8278
static core::Property EventHeaderDelimiter;
8379
static core::Property EventHeader;
8480
static core::Property OutputFormat;
81+
static core::Property JSONFormat;
8582
static core::Property BatchCommitSize;
8683
static core::Property BookmarkRootDirectory;
8784
static core::Property ProcessOldEvents;
@@ -115,9 +112,10 @@ class ConsumeWindowsEventLog : public core::Processor {
115112
static constexpr const char* XML = "XML";
116113
static constexpr const char* Both = "Both";
117114
static constexpr const char* Plaintext = "Plaintext";
118-
static constexpr const char* JSONRaw = "JSON::Raw";
119-
static constexpr const char* JSONSimple = "JSON::Simple";
120-
static constexpr const char* JSONFlattened = "JSON::Flattened";
115+
static constexpr const char* JSON = "JSON";
116+
static constexpr const char* JSONRaw = "Raw";
117+
static constexpr const char* JSONSimple = "Simple";
118+
static constexpr const char* JSONFlattened = "Flattened";
121119

122120
private:
123121
struct TimeDiff {
@@ -150,17 +148,17 @@ class ConsumeWindowsEventLog : public core::Processor {
150148
std::map<std::string, wel::WindowsEventLogHandler > providers_;
151149
uint64_t batch_commit_size_{};
152150

151+
enum class JSONType {None, Raw, Simple, Flattened};
152+
153153
struct OutputFormat {
154154
bool xml{false};
155155
bool plaintext{false};
156-
struct {
156+
struct JSON {
157+
JSONType type{JSONType::None};
158+
157159
explicit operator bool() const noexcept {
158-
return raw || simple || flattened;
160+
return type != JSONType::None;
159161
}
160-
161-
bool raw{false};
162-
bool simple{false};
163-
bool flattened{false};
164162
} json;
165163
} output_;
166164

extensions/windows-event-log/tests/CWELCustomProviderTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool dispatchCustomEvent(const CustomEventData& event) {
6060

6161
class CustomProviderController : public OutputFormatTestController {
6262
public:
63-
CustomProviderController(std::string format) : OutputFormatTestController(CUSTOM_CHANNEL, "*", std::move(format)) {}
63+
CustomProviderController(std::string format, std::string json_format) : OutputFormatTestController(CUSTOM_CHANNEL, "*", std::move(format), std::move(json_format)) {}
6464

6565
protected:
6666
void dispatchBookmarkEvent() override {
@@ -102,7 +102,7 @@ const std::string EVENT_DATA_JSON = R"(
102102
} // namespace
103103

104104
TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Simple correctly custom provider", "[onTrigger]") {
105-
std::string event = CustomProviderController{"JSON::Simple"}.run();
105+
std::string event = CustomProviderController{"JSON", "Simple"}.run();
106106
verifyJSON(event, R"(
107107
{
108108
"System": {
@@ -117,7 +117,7 @@ TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Simple correctly custom
117117
}
118118

119119
TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Flattened correctly custom provider", "[onTrigger]") {
120-
std::string event = CustomProviderController{"JSON::Flattened"}.run();
120+
std::string event = CustomProviderController{"JSON", "Flattened"}.run();
121121
verifyJSON(event, R"(
122122
{
123123
"Name": ")" + CUSTOM_PROVIDER_NAME + R"(",

extensions/windows-event-log/tests/CWELTestUtils.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "TestBase.h"
2323
#include "utils/TestUtils.h"
2424
#include "utils/file/FileUtils.h"
25+
#include "utils/OptionalUtils.h"
2526

2627
core::Relationship Success{"success", "Everything is fine"};
2728

@@ -30,10 +31,11 @@ using PutFile = org::apache::nifi::minifi::processors::PutFile;
3031

3132
class OutputFormatTestController : public TestController {
3233
public:
33-
OutputFormatTestController(std::string channel, std::string query, std::string output_format)
34+
OutputFormatTestController(std::string channel, std::string query, std::string output_format, utils::optional<std::string> json_format = {})
3435
: channel_(std::move(channel)),
3536
query_(std::move(query)),
36-
output_format_(std::move(output_format)) {}
37+
output_format_(std::move(output_format)),
38+
json_format_(std::move(json_format)) {}
3739

3840
std::string run() {
3941
LogTestController::getInstance().setDebug<ConsumeWindowsEventLog>();
@@ -44,6 +46,9 @@ class OutputFormatTestController : public TestController {
4446
test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::Channel.getName(), channel_);
4547
test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::Query.getName(), query_);
4648
test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::OutputFormat.getName(), output_format_);
49+
if (json_format_) {
50+
test_plan->setProperty(cwel_processor, ConsumeWindowsEventLog::JSONFormat.getName(), json_format_.value());
51+
}
4752

4853
auto dir = utils::createTempDir(this);
4954

@@ -80,6 +85,7 @@ class OutputFormatTestController : public TestController {
8085
std::string channel_;
8186
std::string query_;
8287
std::string output_format_;
88+
utils::optional<std::string> json_format_;
8389
};
8490

8591
// carries out a loose match on objects, i.e. it doesn't matter if the

extensions/windows-event-log/tests/ConsumeWindowsEventLogTests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ TEST_CASE("ConsumeWindowsEventLog prints events in XML correctly", "[onTrigger]"
340340
}
341341

342342
TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Simple correctly", "[onTrigger]") {
343-
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON::Simple"}.run();
343+
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON", "Simple"}.run();
344344
verifyJSON(event, R"json(
345345
{
346346
"System": {
@@ -359,7 +359,7 @@ TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Simple correctly", "[on
359359
}
360360

361361
TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Flattened correctly", "[onTrigger]") {
362-
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON::Flattened"}.run();
362+
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON", "Flattened"}.run();
363363
verifyJSON(event, R"json(
364364
{
365365
"Name": "Application",
@@ -374,7 +374,7 @@ TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Flattened correctly", "
374374
}
375375

376376
TEST_CASE("ConsumeWindowsEventLog prints events in JSON::Raw correctly", "[onTrigger]") {
377-
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON::Raw"}.run();
377+
std::string event = SimpleFormatTestController{APPLICATION_CHANNEL, "*", "JSON", "Raw"}.run();
378378
verifyJSON(event, R"json(
379379
[
380380
{

0 commit comments

Comments
 (0)