A Go client library for the OSAPI REST API.
osapi-sdk/examples/basic/main.go
Lines 21 to 81 in fe66750
| // Package main demonstrates basic usage of the OSAPI SDK. | |
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "os" | |
| "github.com/osapi-io/osapi-sdk/pkg/osapi" | |
| ) | |
| func main() { | |
| url := os.Getenv("OSAPI_URL") | |
| if url == "" { | |
| url = "http://localhost:8080" | |
| } | |
| token := os.Getenv("OSAPI_TOKEN") | |
| if token == "" { | |
| log.Fatal("OSAPI_TOKEN environment variable is required") | |
| } | |
| client, err := osapi.New(url, token) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| ctx := context.Background() | |
| // Get hostname from any available agent. | |
| hostnameResp, err := client.Node.Hostname(ctx, "_any") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("Hostname response status: %d\n", hostnameResp.StatusCode()) | |
| // Execute a command on any available agent. | |
| execResp, err := client.Node.Exec(ctx, osapi.ExecRequest{ | |
| Command: "uptime", | |
| Target: "_any", | |
| }) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("Exec response status: %d\n", execResp.StatusCode()) | |
| // List recent audit log entries. | |
| auditResp, err := client.Audit.List(ctx, 10, 0) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("Audit response status: %d\n", auditResp.StatusCode()) | |
| // Check API server health. | |
| healthResp, err := client.Health.Liveness(ctx) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("Health response status: %d\n", healthResp.StatusCode()) | |
| } |
See the examples section for additional use cases.
See the generated documentation for details on available packages and functions.
See the Development guide for prerequisites, setup, and conventions. See the Contributing guide before submitting a PR.
The MIT License.