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.

193 lines
5.9 KiB

use crate::{
responses::{
VMListResponse,
BillingCreditResponse,
LNImageListResponse,
VolumeListResponse,
FloatingIpListResponse,
NetworkListResponse,
ZoneListResponse,
RecordListResponse,
DynListResponse,
SshKeyResponse,
},
types::{
LNError,
LunaNodeRequest,
ApiKeys,
},
};
use lunanode_macros::lunanode_request;
use parse_display_derive::Display;
use ureq::post;
use serde::{
Serialize,
Deserialize,
de::DeserializeOwned,
};
use hmac::Hmac;
use sha2::Sha512;
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
pub enum SshKeySubArgs {
List(SshKeyRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
pub enum DynSubArgs {
List(DynListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
pub enum ZoneSubArgs {
List(ZoneListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum FloatingSubArgs {
/// List all images on my account.
List(FloatingIpListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum ImageSubArgs {
/// List all images on my account.
List(ImageListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum RecordSubArgs {
List(RecordListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum VmSubArgs {
/// List all VMs on my account.
List(VMListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum VolumeSubArgs {
/// List all volumes I'm paying for.
List(VolumeListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum BillingSubArgs {
/// How much money is left in my account.
Credit(BillingCreditRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Subcommand)]
#[serde(untagged)]
pub enum NetworkSubArgs {
/// Networks on your accounts
List(NetworkListRequest),
}
#[derive(Serialize, Deserialize, Debug, clap::Parser)]
#[serde(untagged)]
pub enum Args {
#[clap(subcommand)]
/// See `lunanode image help`
Image(ImageSubArgs),
#[clap(subcommand)]
/// See `lunanode ip help`
Floating(FloatingSubArgs),
#[clap(subcommand)]
/// See `lunanode network help`
Network(NetworkSubArgs),
#[clap(subcommand)]
/// See `lunanode vm help`
Vm(VmSubArgs),
#[clap(subcommand)]
/// See `lunanode billing help`
Billing(BillingSubArgs),
#[clap(subcommand)]
/// See `lunanode volume help`
Volume(VolumeSubArgs),
#[clap(subcommand)]
/// See `lunanode zone help`
Zone(ZoneSubArgs),
#[clap(subcommand)]
/// See `lunanode record help`
Record(RecordSubArgs),
#[clap(subcommand)]
/// See `lunanode dyn help`
Dyn(DynSubArgs),
#[clap(subcommand)]
/// See `lunanode sshkey help`
SshKey(SshKeySubArgs),
}
/// Specifies an internal function which can only be called on LunaNodeRequests whoes response type implements Debug. This could be set in the structure itself, and probably should be.
fn print_req<T: LunaNodeRequest>(req: &T) -> Result<(), LNError>
where
<T as LunaNodeRequest>::Response: std::fmt::Debug
{
let ans = req.make_request()?;
println!("{:#?}", ans);
Ok(())
}
impl Args {
pub fn make_request(&self) -> Result<(), LNError> {
match self {
Self::SshKey(SshKeySubArgs::List(ssk_list)) => print_req(ssk_list)?,
Self::Record(RecordSubArgs::List(rec_list)) => print_req(rec_list)?,
Self::Network(NetworkSubArgs::List(net_list)) => print_req(net_list)?,
Self::Floating(FloatingSubArgs::List(ip_list)) => print_req(ip_list)?,
Self::Vm(VmSubArgs::List(vm_list)) => print_req(vm_list)?,
Self::Volume(VolumeSubArgs::List(vol_list)) => print_req(vol_list)?,
Self::Image(ImageSubArgs::List(image_list)) => print_req(image_list)?,
Self::Billing(BillingSubArgs::Credit(billing_credit)) => print_req(billing_credit)?,
Self::Zone(ZoneSubArgs::List(zone_list)) => print_req(zone_list)?,
Self::Dyn(DynSubArgs::List(dyn_list)) => print_req(dyn_list)?,
}
Ok(())
}
}
/// ImageListRequest is used to create a new request for the /image/list endpoint.
#[lunanode_request(response="LNImageListResponse", endpoint="image/list/")]
pub struct ImageListRequest {}
/// BillingCreditRequest handles the /billing/credits/ endpoint. It will produce a BillingCreditResponse.
#[lunanode_request(response="BillingCreditResponse", endpoint="billing/credit/")]
pub struct BillingCreditRequest {}
/// VMListRequest is used to create a new request for the /vm/list endpoint.
#[lunanode_request(response="VMListResponse", endpoint="vm/list/")]
pub struct VMListRequest {}
/// VolumeListRequest is used to create a new request for the /volume/list endpoint.
#[lunanode_request(response="VolumeListResponse", endpoint="volume/list/")]
pub struct VolumeListRequest {}
/// IpListRequest is used to create a new request for the /ip/list endpoint.
#[lunanode_request(response="FloatingIpListResponse", endpoint="floating/list/")]
pub struct FloatingIpListRequest {}
/// NetworkListRequest is used to create a new request for the /network/list endpoint.
#[lunanode_request(response="NetworkListResponse", endpoint="network/list/")]
pub struct NetworkListRequest {}
/// ZoneListRequest is used to generate a request for the dns2/zone-list/ endpoint.
#[lunanode_request(response="ZoneListResponse", endpoint="dns2/zone-list/")]
pub struct ZoneListRequest {}
/// RecordListRequest is used to get information about a specific DNS zone (usually an entire domain, but could also be a subdomain).
#[lunanode_request(response="RecordListResponse", endpoint="dns2/record-list/")]
pub struct RecordListRequest {
zone_id: i32
}
/// DynListRequest is used to get information about the dynamic DNS records set for all lunanode zones.
#[lunanode_request(response="DynListResponse", endpoint="dns/dyn-list/")]
pub struct DynListRequest {}
#[lunanode_request(response="SshKeyResponse", endpoint="sshkey/list/")]
pub struct SshKeyRequest {}