10 "github.com/hashicorp/go-multierror"
11 "github.com/hashicorp/terraform/terraform"
14 // Provider represents a resource provider in Terraform, and properly
15 // implements all of the ResourceProvider API.
17 // By defining a schema for the configuration of the provider, the
18 // map of supporting resources, and a configuration function, the schema
19 // framework takes over and handles all the provider operations for you.
21 // After defining the provider structure, it is unlikely that you'll require any
22 // of the methods on Provider itself.
23 type Provider struct {
24 // Schema is the schema for the configuration of this provider. If this
25 // provider has no configuration, this can be omitted.
27 // The keys of this map are the configuration keys, and the value is
28 // the schema describing the value of the configuration.
29 Schema map[string]*Schema
31 // ResourcesMap is the list of available resources that this provider
32 // can manage, along with their Resource structure defining their
33 // own schemas and CRUD operations.
35 // Provider automatically handles routing operations such as Apply,
36 // Diff, etc. to the proper resource.
37 ResourcesMap map[string]*Resource
39 // DataSourcesMap is the collection of available data sources that
40 // this provider implements, with a Resource instance defining
41 // the schema and Read operation of each.
43 // Resource instances for data sources must have a Read function
44 // and must *not* implement Create, Update or Delete.
45 DataSourcesMap map[string]*Resource
47 // ConfigureFunc is a function for configuring the provider. If the
48 // provider doesn't need to be configured, this can be omitted.
50 // See the ConfigureFunc documentation for more information.
51 ConfigureFunc ConfigureFunc
53 // MetaReset is called by TestReset to reset any state stored in the meta
54 // interface. This is especially important if the StopContext is stored by
56 MetaReset func() error
60 // a mutex is required because TestReset can directly repalce the stopCtx
62 stopCtx context.Context
63 stopCtxCancel context.CancelFunc
67 // ConfigureFunc is the function used to configure a Provider.
69 // The interface{} value returned by this function is stored and passed into
70 // the subsequent resources as the meta parameter. This return value is
71 // usually used to pass along a configured API client, a configuration
73 type ConfigureFunc func(*ResourceData) (interface{}, error)
75 // InternalValidate should be called to validate the structure
78 // This should be called in a unit test for any provider to verify
79 // before release that a provider is properly configured for use with
81 func (p *Provider) InternalValidate() error {
83 return errors.New("provider is nil")
86 var validationErrors error
87 sm := schemaMap(p.Schema)
88 if err := sm.InternalValidate(sm); err != nil {
89 validationErrors = multierror.Append(validationErrors, err)
92 for k, r := range p.ResourcesMap {
93 if err := r.InternalValidate(nil, true); err != nil {
94 validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
98 for k, r := range p.DataSourcesMap {
99 if err := r.InternalValidate(nil, false); err != nil {
100 validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
104 return validationErrors
107 // Meta returns the metadata associated with this provider that was
108 // returned by the Configure call. It will be nil until Configure is called.
109 func (p *Provider) Meta() interface{} {
113 // SetMeta can be used to forcefully set the Meta object of the provider.
114 // Note that if Configure is called the return value will override anything
116 func (p *Provider) SetMeta(v interface{}) {
120 // Stopped reports whether the provider has been stopped or not.
121 func (p *Provider) Stopped() bool {
122 ctx := p.StopContext()
131 // StopCh returns a channel that is closed once the provider is stopped.
132 func (p *Provider) StopContext() context.Context {
133 p.stopOnce.Do(p.stopInit)
136 defer p.stopMu.Unlock()
141 func (p *Provider) stopInit() {
143 defer p.stopMu.Unlock()
145 p.stopCtx, p.stopCtxCancel = context.WithCancel(context.Background())
148 // Stop implementation of terraform.ResourceProvider interface.
149 func (p *Provider) Stop() error {
150 p.stopOnce.Do(p.stopInit)
153 defer p.stopMu.Unlock()
159 // TestReset resets any state stored in the Provider, and will call TestReset
160 // on Meta if it implements the TestProvider interface.
161 // This may be used to reset the schema.Provider at the start of a test, and is
162 // automatically called by resource.Test.
163 func (p *Provider) TestReset() error {
165 if p.MetaReset != nil {
171 // Input implementation of terraform.ResourceProvider interface.
172 func (p *Provider) Input(
173 input terraform.UIInput,
174 c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
175 return schemaMap(p.Schema).Input(input, c)
178 // Validate implementation of terraform.ResourceProvider interface.
179 func (p *Provider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
180 if err := p.InternalValidate(); err != nil {
181 return nil, []error{fmt.Errorf(
182 "Internal validation of the provider failed! This is always a bug\n"+
183 "with the provider itself, and not a user issue. Please report\n"+
184 "this bug:\n\n%s", err)}
187 return schemaMap(p.Schema).Validate(c)
190 // ValidateResource implementation of terraform.ResourceProvider interface.
191 func (p *Provider) ValidateResource(
192 t string, c *terraform.ResourceConfig) ([]string, []error) {
193 r, ok := p.ResourcesMap[t]
195 return nil, []error{fmt.Errorf(
196 "Provider doesn't support resource: %s", t)}
202 // Configure implementation of terraform.ResourceProvider interface.
203 func (p *Provider) Configure(c *terraform.ResourceConfig) error {
205 if p.ConfigureFunc == nil {
209 sm := schemaMap(p.Schema)
211 // Get a ResourceData for this configuration. To do this, we actually
212 // generate an intermediary "diff" although that is never exposed.
213 diff, err := sm.Diff(nil, c)
218 data, err := sm.Data(nil, diff)
223 meta, err := p.ConfigureFunc(data)
232 // Apply implementation of terraform.ResourceProvider interface.
233 func (p *Provider) Apply(
234 info *terraform.InstanceInfo,
235 s *terraform.InstanceState,
236 d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
237 r, ok := p.ResourcesMap[info.Type]
239 return nil, fmt.Errorf("unknown resource type: %s", info.Type)
242 return r.Apply(s, d, p.meta)
245 // Diff implementation of terraform.ResourceProvider interface.
246 func (p *Provider) Diff(
247 info *terraform.InstanceInfo,
248 s *terraform.InstanceState,
249 c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
250 r, ok := p.ResourcesMap[info.Type]
252 return nil, fmt.Errorf("unknown resource type: %s", info.Type)
258 // Refresh implementation of terraform.ResourceProvider interface.
259 func (p *Provider) Refresh(
260 info *terraform.InstanceInfo,
261 s *terraform.InstanceState) (*terraform.InstanceState, error) {
262 r, ok := p.ResourcesMap[info.Type]
264 return nil, fmt.Errorf("unknown resource type: %s", info.Type)
267 return r.Refresh(s, p.meta)
270 // Resources implementation of terraform.ResourceProvider interface.
271 func (p *Provider) Resources() []terraform.ResourceType {
272 keys := make([]string, 0, len(p.ResourcesMap))
273 for k, _ := range p.ResourcesMap {
274 keys = append(keys, k)
278 result := make([]terraform.ResourceType, 0, len(keys))
279 for _, k := range keys {
280 resource := p.ResourcesMap[k]
282 // This isn't really possible (it'd fail InternalValidate), but
283 // we do it anyways to avoid a panic.
285 resource = &Resource{}
288 result = append(result, terraform.ResourceType{
290 Importable: resource.Importer != nil,
297 func (p *Provider) ImportState(
298 info *terraform.InstanceInfo,
299 id string) ([]*terraform.InstanceState, error) {
301 r, ok := p.ResourcesMap[info.Type]
303 return nil, fmt.Errorf("unknown resource type: %s", info.Type)
306 // If it doesn't support import, error
307 if r.Importer == nil {
308 return nil, fmt.Errorf("resource %s doesn't support import", info.Type)
314 data.SetType(info.Type)
316 // Call the import function
317 results := []*ResourceData{data}
318 if r.Importer.State != nil {
320 results, err = r.Importer.State(data, p.meta)
326 // Convert the results to InstanceState values and return it
327 states := make([]*terraform.InstanceState, len(results))
328 for i, r := range results {
329 states[i] = r.State()
332 // Verify that all are non-nil. If there are any nil the error
333 // isn't obvious so we circumvent that with a friendlier error.
334 for _, s := range states {
336 return nil, fmt.Errorf(
337 "nil entry in ImportState results. This is always a bug with\n" +
338 "the resource that is being imported. Please report this as\n" +
339 "a bug to Terraform.")
346 // ValidateDataSource implementation of terraform.ResourceProvider interface.
347 func (p *Provider) ValidateDataSource(
348 t string, c *terraform.ResourceConfig) ([]string, []error) {
349 r, ok := p.DataSourcesMap[t]
351 return nil, []error{fmt.Errorf(
352 "Provider doesn't support data source: %s", t)}
358 // ReadDataDiff implementation of terraform.ResourceProvider interface.
359 func (p *Provider) ReadDataDiff(
360 info *terraform.InstanceInfo,
361 c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
363 r, ok := p.DataSourcesMap[info.Type]
365 return nil, fmt.Errorf("unknown data source: %s", info.Type)
368 return r.Diff(nil, c)
371 // RefreshData implementation of terraform.ResourceProvider interface.
372 func (p *Provider) ReadDataApply(
373 info *terraform.InstanceInfo,
374 d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
376 r, ok := p.DataSourcesMap[info.Type]
378 return nil, fmt.Errorf("unknown data source: %s", info.Type)
381 return r.ReadDataApply(d, p.meta)
384 // DataSources implementation of terraform.ResourceProvider interface.
385 func (p *Provider) DataSources() []terraform.DataSource {
386 keys := make([]string, 0, len(p.DataSourcesMap))
387 for k, _ := range p.DataSourcesMap {
388 keys = append(keys, k)
392 result := make([]terraform.DataSource, 0, len(keys))
393 for _, k := range keys {
394 result = append(result, terraform.DataSource{