github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/terraform/eval_provider.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/config" 7 ) 8 9 // EvalBuildProviderConfig outputs a *ResourceConfig that is properly 10 // merged with parents and inputs on top of what is configured in the file. 11 type EvalBuildProviderConfig struct { 12 Provider string 13 Config **ResourceConfig 14 Output **ResourceConfig 15 } 16 17 func (n *EvalBuildProviderConfig) Eval(ctx EvalContext) (interface{}, error) { 18 cfg := *n.Config 19 20 // If we have a configuration set, then merge that in 21 if input := ctx.ProviderInput(n.Provider); input != nil { 22 rc, err := config.NewRawConfig(input) 23 if err != nil { 24 return nil, err 25 } 26 27 merged := cfg.raw.Merge(rc) 28 cfg = NewResourceConfig(merged) 29 } 30 31 // Get the parent configuration if there is one 32 if parent := ctx.ParentProviderConfig(n.Provider); parent != nil { 33 merged := cfg.raw.Merge(parent.raw) 34 cfg = NewResourceConfig(merged) 35 } 36 37 *n.Output = cfg 38 return nil, nil 39 } 40 41 // EvalConfigProvider is an EvalNode implementation that configures 42 // a provider that is already initialized and retrieved. 43 type EvalConfigProvider struct { 44 Provider string 45 Config **ResourceConfig 46 } 47 48 func (n *EvalConfigProvider) Eval(ctx EvalContext) (interface{}, error) { 49 return nil, ctx.ConfigureProvider(n.Provider, *n.Config) 50 } 51 52 // EvalInitProvider is an EvalNode implementation that initializes a provider 53 // and returns nothing. The provider can be retrieved again with the 54 // EvalGetProvider node. 55 type EvalInitProvider struct { 56 Name string 57 } 58 59 func (n *EvalInitProvider) Eval(ctx EvalContext) (interface{}, error) { 60 return ctx.InitProvider(n.Name) 61 } 62 63 // EvalGetProvider is an EvalNode implementation that retrieves an already 64 // initialized provider instance for the given name. 65 type EvalGetProvider struct { 66 Name string 67 Output *ResourceProvider 68 } 69 70 func (n *EvalGetProvider) Eval(ctx EvalContext) (interface{}, error) { 71 result := ctx.Provider(n.Name) 72 if result == nil { 73 return nil, fmt.Errorf("provider %s not initialized", n.Name) 74 } 75 76 if n.Output != nil { 77 *n.Output = result 78 } 79 80 return nil, nil 81 } 82 83 // EvalInputProvider is an EvalNode implementation that asks for input 84 // for the given provider configurations. 85 type EvalInputProvider struct { 86 Name string 87 Provider *ResourceProvider 88 Config *config.RawConfig 89 } 90 91 func (n *EvalInputProvider) Eval(ctx EvalContext) (interface{}, error) { 92 // If we already configured this provider, then don't do this again 93 if v := ctx.ProviderInput(n.Name); v != nil { 94 return nil, nil 95 } 96 97 rc := NewResourceConfig(n.Config) 98 rc.Config = make(map[string]interface{}) 99 100 // Wrap the input into a namespace 101 input := &PrefixUIInput{ 102 IdPrefix: fmt.Sprintf("provider.%s", n.Name), 103 QueryPrefix: fmt.Sprintf("provider.%s.", n.Name), 104 UIInput: ctx.Input(), 105 } 106 107 // Go through each provider and capture the input necessary 108 // to satisfy it. 109 config, err := (*n.Provider).Input(input, rc) 110 if err != nil { 111 return nil, fmt.Errorf( 112 "Error configuring %s: %s", n.Name, err) 113 } 114 115 // Set the input that we received so that child modules don't attempt 116 // to ask for input again. 117 if config != nil && len(config.Config) > 0 { 118 ctx.SetProviderInput(n.Name, config.Config) 119 } else { 120 ctx.SetProviderInput(n.Name, map[string]interface{}{}) 121 } 122 123 return nil, nil 124 }