From 896f4cebbd835ceaf6dfab7b69e050aca32b1cec Mon Sep 17 00:00:00 2001 From: Daniel Pawlik Date: Wed, 12 Sep 2018 11:46:21 +0000 Subject: [PATCH] Added tag property for glance_image provider class Change-Id: I1772a231c6e3c8ece7f13ca6935d38e3818d2990 --- lib/puppet/provider/glance_image/openstack.rb | 9 +- lib/puppet/type/glance_image.rb | 6 ++ spec/unit/provider/glance_image_spec.rb | 86 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/glance_image/openstack.rb b/lib/puppet/provider/glance_image/openstack.rb index 7678ba31..76bb3884 100644 --- a/lib/puppet/provider/glance_image/openstack.rb +++ b/lib/puppet/provider/glance_image/openstack.rb @@ -71,6 +71,7 @@ Puppet::Type.type(:glance_image).provide( 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 << "--tag=#{@resource[:image_tag]}" if @resource[:image_tag] opts << location begin @@ -122,6 +123,10 @@ Puppet::Type.type(:glance_image).provide( @property_flush[:properties] = value end + def image_tag=(value) + @property_flush[:image_tag] = value + end + def id=(id) fail('id for existing images can not be modified') end @@ -140,7 +145,8 @@ Puppet::Type.type(:glance_image).provide( :disk_format => attrs[:disk_format], :min_disk => attrs[:min_disk], :min_ram => attrs[:min_ram], - :properties => exclude_readonly_props(properties) + :properties => exclude_readonly_props(properties), + :image_tag => attrs[:image_tag] ) end end @@ -165,6 +171,7 @@ Puppet::Type.type(:glance_image).provide( (opts << "--min-ram=#{@property_flush[:min_ram]}") if @property_flush[:min_ram] (opts << "--min-disk=#{@property_flush[:min_disk]}") if @property_flush[:min_disk] (opts << props_to_s(@property_flush[:properties])) if @property_flush[:properties] + (opts << "--tag=#{@property_flush[:image_tag]}") if @property_flush[:image_tag] self.class.request('image', 'set', opts) @property_flush.clear diff --git a/lib/puppet/type/glance_image.rb b/lib/puppet/type/glance_image.rb index 3f57eaab..9798f5de 100644 --- a/lib/puppet/type/glance_image.rb +++ b/lib/puppet/type/glance_image.rb @@ -15,6 +15,7 @@ Puppet::Type.newtype(:glance_image) do min_ram => 1234, min_disk => 1234, properties => { 'img_key' => img_value }, + image_tag => 'amphora', } Known problems / limitations: @@ -107,6 +108,11 @@ Puppet::Type.newtype(:glance_image) do end end + newproperty(:image_tag) do + desc "The image tag" + newvalues(/\S+/) + end + # Require the Glance service to be running autorequire(:service) do ['glance-api', 'glance-registry'] diff --git a/spec/unit/provider/glance_image_spec.rb b/spec/unit/provider/glance_image_spec.rb index 586891f1..dc688b2b 100644 --- a/spec/unit/provider/glance_image_spec.rb +++ b/spec/unit/provider/glance_image_spec.rb @@ -290,4 +290,90 @@ virtual_size="None" end + describe 'when creating image with tag' do + + let(:tenant_attrs) do + { + :ensure => 'present', + :name => 'image1', + :is_public => 'yes', + :container_format => 'bare', + :disk_format => 'qcow2', + :source => '/var/tmp/image1.img', + :image_tag => 'testtag', + } + 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', '--tag=testtag', '--file=/var/tmp/image1.img' ]) + .returns('checksum="ee1eca47dc88f4879d8a229cc70a07c6" +container_format="bare" +created_at="2016-03-29T20:52:24Z" +disk_format="qcow2" +file="/v2/images/8801c5b0-c505-4a15-8ca3-1d2383f8c015/file" +id="8801c5b0-c505-4a15-8ca3-1d2383f8c015" +name="image1" +owner="5a9e521e17014804ab8b4e8b3de488a4" +tag="testtag" +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","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" +container_format="bare" +created_at="2015-04-08T18:28:01" +deleted="False" +deleted_at="None" +disk_format="qcow2" +id="5345b502-efe4-4852-a45d-edaba3a3acc6" +visibility="public" +name="image1" +owner="None" +tag="testtag" +tags="" +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) +# expect(instances[0].image_tag).to eq('testtag') + end + end + + end + end