Skip to content

Commit

Permalink
ref: Refactor watcher system to use genericWatcher; improve project d…
Browse files Browse the repository at this point in the history
…ocumentation (#20)

* Refactor watcher system to use genericWatcher; improve docs

* oops

* Add examples to package docs
  • Loading branch information
lgarber-akamai authored Dec 12, 2023
1 parent fae2920 commit 68e7efa
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 145 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func main() {
}
```

For more examples, visit the [examples directory](./examples).

## Documentation

See [godoc](https://pkg.go.dev/github.com/linode/go-metadata) for a complete documentation reference.
Expand Down
36 changes: 36 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
go-metadata allows Go projects to easily interact with the Linode Metadata Service.
Basic example:
package main
import (
"context"
"fmt"
"log"
metadata "github.com/linode/go-metadata"
)
func main() {
// Create a new client
client, err := metadata.NewClient(context.Background())
if err != nil {
log.Fatal(err)
}
// Retrieve metadata about the current instance from the metadata API
instanceInfo, err := client.GetInstance(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("Instance Label:", instanceInfo.Label)
}
For more examples see: https://github.com/linode/go-metadata/tree/ref/watcher-refactor/examples
To learn more about the Linode Metadata Service, see the official guide: https://www.linode.com/docs/products/compute/compute-instances/guides/metadata/?tabs=linode-api
*/

package metadata
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# go-metadata Examples

This directory contains various runnable examples for using this package.

- [Instance Watcher](./instancewatcher) - Watch for changes to the current Linode instance.
- [Network Watcher](./networkwatcher) - Watch for changes to the current Linode instance's networking.
11 changes: 11 additions & 0 deletions examples/instancewatcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instance Watcher Example

This example makes use of the InstanceWatcher struct to watch for changes to the current Linode instance.

### Running this Example

NOTE: This example must be run from within a Linode instance.

```bash
go run main.go
```
39 changes: 39 additions & 0 deletions examples/instancewatcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
metadata "github.com/linode/go-metadata"
"log"
"time"
)

func main() {
ctx := context.Background()

// Create a new client
client, err := metadata.NewClient(ctx)
if err != nil {
log.Fatal(err)
}

// Create a new instance watcher
instanceWatcher := client.NewInstanceWatcher(
metadata.WatcherWithInterval(10 * time.Second),
)

// Start the network watcher in a goroutine.
go instanceWatcher.Start(ctx)

// Wait for changes
for {
select {
case data := <-instanceWatcher.Updates:
log.Printf(
"Change to instance detected.\nNew data: %v\n",
data,
)
case err := <-instanceWatcher.Errors:
log.Fatalf("Got error from instance watcher: %s", err)
}
}
}
11 changes: 11 additions & 0 deletions examples/networkwatcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Network Watcher Example

This example makes use of the NetworkWatcher struct to watch for changes to the current Linode instance's networking.

### Running this Example

NOTE: This example must be run from within a Linode instance.

```bash
go run main.go
```
39 changes: 39 additions & 0 deletions examples/networkwatcher/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
metadata "github.com/linode/go-metadata"
"log"
"time"
)

func main() {
ctx := context.Background()

// Create a new client
client, err := metadata.NewClient(ctx)
if err != nil {
log.Fatal(err)
}

// Create a new network watcher
networkWatcher := client.NewNetworkWatcher(
metadata.WatcherWithInterval(10 * time.Second),
)

// Start the network watcher in a goroutine.
go networkWatcher.Start(ctx)

// Wait for changes
for {
select {
case data := <-networkWatcher.Updates:
log.Printf(
"Change to network configuration detected.\nNew data: %v\n",
data,
)
case err := <-networkWatcher.Errors:
log.Fatalf("Got error from network watcher: %s", err)
}
}
}
4 changes: 2 additions & 2 deletions test/integration/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestNetworkWatcher(t *testing.T) {
assert.NoError(t, err)

watcher := metadataClient.NewNetworkWatcher(metadata.WatcherWithInterval(1 * time.Second))
watcher.Start(ctx)
go watcher.Start(ctx)
numUpdates := 0
for i := 1; i <= 5; i++ {
updateData := <-watcher.Updates
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestInstanceWatcher(t *testing.T) {
assert.NoError(t, err)

watcher := metadataClient.NewInstanceWatcher(metadata.WatcherWithInterval(1 * time.Second))
watcher.Start(ctx)
go watcher.Start(ctx)
numUpdates := 0
for i := 1; i <= 5; i++ {
updateData := <-watcher.Updates
Expand Down
143 changes: 0 additions & 143 deletions watcher.go

This file was deleted.

Loading

0 comments on commit 68e7efa

Please sign in to comment.