Add option id to glance_image

In glance, user can specify the desired id when creating
a new image. This can be convenient for some cases where
users want predictable ids for image, as referencing ramdisk_id
or kernel_id for AMI images.

Change-Id: I7868f2005041dea6ecd63cd601e9244cefd08e5b
This commit is contained in:
Alfredo Moralejo 2016-05-25 09:26:27 +00:00
parent 13281189b2
commit 3079b7caae
4 changed files with 96 additions and 4 deletions

View File

@ -42,6 +42,7 @@ Puppet::Type.type(:glance_image).provide(
opts << "--disk-format=#{@resource[:disk_format]}"
opts << "--min-disk=#{@resource[:min_disk]}" if @resource[:min_disk]
opts << "--min-ram=#{@resource[:min_ram]}" if @resource[:min_ram]
opts << "--id=#{@resource[:id]}" if @resource[:id]
opts << props_to_s(@resource[:properties]) if @resource[:properties]
opts << location
@ -89,7 +90,7 @@ Puppet::Type.type(:glance_image).provide(
end
def id=(id)
fail('id is read only')
fail('id for existing images can not be modified')
end
def self.instances

View File

@ -35,9 +35,7 @@ Puppet::Type.newtype(:glance_image) do
newproperty(:id) do
desc 'The unique id of the image'
validate do |v|
raise(Puppet::Error, 'This is a read only property')
end
newvalues(/[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}/)
end
newparam(:location) do

View File

@ -0,0 +1,5 @@
---
features:
- Add the ability to create an image with a id
specified by the user. The provider now accepts
id parameter to create the image.

View File

@ -193,6 +193,94 @@ size="1270"
status="active"
updated_at="2015-04-10T18:18:18"
virtual_size="None"
')
instances = provider_class.instances
expect(instances.count).to eq(1)
end
end
end
describe 'when creating an image with id' do
let(:tenant_attrs) do
{
:ensure => 'present',
:name => 'image1',
:is_public => 'yes',
:container_format => 'bare',
:disk_format => 'qcow2',
:source => '/var/tmp/image1.img',
:id => '2b4be0b8-aec0-43af-a404-33c3335a0b3f'
}
end
let(:resource) do
Puppet::Type::Glance_image.new(tenant_attrs)
end
let(:provider) do
provider_class.new(resource)
end
it_behaves_like 'authenticated with environment variables' do
describe '#create' do
it 'creates an image' do
provider.class.stubs(:openstack)
.with('image', 'create', '--format', 'shell', ['image1', '--public', '--container-format=bare', '--disk-format=qcow2', '--id=2b4be0b8-aec0-43af-a404-33c3335a0b3f', '--file=/var/tmp/image1.img' ])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
container_format="bare"
created_at="2016-03-29T20:52:24Z"
disk_format="qcow2"
file="/v2/images/2b4be0b8-aec0-43af-a404-33c3335a0b3f/file"
id="2b4be0b8-aec0-43af-a404-33c3335a0b3f"
min_disk="0"
min_ram="0"
name="image1"
owner="5a9e521e17014804ab8b4e8b3de488a4"
properties="{}"
protected="False"
schema="/v2/schemas/image"
size="13287936"
status="active"
tags=""
updated_at="2016-03-29T20:52:40Z"
virtual_size="None"
visibility="public"
')
provider.create
expect(provider.exists?).to be_truthy
end
end
end
describe '.instances' do
it 'finds every image' do
provider.class.stubs(:openstack)
.with('image', 'list', '--quiet', '--format', 'csv', '--long')
.returns('"ID","Name","Disk Format","Container Format","Size","Status"
"2b4be0b8-aec0-43af-a404-33c3335a0b3f","image1","raw","bare",1270,"active"
')
provider.class.stubs(:openstack)
.with('image', 'show', '--format', 'shell', '2b4be0b8-aec0-43af-a404-33c3335a0b3f')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
container_format="bare"
created_at="2015-04-08T18:28:01"
deleted="False"
deleted_at="None"
disk_format="qcow2"
id="2b4be0b8-aec0-43af-a404-33c3335a0b3f"
min_disk="0"
min_ram="0"
is_public="True"
name="image1"
owner="None"
properties="{}"
protected="False"
size="1270"
status="active"
updated_at="2015-04-10T18:18:18"
virtual_size="None"
')
instances = provider_class.instances
expect(instances.count).to eq(1)