Skip to content

Instantly share code, notes, and snippets.

@reidmv
Created September 17, 2012 20:26
Show Gist options
  • Save reidmv/3739568 to your computer and use it in GitHub Desktop.
Save reidmv/3739568 to your computer and use it in GitHub Desktop.
Prefetch match composite namevar(s)
def prefetch(resources = {})
# generate hash of {provider_name => provider}
providers = instances.inject({}) do |hash, instance|
hash[instance.name] = instance
hash
end
# Identify the namevar(s) for the type
nv_properties = resource_type.properties.select(&:isnamevar?).map(&:name)
nv_params = resource_type.parameters.select do |param|
resource_type.paramclass(param).isnamevar?
end
namevars = (nv_properties | nv_params || [:name])
# For each prefetched resource, try to match it to a provider
resources.each_pair do |resource_name, resource|
if provider = providers[resource_name]
resource.provider = provider
end
end
end
@crayfishx
Copy link

Checkout resource_type.key_attribute_parameters or resource_type.class.key_attribute_parameters - can't remember which, but that will give you the namevars.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment