mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
configs: Helper methods to integrate with "addrs" package
Our new "addrs" package gives us some nice representations of various kinds of "address" within Terraform. To talk to APIs that use these, it's convenient to be able to easily derive such addresses from the configuration objects. These new methods, along with a recasting of the existing Resource.ProviderConfigKey method to Resource.ProviderConfigAddr, give us some key integration points to support the configuration graph transforms in the main "terraform" package.
This commit is contained in:
parent
072322336e
commit
24dce0c624
@ -9,6 +9,8 @@ import (
|
||||
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/convert"
|
||||
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
)
|
||||
|
||||
// A consistent detail message for all "not a valid identifier" diagnostics.
|
||||
@ -311,6 +313,14 @@ func decodeLocalsBlock(block *hcl.Block) ([]*Local, hcl.Diagnostics) {
|
||||
return locals, diags
|
||||
}
|
||||
|
||||
// Addr returns the address of the local value declared by the receiver,
|
||||
// relative to its containing module.
|
||||
func (l *Local) Addr() addrs.LocalValue {
|
||||
return addrs.LocalValue{
|
||||
Name: l.Name,
|
||||
}
|
||||
}
|
||||
|
||||
var variableBlockSchema = &hcl.BodySchema{
|
||||
Attributes: []hcl.AttributeSchema{
|
||||
{
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"github.com/hashicorp/hcl2/gohcl"
|
||||
"github.com/hashicorp/hcl2/hcl"
|
||||
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
||||
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
)
|
||||
|
||||
// Provider represents a "provider" block in a module or file. A provider
|
||||
@ -57,6 +59,15 @@ func decodeProviderBlock(block *hcl.Block) (*Provider, hcl.Diagnostics) {
|
||||
return provider, diags
|
||||
}
|
||||
|
||||
// Addr returns the address of the receiving provider configuration, relative
|
||||
// to its containing module.
|
||||
func (p *Provider) Addr() addrs.ProviderConfig {
|
||||
return addrs.ProviderConfig{
|
||||
Type: p.Name,
|
||||
Alias: p.Alias,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Provider) moduleUniqueKey() string {
|
||||
if p.Alias != "" {
|
||||
return fmt.Sprintf("%s.%s", p.Name, p.Alias)
|
||||
|
@ -2,13 +2,12 @@ package configs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
|
||||
"github.com/hashicorp/hcl2/gohcl"
|
||||
"github.com/hashicorp/hcl2/hcl"
|
||||
"github.com/hashicorp/hcl2/hcl/hclsyntax"
|
||||
|
||||
"github.com/hashicorp/terraform/addrs"
|
||||
)
|
||||
|
||||
// Resource represents a "resource" or "data" block in a module or file.
|
||||
@ -58,20 +57,29 @@ func (r *Resource) moduleUniqueKey() string {
|
||||
}
|
||||
}
|
||||
|
||||
// ProviderConfigKey returns a string key for the provider configuration
|
||||
// Addr returns a resource address for the receiver that is relative to the
|
||||
// resource's containing module.
|
||||
func (r *Resource) Addr() addrs.Resource {
|
||||
return addrs.Resource{
|
||||
Mode: r.Mode,
|
||||
Type: r.Type,
|
||||
Name: r.Name,
|
||||
}
|
||||
}
|
||||
|
||||
// ProviderConfigAddr returns the address for the provider configuration
|
||||
// that should be used for this resource. This function implements the
|
||||
// default behavior of extracting the type from the resource type name if
|
||||
// an explicit "provider" argument was not provided.
|
||||
func (r *Resource) ProviderConfigKey() string {
|
||||
func (r *Resource) ProviderConfigAddr() addrs.ProviderConfig {
|
||||
if r.ProviderConfigRef == nil {
|
||||
typeName := r.Type
|
||||
if under := strings.Index(typeName, "_"); under != -1 {
|
||||
return typeName[:under]
|
||||
}
|
||||
return typeName
|
||||
return r.Addr().DefaultProviderConfig()
|
||||
}
|
||||
|
||||
return r.ProviderConfigRef.String()
|
||||
return addrs.ProviderConfig{
|
||||
Type: r.ProviderConfigRef.Name,
|
||||
Alias: r.ProviderConfigRef.Alias,
|
||||
}
|
||||
}
|
||||
|
||||
func decodeResourceBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) {
|
||||
@ -389,6 +397,18 @@ func decodeProviderConfigRef(expr hcl.Expression, argName string) (*ProviderConf
|
||||
return ret, diags
|
||||
}
|
||||
|
||||
// Addr returns the provider config address corresponding to the receiving
|
||||
// config reference.
|
||||
//
|
||||
// This is a trivial conversion, essentially just discarding the source
|
||||
// location information and keeping just the addressing information.
|
||||
func (r *ProviderConfigRef) Addr() addrs.ProviderConfig {
|
||||
return addrs.ProviderConfig{
|
||||
Type: r.Name,
|
||||
Alias: r.Alias,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProviderConfigRef) String() string {
|
||||
if r == nil {
|
||||
return "<nil>"
|
||||
|
Loading…
Reference in New Issue
Block a user