Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: deduplicate / type license feature code #5734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 17, 2023
61 changes: 45 additions & 16 deletions codersdk/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"strings"
)

type Entitlement string
Expand All @@ -14,19 +15,24 @@ const (
EntitlementNotEntitled Entitlement = "not_entitled"
)

// To add a new feature, modify this set of enums as well as the FeatureNames
// array below.
type FeatureName string

const (
FeatureUserLimit = "user_limit"
FeatureAuditLog = "audit_log"
FeatureBrowserOnly = "browser_only"
FeatureSCIM = "scim"
FeatureTemplateRBAC = "template_rbac"
FeatureHighAvailability = "high_availability"
FeatureMultipleGitAuth = "multiple_git_auth"
FeatureExternalProvisionerDaemons = "external_provisioner_daemons"
FeatureAppearance = "appearance"
FeatureUserLimit FeatureName = "user_limit"
FeatureAuditLog FeatureName = "audit_log"
FeatureBrowserOnly FeatureName = "browser_only"
FeatureSCIM FeatureName = "scim"
FeatureTemplateRBAC FeatureName = "template_rbac"
FeatureHighAvailability FeatureName = "high_availability"
FeatureMultipleGitAuth FeatureName = "multiple_git_auth"
FeatureExternalProvisionerDaemons FeatureName = "external_provisioner_daemons"
FeatureAppearance FeatureName = "appearance"
)

