Sha256: 09638184a4c7d8cf699e591673ae71d8b14465c6616d8c1f5bcee7c656e3ca20

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require 'puppet/indirector/resource/validator'

class Puppet::Resource::Ral < Puppet::Indirector::Code
  include Puppet::Resource::Validator

  def find( request )
    # find by name
    res   = type(request).instances.find { |o| o.name == resource_name(request) }
    res ||= type(request).new(:name => resource_name(request), :audit => type(request).properties.collect { |s| s.name })

    res.to_resource
  end

  def search( request )
    conditions = request.options.dup
    conditions[:name] = resource_name(request) if resource_name(request)

    type(request).instances.map do |res|
      res.to_resource
    end.find_all do |res|
      conditions.all? {|property, value| res.to_resource[property].to_s == value.to_s}
    end.sort do |a,b|
      a.title <=> b.title
    end
  end

  def save( request )
    # In RAL-land, to "save" means to actually try to change machine state
    res = request.instance
    ral_res = res.to_ral

    catalog = Puppet::Resource::Catalog.new
    catalog.add_resource ral_res
    transaction = catalog.apply

    [ral_res.to_resource, transaction.report]
  end

  private

  # {type,resource}_name: the resource name may contain slashes:
  # File["/etc/hosts"]. To handle, assume the type name does
  # _not_ have any slashes in it, and split only on the first.

  def type_name( request )
    request.key.split('/', 2)[0]
  end

  def resource_name( request )
    name = request.key.split('/', 2)[1]
    name unless name == ""
  end

  def type( request )
    Puppet::Type.type(type_name(request)) or raise Puppet::Error, "Could not find type #{type}"
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
puppet-2.7.26 lib/puppet/indirector/resource/ral.rb
puppet-2.7.25 lib/puppet/indirector/resource/ral.rb
puppet-2.7.24 lib/puppet/indirector/resource/ral.rb
puppet-2.7.23 lib/puppet/indirector/resource/ral.rb
puppet-2.7.22 lib/puppet/indirector/resource/ral.rb
puppet-2.7.21 lib/puppet/indirector/resource/ral.rb