2014-05-23 01:56:28 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-05-24 00:11:57 +02:00
|
|
|
"path/filepath"
|
2014-05-23 01:56:28 +02:00
|
|
|
|
|
|
|
"github.com/mitchellh/go-libucl"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Put the parse flags we use for libucl in a constant so we can get
|
|
|
|
// equally behaving parsing everywhere.
|
2014-07-23 17:38:43 +02:00
|
|
|
const libuclParseFlags = libucl.ParserNoTime
|
2014-05-23 01:56:28 +02:00
|
|
|
|
2014-05-24 00:32:34 +02:00
|
|
|
// libuclConfigurable is an implementation of configurable that knows
|
|
|
|
// how to turn libucl configuration into a *Config object.
|
2014-05-24 00:28:19 +02:00
|
|
|
type libuclConfigurable struct {
|
|
|
|
Object *libucl.Object
|
2014-05-24 00:06:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 01:30:28 +02:00
|
|
|
func (t *libuclConfigurable) Close() error {
|
|
|
|
return t.Object.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
func (t *libuclConfigurable) Config() (*Config, error) {
|
2014-07-19 01:00:21 +02:00
|
|
|
validKeys := map[string]struct{}{
|
|
|
|
"output": struct{}{},
|
|
|
|
"provider": struct{}{},
|
|
|
|
"resource": struct{}{},
|
|
|
|
"variable": struct{}{},
|
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
type LibuclVariable struct {
|
2014-07-21 16:32:36 +02:00
|
|
|
Default interface{}
|
2014-06-04 00:55:51 +02:00
|
|
|
Description string
|
|
|
|
Fields []string `libucl:",decodedFields"`
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
var rawConfig struct {
|
2014-06-04 00:55:51 +02:00
|
|
|
Variable map[string]*LibuclVariable
|
2014-05-24 00:28:19 +02:00
|
|
|
}
|
2014-05-24 00:06:37 +02:00
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
if err := t.Object.Decode(&rawConfig); err != nil {
|
2014-05-24 00:06:37 +02:00
|
|
|
return nil, err
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-06-04 00:55:51 +02:00
|
|
|
// Start building up the actual configuration. We start with
|
|
|
|
// variables.
|
2014-07-19 02:48:30 +02:00
|
|
|
// TODO(mitchellh): Make function like loadVariablesLibucl so that
|
2014-08-07 09:19:56 +02:00
|
|
|
// duplicates aren't overridden
|
2014-05-24 00:28:19 +02:00
|
|
|
config := new(Config)
|
2014-07-19 07:13:15 +02:00
|
|
|
if len(rawConfig.Variable) > 0 {
|
|
|
|
config.Variables = make([]*Variable, 0, len(rawConfig.Variable))
|
|
|
|
for k, v := range rawConfig.Variable {
|
2014-07-22 17:06:09 +02:00
|
|
|
// Defaults turn into a slice of map[string]interface{} and
|
|
|
|
// we need to make sure to convert that down into the
|
|
|
|
// proper type for Config.
|
|
|
|
if ms, ok := v.Default.([]map[string]interface{}); ok {
|
|
|
|
def := make(map[string]interface{})
|
|
|
|
for _, m := range ms {
|
|
|
|
for k, v := range m {
|
|
|
|
def[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Default = def
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:34:44 +02:00
|
|
|
newVar := &Variable{
|
2014-07-19 07:13:15 +02:00
|
|
|
Name: k,
|
|
|
|
Default: v.Default,
|
|
|
|
Description: v.Description,
|
2014-07-21 17:34:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config.Variables = append(config.Variables, newVar)
|
2014-07-19 07:13:15 +02:00
|
|
|
}
|
2014-06-04 00:55:51 +02:00
|
|
|
}
|
2014-05-24 00:28:19 +02:00
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
// Build the provider configs
|
|
|
|
providers := t.Object.Get("provider")
|
|
|
|
if providers != nil {
|
|
|
|
var err error
|
|
|
|
config.ProviderConfigs, err = loadProvidersLibucl(providers)
|
|
|
|
providers.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
// Build the resources
|
|
|
|
resources := t.Object.Get("resource")
|
|
|
|
if resources != nil {
|
|
|
|
var err error
|
|
|
|
config.Resources, err = loadResourcesLibucl(resources)
|
|
|
|
resources.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
// Build the outputs
|
|
|
|
if outputs := t.Object.Get("output"); outputs != nil {
|
|
|
|
var err error
|
|
|
|
config.Outputs, err = loadOutputsLibucl(outputs)
|
|
|
|
outputs.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:00:21 +02:00
|
|
|
// Check for invalid keys
|
|
|
|
iter := t.Object.Iterate(true)
|
|
|
|
defer iter.Close()
|
|
|
|
for o := iter.Next(); o != nil; o = iter.Next() {
|
|
|
|
k := o.Key()
|
|
|
|
o.Close()
|
|
|
|
|
|
|
|
if _, ok := validKeys[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
config.unknownKeys = append(config.unknownKeys, k)
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
return config, nil
|
2014-05-24 00:06:37 +02:00
|
|
|
}
|
2014-05-23 01:56:28 +02:00
|
|
|
|
2014-05-24 00:32:34 +02:00
|
|
|
// loadFileLibucl is a fileLoaderFunc that knows how to read libucl
|
|
|
|
// files and turn them into libuclConfigurables.
|
2014-05-24 00:28:19 +02:00
|
|
|
func loadFileLibucl(root string) (configurable, []string, error) {
|
2014-05-24 00:06:37 +02:00
|
|
|
var obj *libucl.Object = nil
|
2014-05-23 01:56:28 +02:00
|
|
|
|
2014-05-24 00:06:37 +02:00
|
|
|
// Parse and store the object. We don't use a defer here so that
|
|
|
|
// we clear resources right away rather than stack them up all the
|
|
|
|
// way through our recursive calls.
|
|
|
|
parser := libucl.NewParser(libuclParseFlags)
|
|
|
|
err := parser.AddFile(root)
|
|
|
|
if err == nil {
|
|
|
|
obj = parser.Object()
|
|
|
|
defer obj.Close()
|
|
|
|
}
|
|
|
|
parser.Close()
|
2014-05-23 01:56:28 +02:00
|
|
|
|
2014-05-24 00:06:37 +02:00
|
|
|
// If there was an error, return early
|
|
|
|
if err != nil {
|
2014-05-24 00:28:19 +02:00
|
|
|
return nil, nil, err
|
2014-05-24 00:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start building the result
|
2014-05-24 00:28:19 +02:00
|
|
|
result := &libuclConfigurable{
|
2014-05-24 00:06:37 +02:00
|
|
|
Object: obj,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, dive in, find the imports.
|
|
|
|
imports := obj.Get("import")
|
|
|
|
if imports == nil {
|
|
|
|
result.Object.Ref()
|
2014-05-24 00:28:19 +02:00
|
|
|
return result, nil, nil
|
2014-05-24 00:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if imports.Type() != libucl.ObjectTypeString {
|
|
|
|
imports.Close()
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
return nil, nil, fmt.Errorf(
|
2014-05-24 00:06:37 +02:00
|
|
|
"Error in %s: all 'import' declarations should be in the format\n"+
|
|
|
|
"`import \"foo\"` (Got type %s)",
|
|
|
|
root,
|
|
|
|
imports.Type())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather all the import paths
|
|
|
|
importPaths := make([]string, 0, imports.Len())
|
|
|
|
iter := imports.Iterate(false)
|
|
|
|
for imp := iter.Next(); imp != nil; imp = iter.Next() {
|
2014-05-24 00:28:19 +02:00
|
|
|
path := imp.ToString()
|
2014-05-24 00:11:57 +02:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
// Relative paths are relative to the Terraform file itself
|
|
|
|
dir := filepath.Dir(root)
|
|
|
|
path = filepath.Join(dir, path)
|
|
|
|
}
|
|
|
|
|
2014-05-24 00:28:19 +02:00
|
|
|
importPaths = append(importPaths, path)
|
|
|
|
imp.Close()
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
2014-05-24 00:28:19 +02:00
|
|
|
iter.Close()
|
|
|
|
imports.Close()
|
2014-05-23 01:56:28 +02:00
|
|
|
|
2014-05-24 00:06:37 +02:00
|
|
|
result.Object.Ref()
|
2014-05-24 00:28:19 +02:00
|
|
|
return result, importPaths, nil
|
2014-05-23 01:56:28 +02:00
|
|
|
}
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
// LoadOutputsLibucl recurses into the given libucl object and turns
|
|
|
|
// it into a mapping of outputs.
|
2014-07-19 07:21:52 +02:00
|
|
|
func loadOutputsLibucl(o *libucl.Object) ([]*Output, error) {
|
2014-07-04 19:43:06 +02:00
|
|
|
objects := make(map[string]*libucl.Object)
|
|
|
|
|
|
|
|
// Iterate over all the "output" blocks and get the keys along with
|
|
|
|
// their raw configuration objects. We'll parse those later.
|
|
|
|
iter := o.Iterate(false)
|
|
|
|
for o1 := iter.Next(); o1 != nil; o1 = iter.Next() {
|
|
|
|
iter2 := o1.Iterate(true)
|
|
|
|
for o2 := iter2.Next(); o2 != nil; o2 = iter2.Next() {
|
|
|
|
objects[o2.Key()] = o2
|
|
|
|
defer o2.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
o1.Close()
|
|
|
|
iter2.Close()
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
|
2014-07-19 07:21:52 +02:00
|
|
|
// If we have none, just return nil
|
|
|
|
if len(objects) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-07-04 19:43:06 +02:00
|
|
|
// Go through each object and turn it into an actual result.
|
2014-07-19 07:21:52 +02:00
|
|
|
result := make([]*Output, 0, len(objects))
|
2014-07-04 19:43:06 +02:00
|
|
|
for n, o := range objects {
|
|
|
|
var config map[string]interface{}
|
|
|
|
|
|
|
|
if err := o.Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for output %s: %s",
|
|
|
|
n,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2014-07-19 07:21:52 +02:00
|
|
|
result = append(result, &Output{
|
2014-07-04 19:43:06 +02:00
|
|
|
Name: n,
|
|
|
|
RawConfig: rawConfig,
|
2014-07-19 07:21:52 +02:00
|
|
|
})
|
2014-07-04 19:43:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
// LoadProvidersLibucl recurses into the given libucl object and turns
|
|
|
|
// it into a mapping of provider configs.
|
2014-07-20 01:05:48 +02:00
|
|
|
func loadProvidersLibucl(o *libucl.Object) ([]*ProviderConfig, error) {
|
2014-05-26 03:05:18 +02:00
|
|
|
objects := make(map[string]*libucl.Object)
|
|
|
|
|
|
|
|
// Iterate over all the "provider" blocks and get the keys along with
|
|
|
|
// their raw configuration objects. We'll parse those later.
|
|
|
|
iter := o.Iterate(false)
|
|
|
|
for o1 := iter.Next(); o1 != nil; o1 = iter.Next() {
|
|
|
|
iter2 := o1.Iterate(true)
|
|
|
|
for o2 := iter2.Next(); o2 != nil; o2 = iter2.Next() {
|
|
|
|
objects[o2.Key()] = o2
|
|
|
|
defer o2.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
o1.Close()
|
|
|
|
iter2.Close()
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
|
2014-07-20 01:05:48 +02:00
|
|
|
if len(objects) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:18 +02:00
|
|
|
// Go through each object and turn it into an actual result.
|
2014-07-20 01:05:48 +02:00
|
|
|
result := make([]*ProviderConfig, 0, len(objects))
|
2014-05-26 03:05:18 +02:00
|
|
|
for n, o := range objects {
|
|
|
|
var config map[string]interface{}
|
|
|
|
|
|
|
|
if err := o.Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:40:59 +02:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
2014-05-26 03:05:18 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for provider config %s: %s",
|
|
|
|
n,
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:05:48 +02:00
|
|
|
result = append(result, &ProviderConfig{
|
|
|
|
Name: n,
|
2014-06-13 02:40:59 +02:00
|
|
|
RawConfig: rawConfig,
|
2014-07-20 01:05:48 +02:00
|
|
|
})
|
2014-05-26 03:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-05-23 19:52:19 +02:00
|
|
|
// Given a handle to a libucl object, this recurses into the structure
|
|
|
|
// and pulls out a list of resources.
|
|
|
|
//
|
|
|
|
// The resulting resources may not be unique, but each resource
|
|
|
|
// represents exactly one resource definition in the libucl configuration.
|
|
|
|
// We leave it up to another pass to merge them together.
|
2014-05-24 22:57:51 +02:00
|
|
|
func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
|
2014-05-23 01:56:28 +02:00
|
|
|
var allTypes []*libucl.Object
|
|
|
|
|
|
|
|
// Libucl object iteration is really nasty. Below is likely to make
|
|
|
|
// no sense to anyone approaching this code. Luckily, it is very heavily
|
|
|
|
// tested. If working on a bug fix or feature, we recommend writing a
|
|
|
|
// test first then doing whatever you want to the code below. If you
|
|
|
|
// break it, the tests will catch it. Likewise, if you change this,
|
|
|
|
// MAKE SURE you write a test for your change, because its fairly impossible
|
|
|
|
// to reason about this mess.
|
|
|
|
//
|
|
|
|
// Functionally, what the code does below is get the libucl.Objects
|
|
|
|
// for all the TYPES, such as "aws_security_group".
|
|
|
|
iter := o.Iterate(false)
|
|
|
|
for o1 := iter.Next(); o1 != nil; o1 = iter.Next() {
|
|
|
|
// Iterate the inner to get the list of types
|
|
|
|
iter2 := o1.Iterate(true)
|
|
|
|
for o2 := iter2.Next(); o2 != nil; o2 = iter2.Next() {
|
|
|
|
// Iterate all of this type to get _all_ the types
|
|
|
|
iter3 := o2.Iterate(false)
|
|
|
|
for o3 := iter3.Next(); o3 != nil; o3 = iter3.Next() {
|
|
|
|
allTypes = append(allTypes, o3)
|
|
|
|
}
|
|
|
|
|
|
|
|
o2.Close()
|
|
|
|
iter3.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
o1.Close()
|
|
|
|
iter2.Close()
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
|
|
|
|
// Where all the results will go
|
2014-05-24 22:57:51 +02:00
|
|
|
var result []*Resource
|
2014-05-23 01:56:28 +02:00
|
|
|
|
|
|
|
// Now go over all the types and their children in order to get
|
|
|
|
// all of the actual resources.
|
|
|
|
for _, t := range allTypes {
|
|
|
|
// Release the resources for this raw type since we don't need it.
|
|
|
|
// Note that this makes it unsafe now to use allTypes again.
|
|
|
|
defer t.Close()
|
|
|
|
|
|
|
|
iter := t.Iterate(true)
|
|
|
|
defer iter.Close()
|
|
|
|
for r := iter.Next(); r != nil; r = iter.Next() {
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := r.Decode(&config); err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2014-07-23 02:10:17 +02:00
|
|
|
// Remove the fields we handle specially
|
|
|
|
delete(config, "connection")
|
2014-07-04 05:11:58 +02:00
|
|
|
delete(config, "count")
|
2014-07-23 02:10:17 +02:00
|
|
|
delete(config, "depends_on")
|
2014-07-03 05:20:13 +02:00
|
|
|
delete(config, "provisioner")
|
|
|
|
|
2014-06-13 02:40:59 +02:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
2014-05-24 06:58:06 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading config for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:11:58 +02:00
|
|
|
// If we have a count, then figure it out
|
|
|
|
var count int = 1
|
|
|
|
if o := r.Get("count"); o != nil {
|
|
|
|
err = o.Decode(&count)
|
|
|
|
o.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error parsing count for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 02:14:47 +02:00
|
|
|
// If we have connection info, then parse those out
|
|
|
|
var connInfo map[string]interface{}
|
|
|
|
if conn := r.Get("connection"); conn != nil {
|
|
|
|
var err error
|
|
|
|
connInfo, err = loadConnInfoLibucl(conn)
|
|
|
|
conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading connection info for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 02:10:17 +02:00
|
|
|
// If we have depends fields, then add those in
|
|
|
|
var dependsOn []string
|
|
|
|
if deps := r.Get("depends_on"); deps != nil {
|
|
|
|
err := deps.Decode(&dependsOn)
|
|
|
|
deps.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading depends_on for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:20:13 +02:00
|
|
|
// If we have provisioners, then parse those out
|
|
|
|
var provisioners []*Provisioner
|
|
|
|
if po := r.Get("provisioner"); po != nil {
|
|
|
|
var err error
|
2014-07-11 02:14:47 +02:00
|
|
|
provisioners, err = loadProvisionersLibucl(po, connInfo)
|
2014-07-03 05:20:13 +02:00
|
|
|
po.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Error reading provisioners for %s[%s]: %s",
|
|
|
|
t.Key(),
|
|
|
|
r.Key(),
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-24 22:57:51 +02:00
|
|
|
result = append(result, &Resource{
|
2014-07-03 05:20:13 +02:00
|
|
|
Name: r.Key(),
|
|
|
|
Type: t.Key(),
|
|
|
|
Count: count,
|
|
|
|
RawConfig: rawConfig,
|
|
|
|
Provisioners: provisioners,
|
2014-07-23 02:10:17 +02:00
|
|
|
DependsOn: dependsOn,
|
2014-05-23 01:56:28 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2014-07-03 05:20:13 +02:00
|
|
|
|
2014-07-11 02:14:47 +02:00
|
|
|
func loadProvisionersLibucl(o *libucl.Object, connInfo map[string]interface{}) ([]*Provisioner, error) {
|
2014-07-03 05:20:13 +02:00
|
|
|
pos := make([]*libucl.Object, 0, int(o.Len()))
|
|
|
|
|
|
|
|
// Accumulate all the actual provisioner configuration objects. We
|
|
|
|
// have to iterate twice here:
|
|
|
|
//
|
|
|
|
// 1. The first iteration is of the list of `provisioner` blocks.
|
|
|
|
// 2. The second iteration is of the dictionary within the
|
|
|
|
// provisioner which will have only one element which is the
|
|
|
|
// type of provisioner to use along with tis config.
|
|
|
|
//
|
|
|
|
// In JSON it looks kind of like this:
|
|
|
|
//
|
|
|
|
// [
|
|
|
|
// {
|
|
|
|
// "shell": {
|
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ]
|
|
|
|
//
|
|
|
|
iter := o.Iterate(false)
|
|
|
|
for o1 := iter.Next(); o1 != nil; o1 = iter.Next() {
|
|
|
|
iter2 := o1.Iterate(true)
|
|
|
|
for o2 := iter2.Next(); o2 != nil; o2 = iter2.Next() {
|
|
|
|
pos = append(pos, o2)
|
|
|
|
}
|
|
|
|
|
|
|
|
o1.Close()
|
|
|
|
iter2.Close()
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
|
|
|
|
result := make([]*Provisioner, 0, len(pos))
|
|
|
|
for _, po := range pos {
|
|
|
|
defer po.Close()
|
|
|
|
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := po.Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:19:56 +02:00
|
|
|
// Delete the "connection" section, handle separately
|
2014-07-11 02:14:47 +02:00
|
|
|
delete(config, "connection")
|
|
|
|
|
2014-07-03 05:20:13 +02:00
|
|
|
rawConfig, err := NewRawConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-11 02:14:47 +02:00
|
|
|
// Check if we have a provisioner-level connection
|
|
|
|
// block that overrides the resource-level
|
|
|
|
var subConnInfo map[string]interface{}
|
|
|
|
if conn := po.Get("connection"); conn != nil {
|
|
|
|
var err error
|
|
|
|
subConnInfo, err = loadConnInfoLibucl(conn)
|
|
|
|
conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inherit from the resource connInfo any keys
|
2014-08-07 09:19:56 +02:00
|
|
|
// that are not explicitly overridden.
|
2014-07-11 02:14:47 +02:00
|
|
|
if connInfo != nil && subConnInfo != nil {
|
|
|
|
for k, v := range connInfo {
|
|
|
|
if _, ok := subConnInfo[k]; !ok {
|
|
|
|
subConnInfo[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if subConnInfo == nil {
|
|
|
|
subConnInfo = connInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the connInfo
|
|
|
|
connRaw, err := NewRawConfig(subConnInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:20:13 +02:00
|
|
|
result = append(result, &Provisioner{
|
|
|
|
Type: po.Key(),
|
|
|
|
RawConfig: rawConfig,
|
2014-07-11 02:14:47 +02:00
|
|
|
ConnInfo: connRaw,
|
2014-07-03 05:20:13 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2014-07-11 02:14:47 +02:00
|
|
|
|
|
|
|
func loadConnInfoLibucl(o *libucl.Object) (map[string]interface{}, error) {
|
|
|
|
var config map[string]interface{}
|
|
|
|
if err := o.Decode(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|