You've already forked go-ztcentral
mirror of
https://github.com/zerotier/go-ztcentral.git
synced 2026-05-22 16:25:13 -07:00
a17aaa2ceb
This patch is a large patch and should be committed only for v0.3 or up. It changes the API in breaking ways. This leverages https://github.com/deepmap/oapi-codegen to generate a client which we can use to talk to central; the idea here is that this is a future-proofing exercise, as well as a means to have more meaningful conversations with central later. The makefile was tuned to download the spec, and `//go:generate` lines were added to generate it automatically as a part of our release process. Downloading the spec is still a manual step; assuming we will want to stage this with releases of central later. The client was ported to use the inner `pkg/spec` client which does better validation and puts additional burden on the programmer to make sure they are submitting the correct content. Finally, there are a few issues this change exposed: - https://github.com/zerotier/terraform-provider-zerotier/issues/1 is proven as of this client build; I had built a test for this but it was busted, and now we actually need to address this in central. - Lots of optional paramaters (that aren't). This will cause issues for users, and we should make an effort to add `nullable: false` or `required: true` in more spots where necessary. This was most notable in structs containing the network and node ID. - Finally, the notion of a `member ID` has been reverted to use the idioms in the spec, which rewrite it to be called a `node ID`. I have been told that this is antiquated language and we should expect a change. I just wanted to make a note of it for future users. Signed-off-by: Erik Hollensbe <linux@hollensbe.org>
40 lines
689 B
Go
40 lines
689 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
|
|
ztcentral "github.com/zerotier/go-ztcentral"
|
|
)
|
|
|
|
func main() {
|
|
c, err := ztcentral.NewClient(os.Getenv("ZEROTIER_CENTRAL_API_KEY"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// get list of networks
|
|
networks, err := c.GetNetworks(ctx)
|
|
if err != nil {
|
|
log.Println("error:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
// print networks and members
|
|
for _, n := range networks {
|
|
log.Printf("%s\t%s", *n.Id, *n.Config.Name)
|
|
members, err := c.GetMembers(ctx, *n.Id)
|
|
if err != nil {
|
|
log.Println("error:", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
for _, m := range members {
|
|
log.Printf("\t%s\t %s", *m.NodeId, *m.Name)
|
|
}
|
|
}
|
|
}
|