config: Parse out special 'connection' blocks

This commit is contained in:
Armon Dadgar 2014-07-10 17:14:47 -07:00
parent 9ad4531d10
commit 36b1a2b9e8
2 changed files with 64 additions and 2 deletions

View File

@ -42,6 +42,7 @@ type Resource struct {
type Provisioner struct { type Provisioner struct {
Type string Type string
RawConfig *RawConfig RawConfig *RawConfig
ConnInfo *RawConfig
} }
// Variable is a variable defined within the configuration. // Variable is a variable defined within the configuration.

View File

@ -316,6 +316,10 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
// that is treated specially. // that is treated specially.
delete(config, "provisioner") delete(config, "provisioner")
// Delete the "connection" section since we handle that
// seperately
delete(config, "connection")
rawConfig, err := NewRawConfig(config) rawConfig, err := NewRawConfig(config)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
@ -339,11 +343,26 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
} }
} }
// 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)
}
}
// If we have provisioners, then parse those out // If we have provisioners, then parse those out
var provisioners []*Provisioner var provisioners []*Provisioner
if po := r.Get("provisioner"); po != nil { if po := r.Get("provisioner"); po != nil {
var err error var err error
provisioners, err = loadProvisionersLibucl(po) provisioners, err = loadProvisionersLibucl(po, connInfo)
po.Close() po.Close()
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
@ -367,7 +386,7 @@ func loadResourcesLibucl(o *libucl.Object) ([]*Resource, error) {
return result, nil return result, nil
} }
func loadProvisionersLibucl(o *libucl.Object) ([]*Provisioner, error) { func loadProvisionersLibucl(o *libucl.Object, connInfo map[string]interface{}) ([]*Provisioner, error) {
pos := make([]*libucl.Object, 0, int(o.Len())) pos := make([]*libucl.Object, 0, int(o.Len()))
// Accumulate all the actual provisioner configuration objects. We // Accumulate all the actual provisioner configuration objects. We
@ -409,16 +428,58 @@ func loadProvisionersLibucl(o *libucl.Object) ([]*Provisioner, error) {
return nil, err return nil, err
} }
// Delete the "connection" section, handle seperately
delete(config, "connection")
rawConfig, err := NewRawConfig(config) rawConfig, err := NewRawConfig(config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// 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
// that are not explicitly overriden.
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
}
result = append(result, &Provisioner{ result = append(result, &Provisioner{
Type: po.Key(), Type: po.Key(),
RawConfig: rawConfig, RawConfig: rawConfig,
ConnInfo: connRaw,
}) })
} }
return result, nil return result, nil
} }
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
}