Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Apr 4, 2024
1 parent 70cb2f0 commit 871d895
Show file tree
Hide file tree
Showing 30 changed files with 1,533 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/target
Cargo.lock

.env
.ignore
examples/playground.rs
21 changes: 17 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
[package]
name = "neolite-sdk"
name = "neolite"
version = "0.1.0"
authors = ["azzamsa <azzam@biznetgio.com>"]
edition = "2021"
license = "MIT"
readme = "README.md"
repository = "https://github.com/BiznetGIO/neolite-sdk"
rust-version = "1.73.0"
description = "Neo Lite SDK"
repository = "https://github.com/BiznetGIO/neolite"
rust-version = "1.77.1"
description = "NEO Lite SDK"

[dependencies]
http = "1.1.0"
log = "0.4.21"
reqwest = { version = "0.12.2", default-features = false, features = ["rustls-tls", "json", "multipart"] }
serde = { version = "1.0.197", features = ["derive"] }
serde-aux = "4.5.0"
serde_json = "1.0.115"
thiserror = "1.0.58"

[dev-dependencies]
anyhow = "1.0.81"
dotenvy = "0.15.7"
env_logger = "0.11.3"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }

[package.metadata.release]
sign-commit = true
Expand Down
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<div align="center">
<h1>neolite</h1>

NEO Lite SDK.

<a href="https://github.com/BiznetGIO/neolite/actions/workflows/ci.yml">
<img src="https://github.com/BiznetGIO/neolite/actions/workflows/ci.yml/badge.svg">
</a>
<a href="https://crates.io/crates/neolite">
<img src="https://img.shields.io/crates/v/neolite.svg">
</a>

</div>

---

The `neolite` SDK makes it easy to work with Biznet Gio's [NEO Lite](https://www.biznetgio.com/product/neo-lite) service. With NEO Lite SDK, developers can effortlessly manage and control their VPS instances for hosting websites and applications.

## Usage

```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let url = "https://api.portal.biznetgio.dev/v1/neolites".parse::<http::Uri>()?;
let token = env::var("TOKEN").context("TOKEN env not found.")?;

let config = Config::new(url, &token);
let client = Client::new(config)?;

let keypair = Lite::new(client).keypair().await?;
let key = keypair.create("gandalf0").await?;
println!("{}", key.name);
Ok(())
}
```

To learn more, see other [examples](/examples).

## Development

```bash
git clone https://github.com/BiznetGIO/neolite
cd neolite

# Run unit tests and integration tests
cargo test
```

## Contributing

