github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/json/resource_addr.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package json
     5  
     6  import (
     7  	"github.com/zclconf/go-cty/cty"
     8  	ctyjson "github.com/zclconf/go-cty/cty/json"
     9  
    10  	"github.com/terramate-io/tf/addrs"
    11  )
    12  
    13  type ResourceAddr struct {
    14  	Addr            string                  `json:"addr"`
    15  	Module          string                  `json:"module"`
    16  	Resource        string                  `json:"resource"`
    17  	ImpliedProvider string                  `json:"implied_provider"`
    18  	ResourceType    string                  `json:"resource_type"`
    19  	ResourceName    string                  `json:"resource_name"`
    20  	ResourceKey     ctyjson.SimpleJSONValue `json:"resource_key"`
    21  }
    22  
    23  func newResourceAddr(addr addrs.AbsResourceInstance) ResourceAddr {
    24  	resourceKey := ctyjson.SimpleJSONValue{Value: cty.NilVal}
    25  	if addr.Resource.Key != nil {
    26  		resourceKey.Value = addr.Resource.Key.Value()
    27  	}
    28  	return ResourceAddr{
    29  		Addr:            addr.String(),
    30  		Module:          addr.Module.String(),
    31  		Resource:        addr.Resource.String(),
    32  		ImpliedProvider: addr.Resource.Resource.ImpliedProvider(),
    33  		ResourceType:    addr.Resource.Resource.Type,
    34  		ResourceName:    addr.Resource.Resource.Name,
    35  		ResourceKey:     resourceKey,
    36  	}
    37  }