var FeatureNames = []string{
// FeatureNames must be kept in-sync with the Feature enum above.
var FeatureNames = []FeatureName{
FeatureUserLimit,
FeatureAuditLog,
FeatureBrowserOnly,
Expand All @@ -38,6 +44,29 @@ var FeatureNames = []string{
FeatureAppearance,
}

// Humanize returns the feature name in a human-readable format.
func (n FeatureName) Humanize() string {
switch n {
case FeatureTemplateRBAC:
return "Template RBAC"
case FeatureSCIM:
return "SCIM"
default:
return strings.Title(strings.ReplaceAll(string(n), "_", " "))
}
}

// AlwaysEnable returns if the feature is always enabled if entitled.
// Warning: We don't know if we need this functionality.
// This method may disappear at any time.
func (n FeatureName) AlwaysEnable() bool {
return map[FeatureName]bool{
FeatureMultipleGitAuth: true,
FeatureExternalProvisionerDaemons: true,
FeatureAppearance: true,
}[n]
}

type Feature struct {
Entitlement Entitlement `json:"entitlement"`
Enabled bool `json:"enabled"`
Expand All @@ -46,12 +75,12 @@ type Feature struct {
}

type Entitlements struct {
Features map[string]Feature `json:"features"`
Warnings []string `json:"warnings"`
Errors []string `json:"errors"`
HasLicense bool `json:"has_license"`
Experimental bool `json:"experimental"`
Trial bool `json:"trial"`
Features map[FeatureName]Feature `json:"features"`
Warnings []string `json:"warnings"`
Errors []string `json:"errors"`
HasLicense bool `json:"has_license"`
Experimental bool `json:"experimental"`
Trial bool `json:"trial"`
}

func (c *Client) Entitlements(ctx context.Context) (Entitlements, error) {
Expand Down
25 changes: 25 additions & 0 deletions codersdk/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"
)

type AddLicenseRequest struct {
Expand All @@ -25,6 +26,30 @@ type License struct {
Claims map[string]interface{} `json:"claims"`
}

// Features provides the feature claims in license.
func (l *License) Features() (map[FeatureName]int64, error) {
strMap, ok := l.Claims["features"].(map[string]interface{})
if !ok {
return nil, xerrors.New("features key is unexpected type")
}
fMap := make(map[FeatureName]int64)
for k, v := range strMap {
jn, ok := v.(json.Number)
if !ok {
return nil, xerrors.Errorf("feature %q has unexpected type", k)
}

n, err := jn.Int64()
if err != nil {
return nil, err
}

fMap[FeatureName(k)] = n
}

return fMap, nil
}

func (c *Client) AddLicense(ctx context.Context, r AddLicenseRequest) (License, error) {
res, err := c.Request(ctx, http.MethodPost, "/api/v2/licenses", r)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions enterprise/cli/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ func featuresList() *cobra.Command {
}

type featureRow struct {
Name string `table:"name"`
Entitlement string `table:"entitlement"`
Enabled bool `table:"enabled"`
Limit *int64 `table:"limit"`
Actual *int64 `table:"actual"`
Name codersdk.FeatureName `table:"name"`
Entitlement string `table:"entitlement"`
Enabled bool `table:"enabled"`
Limit *int64 `table:"limit"`
Actual *int64 `table:"actual"`
}

// displayFeatures will return a table displaying all features passed in.
// filterColumns must be a subset of the feature fields and will determine which
// columns to display
func displayFeatures(filterColumns []string, features map[string]codersdk.Feature) (string, error) {
func displayFeatures(filterColumns []string, features map[codersdk.FeatureName]codersdk.Feature) (string, error) {
rows := make([]featureRow, 0, len(features))
for name, feat := range features {
rows = append(rows, featureRow{
Expand Down
6 changes: 5 additions & 1 deletion enterprise/cli/groupcreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/cli"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/pty/ptytest"
)

Expand All @@ -23,7 +25,9 @@ func TestCreateGroup(t *testing.T) {
client := coderdenttest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

var (
Expand Down
9 changes: 7 additions & 2 deletions enterprise/cli/groupdelete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/cli"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
Expand All @@ -26,7 +27,9 @@ func TestGroupDelete(t *testing.T) {
admin := coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

ctx, _ := testutil.Context(t)
Expand Down Expand Up @@ -57,7 +60,9 @@ func TestGroupDelete(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(),
Expand Down
13 changes: 10 additions & 3 deletions enterprise/cli/groupedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/cli"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
Expand All @@ -26,7 +27,9 @@ func TestGroupEdit(t *testing.T) {
admin := coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

ctx, _ := testutil.Context(t)
Expand Down Expand Up @@ -77,7 +80,9 @@ func TestGroupEdit(t *testing.T) {
admin := coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

ctx, _ := testutil.Context(t)
Expand Down Expand Up @@ -106,7 +111,9 @@ func TestGroupEdit(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(), "groups", "edit")
Expand Down
9 changes: 7 additions & 2 deletions enterprise/cli/grouplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/cli"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
Expand All @@ -24,7 +25,9 @@ func TestGroupList(t *testing.T) {
admin := coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

ctx, _ := testutil.Context(t)
Expand Down Expand Up @@ -81,7 +84,9 @@ func TestGroupList(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client)

_ = coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

cmd, root := clitest.NewWithSubcommands(t, cli.EnterpriseSubcommands(), "groups", "list")
Expand Down
2 changes: 1 addition & 1 deletion enterprise/cli/licenses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (s *fakeLicenseAPI) deleteLicense(rw http.ResponseWriter, r *http.Request)
}

func (*fakeLicenseAPI) entitlements(rw http.ResponseWriter, r *http.Request) {
features := make(map[string]codersdk.Feature)
features := make(map[codersdk.FeatureName]codersdk.Feature)
for _, f := range codersdk.FeatureNames {
features[f] = codersdk.Feature{
Entitlement: codersdk.EntitlementEntitled,
Expand Down
5 changes: 4 additions & 1 deletion enterprise/coderd/appearance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/testutil"
)

Expand All @@ -30,7 +31,9 @@ func TestServiceBanners(t *testing.T) {
require.False(t, sb.ServiceBanner.Enabled)

coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
ServiceBanners: true,
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})

// Default state
Expand Down
5 changes: 4 additions & 1 deletion enterprise/coderd/authorize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license"
"github.com/coder/coder/testutil"
)

Expand All @@ -28,7 +29,9 @@ func TestCheckACLPermissions(t *testing.T) {
// Create adminClient, member, and org adminClient
adminUser := coderdtest.CreateFirstUser(t, adminClient)
_ = coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
TemplateRBAC: true,
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
})

memberClient := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
Expand Down
4 changes: 2 additions & 2 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
api.entitlementsMu.Lock()
defer api.entitlementsMu.Unlock()

entitlements, err := license.Entitlements(ctx, api.Database, api.Logger, len(api.replicaManager.All()), len(api.GitAuthConfigs), api.Keys, map[string]bool{
entitlements, err := license.Entitlements(ctx, api.Database, api.Logger, len(api.replicaManager.All()), len(api.GitAuthConfigs), api.Keys, map[codersdk.FeatureName]bool{
codersdk.FeatureAuditLog: api.AuditLogging,
codersdk.FeatureBrowserOnly: api.BrowserOnly,
codersdk.FeatureSCIM: len(api.SCIMAPIKey) != 0,
Expand All @@ -252,7 +252,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
}
entitlements.Experimental = api.DeploymentConfig.Experimental.Value

featureChanged := func(featureName string) (changed bool, enabled bool) {
featureChanged := func(featureName codersdk.FeatureName) (changed bool, enabled bool) {
if api.entitlements.Features == nil {
return true, entitlements.Features[featureName].Enabled
}
Expand Down
Loading