Class: Chef::Knife::CloudstackTemplateList

Inherits:
Chef::Knife
  • Object
show all
Includes:
CloudstackBase
Defined in:
lib/chef/knife/cloudstack_template_list.rb

Instance Method Summary collapse

Methods included from CloudstackBase

#connection, included, #locate_config_value, #msg_pair, #validate!

Instance Method Details



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/knife/cloudstack_template_list.rb', line 55

def print_templates(template_list,templates,options={})
  temp = templates

  if templateid = options[:templateid]
    temp.reject!{|t| t['id'] != templateid}
  end
  if zoneid = options[:zoneid]
    temp.reject!{|t| t['zoneid'] != zoneid}
  end
  if zone = options[:zone]
    temp.reject!{|t| t['zonename'] != zone}
  end
  if hypervisor = options[:hypervisor]
    temp.reject!{|t| t['hypervisor'] != hypervisor}
  end

  # Sorting to group by zone ID first, then ID

  sort1 = temp.sort_by { |hsh| hsh["id"] }
  sorted = sort1.sort_by { |hsh| hsh["zoneid"] }

  sorted.each do |template|
    template_list << template['id'].to_s
    template['hypervisor'] = ' ' if template['hypervisor'].nil?
    template_list << template['hypervisor']

    template_size = template['size'].to_i
    template_size = template_size / 1024 / 1024 / 1024
    template_list << template_size.to_s

    template_list << template['zonename']
    template_list << template['zoneid'].to_s
    template_list << template['name']
  end
end

#runObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/knife/cloudstack_template_list.rb', line 91

def run
  validate!

  template_list = [
    ui.color('ID', :bold),
    ui.color('Hypervisor', :bold),
    ui.color('Size (in GB)', :bold),
    ui.color('Zone Name', :bold),
    ui.color('Zone ID', :bold),
    ui.color('Name', :bold)
  ]

  filter = locate_config_value(:filter)
  zone = locate_config_value(:zone)
  zoneid = locate_config_value(:zoneid)
  hypervisor = locate_config_value(:hypervisor)
  templateid = locate_config_value(:templateid)

  settings = connection.list_templates(filter)
  if response = settings['listtemplatesresponse']
    Chef::Log.debug("Response: #{response}")
    if templates = response['template']
      filters = {}
      filters[:hypervisor] = hypervisor unless hypervisor == 'all'
      filters[:zone] = zone unless zone == 'all'
      filters[:zoneid] = zoneid unless zoneid == 'all'
      filters[:templateid] = templateid unless templateid == 'all'

      print_templates(template_list,templates,filters)
    end
    puts ui.list(template_list, :uneven_columns_across, 6)
  end

end