Skip to content

Commit

Permalink
CsarParser
Browse files Browse the repository at this point in the history
  • Loading branch information
owulveryck committed Jan 5, 2016
1 parent 3fdbf2e commit 945da4b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 8 deletions.
104 changes: 104 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package toscalib

import (
"archive/zip"
"fmt"
"golang.org/x/tools/godoc/vfs"
"golang.org/x/tools/godoc/vfs/zipfs"
"gopkg.in/yaml.v2"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"path/filepath"
)

// GetNodeTemplate returns a pointer to a node template given its name
Expand Down Expand Up @@ -88,6 +93,105 @@ func merge(s, t ServiceTemplateDefinition) ServiceTemplateDefinition {
return s
}

// Open and parse the Csar file c
func (t *ServiceTemplateDefinition) ParseCsar(zipfile string) error {

type meta struct {
Version string `yaml:"TOSCA-Meta-File-Version"`
CsarVersion string `yaml:"CSAR-Version"`
CreatedBy string `yaml:"Created-By"`
EntryDefinition string `yaml:"Entry-Definitions"`
}

rc, err := zip.OpenReader(zipfile)
defer rc.Close()
if err != nil {
log.Fatalf("%s: %s\n", zipfile, err)

}
fs := zipfs.New(rc, zipfile)
out, err := vfs.ReadFile(fs, "/TOSCA-Metadata/TOSCA.meta")
if err != nil {
log.Fatal(err)

}
var m meta
err = yaml.Unmarshal(out, &m)
if err != nil {
log.Println("Cannont unmarshal", string(out))
return err
}
log.Println(m.EntryDefinition)
dirname := fmt.Sprintf("/%v", filepath.Dir(m.EntryDefinition))
base := filepath.Base(m.EntryDefinition)
ns := vfs.NameSpace{}
ns.Bind("/", fs, dirname, vfs.BindReplace)
// Now read the yaml
rsc, err := ns.Open(base)
if err != nil {
return err

}
var std ServiceTemplateDefinition
data, err := ioutil.ReadAll(rsc)
if err != nil {
return err
}

// Unmarshal the data in an interface
err = yaml.Unmarshal(data, &std)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
// Import de normative types by default
for _, normType := range []string{"interface_types", "relationship_types", "node_types", "capability_types"} {
data, err := Asset(normType)
if err != nil {
log.Panic("Normative type not found")
return err
}
var tt ServiceTemplateDefinition
err = yaml.Unmarshal(data, &tt)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
std = merge(std, tt)
}
for _, im := range std.Imports {
rsc, err := ns.Open(im)
if err != nil {
return err

}
var tt ServiceTemplateDefinition
data, err := ioutil.ReadAll(rsc)
if err != nil {
return err
}

// Unmarshal the data in an interface
err = yaml.Unmarshal(data, &tt)
if err != nil {
log.Println("Cannont unmarshal", string(data))
return err
}
std = merge(std, tt)
}
// Free the imports
std.Imports = []string{}
*t = std
for name, node := range t.TopologyTemplate.NodeTemplates {
node.fillInterface(*t)
node.setRefs(t)
node.setName(name)
t.TopologyTemplate.NodeTemplates[name] = node
}

return nil
}

// Parse a TOSCA document and fill in the structure
func (t *ServiceTemplateDefinition) Parse(r io.Reader) error {
var std ServiceTemplateDefinition
Expand Down
35 changes: 27 additions & 8 deletions service_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

Expand All @@ -12,15 +13,33 @@ func TestParse(t *testing.T) {
for _, f := range files {
if !f.IsDir() {
fname := fmt.Sprintf("./tests/%v", f.Name())
var s ServiceTemplateDefinition
o, err := os.Open(fname)
if err != nil {
t.Fatal(err)
if filepath.Ext(fname) == "yaml" {
var s ServiceTemplateDefinition
o, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
err = s.Parse(o)
if err != nil {
t.Log("Error in processing", fname)
t.Fatal(err)
}
}
err = s.Parse(o)
if err != nil {
t.Log("Error in processing", fname)
t.Fatal(err)
}

}
}
func TestParseCsar(t *testing.T) {
files, _ := ioutil.ReadDir("./tests")
for _, f := range files {
if !f.IsDir() {
fname := fmt.Sprintf("./tests/%v", f.Name())
if filepath.Ext(fname) == "zip" {
var s ServiceTemplateDefinition
err := s.ParseCsar(fname)
if err != nil {
t.Fatal(err)
}
}
}

Expand Down
Binary file added tests/csar_elk.zip
Binary file not shown.
Binary file added tests/csar_hello_world.zip
Binary file not shown.
Binary file added tests/csar_metadata_not_yaml.zip
Binary file not shown.
1 change: 1 addition & 0 deletions tests/csar_not_zip.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is an invalid CSAR file.
Binary file added tests/csar_single_instance_wordpress.zip
Binary file not shown.
Binary file added tests/csar_wordpress_invalid_import_path.zip
Binary file not shown.
Binary file added tests/csar_wordpress_invalid_script_url.zip
Binary file not shown.
Binary file added tests/csar_wrong_metadata_file.zip
Binary file not shown.

0 comments on commit 945da4b

Please sign in to comment.