Skip to content

Add an example that lists the VM Images that run Linux without a publisher plan #921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions services/mgmt/compute/examples/list_free_linux_images.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Lists the VM images in specified location that do not have a publisher plan that must be
agreed to before use.

Note, for this example, if any image has a publisher plan, the all of the rest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then

of the images in the offer are skipped.

cargo run --package azure_mgmt_compute --example list_free_linux_images eastus
*/

use azure_identity::AzureCliCredential;
use azure_mgmt_compute::models::os_disk_image::OperatingSystem;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let location = std::env::args().nth(1).expect("please specify location");

let credential = Arc::new(AzureCliCredential {});
let subscription_id = AzureCliCredential::get_subscription()?;
let client = azure_mgmt_compute::ClientBuilder::new(credential)
.build()
.virtual_machine_images_client();

let mut publishers = client
.list_publishers(&location, &subscription_id)
.into_future()
.await?
.into_iter()
.map(|x| x.name)
.collect::<Vec<_>>();
publishers.sort();

for publisher in publishers {
let mut offers = client
.list_offers(&location, &publisher, &subscription_id)
.into_future()
.await?
.into_iter()
.map(|x| x.name)
.collect::<Vec<_>>();
offers.sort();

'offer: for offer in offers {
let mut skus = client
.list_skus(&location, &publisher, &offer, &subscription_id)
.into_future()
.await?
.into_iter()
.map(|x| x.name)
.collect::<Vec<_>>();
skus.sort();

for sku in skus {
let mut versions = client
.list(&location, &publisher, &offer, &sku, &subscription_id)
.into_future()
.await?
.into_iter()
.map(|x| x.name)
.collect::<Vec<_>>();
versions.sort();

for version in versions {
let id = format!("{}:{}:{}:{}", publisher, offer, sku, version);
eprintln!("checking {}", id);

let vm = client
.get(&location, &publisher, &offer, &sku, &version, &subscription_id)
.into_future()
.await?;

if let Some(props) = vm.properties {
if props.plan.is_some() {
continue 'offer;
}

if let Some(os) = props.os_disk_image {
if os.operating_system == OperatingSystem::Linux {
println!("{}", id);
break;
}
}
}
}
}
}
}

Ok(())
}