Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcae444b

Browse files
committed
chore: implement feature sets to licenses
Implement different sets of licensed features.Refactor the license logic to fix up some edge cases
1 parent1f24ace commitcae444b

File tree

3 files changed

+274
-104
lines changed

3 files changed

+274
-104
lines changed

‎codersdk/deployment.go‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ const (
3333
EntitlementNotEntitledEntitlement="not_entitled"
3434
)
3535

36+
funcCompareEntitlements(a,bEntitlement)int {
37+
returnentitlementWeight(a)-entitlementWeight(b)
38+
}
39+
40+
funcentitlementWeight(eEntitlement)int {
41+
switche {
42+
caseEntitlementEntitled:
43+
return2
44+
caseEntitlementGracePeriod:
45+
return1
46+
caseEntitlementNotEntitled:
47+
return0
48+
default:
49+
return-1
50+
}
51+
}
52+
3653
// FeatureName represents the internal name of a feature.
3754
// To add a new feature, add it to this set of enums as well as the FeatureNames
3855
// array below.
@@ -107,6 +124,51 @@ func (n FeatureName) AlwaysEnable() bool {
107124
}[n]
108125
}
109126

127+
// FeatureSet represents a grouping of features. This is easier
128+
// than manually assigning features al-la-carte when making a license.
129+
// These sets are dynamic in the sense a feature can be added to an existing
130+
// set to grant an additional feature to an existing license.
131+
// If features were granted al-la-carte, we would need to reissue the license
132+
// to include the new feature.
133+
typeFeatureSetstring
134+
135+
const (
136+
FeatureSetNoneFeatureSet=""
137+
FeatureSetEnterpriseFeatureSet="enterprise"
138+
FeatureSetPremiumFeatureSet="premium"
139+
)
140+
141+
func (setFeatureSet)Features() []FeatureName {
142+
switchFeatureSet(strings.ToLower(string(set))) {
143+
caseFeatureSetEnterprise:
144+
// List all features that should be included in the Enterprise feature set.
145+
return []FeatureName{
146+
FeatureUserLimit,
147+
FeatureAuditLog,
148+
FeatureBrowserOnly,
149+
FeatureSCIM,
150+
FeatureTemplateRBAC,
151+
FeatureHighAvailability,
152+
FeatureMultipleExternalAuth,
153+
FeatureExternalProvisionerDaemons,
154+
FeatureAppearance,
155+
FeatureAdvancedTemplateScheduling,
156+
FeatureWorkspaceProxy,
157+
FeatureUserRoleManagement,
158+
FeatureExternalTokenEncryption,
159+
FeatureWorkspaceBatchActions,
160+
FeatureAccessControl,
161+
FeatureControlSharedPorts,
162+
FeatureCustomRoles,
163+
}
164+
caseFeatureSetPremium:
165+
// FeatureSetPremium is a superset of Enterprise
166+
returnappend(FeatureSetEnterprise.Features())
167+
}
168+
// By default, return an empty set.
169+
return []FeatureName{}
170+
}
171+
110172
typeFeaturestruct {
111173
EntitlementEntitlement`json:"entitlement"`
112174
Enabledbool`json:"enabled"`
@@ -124,6 +186,56 @@ type Entitlements struct {
124186
RefreshedAt time.Time`json:"refreshed_at" format:"date-time"`
125187
}
126188

189+
// AddFeature will add the feature to the entitlements iff it expands
190+
// the set of features granted by the entitlements. If it does not, it will
191+
// be ignored and the existing feature with the same name will remain.
192+
//
193+
// All features should be added as atomic items, and not merged in any way.
194+
// Merging entitlements could lead to unexpected behavior, like a larger user
195+
// limit in grace period merging with a smaller one in a grace period. This could
196+
// lead to the larger limit being extended as "entitled", which is not correct.
197+
func (e*Entitlements)AddFeature(nameFeatureName,addFeature) {
198+
existing,ok:=e.Features[name]
199+
if!ok {
200+
e.Features[name]=add
201+
return
202+
}
203+
204+
comparison:=CompareEntitlements(add.Entitlement,existing.Entitlement)
205+
// If the new entitlement is greater than the existing entitlement, replace it.
206+
// The edge case is if the previous entitlement is in a grace period with a
207+
// higher value.
208+
// TODO: Address the edge case.
209+
ifcomparison>0 {
210+
e.Features[name]=add
211+
return
212+
}
213+
214+
// If they have the same entitlement, then we can compare the limits.
215+
ifcomparison==0 {
216+
ifadd.Limit!=nil {
217+
ifexisting.Limit==nil||*add.Limit>*existing.Limit {
218+
e.Features[name]=add
219+
return
220+
}
221+
}
222+
223+
// Enabled is better than disabled.
224+
ifadd.Enabled&&!existing.Enabled {
225+
e.Features[name]=add
226+
return
227+
}
228+
229+
// If the actual value is greater than the existing actual value, replace it.
230+
ifadd.Actual!=nil {
231+
ifexisting.Actual==nil||*add.Actual>*existing.Actual {
232+
e.Features[name]=add
233+
return
234+
}
235+
}
236+
}
237+
}
238+
127239
func (c*Client)Entitlements(ctx context.Context) (Entitlements,error) {
128240
res,err:=c.Request(ctx,http.MethodGet,"/api/v2/entitlements",nil)
129241
iferr!=nil {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp