Make resource "kind" determination more obvious

By adding the "kind" to the base resource, it is still identifiable even
when the resource specific elements are gone in calls that are only
defined in the base resource. This is also more logical for building
resources!

This also switches resources to use an Init() method. This will be
useful for when resources have more complex initialization to do.
This commit is contained in:
James Shubin
2016-03-03 19:12:44 -05:00
parent c999f0c2cd
commit 10b8c93da4
6 changed files with 61 additions and 53 deletions

View File

@@ -45,11 +45,11 @@ type edgeConfig struct {
type GraphConfig struct { type GraphConfig struct {
Graph string `yaml:"graph"` Graph string `yaml:"graph"`
Resources struct { Resources struct {
Noop []NoopRes `yaml:"noop"` Noop []*NoopRes `yaml:"noop"`
Pkg []PkgRes `yaml:"pkg"` Pkg []*PkgRes `yaml:"pkg"`
File []FileRes `yaml:"file"` File []*FileRes `yaml:"file"`
Svc []SvcRes `yaml:"svc"` Svc []*SvcRes `yaml:"svc"`
Exec []ExecRes `yaml:"exec"` Exec []*ExecRes `yaml:"exec"`
} `yaml:"resources"` } `yaml:"resources"`
Collector []collectorResConfig `yaml:"collect"` Collector []collectorResConfig `yaml:"collect"`
Edges []edgeConfig `yaml:"edges"` Edges []edgeConfig `yaml:"edges"`
@@ -114,11 +114,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO
var keep []*Vertex // list of vertex which are the same in new graph var keep []*Vertex // list of vertex which are the same in new graph
for _, t := range config.Resources.Noop { for _, obj := range config.Resources.Noop {
obj := NewNoopRes(t.Name)
obj.Meta = t.Meta
v := g.GetVertexMatch(obj) v := g.GetVertexMatch(obj)
if v == nil { // no match found if v == nil { // no match found
obj.Init()
v = NewVertex(obj) v = NewVertex(obj)
g.AddVertex(v) // call standalone in case not part of an edge g.AddVertex(v) // call standalone in case not part of an edge
} }
@@ -126,11 +125,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO
keep = append(keep, v) // append keep = append(keep, v) // append
} }
for _, t := range config.Resources.Pkg { for _, obj := range config.Resources.Pkg {
obj := NewPkgRes(t.Name, t.State, false, false, false)
obj.Meta = t.Meta
v := g.GetVertexMatch(obj) v := g.GetVertexMatch(obj)
if v == nil { // no match found if v == nil { // no match found
obj.Init()
v = NewVertex(obj) v = NewVertex(obj)
g.AddVertex(v) // call standalone in case not part of an edge g.AddVertex(v) // call standalone in case not part of an edge
} }
@@ -138,24 +136,23 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO
keep = append(keep, v) // append keep = append(keep, v) // append
} }
for _, t := range config.Resources.File { for _, obj := range config.Resources.File {
// XXX: should we export based on a @@ prefix, or a metaparam // XXX: should we export based on a @@ prefix, or a metaparam
// like exported => true || exported => (host pattern)||(other pattern?) // like exported => true || exported => (host pattern)||(other pattern?)
if strings.HasPrefix(t.Name, "@@") { // exported resource if strings.HasPrefix(obj.Name, "@@") { // exported resource
// add to etcd storage... // add to etcd storage...
t.Name = t.Name[2:] //slice off @@ obj.Name = obj.Name[2:] //slice off @@
if !etcdO.EtcdPut(hostname, t.Name, "file", t) { if !etcdO.EtcdPut(hostname, obj.Name, "file", obj) {
log.Printf("Problem exporting file resource %v.", t.Name) log.Printf("Problem exporting file resource %v.", obj.Name)
continue continue
} }
} else { } else {
obj := NewFileRes(t.Name, t.Path, t.Dirname, t.Basename, t.Content, t.State)
// XXX: we don't have a way of knowing if any of the // XXX: we don't have a way of knowing if any of the
// metaparams are undefined, and as a result to set the // metaparams are undefined, and as a result to set the
// defaults that we want! I hate the go yaml parser!!! // defaults that we want! I hate the go yaml parser!!!
obj.Meta = t.Meta
v := g.GetVertexMatch(obj) v := g.GetVertexMatch(obj)
if v == nil { // no match found if v == nil { // no match found
obj.Init()
v = NewVertex(obj) v = NewVertex(obj)
g.AddVertex(v) // call standalone in case not part of an edge g.AddVertex(v) // call standalone in case not part of an edge
} }
@@ -164,11 +161,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO
} }
} }
for _, t := range config.Resources.Svc { for _, obj := range config.Resources.Svc {
obj := NewSvcRes(t.Name, t.State, t.Startup)
obj.Meta = t.Meta
v := g.GetVertexMatch(obj) v := g.GetVertexMatch(obj)
if v == nil { // no match found if v == nil { // no match found
obj.Init()
v = NewVertex(obj) v = NewVertex(obj)
g.AddVertex(v) // call standalone in case not part of an edge g.AddVertex(v) // call standalone in case not part of an edge
} }
@@ -176,11 +172,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO
keep = append(keep, v) // append keep = append(keep, v) // append
} }
for _, t := range config.Resources.Exec { for _, obj := range config.Resources.Exec {
obj := NewExecRes(t.Name, t.Cmd, t.Shell, t.Timeout, t.WatchCmd, t.WatchShell, t.IfCmd, t.IfShell, t.PollInt, t.State)
obj.Meta = t.Meta
v := g.GetVertexMatch(obj) v := g.GetVertexMatch(obj)
if v == nil { // no match found if v == nil { // no match found
obj.Init()
v = NewVertex(obj) v = NewVertex(obj)
g.AddVertex(v) // call standalone in case not part of an edge g.AddVertex(v) // call standalone in case not part of an edge
} }

12
exec.go
View File

@@ -40,12 +40,9 @@ type ExecRes struct {
} }
func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecRes { func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecRes {
// FIXME if path = nil, path = name ... obj := &ExecRes{
return &ExecRes{
BaseRes: BaseRes{ BaseRes: BaseRes{
Name: name, Name: name,
events: make(chan Event),
vertex: nil,
}, },
Cmd: cmd, Cmd: cmd,
Shell: shell, Shell: shell,
@@ -57,10 +54,13 @@ func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcm
PollInt: pollint, PollInt: pollint,
State: state, State: state,
} }
obj.Init()
return obj
} }
func (obj *ExecRes) Kind() string { func (obj *ExecRes) Init() {
return "Exec" obj.BaseRes.kind = "Exec"
obj.BaseRes.Init() // call base init, b/c we're overriding
} }
// validate if the params passed in are valid data // validate if the params passed in are valid data

