Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: remove base.PlanType (#16458)

Danny Xu183235d25c3f
diff --git a/backend/api/v1/idp_service.go b/backend/api/v1/idp_service.go
index a51590fde3..838b8de482 100644
--- a/backend/api/v1/idp_service.go
+++ b/backend/api/v1/idp_service.go
@@ -205,11 +205,11 @@ func (s *IdentityProviderService) checkFeatureAvailable(ssoType v1pb.IdentityPro
}
plan := s.licenseService.GetEffectivePlan()
switch plan {
- case base.FREE:
+ case v1pb.PlanType_FREE:
return status.Error(codes.PermissionDenied, "feature is not available for free plan")
- case base.ENTERPRISE:
+ case v1pb.PlanType_ENTERPRISE:
return nil
- case base.TEAM:
+ case v1pb.PlanType_TEAM:
if ssoType != v1pb.IdentityProviderType_OAUTH2 {
return status.Error(codes.PermissionDenied, "only oauth type is available")
}
diff --git a/backend/api/v1/subscription_service.go b/backend/api/v1/subscription_service.go
index 1bc4826f48..8119d4a5f9 100644
--- a/backend/api/v1/subscription_service.go
+++ b/backend/api/v1/subscription_service.go
@@ -51,7 +51,8 @@ func (*SubscriptionService) GetFeatureMatrix(_ context.Context, _ *v1pb.GetFeatu
for key, val := range base.FeatureMatrix {
matrix := map[string]bool{}
for i, enabled := range val {
- plan := covertToV1PlanType(base.PlanType(i))
+ // Convert array index to PlanType (0=FREE, 1=TEAM, 2=ENTERPRISE)
+ plan := v1pb.PlanType(i + 1) // +1 because proto enums start at 1
matrix[plan.String()] = enabled
}
resp.Features = append(resp.Features, &v1pb.Feature{
@@ -88,28 +89,15 @@ func (s *SubscriptionService) loadSubscription(ctx context.Context) (*v1pb.Subsc
subscription := &v1pb.Subscription{
SeatCount: int32(sub.Seat),
InstanceCount: int32(sub.InstanceCount),
- Plan: covertToV1PlanType(sub.Plan),
+ Plan: sub.Plan,
Trialing: sub.Trialing,
OrgId: sub.OrgID,
OrgName: sub.OrgName,
}
- if sub.Plan != base.FREE {
+ if sub.Plan != v1pb.PlanType_FREE {
subscription.ExpiresTime = timestamppb.New(time.Unix(sub.ExpiresTS, 0))
subscription.StartedTime = timestamppb.New(time.Unix(sub.StartedTS, 0))
}
return subscription, nil
}
-
-func covertToV1PlanType(planType base.PlanType) v1pb.PlanType {
- switch planType {
- case base.FREE:
- return v1pb.PlanType_FREE
- case base.TEAM:
- return v1pb.PlanType_TEAM
- case base.ENTERPRISE:
- return v1pb.PlanType_ENTERPRISE
- default:
- return v1pb.PlanType_PLAN_TYPE_UNSPECIFIED
- }
-}
diff --git a/backend/base/plan.go b/backend/base/plan.go
index f2bca8ba6d..0cc090bc70 100644
--- a/backend/base/plan.go
+++ b/backend/base/plan.go
@@ -2,47 +2,10 @@ package base
import (
"fmt"
-)
-
-// PlanType is the type for a plan.
-type PlanType int
-const (
- // FREE is the plan type for FREE.
- FREE PlanType = iota
- // TEAM is the plan type for TEAM.
- TEAM
- // ENTERPRISE is the plan type for ENTERPRISE.
- ENTERPRISE
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
-// String returns the string format of plan type.
-func (p PlanType) String() string {
- switch p {
- case FREE:
- return "FREE"
- case TEAM:
- return "TEAM"
- case ENTERPRISE:
- return "ENTERPRISE"
- }
- return ""
-}
-
-// Priority returns the priority of the plan type.
-// Higher priority means the plan supports more features.
-func (p PlanType) Priority() int {
- switch p {
- case FREE:
- return 1
- case TEAM:
- return 2
- case ENTERPRISE:
- return 3
- }
- return 0
-}
-
// FeatureType is the type of a feature.
type FeatureType string
@@ -261,18 +224,19 @@ func (e FeatureType) AccessErrorMessage() string {
}
// minimumSupportedPlan will find the minimum plan which supports the target feature.
-func (e FeatureType) minimumSupportedPlan() PlanType {
+func (e FeatureType) minimumSupportedPlan() v1pb.PlanType {
for i, enabled := range FeatureMatrix[e] {
if enabled {
- return PlanType(i)
+ return v1pb.PlanType(i + 1) // +1 because proto enums start at 1
}
}
- return ENTERPRISE
+ return v1pb.PlanType_ENTERPRISE
}
// FeatureMatrix is a map from the a particular feature to the respective enablement of a particular
// plan in [FREE, TEAM, Enterprise].
+// Index 0 = FREE, 1 = TEAM, 2 = ENTERPRISE
var FeatureMatrix = map[FeatureType][3]bool{
// Admin & Security
FeatureSSO: {false, true, true},
@@ -326,10 +290,14 @@ var InstanceLimitFeature = map[FeatureType]bool{
}
// Feature returns whether a particular feature is available in a particular plan.
-func Feature(feature FeatureType, plan PlanType) bool {
+func Feature(feature FeatureType, plan v1pb.PlanType) bool {
matrix, ok := FeatureMatrix[feature]
if !ok {
return false
}
- return matrix[plan]
+ // Convert proto enum to array index (FREE=1 -> 0, TEAM=2 -> 1, ENTERPRISE=3 -> 2)
+ if plan < v1pb.PlanType_FREE || plan > v1pb.PlanType_ENTERPRISE {
+ return false
+ }
+ return matrix[plan-1]
}
diff --git a/backend/enterprise/api/license.go b/backend/enterprise/api/license.go
index b4ce128cbf..02e1531556 100644
--- a/backend/enterprise/api/license.go
+++ b/backend/enterprise/api/license.go
@@ -10,12 +10,13 @@ import (
"github.com/bytebase/bytebase/backend/base"
"github.com/bytebase/bytebase/backend/store"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
// validPlans is a string array of valid plan types.
-var validPlans = []base.PlanType{
- base.TEAM,
- base.ENTERPRISE,
+var validPlans = []v1pb.PlanType{
+ v1pb.PlanType_TEAM,
+ v1pb.PlanType_ENTERPRISE,
}
// License is the API message for enterprise license.
@@ -25,7 +26,7 @@ type License struct {
Seat int
ExpiresTS int64
IssuedTS int64
- Plan base.PlanType
+ Plan v1pb.PlanType
Trialing bool
OrgName string
}
@@ -48,8 +49,8 @@ func (l *License) validPlanType() error {
return errors.Errorf("plan %q is not valid, expect %s or %s",
l.Plan.String(),
- base.TEAM.String(),
- base.ENTERPRISE.String(),
+ v1pb.PlanType_TEAM.String(),
+ v1pb.PlanType_ENTERPRISE.String(),
)
}
@@ -69,7 +70,7 @@ type LicenseService interface {
// IsFeatureEnabledForInstance returns whether a feature is enabled for the instance.
IsFeatureEnabledForInstance(feature base.FeatureType, instance *store.InstanceMessage) error
// GetEffectivePlan gets the effective plan.
- GetEffectivePlan() base.PlanType
+ GetEffectivePlan() v1pb.PlanType
// GetPlanLimitValue gets the limit value for the plan.
GetPlanLimitValue(ctx context.Context, name PlanLimit) int
// GetInstanceLicenseCount returns the instance count limit for current subscription.
diff --git a/backend/enterprise/api/limit.go b/backend/enterprise/api/limit.go
index 8ba0a3caf0..5fb8796657 100644
--- a/backend/enterprise/api/limit.go
+++ b/backend/enterprise/api/limit.go
@@ -7,7 +7,6 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/yaml.v3"
- "github.com/bytebase/bytebase/backend/base"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -25,7 +24,7 @@ const (
)
// PlanLimitValues is the plan limit value mapping.
-var PlanLimitValues = map[PlanLimit]map[base.PlanType]int{
+var PlanLimitValues = map[PlanLimit]map[v1pb.PlanType]int{
PlanLimitMaximumInstance: {},
PlanLimitMaximumUser: {},
}
@@ -49,22 +48,8 @@ func init() {
panic("failed to unmarshal plan config proto: " + err.Error())
}
- for _, planLimit := range conf.Plans {
- planType := convertProtoPlanType(planLimit.Type)
- PlanLimitValues[PlanLimitMaximumInstance][planType] = int(planLimit.MaximumInstanceCount)
- PlanLimitValues[PlanLimitMaximumUser][planType] = int(planLimit.MaximumSeatCount)
- }
-}
-
-func convertProtoPlanType(protoPlanType v1pb.PlanType) base.PlanType {
- switch protoPlanType {
- case v1pb.PlanType_FREE:
- return base.FREE
- case v1pb.PlanType_TEAM:
- return base.TEAM
- case v1pb.PlanType_ENTERPRISE:
- return base.ENTERPRISE
- default:
- return base.FREE
+ for _, plan := range conf.Plans {
+ PlanLimitValues[PlanLimitMaximumInstance][plan.Type] = int(plan.MaximumInstanceCount)
+ PlanLimitValues[PlanLimitMaximumUser][plan.Type] = int(plan.MaximumSeatCount)
}
}
diff --git a/backend/enterprise/api/subscription.go b/backend/enterprise/api/subscription.go
index e3b9dcb7dd..13feb9a137 100644
--- a/backend/enterprise/api/subscription.go
+++ b/backend/enterprise/api/subscription.go
@@ -3,7 +3,7 @@ package api
import (
"time"
- "github.com/bytebase/bytebase/backend/base"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
// SubscriptionPatch is the API message for update the subscription.
@@ -18,7 +18,7 @@ type Subscription struct {
Seat int
ExpiresTS int64
StartedTS int64
- Plan base.PlanType
+ Plan v1pb.PlanType
Trialing bool
OrgID string
OrgName string
@@ -26,7 +26,7 @@ type Subscription struct {
// IsExpired returns if the subscription is expired.
func (s *Subscription) IsExpired() bool {
- if s.Plan == base.FREE || s.ExpiresTS < 0 {
+ if s.Plan == v1pb.PlanType_FREE || s.ExpiresTS < 0 {
return false
}
return time.Unix(s.ExpiresTS, 0).Before(time.Now())
diff --git a/backend/enterprise/plugin/aws/aws.go b/backend/enterprise/plugin/aws/aws.go
index c7eeb00adc..ac00bc90ac 100644
--- a/backend/enterprise/plugin/aws/aws.go
+++ b/backend/enterprise/plugin/aws/aws.go
@@ -8,10 +8,10 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/base"
"github.com/bytebase/bytebase/backend/common/log"
enterprise "github.com/bytebase/bytebase/backend/enterprise/api"
"github.com/bytebase/bytebase/backend/enterprise/plugin"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
@@ -69,7 +69,7 @@ func (p *Provider) LoadSubscription(ctx context.Context) *enterprise.Subscriptio
subscription := &enterprise.Subscription{
InstanceCount: 0,
Seat: 0,
- Plan: base.FREE,
+ Plan: v1pb.PlanType_FREE,
OrgID: aws.ToString(p.identity.Account),
OrgName: aws.ToString(p.identity.Arn),
}
@@ -86,7 +86,7 @@ func (p *Provider) LoadSubscription(ctx context.Context) *enterprise.Subscriptio
return subscription
}
- subscription.Plan = base.ENTERPRISE
+ subscription.Plan = v1pb.PlanType_ENTERPRISE
if v := license.Validity; v != nil {
begin, err := time.Parse(time.RFC3339, aws.ToString(v.Begin))
diff --git a/backend/enterprise/plugin/hub/hub.go b/backend/enterprise/plugin/hub/hub.go
index 3df92b2562..c01af9d436 100644
--- a/backend/enterprise/plugin/hub/hub.go
+++ b/backend/enterprise/plugin/hub/hub.go
@@ -9,7 +9,6 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/base"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
enterprise "github.com/bytebase/bytebase/backend/enterprise/api"
@@ -17,6 +16,7 @@ import (
"github.com/bytebase/bytebase/backend/enterprise/plugin"
"github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
var _ plugin.LicenseProvider = (*Provider)(nil)
@@ -76,7 +76,7 @@ func (p *Provider) LoadSubscription(ctx context.Context) *enterprise.Subscriptio
license := p.loadLicense(ctx)
if license == nil {
return &enterprise.Subscription{
- Plan: base.FREE,
+ Plan: v1pb.PlanType_FREE,
// -1 means not expire, just for free plan
ExpiresTS: -1,
// Instance license count.
@@ -192,7 +192,7 @@ func (p *Provider) parseClaims(ctx context.Context, claim *claims) (*enterprise.
return nil, common.Errorf(common.Invalid, "plan type %q is not valid", planType)
}
- if claim.WorkspaceID != "" && planType == base.ENTERPRISE && !claim.Trialing {
+ if claim.WorkspaceID != "" && planType == v1pb.PlanType_ENTERPRISE && !claim.Trialing {
workspaceID, err := p.store.GetWorkspaceID(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to get workspace id from setting")
@@ -216,15 +216,15 @@ func (p *Provider) parseClaims(ctx context.Context, claim *claims) (*enterprise.
return license, nil
}
-func convertPlanType(candidate string) (base.PlanType, error) {
+func convertPlanType(candidate string) (v1pb.PlanType, error) {
switch candidate {
- case base.TEAM.String():
- return base.TEAM, nil
- case base.ENTERPRISE.String():
- return base.ENTERPRISE, nil
- case base.FREE.String():
- return base.FREE, nil
+ case "TEAM":
+ return v1pb.PlanType_TEAM, nil
+ case "ENTERPRISE":
+ return v1pb.PlanType_ENTERPRISE, nil
+ case "FREE":
+ return v1pb.PlanType_FREE, nil
default:
- return base.FREE, errors.Errorf("cannot conver plan type %q", candidate)
+ return v1pb.PlanType_FREE, errors.Errorf("cannot convert plan type %q", candidate)
}
}
diff --git a/backend/enterprise/service/license.go b/backend/enterprise/service/license.go
index 5a4bc6577c..88e16217c1 100644
--- a/backend/enterprise/service/license.go
+++ b/backend/enterprise/service/license.go
@@ -14,6 +14,7 @@ import (
enterprise "github.com/bytebase/bytebase/backend/enterprise/api"
"github.com/bytebase/bytebase/backend/enterprise/plugin"
"github.com/bytebase/bytebase/backend/store"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
var _ enterprise.LicenseService = (*LicenseService)(nil)
@@ -60,7 +61,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) *enterprise.Subsc
s.mu.RUnlock()
if cached != nil {
- if cached.Plan == base.FREE || cached.IsExpired() {
+ if cached.Plan == v1pb.PlanType_FREE || cached.IsExpired() {
// refresh expired subscription
s.mu.Lock()
s.cachedSubscription = nil
@@ -76,7 +77,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) *enterprise.Subsc
s.mu.Lock()
defer s.mu.Unlock()
// Double-check after acquiring write lock
- if s.cachedSubscription != nil && s.cachedSubscription.Plan != base.FREE && !s.cachedSubscription.IsExpired() {
+ if s.cachedSubscription != nil && s.cachedSubscription.Plan != v1pb.PlanType_FREE && !s.cachedSubscription.IsExpired() {
return s.cachedSubscription
}
s.cachedSubscription = s.provider.LoadSubscription(ctx)
@@ -95,7 +96,7 @@ func (s *LicenseService) IsFeatureEnabled(feature base.FeatureType) error {
func (s *LicenseService) IsFeatureEnabledForInstance(feature base.FeatureType, instance *store.InstanceMessage) error {
plan := s.GetEffectivePlan()
// DONOT check instance license fo FREE plan.
- if plan == base.FREE {
+ if plan == v1pb.PlanType_FREE {
return s.IsFeatureEnabled(feature)
}
if err := s.IsFeatureEnabled(feature); err != nil {
@@ -121,11 +122,11 @@ func (s *LicenseService) GetInstanceLicenseCount(ctx context.Context) int {
}
// GetEffectivePlan gets the effective plan.
-func (s *LicenseService) GetEffectivePlan() base.PlanType {
+func (s *LicenseService) GetEffectivePlan() v1pb.PlanType {
ctx := context.Background()
subscription := s.LoadSubscription(ctx)
if expireTime := time.Unix(subscription.ExpiresTS, 0); expireTime.Before(time.Now()) {
- return base.FREE
+ return v1pb.PlanType_FREE
}
return subscription.Plan
}
@@ -143,9 +144,9 @@ func (s *LicenseService) GetPlanLimitValue(ctx context.Context, name enterprise.
}
switch subscription.Plan {
- case base.FREE:
+ case v1pb.PlanType_FREE:
return limit
- case base.TEAM, base.ENTERPRISE:
+ case v1pb.PlanType_TEAM, v1pb.PlanType_ENTERPRISE:
switch name {
case enterprise.PlanLimitMaximumInstance:
return limit
diff --git a/backend/runner/metricreport/reporter.go b/backend/runner/metricreport/reporter.go
index 4fc21edb79..b9f22b472e 100644
--- a/backend/runner/metricreport/reporter.go
+++ b/backend/runner/metricreport/reporter.go
@@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/base"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/config"
@@ -18,6 +17,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/metric"
"github.com/bytebase/bytebase/backend/plugin/metric/segment"
"github.com/bytebase/bytebase/backend/store"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
const (
@@ -165,7 +165,7 @@ func (m *Reporter) identify(ctx context.Context) (string, error) {
subscriptionStartDate := ""
subscriptionEndDate := ""
- if subscription.Plan != base.FREE {
+ if subscription.Plan != v1pb.PlanType_FREE {
subscriptionStartDate = time.Unix(subscription.StartedTS, 0).Format(time.RFC3339)
subscriptionEndDate = time.Unix(subscription.ExpiresTS, 0).Format(time.RFC3339)
}