class Fog::Metering::OpenStack::Real

Public Class Methods

new(options={}) click to toggle source
# File lib/fog/openstack/metering.rb, line 96
def initialize(options={})
  initialize_identity options

  @openstack_service_type           = options[:openstack_service_type] || ['metering']
  @openstack_service_name           = options[:openstack_service_name]
  @openstack_endpoint_type          = options[:openstack_endpoint_type] || 'adminURL'

  @connection_options               = options[:connection_options] || {}

  authenticate

  @persistent = options[:persistent] || false
  @connection = Fog::Core::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

Public Instance Methods

get_event(message_id) click to toggle source
# File lib/fog/openstack/requests/metering/get_event.rb, line 5
def get_event(message_id)
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "events/#{message_id}"
  )
end
get_resource(resource_id) click to toggle source
# File lib/fog/openstack/requests/metering/get_resource.rb, line 5
def get_resource(resource_id)
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "resources/#{resource_id}"
  )
end
get_samples(meter_id, options=[]) click to toggle source
# File lib/fog/openstack/requests/metering/get_samples.rb, line 5
def get_samples(meter_id, options=[])
  data = {
    'q' => Array.new
  }

  options.each do |opt|
    filter = {}

    ['field', 'op', 'value'].each do |key|
      filter[key] = opt[key] if opt[key]
    end

    data['q'] << filter unless filter.empty?
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'GET',
    :path     => "meters/#{meter_id}"
  )
end
get_statistics(meter_id, options={}) click to toggle source
# File lib/fog/openstack/requests/metering/get_statistics.rb, line 5
def get_statistics(meter_id, options={})
  data = {
    'period' => options['period'],
    'q'      => Array.new
  }

  options['q'].each do |opt|
    filter = {}

    ['field', 'op', 'value'].each do |key|
      filter[key] = opt[key] if opt[key]
    end

    data['q'] << filter unless filter.empty?
  end if options['q'].is_a? Array

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'GET',
    :path     => "meters/#{meter_id}/statistics"
  )
end
list_events(options=[]) click to toggle source
# File lib/fog/openstack/requests/metering/list_events.rb, line 5
def list_events(options=[])
  data = {
    'q' => Array.new
  }

  options.each do |opt|
    filter = {}

    ['field', 'op', 'value'].each do |key|
      filter[key] = opt[key] if opt[key]
    end

    data['q'] << filter unless filter.empty?
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'GET',
    :path     => 'events'
  )
end
list_meters(options=[]) click to toggle source
# File lib/fog/openstack/requests/metering/list_meters.rb, line 5
def list_meters(options=[])
  data = {
    'q' => Array.new
  }

  options.each do |opt|
    filter = {}

    ['field', 'op', 'value'].each do |key|
      filter[key] = opt[key] if opt[key]
    end

    data['q'] << filter unless filter.empty?
  end

  request(
    :body     => Fog::JSON.encode(data),
    :expects  => 200,
    :method   => 'GET',
    :path     => 'meters'
  )
end
list_resources(options = {}) click to toggle source
# File lib/fog/openstack/requests/metering/list_resources.rb, line 5
def list_resources(options = {})
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => 'resources'
  )
end
request(params) click to toggle source
# File lib/fog/openstack/metering.rb, line 111
def request(params)
  begin
    response = @connection.request(params.merge({
      :headers  => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
        'X-Auth-Token' => @auth_token
      }.merge!(params[:headers] || {}),
      :path     => "#{@path}/v2/#{params[:path]}"#,
      # Causes errors for some requests like tenants?limit=1
      # :query    => ('ignore_awful_caching' << Time.now.to_i.to_s)
    }))
  rescue Excon::Errors::Unauthorized => error
    if error.response.body != 'Bad username or password' # token expiration
      @openstack_must_reauthenticate = true
      authenticate
      retry
    else # bad credentials
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
    when Excon::Errors::NotFound
      Fog::Metering::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end