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
- #data_sort(resources) ⇒ Object
- #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
Methods included from Helpers
#after_exec_command, #before_exec_command, #create_service_instance, #msg_pair, #pretty_key, #set_default_config, #validate!, #validate_params!
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
#data_sort(resources) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 102 def data_sort(resources) if resources.length > 0 resources[0].respond_to?(:kind) && resources[0].kind == "compute#image" ? resources : resources.sort_by { |x| x.send(@sort_by_field).downcase } else [] end end |
#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.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 59 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]))) unless 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 56 |
# 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] && value =~ filter[:regex] return true end end false end |
#list(resources) ⇒ Object
When @columns_with_info is nil display all
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/chef/knife/cloud/list_resource_command.rb', line 73 def list(resources) if config[:format] == "summary" # 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 records = data_sort(resources) records.each do |resource| next if resource.respond_to?(:deprecated) && resource.deprecated 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 else output(format_for_display(resources)) end 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 return resources." end |