-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathlist_stacks.rb
55 lines (48 loc) · 1.58 KB
/
list_stacks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
require 'aws-sdk-elasticbeanstalk'
require 'logger'
# snippet-start:[eb.Ruby.listStacks]
# Manages listing of AWS Elastic Beanstalk solution stacks
# @param [Aws::ElasticBeanstalk::Client] eb_client
# @param [String] filter - Returns subset of results based on match
# @param [Logger] logger
class StackLister
# Initialize with AWS Elastic Beanstalk client
def initialize(eb_client, filter, logger: Logger.new($stdout))
@eb_client = eb_client
@filter = filter.downcase
@logger = logger
end
# Lists and logs Elastic Beanstalk solution stacks
def list_stacks
stacks = @eb_client.list_available_solution_stacks.solution_stacks
orig_length = stacks.length
filtered_length = 0
stacks.each do |stack|
if @filter.empty? || stack.downcase.include?(@filter)
@logger.info(stack)
filtered_length += 1
end
end
log_summary(filtered_length, orig_length)
rescue Aws::Errors::ServiceError => e
@logger.error("Error listing solution stacks: #{e.message}")
end
private
# Logs summary of listed stacks
def log_summary(filtered_length, orig_length)
if @filter.empty?
@logger.info("Showed #{orig_length} stack(s)")
else
@logger.info("Showed #{filtered_length} stack(s) of #{orig_length}")
end
end
end
# snippet-end:[eb.Ruby.listStacks]
# Example usage:
if $PROGRAM_NAME == __FILE__
eb_client = Aws::ElasticBeanstalk::Client.new
stack_lister = StackLister.new(eb_client, 'java')
stack_lister.list_stacks
end