You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.8 KiB
155 lines
3.8 KiB
2 years ago
|
use crate::{
|
||
|
responses::{
|
||
|
VMListResponse,
|
||
|
BillingCreditResponse,
|
||
|
LNImageListResponse,
|
||
|
},
|
||
|
types::{
|
||
|
LNError,
|
||
|
LNErrorResponse,
|
||
|
LunaNodeRequest,
|
||
|
LunaNodeResponse,
|
||
|
Requestable,
|
||
|
ApiKeys,
|
||
|
},
|
||
|
};
|
||
|
use ureq::post;
|
||
|
use serde::{
|
||
|
Serialize,
|
||
|
Deserialize,
|
||
|
de::DeserializeOwned,
|
||
|
};
|
||
|
use hmac::{Hmac, Mac};
|
||
|
use sha2::Sha512;
|
||
|
|
||
|
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum ImageSubArgs {
|
||
|
List(ImageListRequest),
|
||
|
}
|
||
|
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum VmSubArgs {
|
||
|
List(VMListRequest),
|
||
|
}
|
||
|
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum BillingSubArgs {
|
||
|
Credit(BillingCreditRequest),
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize, Deserialize, Debug, clap::Parser)]
|
||
|
#[serde(untagged)]
|
||
|
pub enum Args {
|
||
|
#[clap(subcommand)]
|
||
|
Image(ImageSubArgs),
|
||
|
#[clap(subcommand)]
|
||
|
Vm(VmSubArgs),
|
||
|
#[clap(subcommand)]
|
||
|
Billing(BillingSubArgs),
|
||
|
}
|
||
|
impl ToString for Args {
|
||
|
fn to_string(&self) -> String {
|
||
|
"Not implemented TODO!".to_string()
|
||
|
}
|
||
|
}
|
||
|
impl Args {
|
||
|
pub fn make_request(&self) -> Result<(), LNError> {
|
||
|
match self {
|
||
|
Self::Vm(VmSubArgs::List(vm_list)) => {
|
||
|
let list = vm_list.make_request()?;
|
||
|
println!("{:#?}", list);
|
||
|
},
|
||
|
Self::Image(ImageSubArgs::List(image_list)) => {
|
||
|
let list = image_list.make_request()?;
|
||
|
println!("{:#?}", list);
|
||
|
},
|
||
|
Self::Billing(BillingSubArgs::Credit(billing_credit)) => {
|
||
|
let credit =billing_credit.make_request()?;
|
||
|
println!("{:#?}", credit);
|
||
|
},
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|
||
|
fn get_info(&self) -> (&str, ApiKeys) {
|
||
|
match self {
|
||
|
Self::Vm(VmSubArgs::List(vm_list)) => ("vm/list/", vm_list.keys.clone()),
|
||
|
Self::Image(ImageSubArgs::List(image_list)) => ("image/list/", image_list.keys.clone()),
|
||
|
Self::Billing(BillingSubArgs::Credit(credit)) => ("billing/credit/", credit.keys.clone()),
|
||
|
}
|
||
|
}
|
||
|
fn get_keys(&self) -> ApiKeys {
|
||
|
self.get_info().1
|
||
|
}
|
||
|
fn url_endpoint(&self) -> &str {
|
||
|
self.get_info().0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq, clap::Args)]
|
||
|
/// ImageListRequest is used to create a new request for the /vm/list endpoint.
|
||
|
pub struct ImageListRequest {
|
||
|
#[serde(flatten)]
|
||
|
#[clap(flatten)]
|
||
|
pub keys: ApiKeys,
|
||
|
}
|
||
|
impl ToString for ImageListRequest {
|
||
|
fn to_string(&self) -> String {
|
||
|
"N/A".to_string()
|
||
|
}
|
||
|
}
|
||
|
impl Requestable for ImageListRequest {}
|
||
|
impl LunaNodeRequest for ImageListRequest {
|
||
|
type response = LNImageListResponse;
|
||
|
fn url_endpoint(&self) -> String {
|
||
|
"image/list/".to_string()
|
||
|
}
|
||
|
fn get_keys(&self) -> ApiKeys {
|
||
|
self.keys.clone()
|
||
|
}
|
||
|
}
|
||
|
#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq, clap::Args)]
|
||
|
/// BillingCreditRequest handles the /billing/credits/ endpoint. It will produce a BillingCreditResponse.
|
||
|
pub struct BillingCreditRequest {
|
||
|
#[serde(flatten)]
|
||
|
#[clap(flatten)]
|
||
|
pub keys: ApiKeys,
|
||
|
}
|
||
|
impl ToString for BillingCreditRequest {
|
||
|
fn to_string(&self) -> String {
|
||
|
"N/A".to_string()
|
||
|
}
|
||
|
}
|
||
|
impl Requestable for BillingCreditRequest {}
|
||
|
impl LunaNodeRequest for BillingCreditRequest {
|
||
|
type response = BillingCreditResponse;
|
||
|
fn get_keys(&self) -> ApiKeys {
|
||
|
self.keys.clone()
|
||
|
}
|
||
|
fn url_endpoint(&self) -> String {
|
||
|
"vm/list/".to_string()
|
||
|
}
|
||
|
}
|
||
|
#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, Eq, clap::Args)]
|
||
|
/// VMListRequest is used to create a new request for the /vm/list endpoint.
|
||
|
pub struct VMListRequest {
|
||
|
#[serde(flatten)]
|
||
|
#[clap(flatten)]
|
||
|
pub keys: ApiKeys,
|
||
|
}
|
||
|
impl ToString for VMListRequest {
|
||
|
fn to_string(&self) -> String {
|
||
|
"N/A".to_string()
|
||
|
}
|
||
|
}
|
||
|
impl Requestable for VMListRequest {}
|
||
|
impl LunaNodeRequest for VMListRequest {
|
||
|
type response = VMListResponse;
|
||
|
fn get_keys(&self) -> ApiKeys {
|
||
|
self.keys.clone()
|
||
|
}
|
||
|
fn url_endpoint(&self) -> String {
|
||
|
"vm/list/".to_string()
|
||
|
}
|
||
|
}
|