Repository: vmware/govmomi
Pull request #2784 (2022-03-22 13:54:57)

Add Alarm Manager Example
## Description

Closes: #2783 (see: https://github.com/vmware/govmomi/discussions/2782)
Signed-off-by: Michael Gasch <mgasch@vmware.com>

## Type of change

Please mark options that are relevant:

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to
  not work as expected)
- [ ] This change requires a documentation update
- [ ] Build related change

## How Has This Been Tested?

- [x] Tested against vCenter 6.7

## Checklist:

- [x] My code follows the CONTRIBUTION [guidelines](./../CONTRIBUTING.md) of
  this project
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged

embano1: Ah sorry I forgot we run these tests in CI against vcsim 🤦🏻‍♂️ 

Patch:
diff --git a/examples/alarms/main.go b/examples/alarms/main.go
new file mode 100644
index 00000000..f96dfef6
--- /dev/null
+++ b/examples/alarms/main.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+	"context"
+	"flag"
+	"fmt"
+
+	"github.com/dougm/pretty"
+
+	"github.com/vmware/govmomi/examples"
+	"github.com/vmware/govmomi/property"
+	"github.com/vmware/govmomi/vim25"
+	"github.com/vmware/govmomi/vim25/methods"
+	"github.com/vmware/govmomi/vim25/mo"
+	"github.com/vmware/govmomi/vim25/types"
+)
+
+func main() {
+	var limit int
+	flag.IntVar(&limit, "limit", 10, "maximum number of alarms to retrieve")
+
+	examples.Run(func(ctx context.Context, c *vim25.Client) error {
+		pc := property.DefaultCollector(c)
+		am := c.ServiceContent.AlarmManager
+
+		fmt.Println("retrieving all alarms")
+		alarms, err := methods.GetAlarm(ctx, c, &types.GetAlarm{
+			This:   *am,
+			Entity: nil, // if not set, alarms are returned for all visible entities
+		})
+		if err != nil {
+			return fmt.Errorf("could not get alarms: %w", err)
+		}
+
+		counter := 0
+		for _, a := range alarms.Returnval {
+			counter++
+			fmt.Printf("retrieving details for alarm %q\n", a.String())
+
+			var info mo.Alarm
+			if err = pc.RetrieveOne(ctx, a, nil, &info); err != nil {
+				return fmt.Errorf("retrieve alarm info: %w", err)
+			}
+
+			_, err = pretty.Println(info)
+			if err != nil {
+				return fmt.Errorf("print alarm: %w", err)
+			}
+
+			if counter == limit {
+				fmt.Printf("reached maximum number of alarms to read (limit=%d)\n", limit)
+				break
+			}
+		}
+
+		return nil
+	})
+}


Test patch:
diff --git a/govc/test/vcsim.bats b/govc/test/vcsim.bats
index 83d66dac..cd06e9ce 100755
--- a/govc/test/vcsim.bats
+++ b/govc/test/vcsim.bats
@@ -35,8 +35,8 @@ EOF
 
   # compile + run examples against vcsim
   for main in ../../examples/*/main.go ; do
-    # TODO: #2567
-    if [[ $main =~ "task" ]]; then
+    # TODO: #2567 #2476
+    if [[ $main =~ task|alarm ]]; then
       continue
     fi
     run go run "$main" -insecure -url "$GOVC_URL"