Class: Fog::Metering::OpenStack::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/metering.rb,
lib/fog/openstack/requests/metering/get_samples.rb,
lib/fog/openstack/requests/metering/list_meters.rb,
lib/fog/openstack/requests/metering/get_resource.rb,
lib/fog/openstack/requests/metering/get_statistics.rb,
lib/fog/openstack/requests/metering/list_resources.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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
# File 'lib/fog/openstack/metering.rb', line 91

def initialize(options={})
  @openstack_auth_token = options[:openstack_auth_token]

  unless @openstack_auth_token
    missing_credentials = Array.new
    @openstack_api_key  = options[:openstack_api_key]
    @openstack_username = options[:openstack_username]

    missing_credentials << :openstack_api_key  unless @openstack_api_key
    missing_credentials << :openstack_username unless @openstack_username
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
  end

  @openstack_tenant               = options[:openstack_tenant]
  @openstack_auth_uri             = URI.parse(options[:openstack_auth_url])
  @openstack_management_url       = options[:openstack_management_url]
  @openstack_must_reauthenticate  = false
  @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] || {}

  @current_user = options[:current_user]
  @current_tenant = options[:current_tenant]

  authenticate

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

Instance Attribute Details

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



89
90
91
# File 'lib/fog/openstack/metering.rb', line 89

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



88
89
90
# File 'lib/fog/openstack/metering.rb', line 88

def current_user
  @current_user
end

Instance Method Details

#credentialsObject



123
124
125
126
127
128
129
130
# File 'lib/fog/openstack/metering.rb', line 123

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :current_user             => @current_user,
    :current_tenant           => @current_tenant }
end

#get_resource(resource_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/metering/get_resource.rb', line 6

def get_resource(resource_id)
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => "resources/#{resource_id}"
  )
end

#get_samples(meter_id, options = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/metering/get_samples.rb', line 6

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 = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/openstack/requests/metering/get_statistics.rb', line 6

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_meters(options = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/metering/list_meters.rb', line 6

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 = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/fog/openstack/requests/metering/list_resources.rb', line 6

def list_resources(options = {})
  request(
    :expects  => 200,
    :method   => 'GET',
    :path     => 'resources'
  )
end

#reloadObject



132
133
134
# File 'lib/fog/openstack/metering.rb', line 132

def reload
  @connection.reset
end

#request(params) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fog/openstack/metering.rb', line 136

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::Compute::OpenStack::NotFound.slurp(error)
    else
      error
    end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end