Skip to content

Instantly share code, notes, and snippets.

@mjs
Created July 27, 2017 22:01
Show Gist options
  • Save mjs/43c83cc6e0e9d3b85d7f5f50f7e501c5 to your computer and use it in GitHub Desktop.
Save mjs/43c83cc6e0e9d3b85d7f5f50f7e501c5 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/juju/juju/api"
"github.com/juju/juju/juju"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/state/multiwatcher"
)
func connectAPI() api.Connection {
store := jujuclient.NewFileClientStore()
controllerName, err := store.CurrentController()
checkErr("CurrentController", err)
accountDetails, err := store.AccountDetails(controllerName)
checkErr("AccountDetails", err)
modelName, err := store.CurrentModel(controllerName)
checkErr("CurrentModel", err)
model, err := store.ModelByName(controllerName, modelName)
checkErr("ModelByName", err)
params := juju.NewAPIConnectionParams{
ControllerName: controllerName,
Store: store,
AccountDetails: accountDetails,
ModelUUID: model.ModelUUID,
}
conn, err := juju.NewAPIConnection(params)
checkErr("NewAPIConnection", err)
return conn
}
func checkErr(label string, err error) {
if err != nil {
panic(fmt.Sprintf("%s: %v", label, err))
}
}
func main() {
conn := connectAPI()
client := conn.Client()
watcher, err := client.WatchAll()
checkErr("watch", err)
for {
ds, err := watcher.Next()
checkErr("next", err)
for _, d := range ds {
if d.Removed {
continue
}
if m, ok := d.Entity.(*multiwatcher.MachineInfo); ok {
fmt.Printf("machine %s: %s %q\n",
m.Id,
m.InstanceStatus.Current,
m.InstanceStatus.Message,
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment