MCPcopy Create free account
hub / github.com/coder/coder / Entitlements

Function Entitlements

enterprise/coderd/license/license.go:27–94  ·  view source on GitHub ↗

Entitlements processes licenses to return whether features are enabled or not. TODO(@deansheather): This function and the related LicensesEntitlements function should be refactored into smaller functions that: 1. evaluate entitlements from fetched licenses 2. populate current usage values on the ent

(
	ctx context.Context,
	db database.Store,
	replicaCount int,
	externalAuthCount int,
	keys map[string]ed25519.PublicKey,
	enablements map[codersdk.FeatureName]bool,
)

Source from the content-addressed store, hash-verified

25// 2. populate current usage values on the entitlements
26// 3. generate warnings related to usage
27func Entitlements(
28 ctx context.Context,
29 db database.Store,
30 replicaCount int,
31 externalAuthCount int,
32 keys map[string]ed25519.PublicKey,
33 enablements map[codersdk.FeatureName]bool,
34) (codersdk.Entitlements, error) {
35 now := time.Now()
36
37 // nolint:gocritic // Getting unexpired licenses is a system function.
38 licenses, err := db.GetUnexpiredLicenses(dbauthz.AsSystemRestricted(ctx))
39 if err != nil {
40 return codersdk.Entitlements{}, err
41 }
42
43 // nolint:gocritic // Getting active user count is a system function.
44 activeUserCount, err := db.GetActiveUserCount(dbauthz.AsSystemRestricted(ctx), false) // Don't include system user in license count.
45 if err != nil {
46 return codersdk.Entitlements{}, xerrors.Errorf("query active user count: %w", err)
47 }
48
49 // nolint:gocritic // Getting active AI seat count is a system function.
50 activeAISeatCount, err := db.GetActiveAISeatCount(dbauthz.AsSystemRestricted(ctx))
51 if err != nil {
52 return codersdk.Entitlements{}, xerrors.Errorf("query active AI seat count: %w", err)
53 }
54
55 // nolint:gocritic // Getting external templates is a system function.
56 externalTemplates, err := db.GetTemplatesWithFilter(dbauthz.AsSystemRestricted(ctx), database.GetTemplatesWithFilterParams{
57 HasExternalAgent: sql.NullBool{
58 Bool: true,
59 Valid: true,
60 },
61 })
62 if err != nil {
63 return codersdk.Entitlements{}, xerrors.Errorf("query external templates: %w", err)
64 }
65
66 entitlements, err := LicensesEntitlements(ctx, now, licenses, enablements, keys, FeatureArguments{
67 ActiveUserCount: activeUserCount,
68 ActiveAISeatCount: activeAISeatCount,
69 ReplicaCount: replicaCount,
70 ExternalAuthCount: externalAuthCount,
71 ExternalTemplateCount: int64(len(externalTemplates)),
72 ManagedAgentCountFn: func(ctx context.Context, startTime time.Time, endTime time.Time) (int64, error) {
73 // This is not super accurate, as the start and end times will be
74 // truncated to the date in UTC timezone. This is an optimization
75 // so we can use an aggregate table instead of scanning the usage
76 // events table.
77 //
78 // High accuracy is not super necessary, as we give buffers in our
79 // licenses (e.g. higher hard limit) to account for additional
80 // usage.
81 //
82 // nolint:gocritic // Requires permission to read all workspaces to read managed agent count.
83 return db.GetTotalUsageDCManagedAgentsV1(dbauthz.AsSystemRestricted(ctx), database.GetTotalUsageDCManagedAgentsV1Params{
84 StartDate: startTime,

Callers 3

updateEntitlementsMethod · 0.92
TestEntitlementsFunction · 0.92
TestAIGovernanceAddonFunction · 0.92

Calls 8

AsSystemRestrictedFunction · 0.92
LicensesEntitlementsFunction · 0.85
GetUnexpiredLicensesMethod · 0.65
GetActiveUserCountMethod · 0.65
GetActiveAISeatCountMethod · 0.65
ErrorfMethod · 0.45

Tested by 2

TestEntitlementsFunction · 0.74
TestAIGovernanceAddonFunction · 0.74