-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (86 loc) · 2.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"context"
"crypto/tls"
"log"
"net"
"net/http"
"os"
"os/signal"
"github.com/Juniper/apstra-go-sdk/apstra"
)
// ourIpForPeer returns a *net.IP representing the local interface selected by
// the system for talking to the passed *net.IP. The returned value might also
// be the best choice for that peer to reach us.
func ourIpForPeer(them net.IP) (*net.IP, error) {
c, err := net.Dial("udp4", them.String()+":1")
if err != nil {
return nil, err
}
return &c.LocalAddr().(*net.UDPAddr).IP, c.Close()
}
func main() {
// create an apstra client object
clientCfg := apstra.ClientCfg{
Url: "https://apstra.example.com",
User: "admin",
Pass: "password",
HttpClient: &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}},
}
client, err := clientCfg.NewClient(context.TODO())
if err != nil {
log.Fatal(err)
}
// figure out our IP address -- we'll tell Apstra to fire protobuf structures at this address
ourIp, err := ourIpForPeer(net.ParseIP(os.Getenv("APSTRA_HOST")))
if err != nil {
log.Fatal(err)
}
// create local stream target object
streamTargetConfig := apstra.StreamTargetCfg{
Certificate: nil, // apstra doesn't support TLS (?!?)
Key: nil, // apstra doesn't support TLS (?!?)
SequencingMode: apstra.StreamingConfigSequencingModeSequenced,
StreamingType: apstra.StreamingConfigStreamingTypeAlerts,
Protocol: apstra.StreamingConfigProtocolProtoBufOverTcp,
Port: 9999,
AosTargetHostname: ourIp.String(),
}
streamTarget, err := apstra.NewStreamTarget(&streamTargetConfig)
if err != nil {
log.Fatal(err)
}
// start the stream target service listening for incoming messages
streamMsgChan, streamErrChan, err := streamTarget.Start()
if err != nil {
log.Fatal(err)
}
quitChan := make(chan os.Signal, 1)
signal.Notify(quitChan, os.Interrupt)
// tell apstra to send protobuf messages to us
err = streamTarget.Register(context.TODO(), client)
if err != nil {
log.Fatal(err)
}
// check the API knows about our receiver
ids, err := client.GetAllStreamingConfigIds(context.TODO())
if err != nil {
log.Fatal(err)
}
log.Printf("Our Streaming Receiver has ID '%s'. The complete set of receivers is %s", streamTarget.Id(), ids)
// loop until ctrl-c, print messages+errors as they arrive
for {
select {
case <-quitChan:
err = streamTarget.Unregister(context.TODO())
if err != nil {
log.Fatal(err)
}
return
case msg := <-streamMsgChan:
log.Println(msg.Message.String())
case err := <-streamErrChan:
log.Println(err.Error())
}
}
}