summaryrefslogtreecommitdiff
path: root/pkg/bindings/system/system.go
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2020-03-14 14:01:20 -0500
committerBrent Baude <bbaude@redhat.com>2020-03-19 09:38:00 -0500
commitd1c26af144b314f30cd69d42e33ab8cbacb080ff (patch)
treec8b2bb6ce98631334e539086b2aa490c7e2bc770 /pkg/bindings/system/system.go
parente87fe4dbbbc55be42d7a31f5415f55d2ff99f81b (diff)
downloadpodman-d1c26af144b314f30cd69d42e33ab8cbacb080ff.tar.gz
podman-d1c26af144b314f30cd69d42e33ab8cbacb080ff.tar.bz2
podman-d1c26af144b314f30cd69d42e33ab8cbacb080ff.zip
apiv2 add bindings for logs|events
add go-bindings for logs and events. tests were also added. Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'pkg/bindings/system/system.go')
-rw-r--r--pkg/bindings/system/system.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/pkg/bindings/system/system.go b/pkg/bindings/system/system.go
new file mode 100644
index 000000000..fce8bbb8e
--- /dev/null
+++ b/pkg/bindings/system/system.go
@@ -0,0 +1,61 @@
+package system
+
+import (
+ "context"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/url"
+
+ "github.com/containers/libpod/pkg/api/handlers"
+ "github.com/containers/libpod/pkg/bindings"
+ "github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
+)
+
+// Events allows you to monitor libdpod related events like container creation and
+// removal. The events are then passed to the eventChan provided. The optional cancelChan
+// can be used to cancel the read of events and close down the HTTP connection.
+func Events(ctx context.Context, eventChan chan (handlers.Event), cancelChan chan bool, since, until *string, filters map[string][]string) error {
+ conn, err := bindings.GetClient(ctx)
+ if err != nil {
+ return err
+ }
+ params := url.Values{}
+ if since != nil {
+ params.Set("since", *since)
+ }
+ if until != nil {
+ params.Set("until", *until)
+ }
+ if filters != nil {
+ filterString, err := bindings.FiltersToString(filters)
+ if err != nil {
+ return errors.Wrap(err, "invalid filters")
+ }
+ params.Set("filters", filterString)
+ }
+ response, err := conn.DoRequest(nil, http.MethodGet, "/events", params)
+ if err != nil {
+ return err
+ }
+ if cancelChan != nil {
+ go func() {
+ <-cancelChan
+ err = response.Body.Close()
+ logrus.Error(errors.Wrap(err, "unable to close event response body"))
+ }()
+ }
+ dec := json.NewDecoder(response.Body)
+ for {
+ e := handlers.Event{}
+ if err := dec.Decode(&e); err != nil {
+ if err == io.EOF {
+ break
+ }
+ return errors.Wrap(err, "unable to decode event response")
+ }
+ eventChan <- e
+ }
+ return nil
+}