mirror of
https://github.com/coder/coder.git
synced 2025-07-10 23:53:15 +00:00
Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Danny Kopping <danny@coder.com> Co-authored-by: Edward Angert <EdwardAngert@users.noreply.github.com> Co-authored-by: EdwardAngert <17991901+EdwardAngert@users.noreply.github.com> Co-authored-by: Jaayden Halko <jaayden.halko@gmail.com> Co-authored-by: Ethan <39577870+ethanndickson@users.noreply.github.com> Co-authored-by: M Atif Ali <atif@coder.com> Co-authored-by: Aericio <16523741+Aericio@users.noreply.github.com> Co-authored-by: M Atif Ali <me@matifali.dev> Co-authored-by: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com>
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package prebuilds
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/prebuilds"
|
|
)
|
|
|
|
type EnterpriseClaimer struct {
|
|
store database.Store
|
|
}
|
|
|
|
func NewEnterpriseClaimer(store database.Store) *EnterpriseClaimer {
|
|
return &EnterpriseClaimer{
|
|
store: store,
|
|
}
|
|
}
|
|
|
|
func (c EnterpriseClaimer) Claim(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
name string,
|
|
presetID uuid.UUID,
|
|
) (*uuid.UUID, error) {
|
|
result, err := c.store.ClaimPrebuiltWorkspace(ctx, database.ClaimPrebuiltWorkspaceParams{
|
|
NewUserID: userID,
|
|
NewName: name,
|
|
PresetID: presetID,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
// No eligible prebuilds found
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
return nil, prebuilds.ErrNoClaimablePrebuiltWorkspaces
|
|
default:
|
|
return nil, xerrors.Errorf("claim prebuild for user %q: %w", userID.String(), err)
|
|
}
|
|
}
|
|
|
|
return &result.ID, nil
|
|
}
|
|
|
|
func (EnterpriseClaimer) Initiator() uuid.UUID {
|
|
return prebuilds.SystemUserID
|
|
}
|
|
|
|
var _ prebuilds.Claimer = &EnterpriseClaimer{}
|