Class: Fog::Volume::OpenStack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/volume.rb,
lib/fog/openstack/requests/volume/get_quota.rb,
lib/fog/openstack/requests/volume/list_zones.rb,
lib/fog/openstack/requests/volume/set_tenant.rb,
lib/fog/openstack/requests/volume/list_volumes.rb,
lib/fog/openstack/requests/volume/update_quota.rb,
lib/fog/openstack/requests/volume/create_volume.rb,
lib/fog/openstack/requests/volume/delete_volume.rb,
lib/fog/openstack/requests/volume/extend_volume.rb,
lib/fog/openstack/requests/volume/list_snapshots.rb,
lib/fog/openstack/requests/volume/delete_snapshot.rb,
lib/fog/openstack/requests/volume/get_quota_usage.rb,
lib/fog/openstack/requests/volume/list_volume_types.rb,
lib/fog/openstack/requests/volume/create_volume_type.rb,
lib/fog/openstack/requests/volume/delete_volume_type.rb,
lib/fog/openstack/requests/volume/get_quota_defaults.rb,
lib/fog/openstack/requests/volume/get_volume_details.rb,
lib/fog/openstack/requests/volume/update_volume_type.rb,
lib/fog/openstack/requests/volume/get_snapshot_details.rb,
lib/fog/openstack/requests/volume/list_volumes_detailed.rb,
lib/fog/openstack/requests/volume/create_volume_snapshot.rb,
lib/fog/openstack/requests/volume/get_volume_type_details.rb,
lib/fog/openstack/requests/volume/list_snapshots_detailed.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fog/openstack/volume.rb', line 89

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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/openstack/volume.rb', line 71

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :users => {},
      :tenants => {},
      :quota => {
        'gigabytes' => 1000,
        'volumes'   => 10,
        'snapshots' => 10
      }
    }
  end
end

.resetObject



85
86
87
# File 'lib/fog/openstack/volume.rb', line 85

def self.reset
  @data = nil
end

Instance Method Details

#create_volume(name, description, size, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fog/openstack/requests/volume/create_volume.rb', line 29

def create_volume(name, description, size, options={})
  response = Excon::Response.new
  response.status = 202
  response.body = {
    'volume' => {
      'id'                  => Fog::Mock.random_numbers(2),
      'display_name'        => name,
      'display_description' => description,
      'metadata'            => options['metadata'] || {},
      'size'                => size,
      'status'              => 'creating',
      'snapshot_id'         => options[:snapshot_id] || nil,
      'image_id'            => options[:imageRef] || nil,
      'volume_type'         => nil,
      'availability_zone'   => 'nova',
      'created_at'          => Time.now,
      'attachments'         => []
    }
  }
  response
end

#create_volume_snapshot(volume_id, name, description, force = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/openstack/requests/volume/create_volume_snapshot.rb', line 25

def create_volume_snapshot(volume_id, name, description, force=false)
  response = Excon::Response.new
  response.status = 202
  response.body = {
    "snapshot"=> {
       "status"=>"creating",
       "display_name"=>name,
       "created_at"=>Time.now,
       "display_description"=>description,
       "volume_id"=>volume_id,
       "id"=>"5",
       "size"=>1
    }
  }
  response
end

#create_volume_type(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/volume/create_volume_type.rb', line 24

def create_volume_type(options = {})
  response = Excon::Response.new
  response.status = 202
  response.body = {
    "volume_type"=> {
      "id"=> "6685584b-1eac-4da6-b5c3-555430cf68ff",
      "name"=> "vol-type-001",
      "extra_specs"=> {
          "capabilities"=> "gpu"
      }
    }
  }
  response
end

#credentialsObject



123
124
125
126
127
128
# File 'lib/fog/openstack/volume.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 }
end

#dataObject



115
116
117
# File 'lib/fog/openstack/volume.rb', line 115

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

#delete_snapshot(snapshot_id) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/volume/delete_snapshot.rb', line 15

def delete_snapshot(snapshot_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#delete_volume(volume_id) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/volume/delete_volume.rb', line 15

def delete_volume(volume_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#delete_volume_type(volume_type_id) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/volume/delete_volume_type.rb', line 15

def delete_volume_type(volume_type_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#extend_volume(volume_id, size) ⇒ Object



17
18
19
20
21
# File 'lib/fog/openstack/requests/volume/extend_volume.rb', line 17

def extend_volume(volume_id, size)
  response = Excon::Response.new
  response.status = 202
  response
end

#get_quota(tenant_id) ⇒ Object



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

def get_quota(tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'quota_set' => (self.data[:quota_updated] or self.data[:quota]).merge({'id' => tenant_id})
  }
  response
end

#get_quota_defaults(tenant_id) ⇒ Object



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

def get_quota_defaults(tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'quota_set' => self.data[:quota].merge({'id' => tenant_id})
  }
  response
end

#get_quota_usage(tenant_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/openstack/requests/volume/get_quota_usage.rb', line 15

def get_quota_usage(tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'quota_set' => {
      'gigabytes' => {
        'reserved' => 0,
        'limit'    => -1,
        'in_use'   => 160
      },
      'snapshots' => {
        'reserved' => 0,
        'limit'    => 50,
        'in_use'   => 3
      },
      'volumes' => {
        'reserved' => 0,
        'limit'    => 50,
        'in_use'    => 5
      },
      'id' => tenant_id
    }
  }
  response
end

#get_snapshot_details(detailed = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/volume/get_snapshot_details.rb', line 15

def get_snapshot_details(detailed=true)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'snapshot' => {
      'id'                 => '1',
      'display_name'        => 'Snapshot1',
      'display_description' => 'Volume1 snapshot',
      'size'               => 1,
      'volume_id'           => '1',
      'status'             => 'available',
      'created_at'          => Time.now
    }
  }
  response
end

#get_volume_details(detailed = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/volume/get_volume_details.rb', line 15

def get_volume_details(detailed=true)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'volume' => {
      'id'                  => '1',
      'display_name'        => Fog::Mock.random_letters(rand(8) + 5),
      'display_description' => Fog::Mock.random_letters(rand(12) + 10),
      'size'                => 3,
      'volume_type'         => nil,
      'snapshot_id'         => '4',
      'status'              => 'online',
      'availability_zone'   => 'nova',
      'created_at'          => Time.now,
      'attachments'         => []
    }
  }
  response
end

#get_volume_type_details(volume_type_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/volume/get_volume_type_details.rb', line 15

def get_volume_type_details(volume_type_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "volume_type" => {
      "id" => "1",
      "name" => "type 1",
      "extra_specs" => {
        "volume_backend_name" => "type 1 backend name"
      }
    }
  }
  response
end

#list_snapshots(detailed = true, options = {}) ⇒ Object



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

def list_snapshots(detailed=true, options={})
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'snapshots' => [get_snapshot_details.body["snapshot"]]
  }
  response
