Class: Chef::Knife::ClcTemplateList

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

Instance Method Summary collapse

Methods included from ClcBase

included

Instance Method Details

#cloud_templatesObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chef/knife/clc_template_list.rb', line 28

def cloud_templates
  if config[:clc_datacenter]
    connection.list_templates(config[:clc_datacenter])
  elsif config[:clc_all]
    datacenters = connection.list_datacenters

    datacenters.map do |dc|
      connection.list_templates(dc['id'])
    end.flatten
  end
end

#executeObject



23
24
25
26
# File 'lib/chef/knife/clc_template_list.rb', line 23

def execute
  context[:templates] = cloud_templates
  render
end

#fieldsObject



63
64
65
66
67
# File 'lib/chef/knife/clc_template_list.rb', line 63

def fields
  # TODO AS: Displaying shortened list of fields for now
  # %w(name osType description storageSizeGB capabilities apiOnly)
  %w(name storageSizeGB capabilities)
end

#filtersObject



46
47
48
49
50
51
52
# File 'lib/chef/knife/clc_template_list.rb', line 46

def filters
  {
    'storageSizeGB' => ->(size) { (size.zero? ? '-' : "#{size} GB").rjust(7) },
    'apiOnly' => ->(api_flag) { (api_flag ? '+' : '-').center(9) },
    'capabilities' => ->(capabilities) { capabilities.empty? ? '-' : capabilities.join(', ') }
  }
end

#headersObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/clc_template_list.rb', line 69

def headers
  {
    'name' => 'Name',
    'osType' => 'OS Type',
    'description' => 'Description',
    'storageSizeGB' => 'Storage',
    'capabilities' => 'Capabilities',
    'apiOnly' => 'API Only'
  }
end

#parse_and_validate_parametersObject



40
41
42
43
44
# File 'lib/chef/knife/clc_template_list.rb', line 40

def parse_and_validate_parameters
  if config[:clc_datacenter].nil? && !config[:clc_all]
    errors << 'Datacenter ID is required'
  end
end

#renderObject



80
81
82
83
84
85
86
87
88
# File 'lib/chef/knife/clc_template_list.rb', line 80

def render
  ui.info Hirb::Helpers::AutoTable.render(context[:templates],
    :headers => headers,
    :fields => fields,
    :filters => filters,
    :max_fields => width_limits,
    :resize => false,
    :description => false)
end

#width_limitsObject



54
55
56
57
58
59
60
61
# File 'lib/chef/knife/clc_template_list.rb', line 54

def width_limits
  {
    'description' => 0.2,
    'storageSizeGB' => 7,
    'capabilities' => 15,
    'apiOnly' => 9
  }
end