# This recipe aims to help discovery process in EC2; it runs AWS commandline
# to tag the current box on EC2 so that it can be discovered by others
#
# AWS commandline tool is provided by artifact-deployer
#
aws_bin = node['ec2']['aws_bin']
box_tags = node['ec2']['box_tags']

# TODO: use aws cookbook
# aws_resource_tag node['ec2']['instance_id'] do
#   aws_access_key aws['aws_access_key_id']
#   aws_secret_access_key aws['aws_secret_access_key']
#   tags({"Name" => "www.example.com app server",
#         "Environment" => node.chef_environment})
#   action :update
# end

# Set tags on the box
if box_tags
  box_tags_str = '--tags '
  instance_id_command = '$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-id)'
  box_tags.each do |tag_name, tag_value|
    box_tags_str += "Key=#{tag_name},Value=#{tag_value} "
  end
  execute 'set-ec2-tags' do
    command "#{aws_bin} ec2 create-tags --resources #{instance_id_command} #{box_tags_str}"
  end
end
