resources, yamlgraph: Drop the kind capitalization

This stopped making sense now that we have a resource with two primary
capitals. It was just a silly formatting hack anyways. Welcome kv!
This commit is contained in:
James Shubin
2017-03-06 00:32:18 -05:00
parent c696ebf53c
commit 22b48e296a
17 changed files with 28 additions and 32 deletions

View File

@@ -80,7 +80,7 @@ work, and finish by calling the `Init` method of the base resource.
```golang ```golang
// Init initializes the Foo resource. // Init initializes the Foo resource.
func (obj *FooRes) Init() error { func (obj *FooRes) Init() error {
obj.BaseRes.kind = "Foo" // must set capitalized resource kind obj.BaseRes.kind = "foo" // must lower case resource kind
// run the resource specific initialization, and error if anything fails // run the resource specific initialization, and error if anything fails
if some_error { if some_error {
return err // something went wrong! return err // something went wrong!

View File

@@ -93,7 +93,7 @@ func (obj *AugeasRes) Validate() error {
// Init initiates the resource. // Init initiates the resource.
func (obj *AugeasRes) Init() error { func (obj *AugeasRes) Init() error {
obj.BaseRes.kind = "Augeas" obj.BaseRes.kind = "augeas"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -74,7 +74,7 @@ func (obj *ExecRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *ExecRes) Init() error { func (obj *ExecRes) Init() error {
obj.BaseRes.kind = "Exec" obj.BaseRes.kind = "exec"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -147,7 +147,7 @@ func (obj *FileRes) Init() error {
obj.path = obj.GetPath() // compute once obj.path = obj.GetPath() // compute once
obj.isDir = strings.HasSuffix(obj.path, "/") // dirs have trailing slashes obj.isDir = strings.HasSuffix(obj.path, "/") // dirs have trailing slashes
obj.BaseRes.kind = "File" obj.BaseRes.kind = "file"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -87,7 +87,7 @@ func (obj *HostnameRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *HostnameRes) Init() error { func (obj *HostnameRes) Init() error {
obj.BaseRes.kind = "Hostname" obj.BaseRes.kind = "hostname"
if obj.PrettyHostname == "" { if obj.PrettyHostname == "" {
obj.PrettyHostname = obj.Hostname obj.PrettyHostname = obj.Hostname
} }

View File

@@ -88,7 +88,7 @@ func (obj *KVRes) Validate() error {
// Init initializes the resource. // Init initializes the resource.
func (obj *KVRes) Init() error { func (obj *KVRes) Init() error {
obj.BaseRes.kind = "KV" obj.BaseRes.kind = "kv"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -75,7 +75,7 @@ func (obj *MsgRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *MsgRes) Init() error { func (obj *MsgRes) Init() error {
obj.BaseRes.kind = "Msg" obj.BaseRes.kind = "msg"
return obj.BaseRes.Init() // call base init, b/c we're overrriding return obj.BaseRes.Init() // call base init, b/c we're overrriding
} }

View File

@@ -49,7 +49,7 @@ func (obj *NoopRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *NoopRes) Init() error { func (obj *NoopRes) Init() error {
obj.BaseRes.kind = "Noop" obj.BaseRes.kind = "noop"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -92,7 +92,7 @@ func (obj *NspawnRes) Init() error {
if err := obj.svc.Init(); err != nil { if err := obj.svc.Init(); err != nil {
return err return err
} }
obj.BaseRes.kind = "Nspawn" obj.BaseRes.kind = "nspawn"
return obj.BaseRes.Init() return obj.BaseRes.Init()
} }

View File

@@ -73,7 +73,7 @@ func (obj *PasswordRes) Validate() error {
// Init generates a new password for this resource if one was not provided. It // Init generates a new password for this resource if one was not provided. It
// will save this into a local file. It will load it back in from previous runs. // will save this into a local file. It will load it back in from previous runs.
func (obj *PasswordRes) Init() error { func (obj *PasswordRes) Init() error {
obj.BaseRes.kind = "Password" // must be set before using VarDir obj.BaseRes.kind = "password" // must be set before using VarDir
dir, err := obj.VarDir("") dir, err := obj.VarDir("")
if err != nil { if err != nil {

View File

@@ -66,7 +66,7 @@ func (obj *PkgRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *PkgRes) Init() error { func (obj *PkgRes) Init() error {
obj.BaseRes.kind = "Pkg" obj.BaseRes.kind = "pkg"
if err := obj.BaseRes.Init(); err != nil { // call base init, b/c we're overriding if err := obj.BaseRes.Init(); err != nil { // call base init, b/c we're overriding
return err return err
} }

View File

@@ -65,7 +65,7 @@ func (obj *SvcRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *SvcRes) Init() error { func (obj *SvcRes) Init() error {
obj.BaseRes.kind = "Svc" obj.BaseRes.kind = "svc"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -58,7 +58,7 @@ func (obj *TimerRes) Validate() error {
// Init runs some startup code for this resource. // Init runs some startup code for this resource.
func (obj *TimerRes) Init() error { func (obj *TimerRes) Init() error {
obj.BaseRes.kind = "Timer" obj.BaseRes.kind = "timer"
return obj.BaseRes.Init() // call base init, b/c we're overrriding return obj.BaseRes.Init() // call base init, b/c we're overrriding
} }

View File

@@ -191,7 +191,7 @@ func (obj *VirtRes) Init() error {
} }
} }
obj.wg = &sync.WaitGroup{} obj.wg = &sync.WaitGroup{}
obj.BaseRes.kind = "Virt" obj.BaseRes.kind = "virt"
return obj.BaseRes.Init() // call base init, b/c we're overriding return obj.BaseRes.Init() // call base init, b/c we're overriding
} }

View File

@@ -8,19 +8,19 @@ timeout --kill-after=20s 15s ./mgmt run --tmp-prefix --yaml=file-move.yaml 2>&1
pid=$! pid=$!
sleep 5s # let it converge sleep 5s # let it converge
initial=$(grep -c 'File\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log) initial=$(grep -c 'file\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log)
mv /tmp/mgmt/f1 /tmp/mgmt/f2 mv /tmp/mgmt/f1 /tmp/mgmt/f2
sleep 3 sleep 3
after_move_count=$(grep -c 'File\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log) after_move_count=$(grep -c 'file\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log)
sleep 3 sleep 3
echo f2 > /tmp/mgmt/f2 echo f2 > /tmp/mgmt/f2
after_moved_file_count=$(grep -c 'File\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log) after_moved_file_count=$(grep -c 'file\[file1\]: contentCheckApply(true)' /tmp/mgmt/file-move.log)
if [[ ${after_move_count} -le ${initial} ]] if [[ ${after_move_count} -le ${initial} ]]

View File

@@ -9,10 +9,10 @@ sleep 10s # let it converge
curl 127.0.0.1:9233/metrics curl 127.0.0.1:9233/metrics
# Three CheckApply for a File ; with events # Three CheckApply for a File ; with events
curl 127.0.0.1:9233/metrics | grep '^mgmt_checkapply_total{apply="true",errorful="false",eventful="true",kind="File"} 3$' curl 127.0.0.1:9233/metrics | grep '^mgmt_checkapply_total{apply="true",errorful="false",eventful="true",kind="file"} 3$'
# One CheckApply for a File ; in noop mode. # One CheckApply for a File ; in noop mode.
curl 127.0.0.1:9233/metrics | grep '^mgmt_checkapply_total{apply="false",errorful="false",eventful="true",kind="File"} 1$' curl 127.0.0.1:9233/metrics | grep '^mgmt_checkapply_total{apply="false",errorful="false",eventful="true",kind="file"} 1$'
# Check mgmt_graph_start_time_seconds # Check mgmt_graph_start_time_seconds
curl 127.0.0.1:9233/metrics | grep "^mgmt_graph_start_time_seconds [1-9]\+" curl 127.0.0.1:9233/metrics | grep "^mgmt_graph_start_time_seconds [1-9]\+"

View File

@@ -28,7 +28,6 @@ import (
"github.com/purpleidea/mgmt/pgraph" "github.com/purpleidea/mgmt/pgraph"
"github.com/purpleidea/mgmt/resources" "github.com/purpleidea/mgmt/resources"
"github.com/purpleidea/mgmt/util"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@@ -116,8 +115,7 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
field := value.FieldByName(name) field := value.FieldByName(name)
iface := field.Interface() // interface type of value iface := field.Interface() // interface type of value
slice := reflect.ValueOf(iface) slice := reflect.ValueOf(iface)
// XXX: should we just drop these everywhere and have the kind strings be all lowercase? kind := strings.ToLower(name)
kind := util.FirstToUpper(name)
for j := 0; j < slice.Len(); j++ { // loop through resources of same kind for j := 0; j < slice.Len(); j++ { // loop through resources of same kind
x := slice.Index(j).Interface() x := slice.Index(j).Interface()
res, ok := x.(resources.Res) // convert to Res type res, ok := x.(resources.Res) // convert to Res type
@@ -158,8 +156,7 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
var hostnameFilter []string // empty to get from everyone var hostnameFilter []string // empty to get from everyone
kindFilter := []string{} kindFilter := []string{}
for _, t := range c.Collector { for _, t := range c.Collector {
// XXX: should we just drop these everywhere and have the kind strings be all lowercase? kind := strings.ToLower(t.Kind)
kind := util.FirstToUpper(t.Kind)
kindFilter = append(kindFilter, kind) kindFilter = append(kindFilter, kind)
} }
// do all the graph look ups in one single step, so that if the backend // do all the graph look ups in one single step, so that if the backend
@@ -175,8 +172,7 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
matched := false matched := false
// see if we find a collect pattern that matches // see if we find a collect pattern that matches
for _, t := range c.Collector { for _, t := range c.Collector {
// XXX: should we just drop these everywhere and have the kind strings be all lowercase? kind := strings.ToLower(t.Kind)
kind := util.FirstToUpper(t.Kind)
// use t.Kind and optionally t.Pattern to collect from storage // use t.Kind and optionally t.Pattern to collect from storage
log.Printf("Collect: %v; Pattern: %v", kind, t.Pattern) log.Printf("Collect: %v; Pattern: %v", kind, t.Pattern)
@@ -219,20 +215,20 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
} }
for _, e := range c.Edges { for _, e := range c.Edges {
if _, ok := lookup[util.FirstToUpper(e.From.Kind)]; !ok { if _, ok := lookup[strings.ToLower(e.From.Kind)]; !ok {
return nil, fmt.Errorf("can't find 'from' resource") return nil, fmt.Errorf("can't find 'from' resource")
} }
if _, ok := lookup[util.FirstToUpper(e.To.Kind)]; !ok { if _, ok := lookup[strings.ToLower(e.To.Kind)]; !ok {
return nil, fmt.Errorf("can't find 'to' resource") return nil, fmt.Errorf("can't find 'to' resource")
} }
if _, ok := lookup[util.FirstToUpper(e.From.Kind)][e.From.Name]; !ok { if _, ok := lookup[strings.ToLower(e.From.Kind)][e.From.Name]; !ok {
return nil, fmt.Errorf("can't find 'from' name") return nil, fmt.Errorf("can't find 'from' name")
} }
if _, ok := lookup[util.FirstToUpper(e.To.Kind)][e.To.Name]; !ok { if _, ok := lookup[strings.ToLower(e.To.Kind)][e.To.Name]; !ok {
return nil, fmt.Errorf("can't find 'to' name") return nil, fmt.Errorf("can't find 'to' name")
} }
from := lookup[util.FirstToUpper(e.From.Kind)][e.From.Name] from := lookup[strings.ToLower(e.From.Kind)][e.From.Name]
to := lookup[util.FirstToUpper(e.To.Kind)][e.To.Name] to := lookup[strings.ToLower(e.To.Kind)][e.To.Name]
edge := pgraph.NewEdge(e.Name) edge := pgraph.NewEdge(e.Name)
edge.Notify = e.Notify edge.Notify = e.Notify
graph.AddEdge(from, to, edge) graph.AddEdge(from, to, edge)