Commit 1660c27f authored by Brendan Melville's avatar Brendan Melville

Type resolver now stores resolved imports in the manifest.

parent d4bf89c5
......@@ -37,7 +37,7 @@ type ExpandedTemplate struct {
// Expander abstracts interactions with the expander and deployer services.
type Expander interface {
ExpandTemplate(t Template) (*ExpandedTemplate, error)
ExpandTemplate(t *Template) (*ExpandedTemplate, error)
}
// NewExpander returns a new initialized Expander.
......@@ -110,7 +110,9 @@ func walkLayout(l *Layout, toReplace map[string]*LayoutResource) map[string]*Lay
}
// 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.
// 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
......@@ -129,17 +131,17 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
newImp, err := e.typeResolver.ResolveTypes(config, t.Imports)
if err != nil {
e := fmt.Errorf("type resolution failed: %s", err)
return nil, expanderError(&t, e)
return nil, expanderError(t, e)
}
t.Imports = append(t.Imports, newImp...)
for {
// Now expand with everything imported.
result, err := e.expandTemplate(&t)
result, err := e.expandTemplate(t)
if err != nil {
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,
......@@ -151,10 +153,10 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
}
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 {
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.
......@@ -163,16 +165,8 @@ func (e *expander) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
return result, nil
}
// Update imports with any new imports from type resolution.
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) {
defer ts.Close()
expander := NewExpander(ts.URL, etc.Resolver)
actualResponse, err := expander.ExpandTemplate(validTemplateTestCaseData)
actualResponse, err := expander.ExpandTemplate(&validTemplateTestCaseData)
if err != nil {
message := err.Error()
if etc.Error == "" {
......
......@@ -142,7 +142,7 @@ func (m *manager) CreateDeployment(t *Template) (*Deployment, error) {
}
func (m *manager) createManifest(t *Template) (*Manifest, error) {
et, err := m.expander.ExpandTemplate(*t)
et, err := m.expander.ExpandTemplate(t)
if err != nil {
log.Printf("Expansion failed %v", err)
return nil, err
......@@ -263,7 +263,7 @@ func (m *manager) PutDeployment(name string, t *Template) (*Deployment, error) {
}
func (m *manager) Expand(t *Template) (*Manifest, error) {
et, err := m.expander.ExpandTemplate(*t)
et, err := m.expander.ExpandTemplate(t)
if err != nil {
log.Printf("Expansion failed %v", err)
return nil, err
......
......@@ -64,8 +64,8 @@ var errTest = errors.New("test")
type expanderStub struct{}
func (expander *expanderStub) ExpandTemplate(t Template) (*ExpandedTemplate, error) {
if reflect.DeepEqual(t, template) {
func (expander *expanderStub) ExpandTemplate(t *Template) (*ExpandedTemplate, error) {
if reflect.DeepEqual(*t, template) {
return &expandedConfig, nil
}
......
......@@ -90,7 +90,11 @@ func (tr *typeResolver) ResolveTypes(config *Configuration, imports []*ImportFil
// Only fetch HTTP URLs that we haven't already imported.
if util.IsHttpUrl(r.Type) && !existing[r.Type] {
toFetch = append(toFetch, 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