]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform/addrs/resource.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / addrs / resource.go
1 package addrs
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 // Resource is an address for a resource block within configuration, which
9 // contains potentially-multiple resource instances if that configuration
10 // block uses "count" or "for_each".
11 type Resource struct {
12 referenceable
13 Mode ResourceMode
14 Type string
15 Name string
16 }
17
18 func (r Resource) String() string {
19 switch r.Mode {
20 case ManagedResourceMode:
21 return fmt.Sprintf("%s.%s", r.Type, r.Name)
22 case DataResourceMode:
23 return fmt.Sprintf("data.%s.%s", r.Type, r.Name)
24 default:
25 // Should never happen, but we'll return a string here rather than
26 // crashing just in case it does.
27 return fmt.Sprintf("<invalid>.%s.%s", r.Type, r.Name)
28 }
29 }
30
31 func (r Resource) Equal(o Resource) bool {
32 return r.String() == o.String()
33 }
34
35 // Instance produces the address for a specific instance of the receiver
36 // that is idenfied by the given key.
37 func (r Resource) Instance(key InstanceKey) ResourceInstance {
38 return ResourceInstance{
39 Resource: r,
40 Key: key,
41 }
42 }
43
44 // Absolute returns an AbsResource from the receiver and the given module
45 // instance address.
46 func (r Resource) Absolute(module ModuleInstance) AbsResource {
47 return AbsResource{
48 Module: module,
49 Resource: r,
50 }
51 }
52
53 // DefaultProviderConfig returns the address of the provider configuration
54 // that should be used for the resource identified by the reciever if it
55 // does not have a provider configuration address explicitly set in
56 // configuration.
57 //
58 // This method is not able to verify that such a configuration exists, nor
59 // represent the behavior of automatically inheriting certain provider
60 // configurations from parent modules. It just does a static analysis of the
61 // receiving address and returns an address to start from, relative to the
62 // same module that contains the resource.
63 func (r Resource) DefaultProviderConfig() ProviderConfig {
64 typeName := r.Type
65 if under := strings.Index(typeName, "_"); under != -1 {
66 typeName = typeName[:under]
67 }
68 return ProviderConfig{
69 Type: typeName,
70 }
71 }
72
73 // ResourceInstance is an address for a specific instance of a resource.
74 // When a resource is defined in configuration with "count" or "for_each" it
75 // produces zero or more instances, which can be addressed using this type.
76 type ResourceInstance struct {
77 referenceable
78 Resource Resource
79 Key InstanceKey
80 }
81
82 func (r ResourceInstance) ContainingResource() Resource {
83 return r.Resource
84 }
85
86 func (r ResourceInstance) String() string {
87 if r.Key == NoKey {
88 return r.Resource.String()
89 }
90 return r.Resource.String() + r.Key.String()
91 }
92
93 func (r ResourceInstance) Equal(o ResourceInstance) bool {
94 return r.String() == o.String()
95 }
96
97 // Absolute returns an AbsResourceInstance from the receiver and the given module
98 // instance address.
99 func (r ResourceInstance) Absolute(module ModuleInstance) AbsResourceInstance {
100 return AbsResourceInstance{
101 Module: module,
102 Resource: r,
103 }
104 }
105
106 // AbsResource is an absolute address for a resource under a given module path.
107 type AbsResource struct {
108 targetable
109 Module ModuleInstance
110 Resource Resource
111 }
112
113 // Resource returns the address of a particular resource within the receiver.
114 func (m ModuleInstance) Resource(mode ResourceMode, typeName string, name string) AbsResource {
115 return AbsResource{
116 Module: m,
117 Resource: Resource{
118 Mode: mode,
119 Type: typeName,
120 Name: name,
121 },
122 }
123 }
124
125 // Instance produces the address for a specific instance of the receiver
126 // that is idenfied by the given key.
127 func (r AbsResource) Instance(key InstanceKey) AbsResourceInstance {
128 return AbsResourceInstance{
129 Module: r.Module,
130 Resource: r.Resource.Instance(key),
131 }
132 }
133
134 // TargetContains implements Targetable by returning true if the given other
135 // address is either equal to the receiver or is an instance of the
136 // receiver.
137 func (r AbsResource) TargetContains(other Targetable) bool {
138 switch to := other.(type) {
139
140 case AbsResource:
141 // We'll use our stringification as a cheat-ish way to test for equality.
142 return to.String() == r.String()
143
144 case AbsResourceInstance:
145 return r.TargetContains(to.ContainingResource())
146
147 default:
148 return false
149
150 }
151 }
152
153 func (r AbsResource) String() string {
154 if len(r.Module) == 0 {
155 return r.Resource.String()
156 }
157 return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String())
158 }
159
160 func (r AbsResource) Equal(o AbsResource) bool {
161 return r.String() == o.String()
162 }
163
164 // AbsResourceInstance is an absolute address for a resource instance under a
165 // given module path.
166 type AbsResourceInstance struct {
167 targetable
168 Module ModuleInstance
169 Resource ResourceInstance
170 }
171
172 // ResourceInstance returns the address of a particular resource instance within the receiver.
173 func (m ModuleInstance) ResourceInstance(mode ResourceMode, typeName string, name string, key InstanceKey) AbsResourceInstance {
174 return AbsResourceInstance{
175 Module: m,
176 Resource: ResourceInstance{
177 Resource: Resource{
178 Mode: mode,
179 Type: typeName,
180 Name: name,
181 },
182 Key: key,
183 },
184 }
185 }
186
187 // ContainingResource returns the address of the resource that contains the
188 // receving resource instance. In other words, it discards the key portion
189 // of the address to produce an AbsResource value.
190 func (r AbsResourceInstance) ContainingResource() AbsResource {
191 return AbsResource{
192 Module: r.Module,
193 Resource: r.Resource.ContainingResource(),
194 }
195 }
196
197 // TargetContains implements Targetable by returning true if the given other
198 // address is equal to the receiver.
199 func (r AbsResourceInstance) TargetContains(other Targetable) bool {
200 switch to := other.(type) {
201
202 case AbsResourceInstance:
203 // We'll use our stringification as a cheat-ish way to test for equality.
204 return to.String() == r.String()
205
206 default:
207 return false
208
209 }
210 }
211
212 func (r AbsResourceInstance) String() string {
213 if len(r.Module) == 0 {
214 return r.Resource.String()
215 }
216 return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String())
217 }
218
219 func (r AbsResourceInstance) Equal(o AbsResourceInstance) bool {
220 return r.String() == o.String()
221 }
222
223 // Less returns true if the receiver should sort before the given other value
224 // in a sorted list of addresses.
225 func (r AbsResourceInstance) Less(o AbsResourceInstance) bool {
226 switch {
227
228 case len(r.Module) != len(o.Module):
229 return len(r.Module) < len(o.Module)
230
231 case r.Module.String() != o.Module.String():
232 return r.Module.Less(o.Module)
233
234 case r.Resource.Resource.Mode != o.Resource.Resource.Mode:
235 return r.Resource.Resource.Mode == DataResourceMode
236
237 case r.Resource.Resource.Type != o.Resource.Resource.Type:
238 return r.Resource.Resource.Type < o.Resource.Resource.Type
239
240 case r.Resource.Resource.Name != o.Resource.Resource.Name:
241 return r.Resource.Resource.Name < o.Resource.Resource.Name
242
243 case r.Resource.Key != o.Resource.Key:
244 return InstanceKeyLess(r.Resource.Key, o.Resource.Key)
245
246 default:
247 return false
248
249 }
250 }
251
252 // ResourceMode defines which lifecycle applies to a given resource. Each
253 // resource lifecycle has a slightly different address format.
254 type ResourceMode rune
255
256 //go:generate stringer -type ResourceMode
257
258 const (
259 // InvalidResourceMode is the zero value of ResourceMode and is not
260 // a valid resource mode.
261 InvalidResourceMode ResourceMode = 0
262
263 // ManagedResourceMode indicates a managed resource, as defined by
264 // "resource" blocks in configuration.
265 ManagedResourceMode ResourceMode = 'M'
266
267 // DataResourceMode indicates a data resource, as defined by
268 // "data" blocks in configuration.
269 DataResourceMode ResourceMode = 'D'
270 )