summaryrefslogtreecommitdiffstats
path: root/lib/agent/apiv1-targets.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/agent/apiv1-targets.go')
-rw-r--r--lib/agent/apiv1-targets.go105
1 files changed, 105 insertions, 0 deletions
diff --git a/lib/agent/apiv1-targets.go b/lib/agent/apiv1-targets.go
index 5a7862a..cc08822 100644
--- a/lib/agent/apiv1-targets.go
+++ b/lib/agent/apiv1-targets.go
@@ -18,6 +18,7 @@
package agent
import (
+ "encoding/json"
"fmt"
"net/http"
@@ -52,6 +53,110 @@ func (s *APIService) targetsPassthroughInit(svr *XdsServer) error {
return nil
}
+// targetsEventsForwardInit Register events forwarder for targets
+func (s *APIService) targetsEventsForwardInit(svr *XdsServer) error {
+
+ if !svr.Connected {
+ return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID)
+ }
+
+ // Forward Target events from XDS-server to client
+ if _, err := svr.EventOn(xsapiv1.EVTTargetAdd, xaapiv1.EVTTargetAdd, s._targetsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetAdd, err)
+ return err
+ }
+ if _, err := svr.EventOn(xsapiv1.EVTTargetRemove, xaapiv1.EVTTargetRemove, s._targetsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetRemove, err)
+ return err
+ }
+ if _, err := svr.EventOn(xsapiv1.EVTTargetStateChange, xaapiv1.EVTTargetStateChange, s._targetsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetStateChange, err)
+ return err
+ }
+
+ return nil
+}
+
+func (s *APIService) _targetsEventCB(privD interface{}, data interface{}) error {
+ evt := xsapiv1.EventMsg{}
+ d, err := json.Marshal(data)
+ if err != nil {
+ s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data)
+ return err
+ }
+ if err = json.Unmarshal(d, &evt); err != nil {
+ s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d))
+ return err
+ }
+
+ // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig
+ target, err := evt.DecodeTargetEvent()
+ if err != nil {
+ s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data)
+ return err
+ }
+
+ evtName := privD.(string)
+
+ if err := s.events.Emit(evtName, target, ""); err != nil {
+ s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err)
+ return err
+ }
+ return nil
+}
+
+// terminalsEventsForwardInit Register events forwarder for terminals
+func (s *APIService) terminalsEventsForwardInit(svr *XdsServer) error {
+
+ if !svr.Connected {
+ return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID)
+ }
+
+ // Forward Terminal events from XDS-server to client
+ if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalAdd, xaapiv1.EVTTargetTerminalAdd, s._terminalsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalAdd, err)
+ return err
+ }
+ if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalRemove, xaapiv1.EVTTargetTerminalRemove, s._terminalsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalRemove, err)
+ return err
+ }
+ if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalStateChange, xaapiv1.EVTTargetTerminalStateChange, s._terminalsEventCB); err != nil {
+ s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalStateChange, err)
+ return err
+ }
+
+ return nil
+}
+
+func (s *APIService) _terminalsEventCB(privD interface{}, data interface{}) error {
+ evt := xsapiv1.EventMsg{}
+ d, err := json.Marshal(data)
+ if err != nil {
+ s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data)
+ return err
+ }
+ if err = json.Unmarshal(d, &evt); err != nil {
+ s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d))
+ return err
+ }
+
+ // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig
+ target, err := evt.DecodeTerminalEvent()
+ if err != nil {
+ s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data)
+ return err
+ }
+
+ evtName := privD.(string)
+
+ if err := s.events.Emit(evtName, target, ""); err != nil {
+ s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err)
+ return err
+ }
+ return nil
+}
+
// GetServerFromTargetID Retrieve XDS Server definition from a target ID
func (s *APIService) GetServerFromTargetID(targetID, termID string) (*XdsServer, string, error) {