end

#list_snapshots_detailed(options = {}) ⇒ Object



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

def list_snapshots_detailed(options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'snapshots' => [get_snapshot_details.body["snapshot"]]
  }
  response
end

#list_volume_types(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/volume/list_volume_types.rb', line 16

def list_volume_types(options = {})
  response = Excon::Response.new
  response.status = 200
  self.data[:volume_types] ||= [
    {
      "id" => "1",
      "name" => "type 1",
      "extra_specs" => {
        "volume_backend_name" => "type 1 backend name"
      }
    },
    {
      "id" => "2",
      "name" => "type 2",
      "extra_specs" => {
        "volume_backend_name" => "type 2 backend name"
      }
    }
  ]
  response.body = { 'volume_types' => self.data[:volume_types] }
  response
end

#list_volumes(options = true, options_deprecated = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fog/openstack/requests/volume/list_volumes.rb', line 30

def list_volumes(options = true, options_deprecated = {})
  response = Excon::Response.new
  response.status = 200
  self.data[:volumes] ||= [
    { "status" => "available",
      "display_description" => "test 1 desc",
      "availability_zone" => "nova",
      "display_name" => "Volume1",
      "attachments" => [{}],
      "volume_type" => nil,
      "snapshot_id" => nil,
      "size" => 1,
      "id" => 1,
      "created_at" => Time.now,
      "metadata" => {} },
    { "status" => "available",
      "display_description" => "test 2 desc",
      "availability_zone" => "nova",
      "display_name" => "Volume2",
      "attachments" => [{}],
      "volume_type" => nil,
      "snapshot_id" => nil,
      "size" => 1,
      "id" => 2,
      "created_at" => Time.now,
      "metadata" => {} }
    ]
  response.body = { 'volumes' => self.data[:volumes] }
  response
end

#list_volumes_detailed(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/openstack/requests/volume/list_volumes_detailed.rb', line 16

def list_volumes_detailed(options = {})
  response = Excon::Response.new
  response.status = 200
  self.data[:volumes] ||= [
    { "status" => "available",
      "display_description" => "test 1 desc",
      "availability_zone" => "nova",
      "display_name" => "Volume1",
      "attachments" => [{}],
      "volume_type" => nil,
      "snapshot_id" => nil,
      "size" => 1,
      "id" => 1,
      "created_at" => Time.now,
      "metadata" => {} },
    { "status" => "available",
      "display_description" => "test 2 desc",
      "availability_zone" => "nova",
      "display_name" => "Volume2",
      "attachments" => [{}],
      "volume_type" => nil,
      "snapshot_id" => nil,
      "size" => 1,
      "id" => 2,
      "created_at" => Time.now,
      "metadata" => {} }
    ]
  response.body = { 'volumes' => self.data[:volumes] }
  response
end

#list_zones(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/volume/list_zones.rb', line 16

def list_zones(options = {})
  Excon::Response.new(
    :body   => {
      "availabilityZoneInfo" => [
        {
          "zoneState" => { "available" => true },
          "zoneName"  => "nova"
        }
      ]
    },
    :status => 200
  )
end

#reset_dataObject



119
120
121
# File 'lib/fog/openstack/volume.rb', line 119

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

#set_tenant(tenant) ⇒ Object



13
14
15
# File 'lib/fog/openstack/requests/volume/set_tenant.rb', line 13

def set_tenant(tenant)
  true
end

#update_quota(tenant_id, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/volume/update_quota.rb', line 17

def update_quota(tenant_id, options = {})
  self.data[:quota_updated] = self.data[:quota].merge options

  response = Excon::Response.new
  response.status = 200
  response.body = { 'quota_set' => self.data[:quota_updated] }
  response
end

#update_volume_type(volume_type_id, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/volume/update_volume_type.rb', line 24

def update_volume_type(volume_type_id, options = {})
  response = Excon::Response.new
  response.status = 202
  response.body = {
    "volume_type"=> {
      "id"=> "6685584b-1eac-4da6-b5c3-555430cf68ff",
      "name"=> "vol-type-001",
      "extra_specs"=> {
          "capabilities"=> "gpu"
      }
    }
  }
  response
end