scaleway-rs 0.2.2

A pure Rust scaleway API binding.
Documentation
use scaleway_rs::{
    LocalImageListType, ScalewayApi, ScalewayArchitecture, ScalewayError, SecurityGroupRuleAction,
    SecurityGroupRuleDirection, SecurityGroupRuleProtocol,
};
use std::env;

#[tokio::main]
async fn main() -> Result<(), ScalewayError> {
    let args: Vec<String> = env::args().collect();
    if args.len() != 3 {
        println!("Call program with the following:");
        println!("{} SCW_ACCESS_KEY SCW_SECRET_KEY", args[0]);
        std::process::exit(1);
    }

    let result = do_stuff(&args[1], &args[2]).await;
    match result {
        Ok(_) => {
            println!("Finished sucessfully");
        }
        Err(e) => {
            println!("Error: {:#?}", e);
        }
    }
    Ok(())
}

async fn do_stuff(access_key: &str, secret_key: &str) -> Result<(), ScalewayError> {
    let api = ScalewayApi::new(secret_key);
    let region = "fr-par-1";

    let own_key = api.get_api_key_async(access_key).await?;
    println!("Api key: {:#?}", own_key);

    let user = api.get_user_async(&own_key.user_id.unwrap()).await?;
    println!("User: {:#?}", user);

    let organization_id = &user.organization_id;

    let project = api
        .create_project("test", organization_id)
        .description("test description")
        .run_async()
        .await?;
    println!("Created project: {:#?}", project);

    let projects = api
        .list_projects()
        .organization_id(organization_id)
        .run_async()
        .await?;
    println!("Projects: {:#?}", projects);

    let ssh_keys = api.list_ssh_keys().run_async().await?;
    println!("sshkeys: {:#?}", ssh_keys);

    let ssh_key = api.get_ssh_key(&ssh_keys[0].id).run_async().await?;
    println!("ssh_key: {:#?}", ssh_key);

    let sgl = api.list_security_groups(region).run_async().await?;
    println!("Security groups: {:#?}", sgl);

    let sg = api
        .create_security_group(region, "test", true)
        .inbound_default_policy(scaleway_rs::SecurityGroupPolicy::Drop)
        .outbound_default_policy(scaleway_rs::SecurityGroupPolicy::Accept)
        .project(&project.id)
        .description("description test")
        .run_async()
        .await?;
    println!("Created security group: {:#?}", sg);

    let sgr = api
        .create_security_group_rule(
            region,
            &sg.id,
            "10.0.0.0/8",
            SecurityGroupRuleProtocol::Tcp,
            SecurityGroupRuleDirection::Inbound,
            SecurityGroupRuleAction::Accept,
        )
        .dest_port_from(22)
        .run_async()
        .await?;
    println!("Security group rule: {:#?}", sgr);

    let sgrl = api
        .list_security_group_rules(region, &sg.id)
        .per_page(100)
        .run_async()
        .await?;
    println!("Security group rules: {:#?}", sgrl);

    api.delete_securitygroup_async(region, &sg.id).await?;
    println!("Deleted security group");

    api.delete_project_async(&project.id).await?;
    println!("Deleted project");

    for region in ScalewayApi::az_list() {
        let types = api.get_server_types_async(region).await?;
        println!("SERVERTYPES: {:#?}", types);

        let images = api
            .list_images(region)
            .arch(ScalewayArchitecture::Arm64)
            .run_async()
            .await?;
        println!("IMAGES: {:#?}", images);

        let instances = api
            .list_instances(region)
            .order("creation_date_asc")
            .run_async()
            .await?;
        println!("INSTANCES: {:#?}", instances);

        let availability = api.list_availability_async(region).await?;
        println!("AVAILABILITY: {:#?}", availability);

        let list_marketplace_images = api
            .list_marketplace_instances()
            .include_eol(false)
            .arch(ScalewayArchitecture::Arm64)
            .run_async()
            .await?;
        println!("MARKETPLACE IMAGES: {:#?}", list_marketplace_images);

        let list_image_version = api
            .list_marketplace_instance_versions(&list_marketplace_images[0].id)
            .run_async()
            .await?;
        println!("MARKETPLACE IMAGE VERSION: {:#?}", list_image_version);

        let list_local_images = api
            .list_marketplace_local_images(LocalImageListType::ByImageId(
                list_marketplace_images[0].id.to_string(),
            ))
            .arch(ScalewayArchitecture::Arm64)
            .run_async()
            .await?;
        println!("MARKETPLACE LOCAL IMAGES: {:#?}", list_local_images);
    }
    Ok(())
}