Module: Sfn::CommandModule::Base::InstanceMethods
- Defined in:
- lib/sfn/command_module/base.rb
Overview
Instance methods for cloudformation command classes
Instance Method Summary collapse
-
#_debug(e, *args) ⇒ Object
Write exception information if debug is enabled.
-
#allowed_attributes ⇒ Array<String>
Attributes to display.
-
#as_title(string) ⇒ String
Format snake cased key to title.
-
#attribute_allowed?(attr) ⇒ TrueClass, FalseClass
Check if attribute is allowed for display.
-
#config ⇒ Smash
Override config method to memoize the result allowing for modifications to the configuration during runtime.
- #custom_stack_types ⇒ Array<String>
-
#default_attributes ⇒ Array<String>
Default attributes to display.
-
#get_things(stack = nil, message = nil) { ... } ⇒ Object
Wrapper for information retrieval.
-
#name_args ⇒ Array<String>
Simple compat proxy method.
-
#name_required! ⇒ NilClass
Force error exception when no name is provided.
-
#poll_stack(name) ⇒ Object
Poll events on stack.
-
#provider_for(location = nil) ⇒ Sfn::Provider
(also: #provider)
Build provider connection for given location.
-
#stack(name = nil) ⇒ Miasma::Models::Orchestration::Stack
Get stack.
- #valid_stack_types ⇒ Array<String>
Instance Method Details
#_debug(e, *args) ⇒ Object
Write exception information if debug is enabled
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sfn/command_module/base.rb', line 73 def _debug(e, *args) if(config[:verbose] || config[:debug]) ui.fatal "Exception information: #{e.class}: #{e.}" if(ENV['DEBUG'] || config[:debug]) puts "#{e.backtrace.join("\n")}\n" if(e.is_a?(Miasma::Error::ApiError)) ui.fatal "Response body: #{e.response.body.to_s.inspect}" end end args.each do |string| ui.fatal string end end end |
#allowed_attributes ⇒ Array<String>
Returns attributes to display.
106 107 108 |
# File 'lib/sfn/command_module/base.rb', line 106 def allowed_attributes opts.fetch(:attributes, config.fetch(:attributes, default_attributes)) end |
#as_title(string) ⇒ String
Format snake cased key to title
92 93 94 |
# File 'lib/sfn/command_module/base.rb', line 92 def as_title(string) string.to_s.split('_').map(&:capitalize).join(' ') end |
#attribute_allowed?(attr) ⇒ TrueClass, FalseClass
Check if attribute is allowed for display
119 120 121 |
# File 'lib/sfn/command_module/base.rb', line 119 def attribute_allowed?(attr) opts.fetch(:all_attributes, config[:all_attributes], allowed_attributes.include?(attr)) end |
#config ⇒ Smash
callback requires are also loaded here
Override config method to memoize the result allowing for modifications to the configuration during runtime
162 163 164 165 166 167 168 169 170 |
# File 'lib/sfn/command_module/base.rb', line 162 def config memoize(:config) do result = super result.fetch(:callbacks, :require, []).each do |c_loader| require c_loader end result end end |
#custom_stack_types ⇒ Array<String>
17 18 19 |
# File 'lib/sfn/command_module/base.rb', line 17 def custom_stack_types [config.fetch(:stack_types, [])].flatten.compact end |
#default_attributes ⇒ Array<String>
Returns default attributes to display.
111 112 113 |
# File 'lib/sfn/command_module/base.rb', line 111 def default_attributes %w(timestamp stack_name id) end |
#get_things(stack = nil, message = nil) { ... } ⇒ Object
Wrapper for information retrieval. Provides consistent error message for failures
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sfn/command_module/base.rb', line 139 def get_things(stack=nil, =nil) begin yield rescue => e ui.fatal "#{ || 'Failed to retrieve information'}#{" for requested stack: #{stack}" if stack}" ui.fatal "Reason: #{e}" _debug(e) exit 1 end end |
#name_args ⇒ Array<String>
Simple compat proxy method
153 154 155 |
# File 'lib/sfn/command_module/base.rb', line 153 def name_args arguments end |
#name_required! ⇒ NilClass
Force error exception when no name is provided
176 177 178 179 180 181 |
# File 'lib/sfn/command_module/base.rb', line 176 def name_required! if(name_args.empty?) ui.error 'Name argument must be provided!' raise ArgumentError.new 'Missing required name argument' end end |
#poll_stack(name) ⇒ Object
Poll events on stack
126 127 128 129 130 |
# File 'lib/sfn/command_module/base.rb', line 126 def poll_stack(name) provider.connection.stacks.reload retry_attempts = 0 events = Sfn::Command::Events.new({:poll => true}, [name]).execute! end |
#provider_for(location = nil) ⇒ Sfn::Provider Also known as: provider
Build provider connection for given location
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 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sfn/command_module/base.rb', line 25 def provider_for(location=nil) key = ['provider', location].compact.map(&:to_s).join('_') if(location) credentials = config.get(:locations, location) unless(credentials) raise ArgumentError.new "Failed to locate provider credentials for location `#{location}`!" end else credentials = config[:credentials] end begin memoize(key) do result = Sfn::Provider.new( :miasma => credentials, :async => false, :fetch => false ) result.connection.data[:stack_types] = ( [ (result.connection.class.const_get(:RESOURCE_MAPPING).detect do |klass, info| info[:api] == :orchestration end || []).first ] + custom_stack_types ).compact.uniq retry_config = config.fetch(:retry, config.fetch(:retries, {}) ) result.connection.data[:retry_ui] = ui result.connection.data[:location] = location.to_s result.connection.data[:locations] = config.fetch(:locations, {}) result.connection.data[:retry_type] = retry_config.fetch(:type, :exponential) result.connection.data[:retry_interval] = retry_config.fetch(:interval, 5) result.connection.data[:retry_max] = retry_config.fetch(:max_attempts, 20) result end rescue => e ui.error 'Failed to create remote API connection. Please validate configuration!' ui.error "Connection failure reason - #{e.class} - #{e}" raise end end |
#stack(name = nil) ⇒ Miasma::Models::Orchestration::Stack
Get stack
100 101 102 103 |
# File 'lib/sfn/command_module/base.rb', line 100 def stack(name=nil) name = name_args.first unless name provider.stacks.get(name) end |
#valid_stack_types ⇒ Array<String>
12 13 14 |
# File 'lib/sfn/command_module/base.rb', line 12 def valid_stack_types provider.connection.data[:stack_types] end |