lib, yamlgraph: Remove old yamlgraph GAPI frontend

I should have removed this a long time ago, but didn't. Now it's done.
The new v2 frontend is loosing the v2 name and just replacing v1.
This commit is contained in:
James Shubin
2018-03-13 13:02:11 -04:00
parent 6e0dfdb16f
commit 4d8faeb826
4 changed files with 109 additions and 586 deletions

View File

@@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"log"
"reflect"
"strings"
"github.com/purpleidea/mgmt/pgraph"
@@ -51,41 +50,93 @@ type Edge struct {
Notify bool `yaml:"notify"`
}
// Resources is the data structure of the set of resources.
type Resources struct {
// in alphabetical order
Augeas []*resources.AugeasRes `yaml:"augeas"`
AwsEc2 []*resources.AwsEc2Res `yaml:"aws:ec2"`
Exec []*resources.ExecRes `yaml:"exec"`
File []*resources.FileRes `yaml:"file"`
Graph []*resources.GraphRes `yaml:"graph"`
Group []*resources.GroupRes `yaml:"group"`
Hostname []*resources.HostnameRes `yaml:"hostname"`
KV []*resources.KVRes `yaml:"kv"`
Msg []*resources.MsgRes `yaml:"msg"`
Net []*resources.NetRes `yaml:"net"`
Noop []*resources.NoopRes `yaml:"noop"`
Nspawn []*resources.NspawnRes `yaml:"nspawn"`
Password []*resources.PasswordRes `yaml:"password"`
Pkg []*resources.PkgRes `yaml:"pkg"`
Print []*resources.PrintRes `yaml:"print"`
Svc []*resources.SvcRes `yaml:"svc"`
Test []*resources.TestRes `yaml:"test"`
Timer []*resources.TimerRes `yaml:"timer"`
User []*resources.UserRes `yaml:"user"`
Virt []*resources.VirtRes `yaml:"virt"`
// ResourceData are the parameters for resource format.
type ResourceData struct {
Name string `yaml:"name"`
}
// GraphConfig is the data structure that describes a single graph to run.
type GraphConfig struct {
// Resource is the object that unmarshalls resources.
type Resource struct {
ResourceData
unmarshal func(interface{}) error
resource resources.Res
}
// Resources is the object that unmarshalls list of resources.
type Resources struct {
Resources map[string][]Resource `yaml:"resources"`
}
// GraphConfigData contains the graph data for GraphConfig.
type GraphConfigData struct {
Graph string `yaml:"graph"`
Resources Resources `yaml:"resources"`
Collector []collectorResConfig `yaml:"collect"`
Edges []Edge `yaml:"edges"`
Comment string `yaml:"comment"`
Remote string `yaml:"remote"`
}
// GraphConfig is the data structure that describes a single graph to run.
type GraphConfig struct {
GraphConfigData
ResList []resources.Res
}
// UnmarshalYAML unmarshalls the complete graph.
func (c *GraphConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Unmarshal the graph data, except the resources
if err := unmarshal(&c.GraphConfigData); err != nil {
return err
}
// Unmarshal resources
var list Resources
list.Resources = map[string][]Resource{}
if err := unmarshal(&list); err != nil {
return err
}
// Finish unmarshalling by giving to each resource its kind
// and store each resource in the graph
for kind, resList := range list.Resources {
for _, res := range resList {
err := res.Decode(kind)
if err != nil {
return err
}
c.ResList = append(c.ResList, res.resource)
}
}
return nil
}
// UnmarshalYAML is the first stage for unmarshaling of resources.
func (r *Resource) UnmarshalYAML(unmarshal func(interface{}) error) error {
r.unmarshal = unmarshal
return unmarshal(&r.ResourceData)
}
// Decode is the second stage for unmarshaling of resources (knowing their
// kind).
func (r *Resource) Decode(kind string) (err error) {
r.resource, err = resources.NewResource(kind)
if err != nil {
return err
}
err = r.unmarshal(r.resource)
if err != nil {
return err
}
// set resource name and kind
r.resource.SetName(r.Name)
r.resource.SetKind(strings.ToLower(kind)) // gets overwritten, so set it
// meta already gets unmarshalled properly with the correct defaults
return
}
// Parse parses a data stream into the graph structure.
func (c *GraphConfig) Parse(data []byte) error {
if err := yaml.Unmarshal(data, c); err != nil {
@@ -118,52 +169,37 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
var keep []pgraph.Vertex // list of vertex which are the same in new graph
var resourceList []resources.Res // list of resources to export
// use reflection to avoid duplicating code... better options welcome!
value := reflect.Indirect(reflect.ValueOf(c.Resources))
vtype := value.Type()
for i := 0; i < vtype.NumField(); i++ { // number of fields in struct
name := vtype.Field(i).Name // string of field name
field := value.FieldByName(name)
iface := field.Interface() // interface type of value
slice := reflect.ValueOf(iface)
kind := strings.ToLower(name)
for j := 0; j < slice.Len(); j++ { // loop through resources of same kind
x := slice.Index(j).Interface()
res, ok := x.(resources.Res) // convert to Res type
if !ok {
return nil, fmt.Errorf("Config: Error: Can't convert: %v of type: %T to Res", x, x)
}
res.SetKind(kind) // cheap init
//if noop { // now done in mgmtmain
// res.Meta().Noop = noop
//}
if _, exists := lookup[kind]; !exists {
lookup[kind] = make(map[string]pgraph.Vertex)
}
// XXX: should we export based on a @@ prefix, or a metaparam
// like exported => true || exported => (host pattern)||(other pattern?)
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
fn := func(v pgraph.Vertex) (bool, error) {
return resources.VtoR(v).Compare(res), nil
}
v, err := graph.VertexMatchFn(fn)
if err != nil {
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
}
if v == nil { // no match found
v = res // a standalone res can be a vertex
graph.AddVertex(v) // call standalone in case not part of an edge
}
lookup[kind][res.GetName()] = v // used for constructing edges
keep = append(keep, v) // append
} else if !noop { // do not export any resources if noop
// store for addition to backend storage...
res.SetName(res.GetName()[2:]) //slice off @@
resourceList = append(resourceList, res)
// Resources
for _, res := range c.ResList {
kind := res.GetKind()
if _, exists := lookup[kind]; !exists {
lookup[kind] = make(map[string]pgraph.Vertex)
}
// XXX: should we export based on a @@ prefix, or a metaparam
// like exported => true || exported => (host pattern)||(other pattern?)
if !strings.HasPrefix(res.GetName(), "@@") { // not exported resource
fn := func(v pgraph.Vertex) (bool, error) {
return resources.VtoR(v).Compare(res), nil
}
v, err := graph.VertexMatchFn(fn)
if err != nil {
return nil, errwrap.Wrapf(err, "could not VertexMatchFn() resource")
}
if v == nil { // no match found
v = res // a standalone res can be a vertex
graph.AddVertex(v) // call standalone in case not part of an edge
}
lookup[kind][res.GetName()] = v // used for constructing edges
keep = append(keep, v) // append
} else if !noop { // do not export any resources if noop
// store for addition to backend storage...
res.SetName(res.GetName()[2:]) // slice off @@
resourceList = append(resourceList, res)
}
}
// store in backend (usually etcd)
if err := world.ResExport(resourceList); err != nil {
return nil, fmt.Errorf("Config: Could not export resources: %v", err)
@@ -240,16 +276,16 @@ func (c *GraphConfig) NewGraphFromConfig(hostname string, world resources.World,
for _, e := range c.Edges {
if _, ok := lookup[strings.ToLower(e.From.Kind)]; !ok {
return nil, fmt.Errorf("can't find 'from' kind: %s", e.From.Kind)
return nil, fmt.Errorf("can't find 'from' resource")
}
if _, ok := lookup[strings.ToLower(e.To.Kind)]; !ok {
return nil, fmt.Errorf("can't find 'to' kind: %s", e.To.Kind)
return nil, fmt.Errorf("can't find 'to' resource")
}
if _, ok := lookup[strings.ToLower(e.From.Kind)][e.From.Name]; !ok {
return nil, fmt.Errorf("can't find 'from' name: %s", e.From.Name)
return nil, fmt.Errorf("can't find 'from' name")
}
if _, ok := lookup[strings.ToLower(e.To.Kind)][e.To.Name]; !ok {
return nil, fmt.Errorf("can't find 'to' name: %s", e.To.Name)
return nil, fmt.Errorf("can't find 'to' name")
}
from := lookup[strings.ToLower(e.From.Kind)][e.From.Name]
to := lookup[strings.ToLower(e.To.Kind)][e.To.Name]