Skip to content
Snippets Groups Projects
set.go 1001 B
Newer Older
  • Learn to ignore specific revisions
  • package cli
    
    import (
    	"code.fbi.h-da.de/cocsn/gosdn/forks/goarista/gnmi"
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"code.fbi.h-da.de/cocsn/gosdn/nucleus/util/proto"
    
    	"context"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	pb "google.golang.org/protobuf/proto"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	"os"
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    // Set sends a gNMI Set request to the specified target. Only one
    // request per invocation supported.
    func Set(a, u, p, typ string, args ...string) error {
    
    	opts := &nucleus.GnmiTransportOptions{
    		Config: gnmi.Config{
    			Addr:     a,
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			Username: u,
    			Password: p,
    
    		},
    	}
    	t, err := nucleus.NewGnmiTransport(opts)
    	if err != nil {
    		return err
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	path := gnmi.SplitPath(args[0])
    	req := []interface{}{
    
    		&gnmi.Operation{
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			Type:   typ,
    
    			Origin: "",
    			Target: "",
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			Path:   path,
    			Val:    args[1],
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	resp, err := t.Set(context.Background(), req...)
    
    	if err != nil {
    		return err
    	}
    
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	_, tap := os.LookupEnv("GOSDN_TAP")
    	if tap {
    		if err := proto.Write(resp.(pb.Message), "resp-set-system-config-hostname"); err != nil {
    			return err
    		}
    
    	}
    	return nil
    }