Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from embano1/issue-9
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Gasch authored Apr 5, 2022
2 parents 32c2095 + a4c5a7e commit dea4548
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
3 changes: 3 additions & 0 deletions client/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func readKey(key string) (string, error) {
// keep-alive configured via environment variables. Commonly used managers are
// exposed for quick access.
//
// A custom logger (zap.Logger) can be injected into the context via the logger
// package.
//
// Use Logout() to release resources and perform a clean logout from vCenter.
func New(ctx context.Context) (*Client, error) {
vclient, err := NewSOAP(ctx)
Expand Down
3 changes: 2 additions & 1 deletion client/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestNewClient(t *testing.T) {
"VCENTER_INSECURE": "",
"VCENTER_SECRET_PATH": "",
},
wantErr: "certificate signed by unknown authority",
// different go versions throw different errors, this is a generic catch all
wantErr: "create vsphere SOAP client: Post",
},
}

Expand Down
67 changes: 53 additions & 14 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,87 @@ package main

import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"

"github.com/vmware/govmomi/event"
"github.com/vmware/govmomi/vim25/types"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/embano1/vsphere/client"
"github.com/embano1/vsphere/event"
"github.com/embano1/vsphere/logger"
)

func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer cancel()

l, err := zap.NewDevelopment()
cfg := zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder

l, err := cfg.Build()
if err != nil {
panic("create logger: " + err.Error())
}
l = l.Named("demo-app")

// inject and overwrite default logger
ctx = logger.Set(ctx, l)

l.Debug("creating vsphere client")
c, err := client.New(ctx)
if err != nil {
l.Fatal("create vsphere client", zap.Error(err))
l.Fatal("could not create vsphere client", zap.Error(err))
}
defer c.Logout()

mgr := event.NewManager(c.SOAP.Client)
objs := []types.ManagedObjectReference{c.SOAP.ServiceContent.RootFolder}
handler := func(reference types.ManagedObjectReference, events []types.BaseEvent) error {
for _, e := range events {
l.Debug("new event", zap.Any("event", e))
}
return nil
// show how to use filters
filterEvents := []string{"UserLoginSessionEvent", "VmPoweredOnEvent", "DrsVmPoweredOnEvent"}
filters := []event.Filter{event.WithEventTypeID(filterEvents)}

// retrieve (filtered) events for all vCenter objects
root := c.SOAP.ServiceContent.RootFolder
collector, err := event.NewHistoryCollector(ctx, c.Events, root, filters...)
if err != nil {
l.Fatal("could not create event stream", zap.Error(err))
}

l.Debug("starting event stream")
if err = mgr.Events(ctx, objs, 10, true, false, handler); err != nil {
l.Fatal("stream events", zap.Error(err))
const (
batch = 10
pollInterval = time.Second * 3
)

ticker := time.NewTicker(pollInterval)
defer ticker.Stop()

l.Info("starting event stream", zap.Any("forEvents", filterEvents))
LOOP:
for {
select {
case <-ctx.Done():
l.Debug("shutting down")
break LOOP

case <-ticker.C:
l.Debug("retrieving events")
events, err := collector.ReadNextEvents(ctx, batch)
if err != nil && !errors.Is(err, context.Canceled) {
l.Fatal("could not read events", zap.Error(err))
}

if len(events) == 0 {
l.Debug("no new events")
continue
}

for _, e := range events {
l.Info("retrieved new event", zap.Any("event", e))
}
}
}

l.Debug("shutdown complete")
}

0 comments on commit dea4548

Please sign in to comment.