class Cloudstats::Collect

Public Class Methods

new(settings) click to toggle source
# File lib/cloudstats/collect.rb, line 8
def initialize(settings)
  @settings = settings.dup
  @config = if @settings[:cloudstack_url] &&
    @settings[:cloudstack_api_key] &&
    @settings[:cloudstack_secret_key]
    {
      url: @settings[:cloudstack_url],
      api_key: @settings[:cloudstack_api_key],
      secret_key: @settings[:cloudstack_secret_key]
    }
  else
    @settings[:config_file] = @settings[:cloudstack_config]
    CloudstackClient::Configuration.load(@settings)
  end
  @cs ||= CloudstackClient::Client.new(
    @config[:url],
    @config[:api_key],
    @config[:secret_key]
  )
  @cs.debug = true if @settings[:debug]
  @cs
end

Public Instance Methods

account_stats() click to toggle source
# File lib/cloudstats/collect.rb, line 31
def account_stats
  {
    type: "account",
    stats: @cs.list_accounts(client_options)
  }
end
project_stats() click to toggle source
# File lib/cloudstats/collect.rb, line 38
def project_stats
  {
    type: "project",
    stats: @cs.list_projects(client_options)
  }
end

Private Instance Methods

client_options() click to toggle source
# File lib/cloudstats/collect.rb, line 47
def client_options
  { listall: true, isrecursive: true }.merge(
   resolve_domain(@settings)
  )
end
resolve_domain(opts) click to toggle source
# File lib/cloudstats/collect.rb, line 53
def resolve_domain(opts)
  if opts[:domain]
    if domain = @cs.list_domains(name: opts[:domain]).first
      opts[:domainid] = domain['id']
    else
      raise "Domain #{opts[:domain]} not found."
    end
  end
  opts
end