Just some context for the code:
// Open aws_instances
file = File::open("aws_instances").expect("File can't be opened.");
// Create aws_tags file
let mut tag_file = File::create("aws_tags.sh");
// Read aws_instances
let mut instances = String::new();
file.read_to_string(&mut instances).expect("File can't be read");
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct InstancesList {
reservations: Vec<Reservation>,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Reservation {
instances: Vec<Instance>,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Instance {
instance_lifecycle: String,
placement: Placement,
r#type: String,
tags: Vec<InstanceTag>,
instance_id: String,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct InstanceTag {
key: String,
value: String,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Placement {
availability_zone: String,
}
// Parse aws_instances as JSON
let instances_list: InstancesList =
serde_json::from_str(&instances).expect("Json was not well formatted.");
// Create a new HashMap
let mut ec2 = HashMap::new();
for reservation in &instances_list.reservations{
for instance in &reservation.instances{
...etc
}
}
I was wondering how to check if instance contains a key. In ruby it would be:
if instance.has_key?("Tags")
For rust I tried adding to the serde json struct, but it says "method not found in &main::Instance
" :
instance: HashMap<String, String> //so then I could possibly write:
if instance.contains_key("Tags"){
Additionally, I have an error "expected struct Vec
, found tuple"
let msg = ("{}", name);
ec2.insert(key, "{}", name);
let msg = ("{}:*{}*", name, eol);
ec2.insert(key, msg);
The ruby line for the corresponding code is:
ec2[key] << "#{name}"
ec2[key] << "#{name}:*#{eol}*"
Finally, I am trying to create a file and write in it:
let mut tag_file = File::create("aws_tags.sh");
//the following line is in the for loop listed in the context above:
let data = ("aws ec2 create-tags --region us-west-2 --resources {} --tags Key=opsworks:layer,Value='#{layer_name}'\n", &instance.instance_id);
aws_tags.write_all(&data);
I get the error that it isn't in the scope, which I understand since I'm trying to write it inside the for loops. Is there a way I can do this?
Thanks for your time and help.