Class: Chef::Knife::Cloud::ResourceListCommand
- Inherits:
-
Command
- Object
- Chef::Knife
- Command
- Chef::Knife::Cloud::ResourceListCommand
- Defined in:
- lib/chef/knife/cloud/list_resource_command.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Command
Instance Method Summary collapse
- #execute_command ⇒ Object
-
#get_resource_col_val(resource) ⇒ Object
Derived class can override this to add more functionality.
-
#initialize(argv = []) ⇒ ResourceListCommand
constructor
A new instance of ResourceListCommand.
- #is_resource_filtered?(attribute, value) ⇒ Boolean
-
#list(resources) ⇒ Object
When @columns_with_info is nil display all.
- #query_resource ⇒ Object
Methods inherited from Command
#after_exec_command, #before_exec_command, #create_service_instance, #pretty_key, #run, #set_default_config, #validate!, #validate_params!
Methods included from Helpers
#locate_config_value, #msg_pair
Constructor Details
#initialize(argv = []) ⇒ ResourceListCommand
Returns a new instance of ResourceListCommand.
26 27 28 29 30 31 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 26 def initialize(argv=[]) super argv # columns_with_info is array of hash with label, key and attribute extraction callback, ex [{:label => "Label text", :key => 'key', value_callback => callback_method to extract/format the required value}, ...] @columns_with_info = [] @sort_by_field = "id" # default sort by id end |
Instance Method Details
#execute_command ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 33 def execute_command # exec the cmd resources = query_resource # handle the response list(resources) end |
#get_resource_col_val(resource) ⇒ Object
Derived class can override this to add more functionality.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 58 def get_resource_col_val(resource) resource_filtered = false list = [] @columns_with_info.each do |col_info| value = (col_info[:value_callback].nil? ? resource.send(col_info[:key]).to_s : col_info[:value_callback].call(resource.send(col_info[:key]))) if !config[:disable_filter] resource_filtered = true if is_resource_filtered?(col_info[:key], value) end list << value end return list unless resource_filtered end |
#is_resource_filtered?(attribute, value) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 46 def is_resource_filtered?(attribute, value) # resource_filters is array of filters in form {:attribute => attribute-name, :regex => 'filter regex value'} return false if @resource_filters.nil? @resource_filters.each do |filter| if attribute == filter[:attribute] and value =~ filter[:regex] return true end end false end |
#list(resources) ⇒ Object
When @columns_with_info is nil display all
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 72 def list(resources) # display column wise only if @columns_with_info is specified, else as a json for readable display. begin resource_list = @columns_with_info.map { |col_info| ui.color(col_info[:label], :bold) } if @columns_with_info.length > 0 resources.sort_by{|x| x.send(@sort_by_field).downcase }.each do |resource| if @columns_with_info.length > 0 list = get_resource_col_val(resource) resource_list.concat(list) unless list.nil? else puts resource.to_json puts "\n" end end rescue => e ui.fatal("Unknown resource error : #{e.}") raise e end puts ui.list(resource_list, :uneven_columns_across, @columns_with_info.length) if @columns_with_info.length > 0 end |
#query_resource ⇒ Object
41 42 43 44 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 41 def query_resource # specific resource type must override this. raise Chef::Exceptions::Override, "You must override query_resource in #{self.to_s} to return resources." end |