Class: Chef::Knife::ClcGroupList

Inherits:
Chef::Knife show all
Includes:
ClcBase
Defined in:
lib/chef/knife/clc_group_list.rb

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#executeObject



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

#fieldsObject



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

#filtersObject



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

#headersObject



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_parametersObject



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

#renderObject



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_tableObject



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_treeObject



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_limitsObject



50
51
52
53
54
# File 'lib/chef/knife/clc_group_list.rb', line 50

def width_limits
  {
    'description' => 0.2
  }
end