Skip to content

Commit 632bf98

Browse files
committed
move convert methods to sdks
1 parent 6234f72 commit 632bf98

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

coderd/agentapi/audit.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
agentproto "github.com/coder/coder/v2/agent/proto"
1616
"github.com/coder/coder/v2/coderd/audit"
1717
"github.com/coder/coder/v2/coderd/database"
18+
"github.com/coder/coder/v2/coderd/database/db2sdk"
1819
"github.com/coder/coder/v2/codersdk/agentsdk"
1920
)
2021

@@ -33,11 +34,11 @@ func (a *AuditAPI) ReportConnection(ctx context.Context, req *agentproto.ReportC
3334
return nil, xerrors.Errorf("connection id from bytes: %w", err)
3435
}
3536

36-
action, err := AgentProtoConnectionActionToAuditAction(req.GetConnection().GetAction())
37+
action, err := db2sdk.AuditActionFromAgentProtoConnectionAction(req.GetConnection().GetAction())
3738
if err != nil {
3839
return nil, err
3940
}
40-
connectionType, err := AgentProtoConnectionTypeToAgentConnectionType(req.GetConnection().GetType())
41+
connectionType, err := agentsdk.ConnectionTypeFromProto(req.GetConnection().GetType())
4142
if err != nil {
4243
return nil, err
4344
}
@@ -102,29 +103,3 @@ func (a *AuditAPI) ReportConnection(ctx context.Context, req *agentproto.ReportC
102103

103104
return &emptypb.Empty{}, nil
104105
}
105-
106-
func AgentProtoConnectionActionToAuditAction(action agentproto.Connection_Action) (database.AuditAction, error) {
107-
switch action {
108-
case agentproto.Connection_CONNECT:
109-
return database.AuditActionConnect, nil
110-
case agentproto.Connection_DISCONNECT:
111-
return database.AuditActionDisconnect, nil
112-
default:
113-
return "", xerrors.Errorf("unknown agent connection action %q", action)
114-
}
115-
}
116-
117-
func AgentProtoConnectionTypeToAgentConnectionType(typ agentproto.Connection_Type) (agentsdk.ConnectionType, error) {
118-
switch typ {
119-
case agentproto.Connection_SSH:
120-
return agentsdk.ConnectionTypeSSH, nil
121-
case agentproto.Connection_VSCODE:
122-
return agentsdk.ConnectionTypeVSCode, nil
123-
case agentproto.Connection_JETBRAINS:
124-
return agentsdk.ConnectionTypeJetBrains, nil
125-
case agentproto.Connection_RECONNECTING_PTY:
126-
return agentsdk.ConnectionTypeReconnectingPTY, nil
127-
default:
128-
return "", xerrors.Errorf("unknown agent connection type %q", typ)
129-
}
130-
}

coderd/agentapi/audit_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/coder/coder/v2/coderd/agentapi"
1919
"github.com/coder/coder/v2/coderd/audit"
2020
"github.com/coder/coder/v2/coderd/database"
21+
"github.com/coder/coder/v2/coderd/database/db2sdk"
2122
"github.com/coder/coder/v2/coderd/database/dbmock"
2223
"github.com/coder/coder/v2/coderd/database/dbtime"
2324
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -160,13 +161,13 @@ func TestAuditReport(t *testing.T) {
160161
}
161162

162163
func agentProtoConnectionActionToAudit(t *testing.T, action agentproto.Connection_Action) database.AuditAction {
163-
a, err := agentapi.AgentProtoConnectionActionToAuditAction(action)
164+
a, err := db2sdk.AuditActionFromAgentProtoConnectionAction(action)
164165
require.NoError(t, err)
165166
return a
166167
}
167168

168169
func agentProtoConnectionTypeToSDK(t *testing.T, typ agentproto.Connection_Type) agentsdk.ConnectionType {
169-
action, err := agentapi.AgentProtoConnectionTypeToAgentConnectionType(typ)
170+
action, err := agentsdk.ConnectionTypeFromProto(typ)
170171
require.NoError(t, err)
171172
return action
172173
}

coderd/database/db2sdk/db2sdk.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"golang.org/x/xerrors"
1616
"tailscale.com/tailcfg"
1717

18+
agentproto "github.com/coder/coder/v2/agent/proto"
1819
"github.com/coder/coder/v2/coderd/database"
1920
"github.com/coder/coder/v2/coderd/rbac"
2021
"github.com/coder/coder/v2/coderd/rbac/policy"
@@ -705,3 +706,26 @@ func TemplateRoleActions(role codersdk.TemplateRole) []policy.Action {
705706
}
706707
return []policy.Action{}
707708
}
709+
710+
func AuditActionFromAgentProtoConnectionAction(action agentproto.Connection_Action) (database.AuditAction, error) {
711+
switch action {
712+
case agentproto.Connection_CONNECT:
713+
return database.AuditActionConnect, nil
714+
case agentproto.Connection_DISCONNECT:
715+
return database.AuditActionDisconnect, nil
716+
default:
717+
// Also Connection_ACTION_UNSPECIFIED, no mapping.
718+
return "", xerrors.Errorf("unknown agent connection action %q", action)
719+
}
720+
}
721+
722+
func AgentProtoConnectionActionToAuditAction(action database.AuditAction) (agentproto.Connection_Action, error) {
723+
switch action {
724+
case database.AuditActionConnect:
725+
return agentproto.Connection_CONNECT, nil
726+
case database.AuditActionDisconnect:
727+
return agentproto.Connection_DISCONNECT, nil
728+
default:
729+
return agentproto.Connection_ACTION_UNSPECIFIED, xerrors.Errorf("unknown agent connection action %q", action)
730+
}
731+
}

codersdk/agentsdk/convert.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,37 @@ func ProtoFromLifecycleState(s codersdk.WorkspaceAgentLifecycle) (proto.Lifecycl
390390
}
391391
return proto.Lifecycle_State(caps), nil
392392
}
393+
394+
func ConnectionTypeFromProto(typ proto.Connection_Type) (ConnectionType, error) {
395+
switch typ {
396+
case proto.Connection_TYPE_UNSPECIFIED:
397+
return ConnectionTypeUnspecified, nil
398+
case proto.Connection_SSH:
399+
return ConnectionTypeSSH, nil
400+
case proto.Connection_VSCODE:
401+
return ConnectionTypeVSCode, nil
402+
case proto.Connection_JETBRAINS:
403+
return ConnectionTypeJetBrains, nil
404+
case proto.Connection_RECONNECTING_PTY:
405+
return ConnectionTypeReconnectingPTY, nil
406+
default:
407+
return "", xerrors.Errorf("unknown connection type %q", typ)
408+
}
409+
}
410+
411+
func ProtoFromConnectionType(typ ConnectionType) (proto.Connection_Type, error) {
412+
switch typ {
413+
case ConnectionTypeUnspecified:
414+
return proto.Connection_TYPE_UNSPECIFIED, nil
415+
case ConnectionTypeSSH:
416+
return proto.Connection_SSH, nil
417+
case ConnectionTypeVSCode:
418+
return proto.Connection_VSCODE, nil
419+
case ConnectionTypeJetBrains:
420+
return proto.Connection_JETBRAINS, nil
421+
case ConnectionTypeReconnectingPTY:
422+
return proto.Connection_RECONNECTING_PTY, nil
423+
default:
424+
return 0, xerrors.Errorf("unknown connection type %q", typ)
425+
}
426+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy