voxpupuli / puppet-filemapper

Map files to puppet resources and back
https://forge.puppet.com/puppet/filemapper
Apache License 2.0
14 stars 24 forks source link

support composite namevars #58

Open ChetHosey opened 6 years ago

ChetHosey commented 6 years ago

Composite namevars aren't currently supported, as the current prefetch method only uses the value of the first key_attribute to match providers and catalog entries.

A few changes may be needed to support composite namevars:

ChetHosey commented 6 years ago

Unfortunately I don't have a PR at this point, but did put together a demo provider to test these ideas.

Key sections:

def create
  @resource.class.parameters.each do |attr|
    attr = attr.to_sym
    next if attr == :name
    @property_hash[attr] = @resource[attr]
  end

  super
end

def self.prefetch(resources = {})
  providers = instances.each_with_object({}) do |instance, hash|
    hash[instance.uniqueness_key] = instance
  end

  catalog = resources.values.first.catalog
  catalog_entries = catalog.vertices.select do |resource|
    resource.provider.class == self
  end

  catalog_entries.each do |resource|
    resource.provider = providers[resource.uniqueness_key] if providers[resource.uniqueness_key]
  end
end

def uniqueness_key
  self.class.resource_type.key_attributes.sort_by { |attribute_name| attribute_name.to_s }.map { |attribute_name| send attribute_name }
end

See the linked provider file for comments and attribution.