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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ammario merged 13 commits intomainfromlicenses
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
355f3c0
refactor: deduplicate / type license feature code
ammarioJan 16, 2023
c2d044b
fixup! refactor: deduplicate / type license feature code
ammarioJan 16, 2023
83cd8f4
fixup! refactor: deduplicate / type license feature code
ammarioJan 16, 2023
e51a077
fixup! refactor: deduplicate / type license feature code
ammarioJan 16, 2023
445640b
fixup! refactor: deduplicate / type license feature code
ammarioJan 16, 2023
32d89dc
Update frontend types
ammarioJan 16, 2023
f1d95e4
fixup! Update frontend types
ammarioJan 17, 2023
48cda29
fixup! Update frontend types
ammarioJan 17, 2023
277ef44
fixup! Update frontend types
ammarioJan 17, 2023
f165432
fixup! Update frontend types
ammarioJan 17, 2023
4bdd3c0
fixup! Update frontend types
ammarioJan 17, 2023
138055a
fixup! Update frontend types
ammarioJan 17, 2023
15dc805
fixup! Update frontend types
ammarioJan 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletionscodersdk/features.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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"
FeatureUserLimitFeatureName= "user_limit"
FeatureAuditLogFeatureName= "audit_log"
FeatureBrowserOnlyFeatureName= "browser_only"
FeatureSCIMFeatureName= "scim"
FeatureTemplateRBACFeatureName= "template_rbac"
FeatureHighAvailabilityFeatureName= "high_availability"
FeatureMultipleGitAuthFeatureName= "multiple_git_auth"
FeatureExternalProvisionerDaemonsFeatureName= "external_provisioner_daemons"
FeatureAppearanceFeatureName= "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 deletionscodersdk/licenses.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsenterprise/cli/features.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,17 +88,17 @@ func featuresList() *cobra.Command {
}

type featureRow struct {
Namestring `table:"name"`
Entitlement string `table:"entitlement"`
Enabled bool `table:"enabled"`
Limit *int64 `table:"limit"`
Actual *int64 `table:"actual"`
Namecodersdk.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 deletionenterprise/cli/groupcreate_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsenterprise/cli/groupdelete_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsenterprise/cli/groupedit_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionsenterprise/cli/grouplist_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionenterprise/cli/licenses_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionenterprise/coderd/appearance_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionenterprise/coderd/authorize_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsenterprise/coderd/coderd.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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(featureNamestring) (changed bool, enabled bool) {
featureChanged := func(featureNamecodersdk.FeatureName) (changed bool, enabled bool) {
if api.entitlements.Features == nil {
return true, entitlements.Features[featureName].Enabled
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp