-
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.
- Loading branch information
Showing
18 changed files
with
3,585 additions
and
743 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
engine/.sqlx/query-ae16a4e27cbdf614ad76ef94059757ac5bcee58fb91746925fdf2f5ee23044ff.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
DROP TABLE IF EXISTS local_operators; | ||
|
||
DELETE FROM policies WHERE resource_type = 'local_operator'; |
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,18 @@ | ||
--- Create LocalOperator table | ||
CREATE TABLE IF NOT EXISTS local_operators ( | ||
operator_id TEXT PRIMARY KEY, | ||
operator_endpoint TEXT NOT NULL, | ||
operator_last_heartbeat TIMESTAMP WITH TIME ZONE NOT NULL | ||
); | ||
|
||
INSERT INTO policies ( | ||
resource_type, resource_id, action, subject_type, subject_id | ||
) VALUES ( | ||
'local_operator', NULL, 'read', 'authed', 'true' | ||
); | ||
|
||
INSERT INTO policies ( | ||
resource_type, resource_id, action, subject_type, subject_id | ||
) VALUES ( | ||
'local_operator', NULL, 'write', 'authed', 'true' | ||
); |
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,40 @@ | ||
use chrono::{DateTime, Utc}; | ||
use poem_openapi::Object; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::query_as; | ||
|
||
use crate::database::Database; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Object)] | ||
pub struct LocalOperator { | ||
pub operator_id: String, | ||
pub operator_endpoint: String, | ||
pub operator_last_heartbeat: DateTime<Utc>, | ||
} | ||
|
||
impl LocalOperator { | ||
pub async fn upsert( | ||
db: &Database, | ||
operator_id: &str, | ||
operator_endpoint: &str, | ||
) -> Result<Self, anyhow::Error> { | ||
let x = query_as!( | ||
LocalOperator, | ||
"INSERT INTO local_operators (operator_id, operator_endpoint, operator_last_heartbeat) VALUES ($1, $2, NOW()) ON CONFLICT (operator_id) DO UPDATE SET operator_last_heartbeat = NOW() RETURNING *", | ||
operator_id, | ||
operator_endpoint | ||
) | ||
.fetch_one(&db.pool) | ||
.await?; | ||
|
||
Ok(x) | ||
} | ||
|
||
pub async fn list_operators(db: &Database) -> Result<Vec<Self>, sqlx::Error> { | ||
let x = query_as!(LocalOperator, "SELECT * FROM local_operators") | ||
.fetch_all(&db.pool) | ||
.await?; | ||
|
||
Ok(x) | ||
} | ||
} |
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
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,61 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::auth::middleware::AuthUser; | ||
use crate::auth::permissions::Action; | ||
use crate::models::local_operators::LocalOperator; | ||
use crate::state::AppState; | ||
use poem::web::Data; | ||
use poem::Result; | ||
use poem_openapi::payload::Json; | ||
use poem_openapi::Object; | ||
use poem_openapi::OpenApi; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::error::HttpError; | ||
use crate::routes::ApiTags; | ||
|
||
#[derive(Debug, Deserialize, Serialize, Object)] | ||
pub struct LocalOperatorPayload { | ||
// The operator decides this themselves | ||
pub operator_id: String, | ||
// This identifies where the operator is running | ||
pub operator_endpoint: String, | ||
} | ||
|
||
pub struct OperatorApi; | ||
|
||
#[OpenApi] | ||
impl OperatorApi { | ||
#[oai(path = "/operators", method = "get", tag = "ApiTags::Operators")] | ||
async fn list_operators( | ||
&self, | ||
user: AuthUser, | ||
state: Data<&Arc<AppState>>, | ||
) -> Result<Json<Vec<LocalOperator>>> { | ||
user.check_policy("local_operator", None, Action::Read) | ||
.await?; | ||
|
||
LocalOperator::list_operators(&state.database) | ||
.await | ||
.map(Json) | ||
.map_err(HttpError::from) | ||
.map_err(poem::Error::from) | ||
} | ||
|
||
#[oai(path = "/operators", method = "post", tag = "ApiTags::Operators")] | ||
async fn create_operator( | ||
&self, | ||
user: AuthUser, | ||
state: Data<&Arc<AppState>>, | ||
payload: Json<LocalOperatorPayload>, | ||
) -> Result<Json<LocalOperator>> { | ||
user.check_policy("local_operator", None, Action::Write) | ||
.await?; | ||
|
||
LocalOperator::upsert(&state.database, &payload.operator_id, &payload.operator_endpoint) | ||
.await | ||
.map(Json) | ||
.map_err(HttpError::from) | ||
.map_err(poem::Error::from) | ||
} | ||
} |
Oops, something went wrong.