mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-18 22:48:57 +02:00
Controller info endpoint and API integration examples (#75)
* Controller API: introduce controller's information endpoint * Prevent generation of empty events after channel closure * Allow events to be buffered in the events channel * Controller API: introduce controller's information endpoint[1] * IntegrationGuide.md: a couple of Python and Golang examples * Rephrase a sentence Co-authored-by: Fedor Korotkov <fedor.korotkov@gmail.com> --------- Co-authored-by: Fedor Korotkov <fedor.korotkov@gmail.com>
This commit is contained in:
co-authored by
Fedor Korotkov
parent
84633d0e45
commit
77656517fd
@@ -266,3 +266,9 @@ func (client *Client) ServiceAccounts() *ServiceAccountsService {
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) Controller() *ControllerService {
|
||||
return &ControllerService{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ControllerService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (service *ControllerService) Info(ctx context.Context) (v1.ControllerInfo, error) {
|
||||
var controllerInfo v1.ControllerInfo
|
||||
|
||||
err := service.client.request(ctx, http.MethodGet, "controller/info", nil, &controllerInfo,
|
||||
nil)
|
||||
if err != nil {
|
||||
return controllerInfo, err
|
||||
}
|
||||
|
||||
return controllerInfo, nil
|
||||
}
|
||||
+13
-6
@@ -20,7 +20,7 @@ func NewEventStreamer(client *Client, endpoint string) *EventStreamer {
|
||||
streamer := &EventStreamer{
|
||||
client: client,
|
||||
endpoint: endpoint,
|
||||
eventsChannel: make(chan v1.Event),
|
||||
eventsChannel: make(chan v1.Event, 64),
|
||||
}
|
||||
go streamer.stream()
|
||||
return streamer
|
||||
@@ -46,16 +46,23 @@ func (streamer *EventStreamer) stream() {
|
||||
}
|
||||
|
||||
func (streamer *EventStreamer) readAvailableEvents() ([]v1.Event, bool) {
|
||||
// blocking wait for at least one event
|
||||
result := []v1.Event{<-streamer.eventsChannel}
|
||||
var result []v1.Event
|
||||
|
||||
// blocking wait for at least one event
|
||||
nextEvent, ok := <-streamer.eventsChannel
|
||||
if !ok {
|
||||
return result, true
|
||||
}
|
||||
result = append(result, nextEvent)
|
||||
|
||||
// non-blocking wait for more events, if any
|
||||
for {
|
||||
select {
|
||||
case nextEvent, more := <-streamer.eventsChannel:
|
||||
result = append(result, nextEvent)
|
||||
if !more {
|
||||
case nextEvent, ok := <-streamer.eventsChannel:
|
||||
if !ok {
|
||||
return result, true
|
||||
}
|
||||
result = append(result, nextEvent)
|
||||
default:
|
||||
return result, false
|
||||
}
|
||||
|
||||
@@ -90,3 +90,8 @@ const (
|
||||
// (either via API or from within a VM via `sudo shutdown -now`).
|
||||
VMStatusStopped VMStatus = "stopped"
|
||||
)
|
||||
|
||||
type ControllerInfo struct {
|
||||
Version string `json:"version"`
|
||||
Commit string `json:"commit"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user