Class: Chef::Knife::ClcGroupList
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::ClcGroupList
- Includes:
- ClcBase
- Defined in:
- lib/chef/knife/clc_group_list.rb
Instance Method Summary collapse
- #execute ⇒ Object
- #fields ⇒ Object
- #filters ⇒ Object
- #headers ⇒ Object
- #parse_and_validate_parameters ⇒ Object
- #render ⇒ Object
- #render_table ⇒ Object
- #render_tree ⇒ Object
- #width_limits ⇒ Object
Methods included from ClcBase
Instance Method Details
#execute ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/chef/knife/clc_group_list.rb', line 32 def execute context[:groups] = connection.list_groups(config[:clc_datacenter]).map do |group| parent_link = group['links'].find { |link| link['rel'] == 'parentGroup' } group['parentId'] = parent_link['id'] if parent_link group end render end |
#fields ⇒ Object
56 57 58 59 60 |
# File 'lib/chef/knife/clc_group_list.rb', line 56 def fields # TODO AS: Displaying shortened list of fields for now # %w(name id parentId description serversCount type status) %w(name id serversCount type) end |
#filters ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/chef/knife/clc_group_list.rb', line 42 def filters { 'serversCount' => ->(count) { count.zero? ? '-' : count }, 'parentId' => ->(id) { id ? id : '-' }, 'description' => ->(description) { description.to_s.empty? ? '-' : description } } end |
#headers ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/chef/knife/clc_group_list.rb', line 62 def headers { 'name' => 'Name', 'id' => 'ID', 'parentId' => 'Parent', 'description' => 'Description', 'serversCount' => 'Servers', 'type' => 'Type', 'status' => 'Status' } end |
#parse_and_validate_parameters ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/chef/knife/clc_group_list.rb', line 22 def parse_and_validate_parameters unless config[:clc_datacenter] errors << 'Datacenter ID is required' end unless %w(tree table).include?(config[:clc_view]) errors << 'View parameter should be either table or a tree' end end |
#render ⇒ Object
74 75 76 77 78 79 |
# File 'lib/chef/knife/clc_group_list.rb', line 74 def render case config[:clc_view] when 'tree' then render_tree when 'table' then render_table end end |
#render_table ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/chef/knife/clc_group_list.rb', line 98 def render_table ui.info Hirb::Helpers::AutoTable.render(context[:groups], :fields => fields, :headers => headers, :filters => filters, :max_fields => width_limits, :resize => false, :description => false) end |
#render_tree ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/chef/knife/clc_group_list.rb', line 81 def render_tree display_value = ->(group) { "#{group['name']} (#{group['id']})" } group_children = ->(parent_group) do context[:groups].select { |group| group['parentId'] == parent_group['id'] } end root = context[:groups].find { |group| group['parentId'].nil? } return unless root ui.info Hirb::Helpers::ParentChildTree.render(root, :type => :directory, :value_method => display_value, :children_method => group_children) end |
#width_limits ⇒ Object
50 51 52 53 54 |
# File 'lib/chef/knife/clc_group_list.rb', line 50 def width_limits { 'description' => 0.2 } end |