set.go 996 Bytes
Newer Older
Manuel Kieweg's avatar
Manuel Kieweg committed
1
2
3
4
package cli

import (
	"context"
Manuel Kieweg's avatar
Manuel Kieweg committed
5
	"os"
Manuel Kieweg's avatar
Manuel Kieweg committed
6

Manuel Kieweg's avatar
Manuel Kieweg committed
7
	"code.fbi.h-da.de/danet/forks/goarista/gnmi"
Manuel Kieweg's avatar
Manuel Kieweg committed
8
9
10
	"code.fbi.h-da.de/danet/gosdn/nucleus"
	"code.fbi.h-da.de/danet/gosdn/nucleus/util/proto"
	pb "google.golang.org/protobuf/proto"
Manuel Kieweg's avatar
Manuel Kieweg committed
11
12
)

Manuel Kieweg's avatar
Manuel Kieweg committed
13
14
15
// 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 {
Manuel Kieweg's avatar
Manuel Kieweg committed
16
17
18
	opts := &nucleus.GnmiTransportOptions{
		Config: gnmi.Config{
			Addr:     a,
Manuel Kieweg's avatar
Manuel Kieweg committed
19
20
			Username: u,
			Password: p,
Manuel Kieweg's avatar
Manuel Kieweg committed
21
22
23
24
25
26
27
		},
	}
	t, err := nucleus.NewGnmiTransport(opts)
	if err != nil {
		return err
	}

Manuel Kieweg's avatar
Manuel Kieweg committed
28
29
	path := gnmi.SplitPath(args[0])
	req := []interface{}{
Manuel Kieweg's avatar
Manuel Kieweg committed
30
		&gnmi.Operation{
Manuel Kieweg's avatar
Manuel Kieweg committed
31
			Type:   typ,
Manuel Kieweg's avatar
Manuel Kieweg committed
32
33
			Origin: "",
			Target: "",
Manuel Kieweg's avatar
Manuel Kieweg committed
34
35
			Path:   path,
			Val:    args[1],
Manuel Kieweg's avatar
Manuel Kieweg committed
36
37
38
		},
	}

Manuel Kieweg's avatar
Manuel Kieweg committed
39
	resp, err := t.Set(context.Background(), req...)
Manuel Kieweg's avatar
Manuel Kieweg committed
40
41
42
43
	if err != nil {
		return err
	}

Manuel Kieweg's avatar
Manuel Kieweg committed
44
45
46
47
48
	_, tap := os.LookupEnv("GOSDN_TAP")
	if tap {
		if err := proto.Write(resp.(pb.Message), "resp-set-system-config-hostname"); err != nil {
			return err
		}
Manuel Kieweg's avatar
Manuel Kieweg committed
49
50
51
	}
	return nil
}