-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft at routes disks and events
- Loading branch information
Patrick Meade
committed
Jun 27, 2024
1 parent
df9b1cc
commit 739836f
Showing
7 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,42 @@ | ||
# wipac-disk-tracking | ||
Archival media tracking service for WIPAC | ||
|
||
## API | ||
The REST API for the wipac-disk-tracking service is documented below. | ||
|
||
The API is rooted at: | ||
|
||
/api/v# | ||
|
||
Where # is the version number of the API under use. | ||
|
||
### Disks | ||
Disks represent a unique archival disk. | ||
While the disk entity does carry some immutable identifying information, it | ||
is mostly a container for archival disk events. | ||
|
||
#### Routes | ||
These routes are implemented to work with disks: | ||
|
||
GET /disks/:disk_id Get the data for a given disk | ||
GET /disks/:disk_id/events Get all of the events for a given disk | ||
GET /disks/:disk_id/events/:event_id Get the data for a given event for a given disk | ||
GET /disks/:disk_id/search?query Find a disk based on a key-value query | ||
|
||
### Events | ||
Events represent a record of an event involving an archival disk. | ||
There are four distinct events that are tracked by the system. | ||
|
||
sighted This disk was observed to be loaded in a host that processes archival disks | ||
formatted This disk was given a file system to make it ready for archival purposes | ||
opened This disk was given a label and was designated for active archival activity | ||
closed This disk was determined to full/finished and archival activity stopped | ||
|
||
The format for each of these events is specified with a JSON Schema file. | ||
|
||
#### Routes | ||
These routes are implemented to work with events: | ||
|
||
POST /events Create a new event | ||
GET /events/:event_id Get the data for a given event | ||
GET /events/search?query Find an event based on key-value query |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# TODO.md | ||
Some notes to keep track of progress | ||
|
||
## JSON Schema | ||
Look at [JSON Schema](https://json-schema.org/) and in particular some validators written in Rust: | ||
|
||
https://json-schema.org/implementations#validators-rust | ||
|
||
Rust | ||
boon 2020-122019-09070604 Apache License 2.0 | ||
jsonschema-rs | ||
Fast due to compiling schema into a validation tree; 2019-09 and 2020-12 are partially supported | ||
2020-122019-09070604 MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// routes.rs | ||
|
||
pub mod v1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// v1.rs | ||
|
||
pub mod disks; | ||
pub mod events; | ||
|
||
pub use disks::{DiskResource, DiskResources}; | ||
pub use events::{EventResource, EventResources}; | ||
|
||
use gotham::router::{build_simple_router, Router}; | ||
use gotham_restful::DrawResources; | ||
|
||
pub fn router() -> Router { | ||
build_simple_router(|route| { | ||
route.resource::<DiskResource>("disks"); | ||
route.resource::<EventResource>("events"); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// disks.rs | ||
|
||
use gotham::{router::response::StaticResponseExtender, state::StateData}; | ||
use gotham_restful::{endpoint, gotham::hyper::Method, read, search, Resource, Success}; | ||
use log::{error, info}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::routes::v1::{EventResource, EventResources}; | ||
|
||
#[derive(Resource, Serialize)] | ||
#[resource(get_disk_by_id)] | ||
#[resource(get_event_by_disk_id_and_event_id)] | ||
#[resource(get_events_by_disk_id)] | ||
#[resource(find_disk_by_query)] | ||
pub struct DiskResource { | ||
pub id: u64, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct DiskResources { | ||
pub disks: Vec<DiskResource>, | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(Clone, Deserialize, StateData, StaticResponseExtender)] | ||
struct DiskSearchQuery { | ||
serial_number: String, | ||
} | ||
|
||
#[search] | ||
fn find_disk_by_query(query: DiskSearchQuery) -> Success<DiskResources> { | ||
info!("find_disk_by_query()"); | ||
DiskResources { | ||
disks: Vec::new() | ||
}.into() | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[read] | ||
fn get_disk_by_id(disk_id: u64) -> Success<DiskResource> { | ||
info!("get_disk_by_id()"); | ||
DiskResource { id: disk_id }.into() | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(Clone, Deserialize, StateData, StaticResponseExtender)] | ||
struct DiskAndEvent { | ||
disk_id: u64, | ||
event_id: u64, | ||
} | ||
|
||
#[endpoint( | ||
uri = ":disk_id/events/:event_id", | ||
method = "Method::GET", | ||
params = false, | ||
body = false | ||
)] | ||
fn get_event_by_disk_id_and_event_id(disk_and_event: DiskAndEvent) -> Success<EventResource> { | ||
info!("get_event_by_disk_id_and_event_id()"); | ||
EventResource { id: disk_and_event.event_id }.into() | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(Clone, Deserialize, StateData, StaticResponseExtender)] | ||
struct Disk { | ||
disk_id: u64, | ||
} | ||
|
||
#[endpoint( | ||
uri = ":disk_id/events", | ||
method = "Method::GET", | ||
params = false, | ||
body = false | ||
)] | ||
fn get_events_by_disk_id(disk: Disk) -> Success<EventResources> { | ||
info!("get_events_by_disk_id()"); | ||
EventResources { | ||
events: Vec::new() | ||
}.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// events.rs | ||
|
||
use gotham::{router::response::StaticResponseExtender, state::StateData}; | ||
use gotham_restful::{create, read, search, Resource, Success}; | ||
use log::{error, info}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::{json, Value}; | ||
|
||
#[derive(Resource, Serialize)] | ||
#[resource(create_event)] | ||
#[resource(find_event_by_query)] | ||
#[resource(get_event_by_id)] | ||
pub struct EventResource { | ||
pub id: u64, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct EventResources { | ||
pub events: Vec<EventResource>, | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(Clone, Deserialize, StateData, StaticResponseExtender)] | ||
struct EventBody { | ||
serial_number: String, | ||
} | ||
|
||
#[create] | ||
fn create_event(body: EventBody) -> Success<EventResource> { | ||
info!("create_event()"); | ||
EventResource { id: 0 }.into() | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[derive(Clone, Deserialize, StateData, StaticResponseExtender)] | ||
struct EventSearchQuery { | ||
serial_number: String, | ||
} | ||
|
||
#[search] | ||
fn find_event_by_query(query: EventSearchQuery) -> Success<EventResources> { | ||
info!("find_event_by_query()"); | ||
EventResources { | ||
events: Vec::new(), | ||
}.into() | ||
} | ||
|
||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
#[read] | ||
fn get_event_by_id(id: u64) -> Success<EventResource> { | ||
info!("get_event_by_id()"); | ||
EventResource { id }.into() | ||
} |