Skip to content

Commit

Permalink
Merge pull request #2 from partik03/tests
Browse files Browse the repository at this point in the history
Completed AWS package
  • Loading branch information
partik03 authored Jul 10, 2024
2 parents ae486d6 + 6ec1a86 commit 4ebb192
Show file tree
Hide file tree
Showing 27 changed files with 1,099 additions and 522 deletions.
209 changes: 131 additions & 78 deletions rustcloud/src/aws/aws_apis/compute/aws_ec2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![allow(clippy::result_large_err)]

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_ec2::{types::Tag, Client, Error};
use aws_sdk_ec2::{operation::run_instances::RunInstancesError, types::Tag, Client, Error};

// use clap::Parser;

pub async fn create_instance(client: &Client, ami_id: &str) -> Result<(), Error> {
Expand All @@ -12,111 +13,163 @@ pub async fn create_instance(client: &Client, ami_id: &str) -> Result<(), Error>
.min_count(1)
.max_count(1)
.send()
.await?;

if run_instances.instances().is_empty() {
panic!("No instances created.");
}

println!("Created instance.");

let instance_id = run_instances.instances()[0].instance_id().unwrap();
client
.create_tags()
.resources(instance_id)
.tags(
Tag::builder()
.key("Name")
.value("From SDK Examples")
.build(),
)
.send()
.await?;

println!("Created {instance_id} and applied tags.",);

Ok(())
.await;

match run_instances {
Ok(run_instances) => {
if run_instances.instances().is_empty() {
panic!("No instances created.");
let instance_id = run_instances.instances()[0].instance_id().unwrap();
client
.create_tags()
.resources(instance_id)
.tags(
Tag::builder()
.key("Name")
.value("From SDK Examples")
.build(),
)
.send()
.await.unwrap();

println!("Created {instance_id} and applied tags.",);

}
Ok(())
}
Err(err) => {
println!("Error: {:?}", err);
Err(err.into())
}

}

}
pub async fn show_state(client: &Client, ids: Option<Vec<String>>) -> Result<(), Error> {
let resp = client
.describe_instances()
.set_instance_ids(ids)
.send()
.await?;

for reservation in resp.reservations() {
for instance in reservation.instances() {
println!("Instance ID: {}", instance.instance_id().unwrap());
println!(
"State: {:?}",
instance.state().unwrap().name().unwrap()
);
println!();
.await;

match resp {
Ok(result) => {
for reservation in result.reservations() {
for instance in reservation.instances() {
println!("Instance ID: {}", instance.instance_id().unwrap());
println!(
"State: {:?}",
instance.state().unwrap().name().unwrap()
);
println!();
}
}
Ok(())
},
Err(e) => {
println!("Error: {:?}", e);
Err(e.into())
}
}

Ok(())
}


pub async fn show_all_events(client: &Client) -> Result<(), Error> {
let resp = client.describe_regions().send().await.unwrap();

for region in resp.regions.unwrap_or_default() {
let reg: &'static str = Box::leak(Box::from(region.region_name().unwrap()));
let region_provider = RegionProviderChain::default_provider().or_else(reg);
let config = aws_config::from_env().region(region_provider).load().await;
let new_client = Client::new(&config);

let resp = new_client.describe_instance_status().send().await;

println!("Instances in region {}:", reg);
println!();

for status in resp.unwrap().instance_statuses() {
println!(
" Events scheduled for instance ID: {}",
status.instance_id().unwrap_or_default()
);
for event in status.events() {
println!(" Event ID: {}", event.instance_event_id().unwrap());
println!(" Description: {}", event.description().unwrap());
println!(" Event code: {}", event.code().unwrap().as_ref());
let resp = client.describe_regions().send().await;
match resp {
Ok(result) => {
for region in result.regions.unwrap_or_default() {
let reg: &'static str = Box::leak(Box::from(region.region_name().unwrap()));
let region_provider = RegionProviderChain::default_provider().or_else(reg);
let config = aws_config::from_env().region(region_provider).load().await;
let new_client = Client::new(&config);

let resp = new_client.describe_instance_status().send().await;

println!("Instances in region {}:", reg);
println!();

for status in resp.unwrap().instance_statuses() {
println!(
" Events scheduled for instance ID: {}",
status.instance_id().unwrap_or_default()
);
for event in status.events() {
println!(" Event ID: {}", event.instance_event_id().unwrap());
println!(" Description: {}", event.description().unwrap());
println!(" Event code: {}", event.code().unwrap().as_ref());
println!();
}
}
}

Ok(())
}
Err(err) => {
println!("Error: {:?}", err);
Err(err.into())
}
}

Ok(())
// let result = resp?;


}

pub async fn enable_monitoring(client: &Client, id: &str) -> Result<(), Error> {
client.monitor_instances().instance_ids(id).send().await?;

println!("Enabled monitoring");
let res = client.monitor_instances().instance_ids(id).send().await;
match res {
Ok(result) =>{
println!("Enabled monitoring: {:?}", result);
Ok(())
}
Err(e) => {
println!("Monitoring failed: {:?}", e);
Err(e.into())
}
}

Ok(())
}

pub async fn reboot_instance(client: &Client, id: &str) -> Result<(), Error> {
client.reboot_instances().instance_ids(id).send().await?;

println!("Rebooted instance.");
Ok(())
let res = client.reboot_instances().instance_ids(id).send().await;
match res {
Ok(result) =>{
println!("Enabled monitoring: {:?}", result);
Ok(())
}
Err(e) => {
println!("Error rebooting instance: {:?}", e);
Err(e.into())
}
}
}

pub async fn start_instance(client: &Client, id: &str) -> Result<(), Error> {
client.start_instances().instance_ids(id).send().await?;

println!("Started instance.");

Ok(())
let res = client.start_instances().instance_ids(id).send().await;
match res {
Ok(result) =>{
println!("Enabled monitoring: {:?}", result);
Ok(())
}
Err(e) => {
println!("Error rebooting instance: {:?}", e);
Err(e.into())
}
}
}

pub async fn stop_instance(client: &Client, id: &str) -> Result<(), Error> {
client.stop_instances().instance_ids(id).send().await?;

println!("Stopped instance.");
let res = client.stop_instances().instance_ids(id).send().await;
match res {
Ok(result) =>{
println!("Enabled monitoring: {:?}", result);
Ok(())
}
Err(e) => {
println!("Error rebooting instance: {:?}", e);
Err(e.into())
}
}
}

Ok(())
}
99 changes: 66 additions & 33 deletions rustcloud/src/aws/aws_apis/compute/aws_ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ use aws_sdk_ecs::types::{ClusterConfiguration, ClusterField, ClusterSetting};
use aws_sdk_ecs::{types::Tag, Client, Error};


pub async fn create_cluster(client: &Client, name: &String, tags: Option<Vec<Tag>>, settings : ClusterSetting,configuration : ClusterConfiguration, capacity_providers: Option<Vec<String>>) -> Result<(), Error> {
let cluster = client.create_cluster().cluster_name(name).set_tags(tags).settings(settings).configuration(configuration).set_capacity_providers(capacity_providers).send().await?;
println!("cluster created: {:?}", cluster);

Ok(())
pub async fn create_cluster(client: &Client, name: &String, tags: Option<Vec<Tag>>, settings : Option<Vec<ClusterSetting>>,configuration : ClusterConfiguration, capacity_providers: Option<Vec<String>>) -> Result<(), Error> {
println!("{name}");
let res = client.create_cluster().cluster_name(name).set_tags(tags).set_settings(settings).configuration(configuration).set_capacity_providers(capacity_providers).send().await;
match res {
Ok(result) =>{
println!("Created: {:?}", result);
Ok(())
}
Err(e) => {
println!("Error: {:?}", e);
Err(e.into())
}
}
}


Expand All @@ -17,45 +25,70 @@ pub async fn delete_cluster(
client: &Client,
name: &String,
) -> Result<(), Error> {
let cluster_deleted = client.delete_cluster().cluster(name).send().await?;
println!("cluster deleted: {:?}", cluster_deleted);

Ok(())
let res = client.delete_cluster().cluster(name).send().await;
match res {
Ok(result) =>{
println!("Deleted : {:?}", result);
Ok(())
}
Err(e) => {
println!("Error : {:?}", e);
Err(e.into())
}
}
}


pub async fn describe_cluster(client: &Client, clusters: Option<Vec<String>>, include:Option<Vec<ClusterField>>) -> Result<(), Error> {

let res = client.describe_clusters().set_clusters(clusters).set_include(include).send().await?;

let clusters = res.clusters();
println!("Found {} clusters:", clusters.len());
for cluster in clusters {
println!(" {}", cluster.cluster_name().unwrap());
}
Ok(())
let res = client.describe_clusters().set_clusters(clusters).set_include(include).send().await;
match res {
Ok(result) =>{
let clusters = result.clusters();
println!("Found {} clusters:", clusters.len());
for cluster in clusters {
println!(" {}", cluster.cluster_name().unwrap());
}
Ok(())
}
Err(e) => {
println!("Error : {:?}", e);
Err(e.into())
}
}
}



pub async fn show_clusters(client: &aws_sdk_ecs::Client, max_results: Option<i32>) -> Result<(), Error> {
let resp = client.list_clusters().set_max_results(max_results).send().await?;

let cluster_arns = resp.cluster_arns();
println!("Found {} clusters:", cluster_arns.len());

let clusters = client
.describe_clusters()
.set_clusters(Some(cluster_arns.into()))
.send()
.await?;

for cluster in clusters.clusters() {
println!(" ARN: {}", cluster.cluster_arn().unwrap());
println!(" Name: {}", cluster.cluster_name().unwrap());
let res = client.list_clusters().set_max_results(max_results).send().await;
match res {
Ok(result) =>{
let cluster_arns = result.cluster_arns();
println!("Found {} clusters:", cluster_arns.len());

let clusters = client
.describe_clusters()
.set_clusters(Some(cluster_arns.into()))
.send()
.await;

if clusters.is_err() {
println!("Error: {:?}", clusters.err());
return Ok(());
}
let describe_cluster_result = clusters?;
for cluster in describe_cluster_result.clusters() {
println!(" ARN: {}", cluster.cluster_arn().unwrap());
println!(" Name: {}", cluster.cluster_name().unwrap());
}
Ok(())
}
Err(e) => {
println!("Error: {:?}", e);
Err(e.into())
}
}

Ok(())
}


Expand Down
Loading

0 comments on commit 4ebb192

Please sign in to comment.