aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/planets.rs
blob: 06773bc406e0f189220a409424c40b8d3bec12a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use axum::{response::IntoResponse, Json, extract::Path};

use tracing::{error, instrument};

use solarlib::star::Star;
use solarlib::planet::Planet;

use crate::{errors::*, get_star};

pub async fn list() -> JsonResult<Json<Vec<Planet>>> {
    let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
    let mut star = Star::new(con_url)?;
    
    let inhabitants = star.inhabitants()?;

    Ok(Json(inhabitants))
}

pub async fn get(Path(uuid): Path<String>) -> JsonResult<Json<Planet>> {

    let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
    let mut star = Star::new(con_url)?;

    if let Ok(p) = star.find_planet(uuid) {
        return Ok(Json(p));
    } else {
        return Err(ServiceError::NotFound);
    }
}

pub async fn shutdown(Path(uuid): Path<String>) -> NoneResult {
    let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
    let mut star = Star::new(con_url)?;

    if let Ok(p) = star.find_planet(uuid) {
        p.shutdown()?;
    } else {
        return Err(ServiceError::NotFound);
    }

    Ok(())
}

pub async fn start(Path(uuid): Path<String>) -> NoneResult {
    let mut s = get_star()?;

    if let Ok(p) = s.find_planet(uuid) {
        p.start()?;
    } else {
        return Err(ServiceError::NotFound);
    }

    Ok(())
}

pub async fn pause(Path(uuid): Path<String>) -> NoneResult {
    let mut s = get_star()?;

    if let Ok(p) = s.find_planet(uuid) {
        p.pause()?;
    } else {
        return Err(ServiceError::NotFound);
    }

    Ok(())
}

pub async fn reboot(Path(uuid): Path<String>) -> NoneResult {
    let mut s = get_star()?;

    if let Ok(p) = s.find_planet(uuid) {
        p.reboot()?;
    } else {
        return Err(ServiceError::NotFound);
    }
    
    Ok(())
}

pub async fn force_reboot(Path(uuid): Path<String>) -> NoneResult {
    let mut s = get_star()?;

    if let Ok(p) = s.find_planet(uuid) {
        p.hard_reboot()?;
    } else {
        return Err(ServiceError::NotFound);
    }

    Ok(())
}

pub async fn force_shutdown(Path(uuid): Path<String>) -> NoneResult {
    let mut s = get_star()?;

    if let Ok(p) = s.find_planet(uuid) {
        p.hard_shutdown()?;
    } else {
        return Err(ServiceError::NotFound);
    }

    Ok(())
}