Skip to content

Commit

Permalink
govc: add event.post command
Browse files Browse the repository at this point in the history
This command can be used to trigger alarms and test use of EventEx in general.
  • Loading branch information
dougm committed Aug 8, 2024
1 parent 18e4354 commit 2975ade
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
80 changes: 80 additions & 0 deletions govc/events/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package events

import (
"context"
"flag"

"github.com/vmware/govmomi/event"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vim25/types"
)

type post struct {
*flags.DatacenterFlag

types.EventEx
}

func init() {
cli.Register("event.post", &post{}, true)
}

func (cmd *post) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)

f.StringVar(&cmd.EventTypeId, "i", "", "Event Type ID")
f.StringVar(&cmd.Message, "m", "", "Event message")
f.StringVar(&cmd.Severity, "s", string(types.EventEventSeverityInfo), "Event severity")
}

func (cmd *post) Usage() string {
return "PATH"
}

func (cmd *post) Description() string {
return `Post Event.
Examples:
govc event.post -s warning -i com.vmware.wcp.RegisterVM.failure $vm
govc event.post -s info -i com.vmware.wcp.RegisterVM.success $vm
govc event.post -m "cluster degraded" /dc1/host/cluster1`
}

func (cmd *post) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

c, err := cmd.Client()
if err != nil {
return err
}

obj, err := cmd.ManagedObject(ctx, f.Arg(0))
if err != nil {
return err
}

cmd.ObjectType = obj.Type
cmd.ObjectId = obj.Value

return event.NewManager(c).PostEvent(ctx, &cmd.EventEx)
}
14 changes: 14 additions & 0 deletions govc/test/events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ load test_helper
govc events 'vm/*'
govc events -json 'vm/*' | jq .
}

@test "events post" {
vcsim_env

export GOVC_SHOW_UNRELEASED=true

run govc event.post -m testing123
assert_failure

run govc event.post -m testing123 /DC0
assert_success

govc events | grep testing123
}

0 comments on commit 2975ade

Please sign in to comment.