Skip to content

Commit

Permalink
Support both v1 & v2 glance API
Browse files Browse the repository at this point in the history
Fix #168

Endpoint /images exists on both v1 and v2 API
The attribute 'visibility' is used to detect
if the call has been made on v1 or v2

In case of v2 we have all the needed information,
but in case of v1 we don't and we have to call
/images/detail to get full details
  • Loading branch information
ggiamarchi committed Nov 21, 2014
1 parent 24d4ad1 commit 977318d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
24 changes: 23 additions & 1 deletion source/lib/vagrant-openstack-provider/client/glance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,31 @@ def get_api_version_list(_env)
JSON.parse(json)['versions']
end

# Endpoint /images exists on both v1 and v2 API
# The attribute 'visibility' is used to detect
# if the call has been made on v1 or v2
#
# In case of v2 we have all the needed information,
# but in case of v1 we don't and we have to call
# /images/detail to get full details
#
def get_all_images(env)
images_json = get(env, "#{@session.endpoints[:image]}/images")
JSON.parse(images_json)['images'].map { |i| Image.new(i['id'], i['name'], i['visibility'], i['size'], i['min_ram'], i['min_disk']) }
images = JSON.parse(images_json)['images']

return images if images.empty?

is_v1 = false
unless images[0].key? 'visibility'
is_v1 = true
images_json = get(env, "#{@session.endpoints[:image]}/images/detail")
images = JSON.parse(images_json)['images']
end

images.map do |i|
i['visibility'] = i['is_public'] ? 'public' : 'private' if is_v1
Image.new(i['id'], i['name'], i['visibility'], i['size'], i['min_ram'], i['min_disk'])
end
end
end
end
Expand Down
82 changes: 64 additions & 18 deletions source/spec/vagrant-openstack-provider/client/glance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,73 @@

describe 'get_all_images' do
context 'with token and project_id acquainted' do
it 'returns all images with details' do
stub_request(:get, 'http://glance/images')
.with(
headers:
context 'and api version is v2' do
it 'returns all images with details' do
stub_request(:get, 'http://glance/images')
.with(
headers:
{
'Accept' => 'application/json',
'X-Auth-Token' => '123456'
})
.to_return(
status: 200,
body: '
{
'Accept' => 'application/json',
'X-Auth-Token' => '123456'
})
.to_return(
status: 200,
body: '{
"images": [
{ "id": "i1", "name": "image1", "visibility": "public", "size": "1024", "min_ram": "1", "min_disk": "10" },
{ "id": "i2", "name": "image2", "visibility": "private", "size": "2048", "min_ram": "2", "min_disk": "20" }
]
}')
"images": [
{ "id": "i1", "name": "image1", "visibility": "public", "size": "1024", "min_ram": "1", "min_disk": "10" },
{ "id": "i2", "name": "image2", "visibility": "private", "size": "2048", "min_ram": "2", "min_disk": "20" }
]
}')

images = @glance_client.get_all_images(env)

expect(images).to eq [Image.new('i1', 'image1', 'public', '1024', '1', '10'),
Image.new('i2', 'image2', 'private', '2048', '2', '20')]
end
end

context 'and api version is v1' do
it 'returns all images with details' do
stub_request(:get, 'http://glance/images')
.with(
headers:
{
'Accept' => 'application/json',
'X-Auth-Token' => '123456'
})
.to_return(
status: 200,
body: '
{
"images": [
{ "id": "i1", "name": "image1", "is_public": true },
{ "id": "i2", "name": "image2", "is_public": false }
]
}')

stub_request(:get, 'http://glance/images/detail')
.with(
headers:
{
'Accept' => 'application/json',
'X-Auth-Token' => '123456'
})
.to_return(
status: 200,
body: '
{
"images": [
{ "id": "i1", "name": "image1", "is_public": true, "size": "1024", "min_ram": "1", "min_disk": "10" },
{ "id": "i2", "name": "image2", "is_public": false, "size": "2048", "min_ram": "2", "min_disk": "20" }
]
}')

images = @glance_client.get_all_images(env)
images = @glance_client.get_all_images(env)

expect(images).to eq [Image.new('i1', 'image1', 'public', '1024', '1', '10'),
Image.new('i2', 'image2', 'private', '2048', '2', '20')]
expect(images).to eq [Image.new('i1', 'image1', 'public', '1024', '1', '10'),
Image.new('i2', 'image2', 'private', '2048', '2', '20')]
end
end
end
end
Expand Down

0 comments on commit 977318d

Please sign in to comment.