2019-12-03 13:11:46 +03:00
//! This should test the usage of "external" schemas. If a property is declared with a path instead
//! of an object, we expect the path to lead to a schema.
use proxmox_api_macro ::api ;
2021-10-07 10:36:06 +03:00
use proxmox_router ::RpcEnvironment ;
use proxmox_schema as schema ;
2019-12-03 13:11:46 +03:00
2020-04-17 19:34:10 +03:00
use anyhow ::Error ;
2019-12-04 16:24:32 +03:00
use serde_json ::{ json , Value } ;
2019-12-03 13:11:46 +03:00
pub const NAME_SCHEMA : schema ::Schema = schema ::StringSchema ::new ( " Archive name. " )
//.format(&FILENAME_FORMAT)
. schema ( ) ;
#[ api(
input : {
properties : {
2019-12-03 15:34:37 +03:00
" archive-name " : {
2019-12-03 13:11:46 +03:00
schema : NAME_SCHEMA ,
}
}
}
) ]
/// Get an archive.
2019-12-04 16:27:57 +03:00
pub fn get_archive ( archive_name : String ) {
2019-12-03 15:34:37 +03:00
let _ = archive_name ;
2019-12-03 13:11:46 +03:00
}
2019-12-04 16:24:32 +03:00
2020-01-08 12:41:29 +03:00
#[ test ]
fn get_archive_schema_check ( ) {
2021-10-07 10:36:06 +03:00
const TEST_METHOD : ::proxmox_router ::ApiMethod = ::proxmox_router ::ApiMethod ::new (
& ::proxmox_router ::ApiHandler ::Sync ( & api_function_get_archive ) ,
& ::proxmox_schema ::ObjectSchema ::new (
2020-01-08 12:41:29 +03:00
" Get an archive. " ,
& [ ( " archive-name " , false , & NAME_SCHEMA ) ] ,
) ,
)
. protected ( false ) ;
assert_eq! ( TEST_METHOD , API_METHOD_GET_ARCHIVE ) ;
}
2019-12-04 16:24:32 +03:00
#[ api(
input : {
properties : {
" archive-name " : {
schema : NAME_SCHEMA ,
}
}
}
) ]
/// Get an archive.
pub fn get_archive_2 ( param : Value , rpcenv : & mut dyn RpcEnvironment ) -> Result < Value , Error > {
let _ = param ;
let _ = rpcenv ;
Ok ( json! ( " test " ) )
}
2019-12-16 14:11:52 +03:00
2020-01-08 12:41:29 +03:00
#[ test ]
fn get_archive_2_schema_check ( ) {
2021-10-07 10:36:06 +03:00
const TEST_METHOD : ::proxmox_router ::ApiMethod = ::proxmox_router ::ApiMethod ::new (
& ::proxmox_router ::ApiHandler ::Sync ( & api_function_get_archive_2 ) ,
& ::proxmox_schema ::ObjectSchema ::new (
2020-01-08 12:41:29 +03:00
" Get an archive. " ,
& [ ( " archive-name " , false , & NAME_SCHEMA ) ] ,
) ,
)
. protected ( false ) ;
assert_eq! ( TEST_METHOD , API_METHOD_GET_ARCHIVE_2 ) ;
}
2019-12-16 14:11:52 +03:00
#[ api(
input : {
properties : {
" data " : {
description : " The data " ,
type : Array ,
items : {
schema : NAME_SCHEMA ,
}
}
}
}
) ]
/// Get data.
pub fn get_data ( param : Value ) -> Result < ( ) , Error > {
let _ = param ;
Ok ( ( ) )
}
2020-01-08 12:41:29 +03:00
#[ test ]
fn get_data_schema_test ( ) {
2021-10-07 10:36:06 +03:00
const TEST_METHOD : ::proxmox_router ::ApiMethod = ::proxmox_router ::ApiMethod ::new (
& ::proxmox_router ::ApiHandler ::Sync ( & api_function_get_data ) ,
& ::proxmox_schema ::ObjectSchema ::new (
2020-01-08 12:41:29 +03:00
" Get data. " ,
& [ (
" data " ,
false ,
2021-10-07 10:36:06 +03:00
& ::proxmox_schema ::ArraySchema ::new ( " The data " , & NAME_SCHEMA ) . schema ( ) ,
2020-01-08 12:41:29 +03:00
) ] ,
) ,
)
. protected ( false ) ;
assert_eq! ( TEST_METHOD , API_METHOD_GET_DATA ) ;
}
2020-07-10 12:03:51 +03:00
#[ api(
returns : {
type : String ,
}
) ]
/// Get some text.
///
/// Returns: some text.
pub fn get_some_text ( ) -> Result < String , Error > {
Ok ( " Hello " . to_string ( ) )
}
#[ api(
returns : {
properties : {
" text " : {
2020-12-18 14:25:51 +03:00
schema : * API_METHOD_GET_SOME_TEXT . returns . schema
2020-07-10 12:03:51 +03:00
} ,
} ,
} ,
) ]
/// Get some text data.
///
/// Returns: data withsome text.
pub fn get_some_text_data ( ) -> Result < Value , Error > {
Ok ( json! ( { " text " : get_some_text ( ) ? } ) )
}