Add agent functions (#1754)

This commit is contained in:
Anbraten 2023-05-14 16:46:54 +02:00 committed by GitHub
parent 3605979df0
commit 1f9d943564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 0 deletions

View file

@ -54,6 +54,9 @@ const (
pathPipelineQueue = "%s/api/pipelines"
pathQueue = "%s/api/queue"
pathLogLevel = "%s/api/log-level"
pathAgents = "%s/api/agents"
pathAgent = "%s/api/agents/%d"
pathAgentTasks = "%s/api/agents/%d/tasks"
// TODO: implement endpoints
// pathFeed = "%s/api/user/feed"
// pathVersion = "%s/version"
@ -515,6 +518,41 @@ func (c *client) CronGet(owner, repo string, cronID int64) (*Cron, error) {
return out, c.get(uri, out)
}
func (c *client) AgentList() ([]*Agent, error) {
out := make([]*Agent, 0, 5)
uri := fmt.Sprintf(pathAgents, c.addr)
return out, c.get(uri, &out)
}
func (c *client) Agent(agentID int64) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgent, c.addr, agentID)
return out, c.get(uri, out)
}
func (c *client) AgentCreate(in *Agent) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgents, c.addr)
return out, c.post(uri, in, out)
}
func (c *client) AgentUpdate(in *Agent) (*Agent, error) {
out := new(Agent)
uri := fmt.Sprintf(pathAgent, c.addr, in.ID)
return out, c.patch(uri, in, out)
}
func (c *client) AgentDelete(agentID int64) error {
uri := fmt.Sprintf(pathAgent, c.addr, agentID)
return c.delete(uri)
}
func (c *client) AgentTasksList(agentID int64) ([]*Task, error) {
out := make([]*Task, 0, 5)
uri := fmt.Sprintf(pathAgentTasks, c.addr, agentID)
return out, c.get(uri, &out)
}
//
// http request helper functions
//

View file

@ -198,4 +198,22 @@ type Client interface {
// CronUpdate update an existing cron job of a repo
CronUpdate(owner, repo string, cron *Cron) (*Cron, error)
// AgentList returns a list of all registered agents
AgentList() ([]*Agent, error)
// Agent returns an agent by id
Agent(int64) (*Agent, error)
// AgentCreate creates a new agent
AgentCreate(*Agent) (*Agent, error)
// AgentUpdate updates an existing agent
AgentUpdate(*Agent) (*Agent, error)
// AgentDelete deletes an agent
AgentDelete(int64) error
// AgentTasksList returns a list of all tasks executed by an agent
AgentTasksList(int64) ([]*Task, error)
}

View file

@ -196,4 +196,31 @@ type (
Branch string `json:"branch"`
Variables map[string]string `json:"variables"`
}
// Agent is the JSON data for an agent
Agent struct {
ID int64 `json:"id"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Name string `json:"name"`
OwnerID int64 `json:"owner_id"`
Token string `json:"token"`
LastContact int64 `json:"last_contact"`
Platform string `json:"platform"`
Backend string `json:"backend"`
Capacity int32 `json:"capacity"`
Version string `json:"version"`
NoSchedule bool `json:"no_schedule"`
}
// Task is the JSON data for a task
Task struct {
ID string `json:"id"`
Data []byte `json:"data"`
Labels map[string]string `json:"labels"`
Dependencies []string `json:"dependencies"`
RunOn []string `json:"run_on"`
DepStatus map[string]string `json:"dep_status"`
AgentID int64 `json:"agent_id"`
}
)