Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Diagrams from YAML #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions cmd/parse/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"github.com/alexflint/go-arg"
"github.com/blushft/go-diagrams/diagram"
"github.com/blushft/go-diagrams/nodes/gcp"
"github.com/blushft/go-diagrams/nodes/generic"
"github.com/sirupsen/logrus"
"log"
"os"
)

func diag1(){
d, err := diagram.New(diagram.Filename("app"), diagram.Label("App"), diagram.Direction("LR"))
if err != nil {
log.Fatal(err)
}

dns := gcp.Network.Dns(diagram.NodeLabel("DNS"))
lb := gcp.Network.LoadBalancing(diagram.NodeLabel("NLB"))
cache := gcp.Database.Memorystore(diagram.NodeLabel("Cache"))
db := gcp.Database.Sql(diagram.NodeLabel("Database"))

dc := diagram.NewGroup("GCP")
dc.NewGroup("services").
Label("Service Layer").
Add(
gcp.Compute.ComputeEngine(diagram.NodeLabel("Server 1")),
gcp.Compute.ComputeEngine(diagram.NodeLabel("Server 2")),
gcp.Compute.ComputeEngine(diagram.NodeLabel("Server 3")),
).
ConnectAllFrom(lb.ID(), diagram.Forward()).
ConnectAllTo(cache.ID(), diagram.Forward())

dc.NewGroup("data").Label("Data Layer").Add(cache, db).Connect(cache, db)

d.Connect(dns, lb, diagram.Forward()).Group(dc)

if err := d.Render(); err != nil {
log.Fatal(err)
}
}

func diag2(){
d, err := diagram.New(diagram.Label("my-diagram"), diagram.Filename("diagram"))
if err != nil {
log.Fatal(err)
}

fw := generic.Network.Firewall().Label("fw")
sw := generic.Network.Switch().Label("sw")

d.Connect(fw, sw)

if err := d.Render(); err != nil {
log.Fatal(err)
}
}

var args struct {
Input string `arg:"-i" help:"Input file path (e.g: /tmp/1.yml)"`
Output string `arg:"-o" help:"Output directory"`
}

func main(){
arg.MustParse(&args)

if args.Input == "" {
logrus.Fatal("Input file not specified")
}

_, err := os.Open(args.Input)
if err != nil {
logrus.Fatalf("unable to open input file: %v", err)
}

_, err = os.Stat(args.Output)
if err != nil {
logrus.Fatal(err)
}
}
19 changes: 19 additions & 0 deletions diagram/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package diagram

type NodeData struct {
Name string `yaml:"name"`
Label string `yaml:"label"`
Type string `yaml:"type"`
Nodes []NodeData `yaml:"nodes"`
ConnectTo []string `yaml:"connectTo"`
ConnectFrom []string `yaml:"connectFrom"`
ConnectAllTo []string `yaml:"connectAllTo"`
ConnectAllFrom []string `yaml:"connectAllFrom"`
BackgroundColor string `yaml:"backgroundColor"`
}

type DiagramData struct {
Direction string `yaml:"direction" default:"LTR"`
Label string `yaml:"label"`
Nodes []NodeData `yaml:"nodes"`
}
68 changes: 63 additions & 5 deletions diagram/diagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package diagram

import (
"errors"
graphviz "github.com/awalterschulze/gographviz"
"io/ioutil"
"os"
"path/filepath"

graphviz "github.com/awalterschulze/gographviz"
)

