mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
feat: implement claiming of prebuilt workspaces (#17458)
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>
This commit is contained in:
committed by
GitHub
parent
25dacd39e7
commit
118f12ac3a
53
enterprise/coderd/prebuilds/claim.go
Normal file
53
enterprise/coderd/prebuilds/claim.go
Normal file
@ -0,0 +1,53 @@
|
||||
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{}
|
Reference in New Issue
Block a user