Class: Fog::Image::OpenStack::V1::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/image/openstack/v1.rb,
lib/fog/image/openstack/v1/requests/get_image.rb,
lib/fog/image/openstack/v1/requests/set_tenant.rb,
lib/fog/image/openstack/v1/requests/create_image.rb,
lib/fog/image/openstack/v1/requests/delete_image.rb,
lib/fog/image/openstack/v1/requests/update_image.rb,
lib/fog/image/openstack/v1/requests/get_image_by_id.rb,
lib/fog/image/openstack/v1/requests/get_image_members.rb,
lib/fog/image/openstack/v1/requests/get_shared_images.rb,
lib/fog/image/openstack/v1/requests/list_public_images.rb,
lib/fog/image/openstack/v1/requests/add_member_to_image.rb,
lib/fog/image/openstack/v1/requests/update_image_members.rb,
lib/fog/image/openstack/v1/requests/remove_member_from_image.rb,
lib/fog/image/openstack/v1/requests/list_public_images_detailed.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



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

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 = 9292
  management_url.path = '/v1'
  @openstack_management_url = management_url.to_s

  @data ||= {:users => {}}
  unless @data[:users].detect { |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



43
44
45
46
47
48
49
# File 'lib/fog/image/openstack/v1.rb', line 43

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

.resetObject



51
52
53
# File 'lib/fog/image/openstack/v1.rb', line 51

def self.reset
  @data = nil
end

Instance Method Details

#add_member_to_image(_image_id, _tenant_id) ⇒ Object



16
17
18
19
20
# File 'lib/fog/image/openstack/v1/requests/add_member_to_image.rb', line 16

def add_member_to_image(_image_id, _tenant_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response
end

#create_image(attributes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fog/image/openstack/v1/requests/create_image.rb', line 47

def create_image(attributes)
  response = Excon::Response.new
  response.status = 201

  image_id = Fog::Mock.random_hex(32)
  image = data[:images][image_id] = {
    'name'             => attributes[:name],
    'size'             => attributes[:size] || Fog::Mock.random_numbers(8).to_i,
    'min_disk'         => attributes[:min_disk] || 0,
    'disk_format'      => attributes[:disk_format] || 'raw',
    'created_at'       => Time.now.strftime('%FT%T.%6N'),
    'container_format' => attributes[:container_format] || 'bare',
    'deleted_at'       => nil,
    'updated_at'       => Time.now.strftime('%FT%T.%6N'),
    'checksum'         => attributes[:checksum] || Fog::Mock.random_hex(32),
    'id'               => image_id,
    'deleted'          => false,
    'protected'        => false,
    'is_public'        => attributes[:is_public].to_s == 'true',
    'status'           => 'queued',
    'min_ram'          => attributes[:min_ram] || 0,
    'owner'            => attributes[:owner],
    'properties'       => attributes[:properties] || {}
  }
  response.body = {'image' => image}
  response
end

#credentialsObject



89
90
91
92
93
94
95
# File 'lib/fog/image/openstack/v1.rb', line 89

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

#dataObject



81
82
83
# File 'lib/fog/image/openstack/v1.rb', line 81

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

#delete_image(_image_id) ⇒ Object



16
17
18
19
20
# File 'lib/fog/image/openstack/v1/requests/delete_image.rb', line 16

def delete_image(_image_id)
  response = Excon::Response.new
  response.status = 200
  response
end

#get_image(_image_id) ⇒ 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
# File 'lib/fog/image/openstack/v1/requests/get_image.rb', line 16

def get_image(_image_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.headers = {"X-Image-Meta-Is_public" => "True",
                     "X-Image-Meta-Min_disk" => "0",
                     "X-Image-Meta-Property-Ramdisk_id" => "b45aa128-cd36-4ad9-a026-1a1c2bfd8fdc",
                     "X-Image-Meta-Disk_format" => "ami",
                     "X-Image-Meta-Created_at" => "2012-02-21T07:32:26",
                     "X-Image-Meta-Container_format" => "ami",
                     "Etag" => "2f81976cae15c16ef0010c51e3a6c163",
                     "Location" => "http://192.168.27.100:9292/v1/images/0e09fbd6-43c5-448a-83e9-0d3d05f9747e",
                     "X-Image-Meta-Protected" => "False",
                     "Date" => "Fri, 24 Feb 2012 02:14:25 GMT",
                     "X-Image-Meta-Name" => "cirros-0.3.0-x86_64-blank",
                     "X-Image-Meta-Min_ram" => "0", "Content-Type" => "text/html; charset=UTF-8",
                     "X-Image-Meta-Updated_at" => "2012-02-21T07:32:29",
                     "X-Image-Meta-Property-Kernel_id" => "cd28951e-e1c2-4bc5-95d3-f0495abbcdc5",
                     "X-Image-Meta-Size" => "25165824",
                     "X-Image-Meta-Checksum" => "2f81976cae15c16ef0010c51e3a6c163",
                     "X-Image-Meta-Deleted" => "False",
                     "Content-Length" => "0",
                     "X-Image-Meta-Owner" => "ff528b20431645ebb5fa4b0a71ca002f",
                     "X-Image-Meta-Status" => "active",
                     "X-Image-Meta-Id" => "0e09fbd6-43c5-448a-83e9-0d3d05f9747e"}
  response.body = ""
  response
end

#get_image_by_id(image_id) ⇒ 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
# File 'lib/fog/image/openstack/v1/requests/get_image_by_id.rb', line 16

def get_image_by_id(image_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.headers = {
    'X-Image-Meta-Checksum'         => '8a40c862b5735975d82605c1dd395796',
    'X-Image-Meta-Container_format' => 'aki',
    'X-Image-Meta-Created_at'       => '2016-01-06T03:22:20.000000',
    'X-Image-Meta-Deleted'          => 'False',
    'X-Image-Meta-Disk_format'      => 'aki',
    'X-Image-Meta-Id'               => image_id,
    'X-Image-Meta-Is_public'        => 'True',
    'X-Image-Meta-Min_disk'         => 0,
    'X-Image-Meta-Min_ram'          => 0,
    'X-Image-Meta-Name'             => 'cirros-0.3.4-x86_64-uec-kernel',
    'X-Image-Meta-Owner'            => '13cc6052265b41529e2fd0fc461fa8ef',
    'X-Image-Meta-Protected'        => 'False',
    'X-Image-Meta-Size'             => 4979632,
    'X-Image-Meta-Status'           => 'deactivated',
    'X-Image-Meta-Updated_at'       => '2016-02-25T03:02:05.000000',
    'X-Image-Meta-Property-foo'     => 'bar'
  }
  response.body = {}
  response
end

#get_image_members(_image_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/image/openstack/v1/requests/get_image_members.rb', line 16

def get_image_members(_image_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.body = {
    "members" => [
      {"member_id" => "ff528b20431645ebb5fa4b0a71ca002f",
       "can_share" => false}
    ]
  }
  response
end

#get_shared_images(_tenant_id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/image/openstack/v1/requests/get_shared_images.rb', line 16

def get_shared_images(_tenant_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.body = {
    "shared_images" => [
      {"image_id"  => "ff528b20431645ebb5fa4b0a71ca002f",
       "can_share" => false}
    ]
  }
  response
end

#list_public_images(_options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/image/openstack/v1/requests/list_public_images.rb', line 17

def list_public_images(_options = {})
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.body = {
    "images" => [{
      "name"             => Fog::Mock.random_letters(10),
      "size"             => Fog::Mock.random_numbers(8).to_i,
      "disk_format"      => "iso",
      "container_format" => "bare",
      "id"               => Fog::Mock.random_hex(36),
      "checksum"         => Fog::Mock.random_hex(32)
    }]
  }
  response
end

#list_public_images_detailed(_options = {}, _query_deprecated = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/fog/image/openstack/v1/requests/list_public_images_detailed.rb', line 27

def list_public_images_detailed(_options = {}, _query_deprecated = nil)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response.body = {'images' => data[:images].values}
  response
end

#remove_member_from_image(_image_id, _member_id) ⇒ Object



16
17
18
19
20
# File 'lib/fog/image/openstack/v1/requests/remove_member_from_image.rb', line 16

def remove_member_from_image(_image_id, _member_id)
  response = Excon::Response.new
  response.status = [200, 204][rand(2)]
  response
end

#reset_dataObject



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

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

#set_tenant(_tenant) ⇒ Object



14
15
16
# File 'lib/fog/image/openstack/v1/requests/set_tenant.rb', line 14

def set_tenant(_tenant)
  true
end

#update_image(attributes) ⇒ Object



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
60
61
# File 'lib/fog/image/openstack/v1/requests/update_image.rb', line 35

def update_image(attributes)
  response = Excon::Response.new
  response.status = 200
  image = images.last
  response.body = {
    'image' => {
      'name'             => attributes[:name] || image.name,
      'size'             => image.size,
      'min_disk'         => (attributes[:min_disk] || image.min_disk).to_i,
      'disk_format'      => attributes[:disk_format] || image.disk_format,
      'created_at'       => image.created_at,
      'container_format' => attributes[:container_format] || image.container_format,
      'deleted_at'       => nil,
      'updated_at'       => Time.now.to_s,
      'checksum'         => image.checksum,
      'id'               => attributes[:id],
      'deleted'          => false,
      'protected'        => false,
      'is_public'        => attributes[:is_public] || image.is_public,
      'status'           => image.status,
      'min_ram'          => (attributes[:min_ram] || image.min_ram).to_i,
      'owner'            => attributes[:owner] || image.owner,
      'properties'       => attributes[:properties] || image.properties
    }
  }
  response
end

#update_image_members(_image_id, _members) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/image/openstack/v1/requests/update_image_members.rb', line 24

def update_image_members(_image_id, _members)
  response = Excon::Response.new
  response.status = 204
  response.body = {
    'members' => [
      {'member_id' => 'ff528b20431645ebb5fa4b0a71ca002f', 'can_share' => false},
      {'member_id' => 'ff528b20431645ebb5fa4b0a71ca002f', 'can_share' => true}
    ]
  }
  response
end