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



101
102
103
104
105
106
107
108
# File 'lib/chef/knife/cs_template_list.rb', line 101

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



110
111
112
113
# File 'lib/chef/knife/cs_template_list.rb', line 110

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

#runObject



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
90
91
92
93
94
95
96
97
98
99
# File 'lib/chef/knife/cs_template_list.rb', line 64

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('ID', :bold),
      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['id']
    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
  
  if config[:output_type].eql? "table"
    puts ui.list(template_list, :columns_across, 6)
  else 
    puts templates.to_json
  end

end