glance_image: Add support for project_id/name

This change adds the new project_id/name attribute to the glance_image
resource type, so that users can customize owner of glance images
managed by the resource.

Change-Id: I79c231d857079896bf101408aa758ef056669585
This commit is contained in:
Takashi Kajinami 2021-11-24 23:18:19 +09:00
parent 6488c46733
commit 983f6199d4
4 changed files with 152 additions and 26 deletions

View File

@ -65,6 +65,12 @@ Puppet::Type.type(:glance_image).provide(
opts << "--tag=#{@resource[:image_tag]}" if @resource[:image_tag]
opts << location
if @resource[:project_name]
opts << "--project=#{@resource[:project_name]}"
elsif @resource[:project_id]
opts << "--project=#{@resource[:project_id]}"
end
begin
attrs = self.class.request('image', 'create', opts)
properties = self.class.parsestring(attrs[:properties]) rescue nil
@ -78,7 +84,8 @@ Puppet::Type.type(:glance_image).provide(
:min_disk => attrs[:min_disk],
:min_ram => attrs[:min_ram],
:properties => self.class.exclude_owner_specified_props(self.class.exclude_readonly_props(properties)),
:image_tag => attrs[:tags]
:image_tag => attrs[:tags],
:project_id => attrs[:owner]
}
ensure
if temp_file
@ -134,6 +141,14 @@ Puppet::Type.type(:glance_image).provide(
fail('id for existing images can not be modified')
end
def project_id=(value)
@property_flush[:project_id] = value
end
def project_name=(value)
@property_flush[:project_name] = value
end
def self.instances
list = request('image', 'list', '--long')
list.collect do |image|
@ -149,7 +164,8 @@ Puppet::Type.type(:glance_image).provide(
:min_disk => attrs[:min_disk],
:min_ram => attrs[:min_ram],
:properties => exclude_owner_specified_props(exclude_readonly_props(properties)),
:image_tag => attrs[:tags]
:image_tag => attrs[:tags],
:project_id => attrs[:owner]
)
end
end
@ -176,6 +192,12 @@ Puppet::Type.type(:glance_image).provide(
(opts << props_to_s(@property_flush[:properties])) if @property_flush[:properties]
(opts << "--tag=#{@property_flush[:image_tag]}") if @property_flush[:image_tag]
if @property_flush[:project_name]
opts << "--project=#{@property_flush[:project_name]}"
elsif @property_flush[:project_id]
opts << "--project=#{@property_flush[:project_id]}"
end
self.class.request('image', 'set', opts)
@property_flush.clear
end

View File

@ -90,6 +90,14 @@ Puppet::Type.newtype(:glance_image) do
newvalues(/\d+/)
end
newparam(:project_name) do
desc 'The name of the project which will own the image.'
end
newproperty(:project_id) do
desc 'A uuid identifying the project which will own the image.'
end
newproperty(:properties) do
desc "The set of image properties"
@ -118,4 +126,16 @@ Puppet::Type.newtype(:glance_image) do
['glance::service::end']
end
validate do
if self[:ensure] != :present
return
end
if self[:project_id] && self[:project_name]
raise(Puppet::Error, <<-EOT
Please provide a value for only one of project_name and project_id
EOT
)
end
end
end

View File

@ -0,0 +1,9 @@
---
features:
- |
The ``glance_image`` resource type now supports the following two new
parameters, to define owner of the image. Note these parameters are
mutually exclusive, and only one can be used.
- ``project_id``
- ``project_name``

View File

@ -40,8 +40,11 @@ describe provider_class 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', '--min-disk=1024', '--min-ram=1024', '--file=/var/tmp/image1.img'])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
.with('image', 'create', '--format', 'shell',
['image1', '--public', '--container-format=bare',
'--disk-format=qcow2', '--min-disk=1024', '--min-ram=1024',
'--file=/var/tmp/image1.img'])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
container_format="bare"
created_at="2016-03-29T20:52:24Z"
disk_format="qcow2"
@ -108,13 +111,14 @@ visibility="public"
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"
.with('image', 'list', '--quiet', '--format', 'csv', '--long')
.returns('"ID","Name","Disk Format","Container Format","Size","Status"
"5345b502-efe4-4852-a45d-edaba3a3acc6","image1","raw","bare",1270,"active"
')
provider.class.stubs(:openstack)
.with('image', 'show', '--format', 'shell', '5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
.with('image', 'show', '--format', 'shell',
'5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
container_format="bare"
created_at="2015-04-08T18:28:01"
deleted="False"
@ -169,8 +173,13 @@ virtual_size="None"
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', '--min-disk=1024', '--min-ram=1024', ['--property', 'something=what', '--property', 'vmware_disktype=sparse'], '--file=/var/tmp/image1.img' ])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
.with('image', 'create', '--format', 'shell',
['image1', '--public', '--container-format=bare',
'--disk-format=qcow2', '--min-disk=1024', '--min-ram=1024',
['--property', 'something=what', '--property',
'vmware_disktype=sparse'],
'--file=/var/tmp/image1.img'])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
container_format="bare"
created_at="2016-03-29T20:52:24Z"
disk_format="qcow2"
@ -199,13 +208,13 @@ visibility="public"
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"
.with('image', 'list', '--quiet', '--format', 'csv', '--long')
.returns('"ID","Name","Disk Format","Container Format","Size","Status"
"5345b502-efe4-4852-a45d-edaba3a3acc6","image1","raw","bare",1270,"active"
')
provider.class.stubs(:openstack)
.with('image', 'show', '--format', 'shell', '5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
.with('image', 'show', '--format', 'shell', '5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
container_format="bare"
created_at="2015-04-08T18:28:01"
deleted="False"
@ -259,8 +268,12 @@ virtual_size="None"
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"
.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"
@ -289,13 +302,14 @@ visibility="public"
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"
.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"
.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"
@ -322,6 +336,64 @@ virtual_size="None"
end
describe 'when creating an image with owner' do
let(:image_attrs) do
{
:ensure => 'present',
:name => 'image1',
:is_public => 'yes',
:container_format => 'bare',
:disk_format => 'qcow2',
:source => '/var/tmp/image1.img',
:project_id => '5a9e521e17014804ab8b4e8b3de488a4'
}
end
let(:resource) do
Puppet::Type::Glance_image.new(image_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',
'--file=/var/tmp/image1.img',
'--project=5a9e521e17014804ab8b4e8b3de488a4'])
.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="os_hash_algo=\'abc123\', os_hash_value=\'test123\', os_hidden=\'true\'"
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
end
describe 'when creating image with tag' do
let(:image_attrs) do
@ -348,8 +420,11 @@ virtual_size="None"
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', '--tag=testtag', '--file=/var/tmp/image1.img' ])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
.with('image', 'create', '--format', 'shell',
['image1', '--public', '--container-format=bare',
'--disk-format=qcow2', '--tag=testtag',
'--file=/var/tmp/image1.img' ])
.returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6"
container_format="bare"
created_at="2016-03-29T20:52:24Z"
disk_format="qcow2"
@ -376,13 +451,13 @@ visibility="public"
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","Tags"
.with('image', 'list', '--quiet', '--format', 'csv', '--long')
.returns('"ID","Name","Disk Format","Container Format","Size","Status","Tags"
"5345b502-efe4-4852-a45d-edaba3a3acc6","image1","raw","bare",1270,"active","testtag"
')
provider.class.stubs(:openstack)
.with('image', 'show', '--format', 'shell', '5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
.with('image', 'show', '--format', 'shell', '5345b502-efe4-4852-a45d-edaba3a3acc6')
.returns('checksum="09b9c392dc1f6e914cea287cb6be34b0"
container_format="bare"
created_at="2015-04-08T18:28:01"
deleted="False"