# File lib/puppet/indirector/catalog/puppetdb.rb, line 134
  def add_namevar_aliases(hash, catalog)
    resources = hash['resources']
    profile("Add namevar aliases (resource count: #{resources.count})",
            [:puppetdb, :namevar_aliases, :add]) do
      resources.each do |resource|
        real_resource = catalog.resource(resource['type'], resource['title'])

        # Resources with composite namevars can't be referred to by
        # anything other than their title when declaring
        # relationships. Trying to snag the :alias for these resources
        # will only return _part_ of the name (a problem with Puppet
        # proper), so skipping the adding of aliases for these resources
        # is both an optimization and a safeguard.
        next if real_resource.key_attributes.count > 1

        aliases = [real_resource[:alias]].flatten.compact

        # Non-isomorphic resources aren't unique based on namevar, so we can't
        # use it as an alias
        type = real_resource.resource_type
        if !type.respond_to?(:isomorphic?) or type.isomorphic?
          # This makes me a little sad.  It turns out that the "to_hash" method
          #  of Puppet::Resource can have side effects.  In particular, if the
          #  resource type specifies a title_pattern, calling "to_hash" will trigger
          #  the title_pattern processing, which can have the side effect of
          #  populating the namevar (potentially with a munged value).  Thus,
          #  it is important that we search for namevar aliases in that hash
          #  rather than in the resource itself.
          real_resource_hash = real_resource.to_hash

          name = real_resource_hash[real_resource.send(:namevar)]
          unless name.nil? or real_resource.title == name or aliases.include?(name)
            aliases << name
          end
        end

        resource['parameters'][:alias] = aliases unless aliases.empty?
      end
    end

    hash
  end