-
Notifications
You must be signed in to change notification settings - Fork 2
/
machine.rs
125 lines (105 loc) · 3.33 KB
/
machine.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::date::Date;
use crate::errors::KeygenError;
use crate::machine_file::MachineFile;
use crate::utils::create_interface;
use keygen_rs::machine::Machine as KeygenRsMachine;
use pyo3::prelude::{PyAnyMethods, PyModule, PyModuleMethods};
use pyo3::{pyclass, pymethods, pymodule, Bound, PyAny, PyResult, Python};
#[pymodule(name = "machine")]
pub fn machine_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// Hack: workaround for https://github.com/PyO3/pyo3/issues/759
Python::with_gil(|py| {
py.import("sys")?
.getattr("modules")?
.set_item("keygen_sh.machine", m)
})?;
m.add_class::<Machine>()?;
Ok(())
}
create_interface!(Machine, KeygenRsMachine);
#[pymethods]
impl Machine {
#[getter]
fn id(&self) -> PyResult<String> {
Ok(self.inner.id.clone())
}
#[getter]
fn fingerprint(&self) -> PyResult<String> {
Ok(self.inner.fingerprint.clone())
}
#[getter]
fn name(&self) -> PyResult<Option<String>> {
Ok(self.inner.name.clone())
}
#[getter]
fn platform(&self) -> PyResult<Option<String>> {
Ok(self.inner.platform.clone())
}
#[getter]
fn hostname(&self) -> PyResult<Option<String>> {
Ok(self.inner.hostname.clone())
}
#[getter]
fn cores(&self) -> PyResult<Option<i32>> {
Ok(self.inner.cores)
}
#[getter]
fn require_heartbeat(&self) -> PyResult<bool> {
Ok(self.inner.require_heartbeat)
}
#[getter]
fn heartbeat_status(&self) -> PyResult<String> {
Ok(self.inner.heartbeat_status.clone())
}
#[getter]
fn heartbeat_duration(&self) -> PyResult<Option<i32>> {
Ok(self.inner.heartbeat_duration)
}
#[getter]
fn created(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.created))
}
#[getter]
fn updated(&self) -> PyResult<Date> {
Ok(Date::from(self.inner.updated))
}
fn deactivate<'a>(&'a self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
let my_struct = self.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let result = my_struct.inner.deactivate().await;
match result {
Ok(_) => Ok(()),
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
#[pyo3(signature = (ttl=None, include=None))]
fn checkout<'a>(
&'a self,
py: Python<'a>,
ttl: Option<i64>,
include: Option<Vec<String>>,
) -> PyResult<Bound<'a, PyAny>> {
let my_struct = self.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let result = my_struct
.inner
.checkout(&keygen_rs::machine::MachineCheckoutOpts { ttl, include })
.await;
match result {
Ok(mf) => Ok(MachineFile::from(mf)),
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
fn ping<'a>(&'a self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
let my_struct = self.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let result = my_struct.inner.ping().await;
match result {
Ok(m) => Ok(Machine::from(m)),
Err(e) => Err(KeygenError::from_error(e)),
}
})
}
}