Commit 92371f1f authored by vaikas-google's avatar vaikas-google

Merge pull request #140 from bmelville/6

Type resolver now stores resolved imports in the manifest.
parents acc176e1 1660c27f
...@@ -37,7 +37,7 @@ type ExpandedTemplate struct { ...@@ -37,7 +37,7 @@ type ExpandedTemplate struct {
// Expander abstracts interactions with the expander and deployer services. // Expander abstracts interactions with the expander and deployer services.
type Expander interface { type Expander interface {
ExpandTemplate(t Template) (*ExpandedTemplate, error) ExpandTemplate(t *Template) (*ExpandedTemplate, error)
} }
// NewExpander returns a new initialized Expander. // NewExpander returns a new initialized Expander.
...@@ -110,7 +110,9 @@ func walkLayout(l *Layout, toReplace map[string]*LayoutResource) map[string]*Lay ...@@ -110,7 +110,9 @@ func walkLayout(l *Layout, toReplace map[string]*LayoutResource) map[string]*Lay
} }
// ExpandTemplate expands the supplied template, and returns a configuration. // ExpandTemplate expands the supplied template, and returns a configuration.
func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) { // It will also update the imports in the provided template if any were added
// during type resolution.
func (e *expander) ExpandTemplate(t *Template) (*ExpandedTemplate, error) {
// We have a fencepost problem here. // We have a fencepost problem here.
// 1. Start by trying to resolve any missing templates // 1. Start by trying to resolve any missing templates
// 2. Expand the configuration using all the of the imports available to us at this point // 2. Expand the configuration using all the of the imports available to us at this point
...@@ -129,17 +131,17 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) { ...@@ -129,17 +131,17 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
newImp, err := e.typeResolver.ResolveTypes(config, t.Imports) newImp, err := e.typeResolver.ResolveTypes(config, t.Imports)
if err != nil { if err != nil {
e := fmt.Errorf("type resolution failed: %s", err) e := fmt.Errorf("type resolution failed: %s", err)
return nil, expanderError(&t, e) return nil, expanderError(t, e)
} }
t.Imports = append(t.Imports, newImp...) t.Imports = append(t.Imports, newImp...)
for { for {
// Now expand with everything imported. // Now expand with everything imported.
result, err := e.expandTemplate(&t) result, err := e.expandTemplate(t)
if err != nil { if err != nil {
e := fmt.Errorf("template expansion: %s", err) e := fmt.Errorf("template expansion: %s", err)
return nil, expanderError(&t, e) return nil, expanderError(t, e)
} }
// Once we set this layout, we're operating on the "needResolve" *LayoutResources, // Once we set this layout, we're operating on the "needResolve" *LayoutResources,
...@@ -151,10 +153,10 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) { ...@@ -151,10 +153,10 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
} }
needResolve = walkLayout(result.Layout, needResolve) needResolve = walkLayout(result.Layout, needResolve)
newImp, err = e.typeResolver.ResolveTypes(result.Config, nil) newImp, err = e.typeResolver.ResolveTypes(result.Config, t.Imports)
if err != nil { if err != nil {
e := fmt.Errorf("type resolution failed: %s", err) e := fmt.Errorf("type resolution failed: %s", err)
return nil, expanderError(&t, e) return nil, expanderError(t, e)
} }
// If the new imports contain nothing, we are done. Everything is fully expanded. // If the new imports contain nothing, we are done. Everything is fully expanded.
...@@ -163,16 +165,8 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) { ...@@ -163,16 +165,8 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
return result, nil return result, nil
} }
// Update imports with any new imports from type resolution.
t.Imports = append(t.Imports, newImp...) t.Imports = append(t.Imports, newImp...)
var content []byte
content, err = yaml.Marshal(result.Config)
t.Content = string(content)
if err != nil {
e := fmt.Errorf("Unable to unmarshal response from expander (%s): %s",
err, result.Config)
return nil, expanderError(&t, e)
}
} }
} }
......
...@@ -262,7 +262,7 @@ func TestExpandTemplate(t *testing.T) { ...@@ -262,7 +262,7 @@ func TestExpandTemplate(t *testing.T) {
defer ts.Close() defer ts.Close()
expander := NewExpander(ts.URL, etc.Resolver) expander := NewExpander(ts.URL, etc.Resolver)
actualResponse, err := expander.ExpandTemplate(validTemplateTestCaseData) actualResponse, err := expander.ExpandTemplate(&validTemplateTestCaseData)
if err != nil { if err != nil {
message := err.Error() message := err.Error()
if etc.Error == "" { if etc.Error == "" {
......
...@@ -142,7 +142,7 @@ func (m *manager) CreateDeployment(t *Template) (*Deployment, error) { ...@@ -142,7 +142,7 @@ func (m *manager) CreateDeployment(t *Template) (*Deployment, error) {
} }
func (m *manager) createManifest(t *Template) (*Manifest, error) { func (m *manager) createManifest(t *Template) (*Manifest, error) {
et, err := m.expander.ExpandTemplate(*t) et, err := m.expander.ExpandTemplate(t)
if err != nil { if err != nil {
log.Printf("Expansion failed %v", err) log.Printf("Expansion failed %v", err)
return nil, err return nil, err
...@@ -263,7 +263,7 @@ func (m *manager) PutDeployment(name string, t *Template) (*Deployment, error) { ...@@ -263,7 +263,7 @@ func (m *manager) PutDeployment(name string, t *Template) (*Deployment, error) {
} }
func (m *manager) Expand(t *Template) (*Manifest, error) { func (m *manager) Expand(t *Template) (*Manifest, error) {
et, err := m.expander.ExpandTemplate(*t) et, err := m.expander.ExpandTemplate(t)
if err != nil { if err != nil {
log.Printf("Expansion failed %v", err) log.Printf("Expansion failed %v", err)
return nil, err return nil, err
......
...@@ -64,8 +64,8 @@ var errTest = errors.New("test") ...@@ -64,8 +64,8 @@ var errTest = errors.New("test")
type expanderStub struct{} type expanderStub struct{}
func (expander *expanderStub) ExpandTemplate(t Template) (*ExpandedTemplate, error) { func (expander *expanderStub) ExpandTemplate(t *Template) (*ExpandedTemplate, error) {
if reflect.DeepEqual(t, template) { if reflect.DeepEqual(*t, template) {
return &expandedConfig, nil return &expandedConfig, nil
} }
......
...@@ -90,7 +90,11 @@ func (tr *typeResolver) ResolveTypes(config *Configuration, imports []*ImportFil ...@@ -90,7 +90,11 @@ func (tr *typeResolver) ResolveTypes(config *Configuration, imports []*ImportFil
// Only fetch HTTP URLs that we haven't already imported. // Only fetch HTTP URLs that we haven't already imported.
if util.IsHttpUrl(r.Type) && !existing[r.Type] { if util.IsHttpUrl(r.Type) && !existing[r.Type] {
toFetch = append(toFetch, r.Type) toFetch = append(toFetch, r.Type)
fetched[r.Type] = append(fetched[r.Type], &ImportFile{Name: r.Type}) fetched[r.Type] = append(fetched[r.Type], &ImportFile{Name: r.Type})
// Add to existing map so it is not fetched multiple times.
existing[r.Type] = true
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment