#!/usr/bin/env ruby
###########################################################################
# AWS Elastic Beanstalk Command Line Client
# Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the “License”). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#    http://aws.amazon.com/apache2.0/
#
# or in the “license” file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#

require File.join(File.dirname(__FILE__), 'setup.rb')
require 'aws/client/commandline'

class EnvironmentResourcesCLI < AWS::Client::CommandLine
  def flatten_resources(resource_list)
    resources = []

    resource_list.each do |resource|
      resources << resource.values
    end
    
    resources.flatten.join(', ')
  end
end

EnvironmentResourcesCLI.new do |client|
  client.service_call = lambda { |params| @elasticbeanstalk.describe_environment_resources(params) }
  client.result_key = 'EnvironmentResources'
  client.empty_response = 'No environments found.'
  
  client.option('-e', '--environment-name NAME', 'Name of the environment') do |env_name|
    client.service_param('EnvironmentName', env_name)
  end

  client.option('-E', '--environment-id ID', 'ID of the environment') do |env_id|
    client.service_param('EnvironmentId', env_id)
  end
  
  client.filter 'AutoScalingGroups'    do |val| client.flatten_resources(val) end
  client.filter 'Instances'            do |val| client.flatten_resources(val) end
  client.filter 'LaunchConfigurations' do |val| client.flatten_resources(val) end
  client.filter 'LoadBalancers'        do |val| client.flatten_resources(val) end
  client.filter 'Triggers'             do |val| client.flatten_resources(val) end
  client.filter 'Queues'               do |val| val.collect { |i| i['URL'] } end

  client.exclude 'Resources'
  client.exclude 'RuntimeSources'
end.run(ARGV)
