Class: Fog::Metering::OpenStack::Mock

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/openstack/metering.rb', line 41

def initialize(options={})
  @openstack_username = options[:openstack_username]
  @openstack_tenant   = options[:openstack_tenant]
  @openstack_auth_uri = URI.parse(options[:openstack_auth_url])

  @auth_token = Fog::Mock.random_base64(64)
  @auth_token_expiration = (Time.now.utc + 86400).iso8601

  management_url = URI.parse(options[:openstack_auth_url])
  management_url.port = 8776
  management_url.path = '/v1'
  @openstack_management_url = management_url.to_s

  @data ||= { :users => {} }
  unless @data[:users].find {|u| u['name'] == options[:openstack_username]}
    id = Fog::Mock.random_numbers(6).to_s
    @data[:users][id] = {
      'id'       => id,
      'name'     => options[:openstack_username],
      'email'    => "#{options[:openstack_username]}@mock.com",
      'tenantId' => Fog::Mock.random_numbers(6).to_s,
      'enabled'  => true
    }
  end
end

Class Method Details

.dataObject



28
29
30
31
32
33
34
35
# File 'lib/fog/openstack/metering.rb', line 28

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :users => {},
      :tenants => {}
    }
  end
end

.resetObject



37
38
39
# File 'lib/fog/openstack/metering.rb', line 37

def self.reset
  @data = nil
end

Instance Method Details

#credentialsObject



75
76
77
78
79
80
# File 'lib/fog/openstack/metering.rb', line 75

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

#dataObject



67
68
69
# File 'lib/fog/openstack/metering.rb', line 67

def data
  self.class.data[@openstack_username]
end

#get_resource(resource_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/metering/get_resource.rb', line 15

def get_resource(resource_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'resource_id'=>'glance',
    'project_id'=>'d646b40dea6347dfb8caee2da1484c56',
    'user_id'=>'1d5fd9eda19142289a60ed9330b5d284',
    'metadata'=>{}}
  response
end

#get_samples(meter_id) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/openstack/requests/metering/get_samples.rb', line 30

def get_samples(meter_id)
  response = Excon::Response.new
  response.status = 200
  response.body = [{
    'counter_name'=>'image.size',
    'user_id'=>'1d5fd9eda19142289a60ed9330b5d284',
    'resource_id'=>'glance',
    'timestamp'=>'2013-04-03T23:44:21',
    'resource_metadata'=>{},
    'source'=>'artificial',
    'counter_unit'=>'bytes',
    'counter_volume'=>10.0,
    'project_id'=>'d646b40dea6347dfb8caee2da1484c56',
    'message_id'=>'14e4a902-9cf3-11e2-a054-003048f5eafc',
    'counter_type'=>'gauge'}]
  response
end

#get_statistics(meter_id, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/openstack/requests/metering/get_statistics.rb', line 31

def get_statistics(meter_id, options={})
  response = Excon::Response.new
  response.status = 200
  response.body = [{
    'count'=>143,
    'duration_start'=>'2013-04-03T23:44:21',
    'min'=>10.0,
    'max'=>10.0,
    'duration_end'=>'2013-04-04T23:24:21',
    'period'=>0,
    'period_end'=>'2013-04-04T23:24:21',
    'duration'=>85200.0,
    'period_start'=>'2013-04-03T23:44:21',
    'avg'=>10.0,
    'sum'=>1430.0}]
  response
end

#list_meters(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/openstack/requests/metering/list_meters.rb', line 30

def list_meters(options={})
  response = Excon::Response.new
  response.status = 200
  response.body = [{
    'user_id'=>'1d5fd9eda19142289a60ed9330b5d284',
    'name'=>'image.size',
    'resource_id'=>'glance',
    'project_id'=>'d646b40dea6347dfb8caee2da1484c56',
    'type'=>'gauge',
    'unit'=>'bytes'}]
  response
end

#list_resources(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/metering/list_resources.rb', line 15

def list_resources(options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = [{
    'resource_id'=>'glance',
    'project_id'=>'d646b40dea6347dfb8caee2da1484c56',
    'user_id'=>'1d5fd9eda19142289a60ed9330b5d284',
    'metadata'=>{}}]
  response
end

#reset_dataObject



71
72
73
# File 'lib/fog/openstack/metering.rb', line 71

def reset_data
  self.class.data.delete(@openstack_username)
end