~muirrum/homeworld

131dd5ab9df295b0bad320675b2307afcbf7d076 — Cara Salter 1 year, 11 months ago 90823a7
colonies: List users

First work towards cloud-init data being stored in homeworld
A migrations/20220710112943_colonies.sql => migrations/20220710112943_colonies.sql +7 -0
@@ 0,0 1,7 @@
-- Add migration script here
CREATE TABLE seed_users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    groups TEXT[] NOT NULL,
    ssh_authorized_keys TEXT[] NOT NULL
);

M sqlx-data.json => sqlx-data.json +12 -0
@@ 54,6 54,11 @@
          "name": "version",
          "ordinal": 4,
          "type_info": "Text"
        },
        {
          "name": "colonizable",
          "ordinal": 5,
          "type_info": "Bool"
        }
      ],
      "nullable": [


@@ 61,6 66,7 @@
        false,
        false,
        false,
        false,
        false
      ],
      "parameters": {


@@ 98,6 104,11 @@
          "name": "version",
          "ordinal": 4,
          "type_info": "Text"
        },
        {
          "name": "colonizable",
          "ordinal": 5,
          "type_info": "Bool"
        }
      ],
      "nullable": [


@@ 105,6 116,7 @@
        false,
        false,
        false,
        false,
        false
      ],
      "parameters": {

A src/handlers/colonies.rs => src/handlers/colonies.rs +46 -0
@@ 0,0 1,46 @@
use std::sync::Arc;

use axum::{Extension, Json};
use sqlx::query_as;

use crate::{
    errors::{JsonResult, ServiceError, StringResult},
    State,
};

use solarlib::colony::{User, UserData};

#[derive(Debug, Clone)]
pub struct DbUser {
    pub id: i32,
    pub name: String,
    pub groups: Vec<String>,
    pub ssh_authorized_keys: Vec<String>,
}

impl From<DbUser> for User {
    fn from(o: DbUser) -> Self {
        Self {
            name: o.name,
            groups: o.groups,
            ssh_authorized_keys: o.ssh_authorized_keys,
        }
    }
}

pub async fn list_users(state: Extension<Arc<State>>) -> JsonResult<Json<UserData>> {
    let mut conn = state.conn.acquire().await?;

    let db_users = query_as!(DbUser, "SELECT * FROM seed_users")
        .fetch_all(&mut conn)
        .await?;

    let users = db_users
        .into_iter()
        .map(|u| u.into())
        .collect::<Vec<User>>();

    let data = UserData { users };

    Ok(Json(data))
}

M src/handlers/mod.rs => src/handlers/mod.rs +1 -0
@@ 1,1 1,2 @@
pub mod colonies;
pub mod ships;

M src/main.rs => src/main.rs +1 -0
@@ 54,6 54,7 @@ async fn main() {
        .route("/ships/new", post(handlers::ships::new))
        .route("/ships/delete/:shasum", delete(handlers::ships::delete))
        .route("/ships/get/:shasum", get(handlers::ships::get))
        .route("/users/list", get(handlers::colonies::list_users))
        .layer(
            ServiceBuilder::new()
                .layer(HandleErrorLayer::new(|error: BoxError| async move {