]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/resource.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform-config-inspect / tfconfig / resource.go
1 package tfconfig
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9 // Resource represents a single "resource" or "data" block within a module.
10 type 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.
22 func (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).
37 type ResourceMode rune
38
39 const InvalidResourceMode ResourceMode = 0
40 const ManagedResourceMode ResourceMode = 'M'
41 const DataResourceMode ResourceMode = 'D'
42
43 func (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.
55 func (m ResourceMode) MarshalJSON() ([]byte, error) {
56 return []byte(strconv.Quote(m.String())), nil
57 }
58
59 func resourceTypeDefaultProviderName(typeName string) string {
60 if underPos := strings.IndexByte(typeName, '_'); underPos != -1 {
61 return typeName[:underPos]
62 }
63 return typeName
64 }