graph

package module
v0.0.0-...-1cf5f05 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2018 License: MPL-2.0 Imports: 6 Imported by: 3

README

graph

Graph is a library for handling graph functions.

Core concepts

Graph : representation of a transition of a group of components to its updated version. A graph is a combination of components and edges that will become on a graph changes.

  • Component : represents an environment component like an instance, a sql server, anything can be a component as long as it exposes this interface
  • Change : the processed component.
  • Edge : transition from a source to a destination

Basic usage

In order to create a new graph you can use the default constructor:

g := graph.New(
  ID:       "id",
  Name:     "name",
  UserID:   "uID",
  Username: "john snow"
)

Lets add an instance as a new component:

instance := make(graph.GenericComponent)
instance["_action"] = "none"
instance["_component"] = "instances"
instance["_component_id"] = "instances::test1"
instance["_provider"] = "test"
instance["name"] = "john"
instance["size"] = "1024"

err = g.AddComponent(instance)

Actions

In order to get the edges for the creation of this component, you may want to change the _action field, this actually accepts different action. The previous example will look like:

instance := make(graph.GenericComponent)
instance["_action"] = "create"
instance["_component"] = "instances"
instance["_component_id"] = "instances::test1"
instance["_provider"] = "test"
instance["name"] = "john"
instance["size"] = "1024"

err = g.AddComponent(instance)

inspecting graph, will result on a set of valid edges to be processed.

Note: none action will result on non processing the specific component, so it wont be processed.

Diffs

Another interesting capability of graph library is the ability to calculate the diff between two graphs. This is accomplished with the Diff method and its variants.

You can find an example on how to diff graphs on the basic example

Managing dependencies

In many scenarios you may want to process a component before another, this causes a dependency between both. Graph library is also capable to manage that with Connect function family.

An easy to understand example is a sql server and its databases, you can't create databases before the server itself.

g := graph.New(
  ID:       "id",
  Name:     "name",
  UserID:   "uID",
  Username: "john snow"
)
server := make(graph.GenericComponent)
server["_component"] = "server"
server["_component_id"] = "server::test"
server["_provider"] = "test"
server["name"] = "mySQL"
err = g.AddComponent(server)

db := make(graph.GenericComponent)
db["_component"] = "db"
db["_component_id"] = "db::test"
db["_provider"] = "test"
db["name"] = "myDB"
err = g.AddComponent(db)

g.Connect(sql, db.GetID())

When you process this graph, you'll see that the sql server will be processed before the database.

Build status

  • master: CircleCI
  • develop: CircleCI

Installation

make deps
make install

or

go get github.com/r3labs/graph

Running Tests

make test

Contributing

Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.

Moreover, if your pull request contains patches or features, you must include relevant unit tests.

Versioning

For transparency into our release cycle and in striving to maintain backward compatibility, this project is maintained under the Semantic Versioning guidelines.

Code and documentation copyright since 2015 r3labs.io authors.

Code released under the Mozilla Public License Version 2.0.

Documentation

Index

Constants