type Connector interface {
Expand All @@ -16,9 +15,7 @@ type Connector interface {

type Diagram struct {
options Options

g *graphviz.Escape

root *Group
}

Expand Down Expand Up @@ -82,6 +79,10 @@ func (d *Diagram) Close() error {
return nil
}

func (d *Diagram) Parse(file *os.File) error {
return d.parse(file)
}

func (d *Diagram) Render() error {
return d.render()
}
Expand Down Expand Up @@ -126,6 +127,63 @@ func (d *Diagram) renderOutput() error {

func (d *Diagram) saveDot() error {
fname := filepath.Join(d.options.Name, d.options.FileName+".dot")
return ioutil.WriteFile(fname, []byte(d.g.String()), 0644)
}

func (d *Diagram) interpretYML(yml *DiagramData) error {
if yml.Label != "" {
d.options.Label = yml.Label
}

return ioutil.WriteFile(fname, []byte(d.g.String()), os.ModePerm)
if yml.Direction != "" {
d.options.Direction = yml.Direction
}

for _, v := range yml.Nodes {
parsedNode := d.parseNode(v)
switch parsedNode.(type) {
case *Node:
d.Add(parsedNode.(*Node))
case *Group:
d.Group(parsedNode.(*Group))
}
}

return nil
}

func (d *Diagram) parseNode(v NodeData) interface{} {
switch v.Type {
case "Group":
groupNode := NewGroup(v.Name)
if v.Label != "" {
groupNode.Label(v.Label)
}

if v.BackgroundColor != "" {
groupNode.BackgroundColor(v.BackgroundColor)
}

for _, child := range v.Nodes {
parsedNode := d.parseNode(child)
switch parsedNode.(type) {
case *Node:
groupNode.Add(parsedNode.(*Node))
case *Group:
groupNode.Group(parsedNode.(*Group))
}
}
return groupNode
/*case "gcp.Network.DNS":
// TODO: find a better way to do the mapping
node := gcp.Network.Dns()
if v.Label != "" {
node.Label(v.Label)
}
return node*/
default:
panic("unimplemented " + v.Type)
}

return nil
}
30 changes: 30 additions & 0 deletions diagram/diagram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package diagram_test

import (
"github.com/blushft/go-diagrams/diagram"
"os"
"testing"
)

func TestDiagramParse(t *testing.T){
file, err := os.Open("../test/assets/parse/ymls/1.yml")
if err != nil {
t.Fatal(err)
}
d, err := diagram.New(diagram.Label("my-diagram"),
diagram.DirName("/tmp/diagrams-1"),
diagram.Filename("1"))
if err != nil {
t.Fatal(err)
}

err = d.Parse(file)
if err != nil {
t.Fatal(err)
}

err = d.Render()
if err != nil {
t.Fatal(err)
}
}
8 changes: 8 additions & 0 deletions diagram/options.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package diagram

import (
"os"
"strconv"
)

type Options struct {
Name string
FileName string
InputFile *os.File
OutFormat string
Direction string
CurveStyle string
Expand Down Expand Up @@ -72,6 +74,12 @@ func Filename(f string) Option {
}
}

func DirName(d string) Option {
return func(o *Options) {
o.Name = d
}
}

func Label(l string) Option {
return func(o *Options) {
o.Label = l
Expand Down
34 changes: 34 additions & 0 deletions diagram/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package diagram

import (
"fmt"
"gopkg.in/yaml.v3"
"os"
)

func (d *Diagram) parseYML(inputFile *os.File) (*DiagramData, error) {
var decodedYML *DiagramData

decoder := yaml.NewDecoder(inputFile)
decoder.KnownFields(true)
err := decoder.Decode(&decodedYML)

if err != nil {
return nil, err
}

return decodedYML, nil
}

func (d *Diagram) parse(inputFile *os.File) error {
if inputFile == nil {
return fmt.Errorf("input file not specified")
}

yml, err := d.parseYML(inputFile)
if err != nil {
return err
}

return d.interpretYML(yml)
}
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ module github.com/blushft/go-diagrams
go 1.14

require (
github.com/UnnoTed/fileb0x v1.1.4 // indirect
github.com/alexflint/go-arg v1.3.0
github.com/awalterschulze/gographviz v0.0.0-20200901124122-0eecad45bd71
github.com/dave/jennifer v1.4.1
github.com/davecgh/go-spew v1.1.1
github.com/google/uuid v1.1.2
github.com/iancoleman/strcase v0.1.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1
golang.org/x/exp v0.0.0-20200908183739-ae8ad444f925 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)
Loading