mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: use v2 API for agent lifecycle updates (#12278)
Agent uses the v2 API to post lifecycle updates. Part of #10534
This commit is contained in:
@ -485,6 +485,9 @@ type PostLifecycleRequest struct {
|
||||
ChangedAt time.Time `json:"changed_at"`
|
||||
}
|
||||
|
||||
// PostLifecycle posts the agent's lifecycle to the Coder server.
|
||||
//
|
||||
// Deprecated: Use UpdateLifecycle on the dRPC API instead
|
||||
func (c *Client) PostLifecycle(ctx context.Context, req PostLifecycleRequest) error {
|
||||
res, err := c.SDK.Request(ctx, http.MethodPost, "/api/v2/workspaceagents/me/report-lifecycle", req)
|
||||
if err != nil {
|
||||
|
@ -311,3 +311,22 @@ func ProtoFromLog(log Log) (*proto.Log, error) {
|
||||
Level: proto.Log_Level(lvl),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ProtoFromLifecycle(req PostLifecycleRequest) (*proto.Lifecycle, error) {
|
||||
s, ok := proto.Lifecycle_State_value[strings.ToUpper(string(req.State))]
|
||||
if !ok {
|
||||
return nil, xerrors.Errorf("unknown lifecycle state: %s", req.State)
|
||||
}
|
||||
return &proto.Lifecycle{
|
||||
State: proto.Lifecycle_State(s),
|
||||
ChangedAt: timestamppb.New(req.ChangedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LifecycleStateFromProto(s proto.Lifecycle_State) (codersdk.WorkspaceAgentLifecycle, error) {
|
||||
caps, ok := proto.Lifecycle_State_name[int32(s)]
|
||||
if !ok {
|
||||
return "", xerrors.Errorf("unknown lifecycle state: %d", s)
|
||||
}
|
||||
return codersdk.WorkspaceAgentLifecycle(strings.ToLower(caps)), nil
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"tailscale.com/tailcfg"
|
||||
|
||||
"github.com/coder/coder/v2/agent/proto"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||
"github.com/coder/coder/v2/tailnet"
|
||||
@ -161,3 +162,17 @@ func TestSubsystems(t *testing.T) {
|
||||
proto.Startup_EXECTRACE,
|
||||
})
|
||||
}
|
||||
|
||||
func TestProtoFromLifecycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
now := dbtime.Now()
|
||||
for _, s := range codersdk.WorkspaceAgentLifecycleOrder {
|
||||
sr := agentsdk.PostLifecycleRequest{State: s, ChangedAt: now}
|
||||
pr, err := agentsdk.ProtoFromLifecycle(sr)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, now, pr.ChangedAt.AsTime())
|
||||
state, err := agentsdk.LifecycleStateFromProto(pr.State)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, s, state)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user