11
file.go
View File

@@ -43,11 +43,9 @@ type FileRes struct {
func NewFileRes(name, path, dirname, basename, content, state string) *FileRes { func NewFileRes(name, path, dirname, basename, content, state string) *FileRes {
// FIXME if path = nil, path = name ... // FIXME if path = nil, path = name ...
return &FileRes{ obj := &FileRes{
BaseRes: BaseRes{ BaseRes: BaseRes{
Name: name, Name: name,
events: make(chan Event),
vertex: nil,
}, },
Path: path, Path: path,
Dirname: dirname, Dirname: dirname,
@@ -56,10 +54,13 @@ func NewFileRes(name, path, dirname, basename, content, state string) *FileRes {
State: state, State: state,
sha256sum: "", sha256sum: "",
} }
obj.Init()
return obj
} }
func (obj *FileRes) Kind() string { func (obj *FileRes) Init() {
return "File" obj.BaseRes.kind = "File"
obj.BaseRes.Init() // call base init, b/c we're overriding
} }
func (obj *FileRes) GetPath() string { func (obj *FileRes) GetPath() string {

10
pkg.go
View File

@@ -33,8 +33,9 @@ type PkgRes struct {
AllowUnsupported bool `yaml:"allowunsupported"` // allow unsupported packages to be found? AllowUnsupported bool `yaml:"allowunsupported"` // allow unsupported packages to be found?
} }
// helper function for creating new pkg resources that calls Init()
func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupported bool) *PkgRes { func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupported bool) *PkgRes {
return &PkgRes{ obj := &PkgRes{
BaseRes: BaseRes{ BaseRes: BaseRes{
Name: name, Name: name,
events: make(chan Event), events: make(chan Event),
@@ -45,6 +46,13 @@ func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupporte
AllowNonFree: allownonfree, AllowNonFree: allownonfree,
AllowUnsupported: allowunsupported, AllowUnsupported: allowunsupported,
} }
obj.Init()
return obj
}
func (obj *PkgRes) Init() {
obj.BaseRes.kind = "Pkg"
obj.BaseRes.Init() // call base init, b/c we're overriding
} }
func (obj *PkgRes) Kind() string { func (obj *PkgRes) Kind() string {

View File

@@ -96,6 +96,7 @@ type Res interface {
type BaseRes struct { type BaseRes struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Meta MetaParams `yaml:"meta"` // struct of all the metaparams Meta MetaParams `yaml:"meta"` // struct of all the metaparams
kind string
timestamp int64 // last updated timestamp ? timestamp int64 // last updated timestamp ?
events chan Event events chan Event
vertex *Vertex vertex *Vertex
@@ -114,14 +115,14 @@ type NoopRes struct {
func NewNoopRes(name string) *NoopRes { func NewNoopRes(name string) *NoopRes {
// FIXME: we could get rid of this New constructor and use raw object creation with a required Init() // FIXME: we could get rid of this New constructor and use raw object creation with a required Init()
return &NoopRes{ obj := &NoopRes{
BaseRes: BaseRes{ BaseRes: BaseRes{
Name: name, Name: name,
events: make(chan Event), // unbuffered chan size to avoid stale events
vertex: nil,
}, },
Comment: "", Comment: "",
} }
obj.Init()
return obj
} }
// wraps the IFF method when used with a list of UUID's // wraps the IFF method when used with a list of UUID's
@@ -163,7 +164,7 @@ func (obj *BaseUUID) Reversed() bool {
// initialize structures like channels if created without New constructor // initialize structures like channels if created without New constructor
func (obj *BaseRes) Init() { func (obj *BaseRes) Init() {
obj.events = make(chan Event) obj.events = make(chan Event) // unbuffered chan size to avoid stale events
} }
// this method gets used by all the resources, if we have one of (obj NoopRes) it would get overridden in that case! // this method gets used by all the resources, if we have one of (obj NoopRes) it would get overridden in that case!
@@ -171,8 +172,9 @@ func (obj *BaseRes) GetName() string {
return obj.Name return obj.Name
} }
// return the kind of resource this is
func (obj *BaseRes) Kind() string { func (obj *BaseRes) Kind() string {
return "Base" return obj.kind
} }
func (obj *BaseRes) GetMeta() MetaParams { func (obj *BaseRes) GetMeta() MetaParams {
@@ -410,8 +412,9 @@ func Process(obj Res) {
} }
} }
func (obj *NoopRes) Kind() string { func (obj *NoopRes) Init() {
return "Noop" obj.BaseRes.kind = "Noop"
obj.BaseRes.Init() // call base init, b/c we're overriding
} }
// validate if the params passed in are valid data // validate if the params passed in are valid data

11
svc.go
View File

@@ -35,19 +35,20 @@ type SvcRes struct {
} }
func NewSvcRes(name, state, startup string) *SvcRes { func NewSvcRes(name, state, startup string) *SvcRes {
return &SvcRes{ obj := &SvcRes{
BaseRes: BaseRes{ BaseRes: BaseRes{
Name: name, Name: name,
events: make(chan Event),
vertex: nil,
}, },
State: state, State: state,
Startup: startup, Startup: startup,
} }
obj.Init()
return obj
} }
func (obj *SvcRes) Kind() string { func (obj *SvcRes) Init() {
return "Svc" obj.BaseRes.kind = "Svc"
obj.BaseRes.Init() // call base init, b/c we're overriding
} }
func (obj *SvcRes) Validate() bool { func (obj *SvcRes) Validate() bool {