Class: KnifeCloudstack::CsTemplateList

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

Constant Summary collapse

MEGABYTES =
1024 * 1024

Instance Method Summary collapse

Instance Method Details

#human_file_size(n) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/chef/knife/cs_template_list.rb', line 85

def human_file_size n
  count = 0
  while  n >= 1024 and count < 4
    n /= 1024.0
    count += 1
  end
  format("%.2f", n) + %w(B KB MB GB TB)[count]
end

#locate_config_value(key) ⇒ Object



94
95
96
97
# File 'lib/chef/knife/cs_template_list.rb', line 94

def locate_config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end

#runObject



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
# File 'lib/chef/knife/cs_template_list.rb', line 56

def run

  connection = CloudstackClient::Connection.new(
      locate_config_value(:cloudstack_url),
      locate_config_value(:cloudstack_api_key),
      locate_config_value(:cloudstack_secret_key)
  )

  template_list = [
      ui.color('Name', :bold),
      ui.color('Size', :bold),
      ui.color('Zone', :bold),
      ui.color('Public', :bold),
      ui.color('Created', :bold),
  ]

  filter = config[:filter]
  templates = connection.list_templates(filter)
  templates.each do |t|
    template_list << t['name']
    template_list << (human_file_size(t['size']) || 'Unknown')
    template_list << t['zonename']
    template_list << t['ispublic'].to_s
    template_list << t['created']
  end
  puts ui.list(template_list, :columns_across, 5)

end