2023-02-28 15:50:35 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UpdateAgentOptions struct {
|
2023-03-07 23:10:42 +01:00
|
|
|
Status *int
|
2023-04-01 13:28:18 +02:00
|
|
|
Label *string
|
2023-03-07 23:10:42 +01:00
|
|
|
Options []OptionFunc
|
2023-02-28 15:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateAgentOptionFunc func(*UpdateAgentOptions)
|
|
|
|
|
|
|
|
func WithAgentStatus(status int) UpdateAgentOptionFunc {
|
|
|
|
return func(opts *UpdateAgentOptions) {
|
|
|
|
opts.Status = &status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 13:28:18 +02:00
|
|
|
func WithAgentLabel(label string) UpdateAgentOptionFunc {
|
|
|
|
return func(opts *UpdateAgentOptions) {
|
|
|
|
opts.Label = &label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
func WithUpdateAgentsOptions(funcs ...OptionFunc) UpdateAgentOptionFunc {
|
|
|
|
return func(opts *UpdateAgentOptions) {
|
|
|
|
opts.Options = funcs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:50:35 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-01 13:28:18 +02:00
|
|
|
if opts.Label != nil {
|
|
|
|
payload["label"] = *opts.Label
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:50:35 +01:00
|
|
|
response := withResponse[struct {
|
|
|
|
Agent *datastore.Agent `json:"agent"`
|
|
|
|
}]()
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/api/v1/agents/%d", agentID)
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
if opts.Options == nil {
|
|
|
|
opts.Options = make([]OptionFunc, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.apiPut(ctx, path, payload, &response, opts.Options...); err != nil {
|
2023-02-28 15:50:35 +01:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
|
|
|
return nil, errors.WithStack(response.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Data.Agent, nil
|
|
|
|
}
|