puppet-glance/lib/puppet/provider/glance_image/openstack.rb
Colleen Murphy 39b58df7a8 Use OpenstackClient for glance_image auth
This patch changes the glance_image provider to use
puppet-openstacklib's authentication methods, which use
python-openstackclient as an interface, instead of the glance command
line client. The benefits of this is:

- Code reduction. This patch reduces the amount of code in the
  glance parent provider and glance_image provider by reusing code from
  Puppet::Provider::Openstack instead of implementing authentication,
  retries, and response parsing in the provider.
- Presumed reduction in sudden API changes that require quick fixes in
  this module, such as f377c0229c.
- Ability to set project-based access control for images

Additional reasoning for this change is in the blueprint.

Important note: this change does not work on Ubuntu under Juno due to a
major bug in the version of python-openstackclient shipped in UCA [1].
This change targets the Kilo releases.

Note about performance: the self.instances and instances methods make
API requests for each image found in the list of instances. This is not
a change from how this was implemented before. The --long formatted
list, either from the glance client or openstackclient, does not
provide all information needed to query an instance and populate
setters. Future patches could possibly improve on this.

Other details of this change:

- Removes auth_glance method, replaced by the request and
  glance_request methods
- Removes auth_glance_stdin method which was not being used
- Removes parse_table which is now unnecessary because openstackclient
  formats its responses as CSV and Puppet::Provider::Openstack#request
  returns the results a hash
- Removes remove_warnings methods which are handled by
  Puppet::Provider::Openstack#request
- Removes list_glance_images and get_glance_image_attr which are
  sufficiently replaced by using the 'list' and 'show' commands with
  Puppet::Provider::Openstack's request method.
- Removed self.prefetch since we can't populate auth parameters during
  a prefetch. Instead we prepopulate the list via the instances method.
- Added a flush method to do updates more efficiently
- Changed is_public property to accept true/false in addition to yes/no
  and to munge to a symbol instead of a capitalized string, for
  consistency with keystone_tenant's enabled property
- Move the reset method into the spec tests since it is only necessary
  for testing
- Added tests for glance_image, which did not exist before
- Removed connection retry test since this is taken care of in
  openstacklib

[1] https://bugs.launchpad.net/python-openstackclient/+bug/1269821

blueprint use-openstackclient-in-module-resources

Change-Id: Iceab5e1c6138e7aba0f883ed4acb8f7ecbcd2830
2015-06-29 17:35:23 -07:00

125 lines
3.3 KiB
Ruby

require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/glance')
Puppet::Type.type(:glance_image).provide(
:openstack,
:parent => Puppet::Provider::Glance
) do
desc <<-EOT
Provider to manage glance_image type.
EOT
@credentials = Puppet::Provider::Openstack::CredentialsV2_0.new
def initialize(value={})
super(value)
@property_flush = {}
end
def create
if resource[:source]
# copy_from cannot handle file://
if resource[:source] =~ /^\// # local file
location = "--file=#{resource[:source]}"
else
location = "--copy-from=#{resource[:source]}"
end
# location cannot handle file://
# location does not import, so no sense in doing anything more than this
elsif resource[:location]
location = "--location=#{resource[:location]}"
else
raise(Puppet::Error, "Must specify either source or location")
end
properties = [resource[:name]]
if resource[:is_public] == :true
properties << "--public"
else
# This is the default, but it's nice to be verbose
properties << "--private"
end
properties << "--container-format=#{resource[:container_format]}"
properties << "--disk-format=#{resource[:disk_format]}"
properties << location
@property_hash = self.class.request('image', 'create', properties)
@property_hash[:ensure] = :present
end
def exists?
@property_hash[:ensure] == :present
end
def destroy
self.class.request('image', 'delete', resource[:name])
@property_hash.clear
end
def is_public=(value)
@property_flush[:is_public] = value
end
def is_public
bool_to_sym(@property_hash[:is_public])
end
def disk_format=(value)
@property_flush[:disk_format] = value
end
def disk_format
@property_hash[:disk_format]
end
def container_format=(value)
@property_flush[:container_format] = value
end
def container_format
@property_hash[:container_format]
end
def id=(id)
fail('id is read only')
end
def id
@property_hash[:id]
end
def self.instances
list = request('image', 'list', '--long')
list.collect do |image|
attrs = request('image', 'show', image[:id])
new(
:ensure => :present,
:name => attrs[:name],
:is_public => attrs[:is_public].downcase.chomp == 'true'? true : false,
:container_format => attrs[:container_format],
:id => attrs[:id],
:disk_format => attrs[:disk_format]
)
end
end
def self.prefetch(resources)
images = instances
resources.keys.each do |name|
if provider = images.find{ |image| image.name == name }
resources[name].provider = provider
end
end
end
def flush
properties = [resource[:name]]
if @property_flush
(properties << '--public') if @property_flush[:is_public] == :true
(properties << '--private') if @property_flush[:is_public] == :false
(properties << "--container-format=#{@property_flush[:container_format]}") if @property_flush[:container_format]
(properties << "--disk-format=#{@property_flush[:disk_format]}") if @property_flush[:disk_format]
self.class.request('image', 'set', properties)
@property_flush.clear
end
end
end