cleanup api3 structure

This commit is contained in:
Dietmar Maurer 2018-12-08 14:44:55 +01:00
parent 678d72df6b
commit 6ce50400c5
6 changed files with 88 additions and 28 deletions

View File

@ -1,11 +1,14 @@
use failure::*; use failure::*;
use std::collections::HashMap; //use std::collections::HashMap;
use crate::api::schema::*; use crate::api::schema::*;
use crate::api::router::*; use crate::api::router::*;
use serde_json::{json, Value}; use serde_json::{json, Value};
mod config;
mod version;
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
println!("This is a test {}", param); println!("This is a test {}", param);
@ -24,20 +27,6 @@ fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error
Ok(json!(null)) Ok(json!(null))
} }
const PROXMOX_PKG_VERSION: &'static str = env!("PROXMOX_PKG_VERSION");
const PROXMOX_PKG_RELEASE: &'static str = env!("PROXMOX_PKG_RELEASE");
const PROXMOX_PKG_REPOID: &'static str = env!("PROXMOX_PKG_REPOID");
fn get_version(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(json!({
"version": PROXMOX_PKG_VERSION,
"release": PROXMOX_PKG_RELEASE,
"repoid": PROXMOX_PKG_REPOID
}))
}
pub fn router() -> Router { pub fn router() -> Router {
let route4 = Router::new() let route4 = Router::new()
@ -62,16 +51,17 @@ pub fn router() -> Router {
let nodes = Router::new() let nodes = Router::new()
.match_all("node", nodeinfo); .match_all("node", nodeinfo);
let version = Router::new()
.get(ApiMethod::new(
get_version,
ObjectSchema::new("Proxmox Backup Server API version.")));
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .get(ApiMethod::new(
get_version, |_,_| Ok(json!([
{"subdir": "config"},
{"subdir": "version"},
{"subdir": "nodes"}
])),
ObjectSchema::new("Directory index."))) ObjectSchema::new("Directory index.")))
.subdir("version", version) .subdir("config", config::router())
.subdir("version", version::router())
.subdir("nodes", nodes); .subdir("nodes", nodes);
route route

22
src/api3/config.rs Normal file
View File

@ -0,0 +1,22 @@
use failure::*;
use std::collections::HashMap;
use crate::api::schema::*;
use crate::api::router::*;
use serde_json::{json, Value};
mod datastore;
pub fn router() -> Router {
let route = Router::new()
.get(ApiMethod::new(
|_,_| Ok(json!([
{"subdir": "datastore"}
])),
ObjectSchema::new("Directory index.")))
.subdir("datastore", datastore::router());
route
}

View File

@ -0,0 +1,21 @@
use failure::*;
use std::collections::HashMap;
use crate::api::schema::*;
use crate::api::router::*;
use serde_json::{json, Value};
fn datastore_list(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
println!("This is a test {}", param);
Ok(json!({}))
}
pub fn router() -> Router {
let route = Router::new()
.get(ApiMethod::new(
datastore_list,
ObjectSchema::new("Directory index.")));
route
}

28
src/api3/version.rs Normal file
View File

@ -0,0 +1,28 @@
use failure::*;
use crate::api::schema::*;
use crate::api::router::*;
use serde_json::{json, Value};
const PROXMOX_PKG_VERSION: &'static str = env!("PROXMOX_PKG_VERSION");
const PROXMOX_PKG_RELEASE: &'static str = env!("PROXMOX_PKG_RELEASE");
const PROXMOX_PKG_REPOID: &'static str = env!("PROXMOX_PKG_REPOID");
fn get_version(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(json!({
"version": PROXMOX_PKG_VERSION,
"release": PROXMOX_PKG_RELEASE,
"repoid": PROXMOX_PKG_REPOID
}))
}
pub fn router() -> Router {
let route = Router::new()
.get(ApiMethod::new(
get_version,
ObjectSchema::new("Proxmox Backup Server API version.")));
route
}

View File

@ -1,6 +1,6 @@
use failure::*; use failure::*;
use std::fs::{File, OpenOptions}; use std::fs::{OpenOptions};
use std::io::Read; use std::io::Read;
//use std::sync::Arc; //use std::sync::Arc;
@ -32,23 +32,23 @@ fn init() -> SectionConfig {
config config
} }
const datastore_cfg: &str = "/etc/proxmox-backup/datastore.cfg"; const DATASTORE_CFG_FILENAME: &str = "/etc/proxmox-backup/datastore.cfg";
fn config() -> Result<SectionConfigData, Error> { fn config() -> Result<SectionConfigData, Error> {
let mut file = match OpenOptions::new() let mut file = match OpenOptions::new()
.create(true) .create(true)
.write(true) .write(true)
.open(datastore_cfg) { .open(DATASTORE_CFG_FILENAME) {
Ok(file) => file, Ok(file) => file,
Err(err) => bail!("Unable to open '{}' - {}", Err(err) => bail!("Unable to open '{}' - {}",
datastore_cfg, err), DATASTORE_CFG_FILENAME, err),
}; };
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
CONFIG.parse(datastore_cfg, &contents) CONFIG.parse(DATASTORE_CFG_FILENAME, &contents)
} }

View File

@ -49,4 +49,3 @@ pub mod storage {
pub mod getopts; pub mod getopts;
pub mod api3; pub mod api3;