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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fog/metering/openstack.rb', line 53

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


40
41
42
43
44
45
46
47
# File 'lib/fog/metering/openstack.rb', line 40

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

.resetObject


49
50
51
# File 'lib/fog/metering/openstack.rb', line 49

def self.reset
  @data = nil
end

Instance Method Details

#credentialsObject


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

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


79
80
81
# File 'lib/fog/metering/openstack.rb', line 79

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

#get_event(_message_id) ⇒ Object


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

def get_event(_message_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'event_type' => 'compute.instance.create',
    'message_id' => 'd646b40dea6347dfb8caee2da1484c56',
  }
  response
end

#get_resource(_resource_id) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/metering/openstack/requests/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
47
# File 'lib/fog/metering/openstack/requests/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
48
# File 'lib/fog/metering/openstack/requests/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_events(_options = {}) ⇒ Object


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

def list_events(_options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = [{
    'event_type' => 'compute.instance.create',
    'message_id' => 'd646b40dea6347dfb8caee2da1484c56',
  }]
  response
end

#list_meters(_options = {}) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/metering/openstack/requests/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
25
# File 'lib/fog/metering/openstack/requests/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


83
84
85
# File 'lib/fog/metering/openstack.rb', line 83

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