Skip to content

Commit 0d621bb

Browse files
authored
Merge pull request #2 from ChocPanda/main
feat(#16369): add a udm events endpoint to the emulator for testing
2 parents 7c853c5 + ce61c1f commit 0d621bb

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM rust:1.62.0 as builder
1+
FROM rust:1.82.0 as builder
22

33
WORKDIR /app
44
RUN apt update && apt install lld clang -y
@@ -7,7 +7,7 @@ COPY . .
77

88
RUN cargo build --release
99

10-
FROM rust:1.62.0 as runtime
10+
FROM rust:1.82.0 as runtime
1111

1212
WORKDIR /app
1313

src/data.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ pub struct UnstructuredLog {
3030
ts_rfc3339: Option<String>,
3131
}
3232

33+
#[derive(Debug, Deserialize, Serialize)]
34+
pub struct UdmEvents {
35+
pub customer_id: String,
36+
pub events: Vec<UdmEvent>,
37+
}
38+
39+
#[derive(Debug, Deserialize, Serialize)]
40+
pub struct UdmEvent {
41+
metadata: UdmMetadata,
42+
pub invalid: Option<bool>,
43+
}
44+
45+
#[derive(Debug, Deserialize, Serialize)]
46+
pub struct UdmMetadata {
47+
log_type: String,
48+
namespace: Option<String>,
49+
ts_epoch_microseconds: Option<i64>,
50+
ts_rfc3339: Option<String>,
51+
}
52+
3353
impl From<UnstructuredLogs> for Vec<Log> {
3454
fn from(logs: UnstructuredLogs) -> Self {
3555
logs.entries
@@ -51,8 +71,32 @@ impl From<UnstructuredLogs> for Vec<Log> {
5171
}
5272
}
5373

74+
impl From<UdmEvents> for Vec<Log> {
75+
fn from(logs: UdmEvents) -> Self {
76+
logs.events.into_iter().map(|event| Log {
77+
customer_id: logs.customer_id.clone(),
78+
log_type: event.metadata.log_type.clone(),
79+
log_text: String::new(),
80+
namespace: event.metadata.namespace.clone(),
81+
ts_rfc3339: event.metadata.ts_rfc3339.unwrap_or_else(|| {
82+
event.metadata
83+
.ts_epoch_microseconds
84+
.map(|ms| Utc.timestamp_millis(ms))
85+
.unwrap_or_else(Utc::now)
86+
.to_rfc3339()
87+
}),
88+
}).collect()
89+
}
90+
}
91+
92+
/// Adds the logs to our (in memory) database.
93+
pub fn add_unstructured_to_data(logs: UnstructuredLogs) {
94+
let mut data = DATA.lock().unwrap();
95+
data.append(&mut Vec::from(logs));
96+
}
97+
5498
/// Adds the logs to our (in memory) database.
55-
pub fn add_to_data(logs: UnstructuredLogs) {
99+
pub fn add_udm_events_to_data(logs: UdmEvents) {
56100
let mut data = DATA.lock().unwrap();
57101
data.append(&mut Vec::from(logs));
58102
}

src/main.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ async fn logs(log_type: Query<LogType>) -> Json<Vec<data::Log>> {
6666
Json(logs)
6767
}
6868

69+
/// Handler that posts a set UDM events.
70+
/// To test unauthorised entries, if the `customer_id` of "INVALID" is passed
71+
/// the handler will return a 401 UNAUTHORISED response.
72+
/// if the invalid UDM field is passed
73+
/// the handler will return 400 BAD_REQUEST response.
74+
async fn create_udm_events(
75+
Json(payload): Json<data::UdmEvents>,
76+
_user: User,
77+
) -> impl IntoResponse {
78+
let contains_invalid_events = payload.events.iter().any(|event| event.invalid.unwrap_or_default());
79+
if payload.customer_id == "INVALID" {
80+
(StatusCode::UNAUTHORIZED, Json(false))
81+
} else if contains_invalid_events {
82+
(StatusCode::BAD_REQUEST, Json(false))
83+
}else {
84+
data::add_udm_events_to_data(payload);
85+
(StatusCode::CREATED, Json(true))
86+
}
87+
}
88+
6989
/// Handler that posts a set of unstructured log entries.
7090
/// To test invalid entries, if a `log_type` of "INVALID" is passed
7191
/// the handler will return a 400 BAD_REQUEST response.
@@ -76,7 +96,7 @@ async fn create_unstructured(
7696
if payload.log_type == "INVALID" {
7797
(StatusCode::BAD_REQUEST, Json(false))
7898
} else {
79-
data::add_to_data(payload);
99+
data::add_unstructured_to_data(payload);
80100
(StatusCode::CREATED, Json(true))
81101
}
82102
}
@@ -166,6 +186,7 @@ async fn main() {
166186
"/v2/unstructuredlogentries:batchCreate",
167187
post(create_unstructured),
168188
)
189+
.route("/v2/udmevents:batchCreate", post(create_udm_events))
169190
// Query the posted logs.
170191
.route("/logs", get(logs));
171192

0 commit comments

Comments
 (0)