aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go')
-rw-r--r--vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go b/vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go
new file mode 100644
index 0000000..401c8fc
--- /dev/null
+++ b/vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go
@@ -0,0 +1,64 @@
1package tfconfig
2
3import (
4 "fmt"
5 "strconv"
6 "strings"
7)
8
9// Resource represents a single "resource" or "data" block within a module.
10type Resource struct {
11 Mode ResourceMode `json:"mode"`
12 Type string `json:"type"`
13 Name string `json:"name"`
14
15 Provider ProviderRef `json:"provider"`
16
17 Pos SourcePos `json:"pos"`
18}
19
20// MapKey returns a string that can be used to uniquely identify the receiver
21// in a map[string]*Resource.
22func (r *Resource) MapKey() string {
23 switch r.Mode {
24 case ManagedResourceMode:
25 return fmt.Sprintf("%s.%s", r.Type, r.Name)
26 case DataResourceMode:
27 return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
28 default:
29 // should never happen
30 return fmt.Sprintf("[invalid_mode!].%s.%s", r.Type, r.Name)
31 }
32}
33
34// ResourceMode represents the "mode" of a resource, which is used to
35// distinguish between managed resources ("resource" blocks in config) and
36// data resources ("data" blocks in config).
37type ResourceMode rune
38
39const InvalidResourceMode ResourceMode = 0
40const ManagedResourceMode ResourceMode = 'M'
41const DataResourceMode ResourceMode = 'D'
42
43func (m ResourceMode) String() string {
44 switch m {
45 case ManagedResourceMode:
46 return "managed"
47 case DataResourceMode:
48 return "data"
49 default:
50 return ""
51 }
52}
53
54// MarshalJSON implements encoding/json.Marshaler.
55func (m ResourceMode) MarshalJSON() ([]byte, error) {
56 return []byte(strconv.Quote(m.String())), nil
57}
58
59func resourceTypeDefaultProviderName(typeName string) string {
60 if underPos := strings.IndexByte(typeName, '_'); underPos != -1 {
61 return typeName[:underPos]
62 }
63 return typeName
64}