51 lines
1018 B
Go
51 lines
1018 B
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type UpdateAgentOptions struct {
|
|
Status *int
|
|
}
|
|
|
|
type UpdateAgentOptionFunc func(*UpdateAgentOptions)
|
|
|
|
func WithAgentStatus(status int) UpdateAgentOptionFunc {
|
|
return func(opts *UpdateAgentOptions) {
|
|
opts.Status = &status
|
|
}
|
|
}
|
|
|
|
func (c *Client) UpdateAgent(ctx context.Context, agentID datastore.AgentID, funcs ...UpdateAgentOptionFunc) (*datastore.Agent, error) {
|
|
opts := &UpdateAgentOptions{}
|
|
for _, fn := range funcs {
|
|
fn(opts)
|
|
}
|
|
|
|
payload := map[string]any{}
|
|
|
|
if opts.Status != nil {
|
|
payload["status"] = *opts.Status
|
|
}
|
|
|
|
response := withResponse[struct {
|
|
Agent *datastore.Agent `json:"agent"`
|
|
}]()
|
|
|
|
path := fmt.Sprintf("/api/v1/agents/%d", agentID)
|
|
|
|
if err := c.apiPut(ctx, path, payload, &response); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
if response.Error != nil {
|
|
return nil, errors.WithStack(response.Error)
|
|
}
|
|
|
|
return response.Data.Agent, nil
|
|
}
|