Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
minor code fixes - added new function to fetch url without template e…
Browse files Browse the repository at this point in the history
…valuation

Signed-off-by: David Chung <[email protected]>
  • Loading branch information
David Chung committed May 9, 2017
1 parent 351a464 commit cefe1c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/infrakit/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
}

if !*quiet {
fmt.Printf("%-20s\t%-50s\t%-s\n", "NAME", "LISTEN", "INTERFACE")
fmt.Printf("%-30s\t%-50s\t%-s\n", "NAME", "LISTEN", "INTERFACE")
}

sort.Strings(keys)

for _, k := range keys {

ep := view[k]
fmt.Printf("%-20s\t%-50s\t%-s\n", ep.name, ep.listen, ep.spi)
fmt.Printf("%-30s\t%-50s\t%-s\n", ep.name, ep.listen, ep.spi)

}

Expand Down
17 changes: 17 additions & 0 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ func (c *Context) Funcs() []template.Function {
return c.defineFlag(n, ftype, desc, defaultValue)
},
},
{
Name: "fetch",
Func: func(p string, opt ...interface{}) (string, error) {
// Overrides the base 'file' to account for the fact that
// some variables in the flag building phase are not set and would
// cause errors. In general, use include for loading files whose
// paths are computed from some flags. Use 'source' for including
// sibling templates that also include other flag definitions.
if c.exec {
content, err := c.template.Fetch(p, opt...)
if err == nil {
return content, nil
}
}
return "", nil
},
},
{
Name: "include",
Func: func(p string, opt ...interface{}) (string, error) {
Expand Down
34 changes: 23 additions & 11 deletions pkg/plugin/instance/hyperkit/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ type hyperkitPlugin struct {
DiskDir string
}

// Properties is the struct that holds the input
type Properties struct {
hyperkit.HyperKit

// Checksum is a checksum for the image
Checksum string
}

// Validate performs local validation on a provision request.
func (p hyperkitPlugin) Validate(req *types.Any) error {
// The guest is just the same data structure used by hyperkit for full fidelity config
guest := hyperkit.HyperKit{}

if err := req.Decode(&guest); err != nil {
properties := Properties{}
if err := req.Decode(&properties); err != nil {
return fmt.Errorf("error decoding guest configuration: %s, err=%v", req.String(), err)
}

// The guest is just the same data structure used by hyperkit for full fidelity config
guest := properties.HyperKit

for key, check := range map[string]int{
"CPUs": guest.CPUs,
"Memory": guest.Memory,
Expand Down Expand Up @@ -82,16 +91,19 @@ func (p hyperkitPlugin) Provision(spec instance.Spec) (*instance.ID, error) {
return nil, fmt.Errorf("missing properties in spec")
}

// The guest is just the same data structure used by hyperkit for full fidelity config
guest := &hyperkit.HyperKit{
HyperKit: p.HyperKit,
VPNKitSock: p.VPNKitSock,
properties := Properties{
HyperKit: hyperkit.HyperKit{
HyperKit: p.HyperKit,
VPNKitSock: p.VPNKitSock,
},
}

if err := spec.Properties.Decode(guest); err != nil {
return nil, fmt.Errorf("error decoding guest configuration: %s", spec.Properties.String())
if err := spec.Properties.Decode(&properties); err != nil {
return nil, fmt.Errorf("error decoding guest configuration: err=%v", err)
}

// The guest is just the same data structure used by hyperkit for full fidelity config
guest := properties.HyperKit

// directory for instance state
instanceDir, err := ioutil.TempDir(p.VMDir, "infrakit-")
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions pkg/template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ func setHeaders(req *http.Request, headers map[string][]string) {
}
}

// Fetch opens and reads a url and returns its content
func (t *Template) Fetch(p string, opt ...interface{}) (string, error) {
headers, _ := headersAndContext(opt...)
loc := p
if strings.Index(loc, "str://") == -1 {
u, err := GetURL(t.url, p)
if err != nil {
return "", err
}
loc = u.String()
}
prev := t.options.CustomizeFetch
t.options.CustomizeFetch = func(req *http.Request) {
setHeaders(req, headers)
if prev != nil {
prev(req)
}
}
buff, err := Fetch(loc, t.options)
return string(buff), err
}

// Source 'sources' the input file at url, also inherits all the variables.
func (t *Template) Source(p string, opt ...interface{}) (string, error) {
headers, context := headersAndContext(opt...)
Expand Down Expand Up @@ -309,6 +331,13 @@ func (t *Template) Include(p string, opt ...interface{}) (string, error) {
func (t *Template) DefaultFuncs() []Function {

return []Function{
{
Name: "fetch",
Description: []string{
"Fetches a resource without evaluation as template",
},
Func: t.Fetch,
},
{
Name: "source",
Description: []string{
Expand Down

0 comments on commit cefe1c5

Please sign in to comment.