use scaleway_rs::{
DnsZoneOrderBy, InstanceOrderBy, 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 regions = ScalewayApi::az_list();
println!("Regions: {:?}", regions);
let region = regions.first().unwrap();
println!("Choosen region: {}", region);
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_id = own_key.default_project_id.unwrap();
let zones = api
.list_dns_zones()
.project(&project_id)
.order_by(DnsZoneOrderBy::DomainAsc)
.run_async()
.await?;
println!("Zones: {:#?}", zones);
for zone in zones.iter().rev() {
println!("Zone: {}", zone.domain);
if !zone.domain.eq("privatedns") {
let records = api.list_dns_records_async(&zone.domain).await?;
println!("records: {:#?}", records);
}
}
let volume_types = api.list_volume_types_async(region).await?;
println!("Volume Types: {:#?}", volume_types);
let project = api
.create_project("test", organization_id)
.description("test description")
.run_async()
.await?;
println!("Created project: {:#?}", project);
let volume_type = volume_types.first().unwrap();
let volume = api
.create_volume(
region,
"testvolume",
volume_type.constraints.min,
&volume_type.id,
)
.project(&project.id)
.tags(vec!["test".to_string()])
.run_async()
.await?;
println!("Volume: {:#?}", volume);
let volumes = api
.list_volumes(region)
.project(&project.id)
.run_async()
.await?;
println!("Volumes: {:#?}", volumes);
let updated_volume = api
.update_volume(region, &volume.id)
.tags(vec!["abc".to_string()])
.run_async()
.await?;
println!("Updated volume: {:#?}", updated_volume);
api.delete_volume_async(region, &volume.id).await?;
println!("Deleted volume: {}", volume.id);
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");
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_by(InstanceOrderBy::CreationDateAsc)
.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);
let list_categories = api.list_marketplace_categories_async().await?;
println!("Categories: {:#?}", list_categories);
Ok(())
}