From 53c0ff40176cf156b2373bc00649e1130c5bb682 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 21 Jun 2017 18:08:37 -0700 Subject: [PATCH] core: ParseResourceAddressForInstanceDiff function This is a specialized thin wrapper around parseResourceAddressInternal that can be used to obtain a ResourceAddress from the keys in ModuleDiff.Resources. This is not something we'd ideally expose, but since the internal address format is already exposed in the ModuleDiff object this ends up being necessary to process the ModuleDiff from other packages, e.g. for display in the UI. --- terraform/resource_address.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/terraform/resource_address.go b/terraform/resource_address.go index c6752d7960..8badca8053 100644 --- a/terraform/resource_address.go +++ b/terraform/resource_address.go @@ -248,6 +248,28 @@ func ParseResourceAddress(s string) (*ResourceAddress, error) { }, nil } +// ParseResourceAddressForInstanceDiff creates a ResourceAddress for a +// resource name as described in a module diff. +// +// For historical reasons a different addressing format is used in this +// context. The internal format should not be shown in the UI and instead +// this function should be used to translate to a ResourceAddress and +// then, where appropriate, use the String method to produce a canonical +// resource address string for display in the UI. +// +// The given path slice must be empty (or nil) for the root module, and +// otherwise consist of a sequence of module names traversing down into +// the module tree. If a non-nil path is provided, the caller must not +// modify its underlying array after passing it to this function. +func ParseResourceAddressForInstanceDiff(path []string, key string) (*ResourceAddress, error) { + addr, err := parseResourceAddressInternal(key) + if err != nil { + return nil, err + } + addr.Path = path + return addr, nil +} + // Contains returns true if and only if the given node is contained within // the receiver. //