To learn more read the [contributing guide](docs/dev/README.md)
4 changes: 2 additions & 2 deletions configs/cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ body = """
### {{ group | striptags | trim | upper_first }}
{% for commit in commits %}
{%- if commit.scope -%}
- **{{ commit.scope }}:** {{ commit.message | upper_first }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/BiznegGio/neolite-sdk/commit/{{ commit.id }}))
- **{{ commit.scope }}:** {{ commit.message | upper_first }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/BiznegGio/neolite/commit/{{ commit.id }}))
{% if commit.breaking -%}
{% raw %} {% endraw %}- **BREAKING!** ⚠️ : {{ commit.breaking_description }}
{% endif -%}
{% if commit.body -%}
{% raw %}\n{% endraw %}{% raw %} {% endraw %}{{ commit.body | indent(width=4) }}{% raw %}\n{% endraw %}
{% endif -%}
{% else -%}
- {{ commit.message | upper_first }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/BiznegGio/neolite-sdk/commit/{{ commit.id }}))
- {{ commit.message | upper_first }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/BiznegGio/neolite/commit/{{ commit.id }}))
{% if commit.breaking -%}
{% raw %} {% endraw %}- **BREAKING!** ⚠️ : {{ commit.breaking_description }}
{% endif -%}
Expand Down
2 changes: 1 addition & 1 deletion configs/typos.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[files]
extend-exclude = ["CHANGELOG.md"]
extend-exclude = ["CHANGELOG.md", "playground.rs"]
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Guide

## Common workflow

- Create NEO Lite Virtual Machine.
- Check available products `product.list()`.
- Select preferred product `product.get(1538)`.
- Select preferred billing cycle `product_resource.get_billing("Monthly")`.
- Check IP availability `ip.is_available()`.
- Select preferred OS `os.get(1001)`.
- Create or Select existing keypair `keypair.create("gandalf0")`.
- Create a virtual Machine `lite.create()`.
4 changes: 4 additions & 0 deletions examples/.example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TOKEN='eyJhbG...'
VM_ID=123
PRODUCT_ID=123
KEYPAIR_ID=123
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Examples

Sorted by dependency order.

1. Keypair
2. Product
3. Virtual Machine (vm)
114 changes: 114 additions & 0 deletions examples/create_snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#![allow(dead_code)]
use std::env;

use anyhow::Context;
use neolite::{client::Client, config::Config, lite::Lite, snapshot::SnapshotOpts};

async fn create(client: Client, vm_id: u32) -> anyhow::Result<()> {
let lite = Lite::new(client);

// (1) Select preferred billing cycle
let product = lite.plan().await?;
let product_resource = product.get_vm(1538).await?;
let billing_resource = product_resource.get_billing("Monthly").await?;
println!(
"::: Billing. label: {}, price: {}",
billing_resource.label, billing_resource.price,
);

// (2) Create a virtual machine snapshot
let snapshot = lite.snapshot().await?;
let opts = SnapshotOpts {
billing: billing_resource,
use_credit_card: false,
promocode: None,
};
let billing_resource = snapshot
.create(
vm_id,
"snapshot-from-sdk".to_string(),
Some("Snapshot from SDK".to_string()),
&opts,
)
.await?;
println!(
"::: Snapshot. account id: {}, order id: {}",
billing_resource.account_id, billing_resource.order_id
);

Ok(())
}

async fn restore_with(client: Client) -> anyhow::Result<()> {
let lite = Lite::new(client);

// (1) Select preferred plan
// let resource = plan.list().await?;
let plan = lite.plan().await?;
let plan_resource = plan.get_vm(1538).await?;
println!(
"::: plan. id: {}, name: {}",
plan_resource.id, plan_resource.name,
);

// (2) Select preferred billing cycle
let billing_resource = plan_resource.get_billing("Monthly").await?;
println!(
"::: Billing. label: {}, price: {}",
billing_resource.label, billing_resource.price,
);

// (3) Create or Select existing keypair
let keypair = lite.keypair().await?;
let keypair_resource = keypair.create("gandalf0").await?;
println!(
"::: Keypair. id: {}, name: {}",
keypair_resource.id, keypair_resource.name,
);

// (4) Create a virtual machine snapshot
let snapshot = lite.snapshot().await?;
let opts = neolite::snapshot::RestoreVirtualMachineOptions {
plan: plan_resource,
keypair: keypair_resource,
billing: billing_resource,
use_credit_card: false,
promocode: None,
};
let snapshot_id = 123;
let billing_resource = snapshot
.restore_with(
snapshot_id,
"thorin-os2".to_string(),
Some("Thorin Virtual Machine".to_string()),
"thethorin".to_string(),
"SpeakFriendAndEnter123".to_string(),
&opts,
)
.await?;
println!(
"::: VM created. account id: {}, order id: {}",
billing_resource.account_id, billing_resource.order_id
);

Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
dotenvy::from_filename("./examples/.env")?;

let url = "https://api.portal.biznetgio.dev/v1/neolites".parse::<http::Uri>()?;
let token = env::var("TOKEN").context("TOKEN env not found.")?;
let id = env::var("VM_ID").context("VM_ID env not found.")?;
let id: u32 = id.parse()?;

let config = Config::new(url, &token);
let client = Client::new(config)?;

create(client, id).await?;
// restore_with(client).await?;

Ok(())
}
87 changes: 87 additions & 0 deletions examples/create_vm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::env;

use anyhow::Context;
use neolite::{client::Client, config::Config, lite::Lite, vm::VirtualMachineOptions};

async fn create(client: Client) -> anyhow::Result<()> {
let lite = Lite::new(client);

// (1) Select preferred plan
// let resource = plan.list().await?;
let plan = lite.plan().await?;
let plan_resource = plan.get_vm(1538).await?;
println!(
"::: plan. id: {}, name: {}",
plan_resource.id, plan_resource.name,
);

// (2) Select preferred billing cycle
let billing_resource = plan_resource.get_billing("Monthly").await?;
println!(
"::: Billing. label: {}, price: {}",
billing_resource.label, billing_resource.price,
);

// (3) Check IP availability
let ip = plan_resource.ip().await?;
let is_ip_available = ip.is_available().await?;
if !is_ip_available {
return Err(anyhow::anyhow!("IP is not available"));
}
println!("::: IP availability: {}", is_ip_available);

// (4) Select preferred OS
let os = plan_resource.os().await?;
// let oses = os.list().await?;
let os_resource = os.get(1001).await?;
println!("::: OS. id: {}, name: {}", os_resource.id, os_resource.name);

// (5) Create or Select existing keypair
let keypair = lite.keypair().await?;
let keypair_resource = keypair.create("gandalf0").await?;
println!(
"::: Keypair. id: {}, name: {}",
keypair_resource.id, keypair_resource.name,
);

// (6) Create a virtual Machine
let vm = lite.vm().await?;
let opts = VirtualMachineOptions {
plan: plan_resource,
os: os_resource,
keypair: keypair_resource,
billing: billing_resource,
use_credit_card: false,
promocode: None,
};
let billing_resource = vm
.create(
"thorin-os2".to_string(),
Some("Thorin Virtual Machine".to_string()),
"thethorin".to_string(),
"SpeakFriendAndEnter123".to_string(),
&opts,
)
.await?;
println!(
"::: NeoLite VM. account id: {}, order id: {}",
billing_resource.account_id, billing_resource.order_id
);

Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
dotenvy::from_filename("./examples/.env")?;

let url = "https://api.portal.biznetgio.dev/v1/neolites".parse::<http::Uri>()?;
let token = env::var("TOKEN").context("TOKEN env not found.")?;
let config = Config::new(url, &token);

let client = Client::new(config)?;
create(client).await?;

Ok(())
}
55 changes: 55 additions & 0 deletions examples/keypair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![allow(dead_code)]
use std::env;

use anyhow::Context;
use neolite::{client::Client, config::Config, keypair::Keypair, lite::Lite};

async fn list(keypair: Keypair) -> anyhow::Result<()> {
let keys = keypair.list().await?;
for key in keys {
println!("{}: {}", key.id, key.name);
}
Ok(())
}

async fn get(keypair: Keypair, id: u32) -> anyhow::Result<()> {
let key = keypair.get(id).await?;
println!("id: {}, name: {}", key.id, key.name);
Ok(())
}

async fn create(keypair: Keypair) -> anyhow::Result<()> {
let key = keypair.create("gandalf0").await?;
println!("{:?}", key);
println!("{}", key.name);
Ok(())
}

async fn delete(keypair: Keypair, id: u32) -> anyhow::Result<()> {
keypair.delete(id).await?;
println!("::: Keypair deleted.");

Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
dotenvy::from_filename("./examples/.env")?;

let url = "https://api.portal.biznetgio.dev/v1/neolites".parse::<http::Uri>()?;
let token = env::var("TOKEN").context("TOKEN env not found.")?;
let id = env::var("KEYPAIR_ID").context("KEYPAIR_ID env not found.")?;
let id: u32 = id.parse()?;

let config = Config::new(url, &token);
let client = Client::new(config)?;
let keypair = Lite::new(client).keypair().await?;

// list(keypair).await?;
// create(keypair).await?;
get(keypair, id).await?;
// delete(keypair, id).await?;

Ok(())
}
Loading

0 comments on commit 871d895

Please sign in to comment.