Переглянути джерело

Separate usage and limits, have new ones

gabrielseibel1 2 роки тому
батько
коміт
87b9bd939f
4 змінених файлів з 36 додано та 26 видалено
  1. 16 4
      controllers/server.go
  2. 1 1
      ee/license.go
  3. 17 17
      ee/types.go
  4. 2 4
      ee/util.go

+ 16 - 4
controllers/server.go

@@ -24,12 +24,16 @@ func serverHandlers(r *mux.Router) {
 	r.HandleFunc("/api/server/status", http.HandlerFunc(getStatus)).Methods(http.MethodGet)
 	r.HandleFunc("/api/server/usage", Authorize(true, false, "user", http.HandlerFunc(getUsage))).Methods(http.MethodGet)
 }
+
+// TODO move to EE package? there is a function and a type there for that already
 func getUsage(w http.ResponseWriter, r *http.Request) {
 	type usage struct {
-		Hosts    int `json:"hosts"`
-		Clients  int `json:"clients"`
-		Networks int `json:"networks"`
-		Users    int `json:"users"`
+		Hosts     int `json:"hosts"`
+		Clients   int `json:"clients"`
+		Networks  int `json:"networks"`
+		Users     int `json:"users"`
+		Ingresses int `json:"ingresses"`
+		Egresses  int `json:"egresses"`
 	}
 	var serverUsage usage
 	hosts, err := logic.GetAllHosts()
@@ -48,6 +52,14 @@ func getUsage(w http.ResponseWriter, r *http.Request) {
 	if err == nil {
 		serverUsage.Networks = len(networks)
 	}
+	ingresses, err := logic.GetAllIngresses()
+	if err == nil {
+		serverUsage.Ingresses = len(ingresses)
+	}
+	egresses, err := logic.GetAllEgresses()
+	if err == nil {
+		serverUsage.Egresses = len(egresses)
+	}
 	w.Header().Set("Content-Type", "application/json")
 	json.NewEncoder(w).Encode(models.SuccessResponse{
 		Code:     http.StatusOK,

+ 1 - 1
ee/license.go

@@ -81,7 +81,7 @@ func ValidateLicense() (err error) {
 
 	licenseSecret := LicenseSecret{
 		AssociatedID: netmakerTenantID,
-		Limits:       getCurrentServerLimit(),
+		Usage:        getCurrentServerUsage(),
 	}
 
 	secretData, err := json.Marshal(&licenseSecret)

+ 17 - 17
ee/types.go

@@ -13,15 +13,17 @@ var errValidation = fmt.Errorf(license_validation_err_msg)
 
 // LicenseKey - the license key struct representation with associated data
 type LicenseKey struct {
-	LicenseValue  string `json:"license_value"` // actual (public) key and the unique value for the key
-	Expiration    int64  `json:"expiration"`
-	LimitServers  int    `json:"limit_servers"`
-	LimitUsers    int    `json:"limit_users"`
-	LimitHosts    int    `json:"limit_hosts"`
-	LimitNetworks int    `json:"limit_networks"`
-	LimitClients  int    `json:"limit_clients"`
-	Metadata      string `json:"metadata"`
-	IsActive      bool   `json:"is_active"` // yes if active
+	LicenseValue   string `json:"license_value"` // actual (public) key and the unique value for the key
+	Expiration     int64  `json:"expiration"`
+	UsageServers   int    `json:"usage_servers"`
+	UsageUsers     int    `json:"usage_users"`
+	UsageClients   int    `json:"usage_clients"`
+	UsageHosts     int    `json:"usage_hosts"`
+	UsageNetworks  int    `json:"usage_networks"`
+	UsageIngresses int    `json:"usage_ingresses"`
+	UsageEgresses  int    `json:"usage_egresses"`
+	Metadata       string `json:"metadata"`
+	IsActive       bool   `json:"is_active"` // yes if active
 }
 
 // ValidatedLicense - the validated license struct
@@ -32,30 +34,28 @@ type ValidatedLicense struct {
 
 // LicenseSecret - the encrypted struct for sending user-id
 type LicenseSecret struct {
-	AssociatedID string        `json:"associated_id" binding:"required"` // UUID for user foreign key to User table
-	Limits       LicenseLimits `json:"limits" binding:"required"`
+	AssociatedID string `json:"associated_id" binding:"required"` // UUID for user foreign key to User table
+	Usage        Usage  `json:"usage" binding:"required"`
 }
 
-// LicenseLimits - struct license limits
-type LicenseLimits struct {
+// Usage - struct for license usage
+type Usage struct {
 	Servers   int `json:"servers"`
 	Users     int `json:"users"`
 	Hosts     int `json:"hosts"`
 	Clients   int `json:"clients"`
 	Networks  int `json:"networks"`
-	Machines  int `json:"machines"`
 	Ingresses int `json:"ingresses"`
 	Egresses  int `json:"egresses"`
 }
 
-// LicenseLimits.SetDefaults - sets the default values for limits
-func (l *LicenseLimits) SetDefaults() {
+// Usage.SetDefaults - sets the default values for usage
+func (l *Usage) SetDefaults() {
 	l.Clients = 0
 	l.Servers = 1
 	l.Hosts = 0
 	l.Users = 1
 	l.Networks = 0
-	l.Machines = 0
 	l.Ingresses = 0
 	l.Egresses = 0
 }

+ 2 - 4
ee/util.go

@@ -30,7 +30,8 @@ func base64decode(input string) []byte {
 
 	return bytes
 }
-func getCurrentServerLimit() (limits LicenseLimits) {
+
+func getCurrentServerUsage() (limits Usage) {
 	limits.SetDefaults()
 	hosts, hErr := logic.GetAllHosts()
 	if hErr == nil {
@@ -40,9 +41,6 @@ func getCurrentServerLimit() (limits LicenseLimits) {
 	if cErr == nil {
 		limits.Clients = len(clients)
 	}
-	if hErr == nil && cErr == nil {
-		limits.Machines = len(hosts) + len(clients)
-	}
 	users, err := logic.GetUsers()
 	if err == nil {
 		limits.Users = len(users)