Browse Source

wip; invite list should count seats

Alexander Belanger 4 years ago
parent
commit
3e0c453800

+ 8 - 4
api/server/handlers/project/get_billing.go

@@ -26,12 +26,16 @@ func NewProjectGetBillingHandler(
 func (p *ProjectGetBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 
-	res := &types.GetProjectBillingResponse{}
+	res := &types.GetProjectBillingResponse{
+		HasBilling: false,
+	}
 
-	// determine if the project has usage attached; if so, set has_billing to true
-	usage, _ := p.Repo().ProjectUsage().ReadProjectUsage(proj.ID)
+	if sc := p.Config().ServerConf; sc.IronPlansAPIKey != "" && sc.IronPlansServerURL != "" {
+		// determine if the project has usage attached; if so, set has_billing to true
+		usage, _ := p.Repo().ProjectUsage().ReadProjectUsage(proj.ID)
 
-	res.HasBilling = usage != nil
+		res.HasBilling = usage != nil
+	}
 
 	p.WriteResult(w, r, res)
 }

+ 6 - 4
dashboard/src/main/home/project-settings/InviteList.tsx

@@ -364,13 +364,15 @@ const InvitePage: React.FunctionComponent<Props> = ({}) => {
     return mappedInviteList || [];
   }, [invites, currentProject?.id, window?.location?.host, isHTTPS, user?.id]);
 
-  const isEnterpriseEdition = () => {
-    return edition === "ee";
-  };
+  const hasSeats = () => {
+    // TODO: if usage limit is 0, the project has unlimited seats. Otherwise, check
+    // the usage limit against the current usage. 
+    return true
+  }
 
   return (
     <>
-      {isEnterpriseEdition() && (
+      {hasSeats() && (
         <>
           <Heading isAtTop={true}>Share Project</Heading>
           <Helper>Generate a project invite for another user.</Helper>

+ 3 - 2
ee/usage/limit.go

@@ -12,11 +12,12 @@ import (
 )
 
 func GetLimit(repo repository.Repository, proj *models.Project) (limit *types.ProjectUsage, err error) {
-	// query for the project limit; if not found, default to basic
+	// query for the project limit; if not found, no limits
 	limitModel, err := repo.ProjectUsage().ReadProjectUsage(proj.ID)
 
 	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
-		copyBasic := types.BasicPlan
+		// place existing users without usage on enterprise plan
+		copyBasic := types.EnterprisePlan
 		limit = &copyBasic
 	} else if err != nil {
 		return nil, err

+ 10 - 1
internal/usage/usage.go

@@ -72,7 +72,7 @@ func GetUsage(opts *GetUsageOpts) (
 			usageCache.ResourceMemory = memory
 		}
 
-		isExceeded := usageCache.ResourceCPU > limit.ResourceCPU || usageCache.ResourceMemory > limit.ResourceMemory
+		isExceeded := isUsageExceeded(usageCache, limit, uint(len(roles)), uint(len(clusters)))
 
 		if !usageCache.Exceeded && isExceeded {
 			// update the usage cache with a time exceeded
@@ -97,6 +97,15 @@ func GetUsage(opts *GetUsageOpts) (
 	}, limit, usageCache, nil
 }
 
+func isUsageExceeded(usageCache *models.ProjectUsageCache, limit *types.ProjectUsage, numUsers, numClusters uint) bool {
+	isCPUExceeded := limit.ResourceCPU != 0 && usageCache.ResourceCPU > limit.ResourceCPU
+	isMemExceeded := limit.ResourceMemory != 0 && usageCache.ResourceMemory > limit.ResourceMemory
+	isUsersExceeded := limit.Users != 0 && numUsers > limit.Users
+	isClustersExceeded := limit.Clusters != 0 && numClusters > limit.Clusters
+
+	return isCPUExceeded || isMemExceeded || isUsersExceeded || isClustersExceeded
+}
+
 // gets the total resource usage across all nodes in all clusters
 func getResourceUsage(opts *GetUsageOpts, clusters []*models.Cluster) (uint, uint, error) {
 	var totCPU, totMem uint = 0, 0