emissary/internal/client/update_agent.go

62 lines
1.2 KiB
Go
Raw Normal View History

package client
import (
"context"
"fmt"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"github.com/pkg/errors"
)
type UpdateAgentOptions struct {
Status *int
Options []OptionFunc
}
type UpdateAgentOptionFunc func(*UpdateAgentOptions)
func WithAgentStatus(status int) UpdateAgentOptionFunc {
return func(opts *UpdateAgentOptions) {
opts.Status = &status
}
}
func WithUpdateAgentsOptions(funcs ...OptionFunc) UpdateAgentOptionFunc {
return func(opts *UpdateAgentOptions) {
opts.Options = funcs
}
}
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 opts.Options == nil {
opts.Options = make([]OptionFunc, 0)
}
if err := c.apiPut(ctx, path, payload, &response, opts.Options...); err != nil {
return nil, errors.WithStack(err)
}
if response.Error != nil {
return nil, errors.WithStack(response.Error)
}
return response.Data.Agent, nil
}