Skip to content

Commit ab01da7

Browse files
committed
Merge experiments.go and features.go into deployment.go
1 parent cb0656c commit ab01da7

File tree

3 files changed

+138
-153
lines changed

3 files changed

+138
-153
lines changed

codersdk/deployment.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,97 @@ import (
1111
"golang.org/x/xerrors"
1212
)
1313

14+
type Entitlement string
15+
16+
const (
17+
EntitlementEntitled Entitlement = "entitled"
18+
EntitlementGracePeriod Entitlement = "grace_period"
19+
EntitlementNotEntitled Entitlement = "not_entitled"
20+
)
21+
22+
// To add a new feature, modify this set of enums as well as the FeatureNames
23+
// array below.
24+
type FeatureName string
25+
26+
const (
27+
FeatureUserLimit FeatureName = "user_limit"
28+
FeatureAuditLog FeatureName = "audit_log"
29+
FeatureBrowserOnly FeatureName = "browser_only"
30+
FeatureSCIM FeatureName = "scim"
31+
FeatureTemplateRBAC FeatureName = "template_rbac"
32+
FeatureHighAvailability FeatureName = "high_availability"
33+
FeatureMultipleGitAuth FeatureName = "multiple_git_auth"
34+
FeatureExternalProvisionerDaemons FeatureName = "external_provisioner_daemons"
35+
FeatureAppearance FeatureName = "appearance"
36+
)
37+
38+
// FeatureNames must be kept in-sync with the Feature enum above.
39+
var FeatureNames = []FeatureName{
40+
FeatureUserLimit,
41+
FeatureAuditLog,
42+
FeatureBrowserOnly,
43+
FeatureSCIM,
44+
FeatureTemplateRBAC,
45+
FeatureHighAvailability,
46+
FeatureMultipleGitAuth,
47+
FeatureExternalProvisionerDaemons,
48+
FeatureAppearance,
49+
}
50+
51+
// Humanize returns the feature name in a human-readable format.
52+
func (n FeatureName) Humanize() string {
53+
switch n {
54+
case FeatureTemplateRBAC:
55+
return "Template RBAC"
56+
case FeatureSCIM:
57+
return "SCIM"
58+
default:
59+
return strings.Title(strings.ReplaceAll(string(n), "_", " "))
60+
}
61+
}
62+
63+
// AlwaysEnable returns if the feature is always enabled if entitled.
64+
// Warning: We don't know if we need this functionality.
65+
// This method may disappear at any time.
66+
func (n FeatureName) AlwaysEnable() bool {
67+
return map[FeatureName]bool{
68+
FeatureMultipleGitAuth: true,
69+
FeatureExternalProvisionerDaemons: true,
70+
FeatureAppearance: true,
71+
}[n]
72+
}
73+
74+
type Feature struct {
75+
Entitlement Entitlement `json:"entitlement"`
76+
Enabled bool `json:"enabled"`
77+
Limit *int64 `json:"limit,omitempty"`
78+
Actual *int64 `json:"actual,omitempty"`
79+
}
80+
81+
type Entitlements struct {
82+
Features map[FeatureName]Feature `json:"features"`
83+
Warnings []string `json:"warnings"`
84+
Errors []string `json:"errors"`
85+
HasLicense bool `json:"has_license"`
86+
Trial bool `json:"trial"`
87+
88+
// DEPRECATED: use Experiments instead.
89+
Experimental bool `json:"experimental"`
90+
}
91+
92+
func (c *Client) Entitlements(ctx context.Context) (Entitlements, error) {
93+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/entitlements", nil)
94+
if err != nil {
95+
return Entitlements{}, err
96+
}
97+
defer res.Body.Close()
98+
if res.StatusCode != http.StatusOK {
99+
return Entitlements{}, readBodyAsError(res)
100+
}
101+
var ent Entitlements
102+
return ent, json.NewDecoder(res.Body).Decode(&ent)
103+
}
104+
14105
// DeploymentConfig is the central configuration for the coder server.
15106
type DeploymentConfig struct {
16107
AccessURL *DeploymentConfigField[string] `json:"access_url" typescript:",notnull"`
@@ -314,3 +405,50 @@ func (c *Client) BuildInfo(ctx context.Context) (BuildInfoResponse, error) {
314405
var buildInfo BuildInfoResponse
315406
return buildInfo, json.NewDecoder(res.Body).Decode(&buildInfo)
316407
}
408+
409+
type Experiment string
410+
411+
const (
412+
// ExperimentAuthzQuerier is an internal experiment that enables the ExperimentAuthzQuerier
413+
// interface for all RBAC operations. NOT READY FOR PRODUCTION USE.
414+
ExperimentAuthzQuerier Experiment = "authz_querier"
415+
416+
// Add new experiments here!
417+
// ExperimentExample Experiment = "example"
418+
)
419+
420+
var (
421+
// ExperimentsAll should include all experiments that are safe for
422+
// users to opt-in to via --experimental='*'.
423+
// Experiments that are not ready for consumption by all users should
424+
// not be included here and will be essentially hidden.
425+
ExperimentsAll = Experiments{}
426+
)
427+
428+
// Experiments is a list of experiments that are enabled for the deployment.
429+
// Multiple experiments may be enabled at the same time.
430+
// Experiments are not safe for production use, and are not guaranteed to
431+
// be backwards compatible. They may be removed or renamed at any time.
432+
type Experiments []Experiment
433+
434+
func (e Experiments) Enabled(ex Experiment) bool {
435+
for _, v := range e {
436+
if v == ex {
437+
return true
438+
}
439+
}
440+
return false
441+
}
442+
443+
func (c *Client) Experiments(ctx context.Context) (Experiments, error) {
444+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/experiments", nil)
445+
if err != nil {
446+
return nil, err
447+
}
448+
defer res.Body.Close()
449+
if res.StatusCode != http.StatusOK {
450+
return nil, readBodyAsError(res)
451+
}
452+
var exp []Experiment
453+
return exp, json.NewDecoder(res.Body).Decode(&exp)
454+
}

codersdk/experiments.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

codersdk/features.go

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)