feat: add port-sharing backend (#11939)

This commit is contained in:
Garrett Delfosse
2024-02-13 09:31:20 -05:00
committed by GitHub
parent c939416702
commit 3ab3a62bef
48 changed files with 1947 additions and 59 deletions
@@ -0,0 +1,40 @@
package portsharing
import (
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/codersdk"
)
type EnterprisePortSharer struct{}
func NewEnterprisePortSharer() *EnterprisePortSharer {
return &EnterprisePortSharer{}
}
func (EnterprisePortSharer) AuthorizedPortSharingLevel(template database.Template, level codersdk.WorkspaceAgentPortShareLevel) error {
max := codersdk.WorkspaceAgentPortShareLevel(template.MaxPortSharingLevel)
switch level {
case codersdk.WorkspaceAgentPortShareLevelPublic:
if max != codersdk.WorkspaceAgentPortShareLevelPublic {
return xerrors.Errorf("port sharing level not allowed. Max level is '%s'", max)
}
case codersdk.WorkspaceAgentPortShareLevelAuthenticated:
if max == codersdk.WorkspaceAgentPortShareLevelOwner {
return xerrors.Errorf("port sharing level not allowed. Max level is '%s'", max)
}
default:
return xerrors.New("port sharing level is invalid.")
}
return nil
}
func (EnterprisePortSharer) ValidateTemplateMaxPortSharingLevel(level codersdk.WorkspaceAgentPortShareLevel) error {
if !level.ValidMaxLevel() {
return xerrors.New("invalid max port sharing level, value must be 'authenticated' or 'public'.")
}
return nil
}