Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add methods blink, update and device to supervisor v1 struct #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions supervisorv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,104 @@ func (s *SupervisorV1Service) Reboot(ctx context.Context, force bool) error {
&request{Force: force},
)
if err != nil {
return fmt.Errorf("unable to create restart service request: %v", err)
return fmt.Errorf("unable to create restart service request: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change? As this is an open source library, I'd be reluctant to exposing the underlying implementation error without a good reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand that though we probably want to respond differently when we receive an internal server error versus a bad request response. If we can wrap this error here, on a higher level we can decide whether to do a retry or ask an operator for help.

}
var resp struct {
Data string `json:"Data"`
Error string `json:"Error"`
}
err = s.client.Do(req, &resp)
if err != nil {
return fmt.Errorf("unable to reboot device: %v", err)
return fmt.Errorf("unable to reboot device: %w", err)
}
if resp.Data != "OK" {
return fmt.Errorf("unable to reboot device: %v", resp.Error)
}
return nil
}

// Blink starts a blink pattern on a LED for 15 seconds, if your device has one.
// https://docs.balena.io/reference/supervisor/supervisor-api/#post-v1blink
func (s *SupervisorV1Service) Blink(ctx context.Context) error {
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv1BasePath+"/blink",
nil,
)
if err != nil {
return fmt.Errorf("unable to create blink request: %w", err)
}

err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("unable to start blink pattern: %w", err)
}

return nil
}

// Update triggers a check for the target state of configurations and app services.
// https://docs.balena.io/reference/supervisor/supervisor-api/#post-v1update
func (s *SupervisorV1Service) Update(ctx context.Context, force bool) error {
type request struct {
Force bool `json:"force"`
}
req, err := s.newRequest(
ctx,
http.MethodPost,
supervisorv1BasePath+"/update",
&request{Force: force},
)
if err != nil {
return fmt.Errorf("unable to create update request: %w", err)
}

err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("unable to trigger update: %w", err)
}

return nil
}

type SupervisorV1DeviceResponse struct {
APIPort uint `json:"api_port,omitempty"`
Commit string `json:"commit,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
MACAddress string `json:"mac_address,omitempty"`
Status string `json:"status,omitempty"`
DownloadProgress uint `json:"download_progress,omitempty"`
OSVersion string `json:"os_version,omitempty"`
SupervisorVersion string `json:"supervisor_version,omitempty"`
UpdatePending bool `json:"update_pending,omitempty"`
UpdateDownloaded bool `json:"update_downloaded,omitempty"`
UpdateFailed bool `json:"update_failed,omitempty"`
}

// Device returns the current device state.
// https://docs.balena.io/reference/supervisor/supervisor-api/#get-v1device
func (s *SupervisorV1Service) Device(ctx context.Context) (*SupervisorV1DeviceResponse, error) {
req, err := s.newRequest(
ctx,
http.MethodGet,
supervisorv1BasePath+"/device",
nil,
)
if err != nil {
return nil, fmt.Errorf("unable to create device request: %w", err)
}

var response SupervisorV1DeviceResponse

err = s.client.Do(req, &response)
if err != nil {
return nil, fmt.Errorf("unable to get device state: %w", err)
}

return &response, nil
}

func (s *SupervisorV1Service) newRequest(
ctx context.Context,
method string,
Expand Down
Loading