View Source
const (
	//ACTIONCREATE : action create
	ACTIONCREATE = "create"
	// ACTIONUPDATE : action update
	ACTIONUPDATE = "update"
	// ACTIONDELETE : action delete
	ACTIONDELETE = "delete"
	// ACTIONFIND : action find
	ACTIONFIND = "find"
	// ACTIONGET : action get
	ACTIONGET = "get"
	// ACTIONNONE : action none, component wont be processed
	ACTIONNONE = "none"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Component

type Component interface {
	GetID() string                          // returns the ID of the component
	GetName() string                        // returns the name of the component
	GetProvider() string                    // returns the components provider
	GetProviderID() string                  // returns the components provider specific ID
	GetType() string                        // returns the type of component
	GetState() string                       // returns the state of the component. i.e. waiting, running, completed, errored
	SetState(string)                        // sets the state of the component
	GetAction() string                      // returns the action of the component, i.e. create, update, delete, get
	SetAction(string)                       // sets the action of the component
	GetGroup() string                       // returns the components group name
	GetTags() map[string]string             // returns the tags associated with the component
	GetTag(string) string                   // returns the tag associated with the component
	Diff(Component) (diff.Changelog, error) // should return changelog
	Update(Component)                       // updates the values stored on the component
	Rebuild(*Graph)                         // rebuilds the internal state of the component, a component set is passed in
	Validate() error                        // validates the component's values
	Dependencies() []string                 // returns a collection of parent component id's
	SetDefaultVariables()                   // sets the default variables for a component
	IsStateful() bool                       // returns if the component is stateful. This is important to work out if a component can be skipped when deleting its dependencies (pruning).
	SequentialDependencies() []string       // returns a list of origin components that restrict the execution of its dependents, allowing only one dependent component to be provisioned at a time (sequentially)
}

Component : representation of a component

type ComponentGroup

type ComponentGroup []Component

ComponentGroup holds a collection of components that can be filtered

func (ComponentGroup) ByGroup

func (cg ComponentGroup) ByGroup(tag, group string) ComponentGroup

ByGroup ...

func (ComponentGroup) ByName

func (cg ComponentGroup) ByName(name string) ComponentGroup

ByName ...

func (ComponentGroup) ByProviderID

func (cg ComponentGroup) ByProviderID(id string) Component

ByProviderID ...

func (ComponentGroup) ByType

func (cg ComponentGroup) ByType(ctype string) ComponentGroup

ByType ...

func (ComponentGroup) NameValues

func (cg ComponentGroup) NameValues() []string

NameValues ...

func (ComponentGroup) TagValues

func (cg ComponentGroup) TagValues(tag string) []string

TagValues ...

type Edge

type Edge struct {
	Source      string `json:"source"`
	Destination string `json:"destination"`
	Length      int    `json:"length"`
}

Edge ...

type GenericComponent

type GenericComponent map[string]interface{}

GenericComponent is a representation of a component backed by a map[string]interface{}

func MapGenericComponent

func MapGenericComponent(m map[string]interface{}) *GenericComponent

func (*GenericComponent) Dependencies

func (gc *GenericComponent) Dependencies() []string

Dependencies : returns a list of component id's upon which the component depends

func (*GenericComponent) Diff

Diff : diff's the component against another component of the same type

func (*GenericComponent) GetAction

func (gc *GenericComponent) GetAction() string

GetAction : returns the action of the component

func (*GenericComponent) GetGroup

func (gc *GenericComponent) GetGroup() string

GetGroup : returns the components group

func (*GenericComponent) GetID

func (gc *GenericComponent) GetID() string

GetID : returns the component's ID

func (*GenericComponent) GetName

func (gc *GenericComponent) GetName() string

GetName returns a components name

func (*GenericComponent) GetProvider

func (gc *GenericComponent) GetProvider() string

GetProvider : returns the provider type

func (*GenericComponent) GetProviderID

func (gc *GenericComponent) GetProviderID() string

GetProviderID returns a components provider id

func (*GenericComponent) GetState

func (gc *GenericComponent) GetState() string

GetState : returns the state of the component

func (*GenericComponent) GetTag

func (gc *GenericComponent) GetTag(string) string

GetTag returns a components tag

func (*GenericComponent) GetTags

func (gc *GenericComponent) GetTags() map[string]string

GetTags returns a components tags

func (*GenericComponent) GetType

func (gc *GenericComponent) GetType() string

GetType : returns the type of the component

func (*GenericComponent) IsStateful

func (gc *GenericComponent) IsStateful() bool

IsStateful : returns true if the component needs to be actioned to be removed.

func (*GenericComponent) Rebuild

func (gc *GenericComponent) Rebuild(g *Graph)

Rebuild : rebuilds the component's internal state, such as templated values

func (*GenericComponent) SequentialDependencies

func (gc *GenericComponent) SequentialDependencies() []string

SequentialDependencies : returns a list of origin components that restrict the execution of its dependents, allowing only one dependent component to be provisioned at a time (sequentially)

func (*GenericComponent) SetAction

func (gc *GenericComponent) SetAction(action string)

SetAction : Sets the action of the component

func (*GenericComponent) SetDefaultVariables

func (gc *GenericComponent) SetDefaultVariables()

SetDefaultVariables : sets up the default template variables for a component

func (*GenericComponent) SetState

func (gc *GenericComponent) SetState(state string)

SetState : sets the state of the component

func (*GenericComponent) Update

func (gc *GenericComponent) Update(v Component)

Update : updates the provider returned values of a component

func (*GenericComponent) Validate

func (gc *GenericComponent) Validate() error

Validate : validates the components values

type Graph

type Graph struct {
	ID         string                 `json:"id" diff:"-"`
	Name       string                 `json:"name" diff:"-"`
	UserID     int                    `json:"user_id" diff:"-"`
	Username   string                 `json:"username" diff:"-"`
	Action     string                 `json:"action" diff:"-"`
	Options    map[string]interface{} `json:"options" diff:"-"`
	Components []Component            `json:"components" diff:"components"`
	Changes    []Component            `json:"changes,omitempty" diff:"-"`
	Edges      []Edge                 `json:"edges,omitempty" diff:"-"`
	Changelog  diff.Changelog         `json:"changelog,omitempty" diff:"-"`
}

Graph ...

func New

func New() *Graph

New returns a new graph

func (*Graph) AddComponent

func (g *Graph) AddComponent(component Component) error

AddComponent adds a component to the graphs vertices if it does not already exist

func (*Graph) Component

func (g *Graph) Component(component string) Component

Component returns a component given the name matches

func (*Graph) ComponentAll

func (g *Graph) ComponentAll(component string) Component

ComponentAll returns a component from either changes or components given the name matches

func (*Graph) Connect

func (g *Graph) Connect(source, destination string) error

Connect adds a dependency between two vertices

func (*Graph) ConnectComplex

func (g *Graph) ConnectComplex(source, destination string) error

ConnectComplex adds a dependency between two vertices. If the source has more than 1 neighbouring vertex, the destination vertex will be connected to that.

func (*Graph) ConnectComplexUpdate

func (g *Graph) ConnectComplexUpdate(source, destination string) error

ConnectComplexUpdate adds a dependency between two vertices. If the source has more than 1 neighbouring vertex, the destination vertex will be connected to that.

func (*Graph) ConnectMutually

func (g *Graph) ConnectMutually(source, destination string) error

ConnectMutually connects two vertices to eachother

func (*Graph) Connected

func (g *Graph) Connected(source, destination string) bool

Connected returns true if two components are connected

func (*Graph) DeleteComponent

func (g *Graph) DeleteComponent(component Component)

DeleteComponent deletes a component from the graph

func (*Graph) Diff

func (g *Graph) Diff(og *Graph) (*Graph, error)

Diff : diff two graphs, new, modified or removed components will be moved to Changes, and components will be

func (*Graph) DiffWithChangelog

func (g *Graph) DiffWithChangelog(og *Graph) (*Graph, error)

DiffWithChangelog : produces a new build graph and changelog

func (*Graph) DisconnectComponent

func (g *Graph) DisconnectComponent(name string) error

DisconnectComponent removes a component from the graph. It will connect any neighbour/origin components together

func (*Graph) GetChanges

func (g *Graph) GetChanges() ComponentGroup

GetChanges returns a component group that can be filtered

func (*Graph) GetComponents

func (g *Graph) GetComponents() ComponentGroup

GetComponents returns a component group that can be filtered

func (*Graph) Graphviz

func (g *Graph) Graphviz() string

Graphviz outputs the graph in graphviz format

func (*Graph) HasComponent

func (g *Graph) HasComponent(componentID string) bool

HasComponent finds if the specified component exists

func (*Graph) LengthBetween

func (g *Graph) LengthBetween(source, destination string) int

LengthBetween returns the length between two edges

func (*Graph) Load

func (g *Graph) Load(gg map[string]interface{}) error

Load loads a graph from map

func (*Graph) Neighbours

func (g *Graph) Neighbours(component string) *Neighbours

Neighbours returns all depencencies of a component

func (*Graph) Origins

func (g *Graph) Origins(component string) *Neighbours

Origins returns all source components of a component

func (*Graph) SetDiffDependencies

func (g *Graph) SetDiffDependencies()

SetDiffDependencies rebuilds the graph's dependencies when diffing

func (*Graph) SetStartFinish

func (g *Graph) SetStartFinish()

SetStartFinish sets a start and finish point

func (*Graph) ToJSON

func (g *Graph) ToJSON() ([]byte, error)

ToJSON serialises the graph as json

func (*Graph) UpdateComponent

func (g *Graph) UpdateComponent(component Component)

UpdateComponent updates the graph

type Neighbours

type Neighbours []Component

Neighbours represents a collection of dependent vertices

func (*Neighbours) Exists

func (n *Neighbours) Exists(component string) bool

Exists checks if the group contains the component

func (*Neighbours) GetComponentGroup

func (n *Neighbours) GetComponentGroup(group string) Component

GetComponentGroup returns the first item in a components group

func (*Neighbours) GetSequentialDependency

func (n *Neighbours) GetSequentialDependency(id string) Component

GetSequentialDependency retuens the sequentially dependent component based on a given id if given

func (*Neighbours) Unique

func (n *Neighbours) Unique() *Neighbours

Unique returns a new collection of Vertices that are unique

type NodeStack

type NodeStack []Component

NodeStack stores a collection of verticies

func (NodeStack) Append

func (n NodeStack) Append(i []Component)

Append a verticies onto the stack

func (NodeStack) Empty

func (n NodeStack) Empty() bool

Empty returns true if there are no more verticies left

func (NodeStack) Pop

func (n NodeStack) Pop() Component

Pop a component from the stack

func (NodeStack) Prepend

func (n NodeStack) Prepend(i []Component)

Prepend a verticies onto the stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL