github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/terraform/addrs/resource.go (about) 1 package addrs 2 3 import ( 4 "fmt" 5 ) 6 7 // Resource is an address for a resource block within configuration, which 8 // contains potentially-multiple resource instances if that configuration 9 // block uses "count" or "for_each". 10 type Resource struct { 11 referenceable 12 Mode ResourceMode 13 Type string 14 Name string 15 } 16 17 func (r Resource) String() string { 18 switch r.Mode { 19 case ManagedResourceMode: 20 return fmt.Sprintf("%s.%s", r.Type, r.Name) 21 case DataResourceMode: 22 return fmt.Sprintf("data.%s.%s", r.Type, r.Name) 23 default: 24 // Should never happen, but we'll return a string here rather than 25 // crashing just in case it does. 26 return fmt.Sprintf("<invalid>.%s.%s", r.Type, r.Name) 27 } 28 } 29 30 // ResourceInstance is an address for a specific instance of a resource. 31 // When a resource is defined in configuration with "count" or "for_each" it 32 // produces zero or more instances, which can be addressed using this type. 33 type ResourceInstance struct { 34 referenceable 35 Resource Resource 36 Key InstanceKey 37 } 38 39 func (r ResourceInstance) String() string { 40 if r.Key == NoKey { 41 return r.Resource.String() 42 } 43 return r.Resource.String() + r.Key.String() 44 } 45 46 // ResourceMode defines which lifecycle applies to a given resource. Each 47 // resource lifecycle has a slightly different address format. 48 type ResourceMode rune 49 50 //go:generate go run golang.org/x/tools/cmd/stringer -type ResourceMode 51 52 const ( 53 // InvalidResourceMode is the zero value of ResourceMode and is not 54 // a valid resource mode. 55 InvalidResourceMode ResourceMode = 0 56 57 // ManagedResourceMode indicates a managed resource, as defined by 58 // "resource" blocks in configuration. 59 ManagedResourceMode ResourceMode = 'M' 60 61 // DataResourceMode indicates a data resource, as defined by 62 // "data" blocks in configuration. 63 DataResourceMode ResourceMode = 'D' 64 )