瀏覽代碼

wrap up integration and separate private/public endpoints

Alexander Belanger 3 年之前
父節點
當前提交
b625b345ba

+ 2 - 2
api/server/handlers/billing/redirect_billing.go

@@ -60,7 +60,7 @@ func (c *RedirectBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 
 	req, err := http.NewRequest(
 		"POST",
-		fmt.Sprintf("%s/api/v1/private/cookie", c.Config().ServerConf.BillingServerURL),
+		fmt.Sprintf("%s/api/v1/private/cookie", c.Config().ServerConf.BillingPrivateServerURL),
 		strings.NewReader(string(strData)),
 	)
 
@@ -96,5 +96,5 @@ func (c *RedirectBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 	}
 
 	w.Header().Add("Set-Cookie", dst.Cookie)
-	http.Redirect(w, r, c.Config().ServerConf.BillingServerURL, 302)
+	http.Redirect(w, r, c.Config().ServerConf.BillingPublicServerURL, 302)
 }

+ 1 - 1
api/server/handlers/project/create.go

@@ -79,7 +79,7 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	p.WriteResult(w, r, proj.ToProjectType())
 
 	// add project to billing team
-	_, err = p.Config().BillingManager.CreateTeam(proj)
+	_, err = p.Config().BillingManager.CreateTeam(user, proj)
 
 	if err != nil {
 		// we do not write error response, since setting up billing error can be

+ 2 - 1
api/server/handlers/project/delete.go

@@ -25,6 +25,7 @@ func NewProjectDeleteHandler(
 }
 
 func (p *ProjectDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	user, _ := r.Context().Value(types.UserScope).(*models.User)
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 
 	proj, err := p.Repo().Project().DeleteProject(proj)
@@ -37,7 +38,7 @@ func (p *ProjectDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	p.WriteResult(w, r, proj.ToProjectType())
 
 	// delete the billing team
-	if err := p.Config().BillingManager.DeleteTeam(proj); err != nil {
+	if err := p.Config().BillingManager.DeleteTeam(user, proj); err != nil {
 		// we do not write error response, since setting up billing error can be
 		// resolved later and may not be fatal
 		p.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(err))

+ 1 - 1
api/server/handlers/project/get_billing.go

@@ -30,7 +30,7 @@ func (p *ProjectGetBillingHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
 		HasBilling: false,
 	}
 
-	if sc := p.Config().ServerConf; sc.IronPlansAPIKey != "" && sc.IronPlansServerURL != "" {
+	if sc := p.Config().ServerConf; sc.BillingPrivateKey != "" && sc.BillingPrivateServerURL != "" {
 		// determine if the project has usage attached; if so, set has_billing to true
 		usage, _ := p.Repo().ProjectUsage().ReadProjectUsage(proj.ID)
 

+ 4 - 6
api/server/shared/config/env/envconfs.go

@@ -62,12 +62,10 @@ type ServerConf struct {
 	SlackClientID     string `env:"SLACK_CLIENT_ID"`
 	SlackClientSecret string `env:"SLACK_CLIENT_SECRET"`
 
-	BillingPrivateKey string `env:"BILLING_PRIVATE_KEY"`
-	BillingServerURL  string `env:"BILLING_URL"`
-
-	IronPlansAPIKey    string `env:"IRON_PLANS_API_KEY"`
-	IronPlansServerURL string `env:"IRON_PLANS_SERVER_URL"`
-	WhitelistedUsers   []uint `env:"WHITELISTED_USERS"`
+	BillingPrivateKey       string `env:"BILLING_PRIVATE_KEY"`
+	BillingPrivateServerURL string `env:"BILLING_PRIVATE_URL"`
+	BillingPublicServerURL  string `env:"BILLING_PUBLIC_URL"`
+	WhitelistedUsers        []uint `env:"WHITELISTED_USERS"`
 
 	DOClientID     string `env:"DO_CLIENT_ID"`
 	DOClientSecret string `env:"DO_CLIENT_SECRET"`

+ 3 - 3
api/server/shared/config/loader/init_ee.go

@@ -24,9 +24,9 @@ func init() {
 		key[i] = b
 	}
 
-	if InstanceEnvConf.ServerConf.IronPlansAPIKey != "" && InstanceEnvConf.ServerConf.IronPlansServerURL != "" {
-		serverURL := InstanceEnvConf.ServerConf.IronPlansServerURL
-		apiKey := InstanceEnvConf.ServerConf.IronPlansAPIKey
+	if InstanceEnvConf.ServerConf.BillingPrivateServerURL != "" && InstanceEnvConf.ServerConf.BillingPrivateKey != "" {
+		serverURL := InstanceEnvConf.ServerConf.BillingPrivateServerURL
+		apiKey := InstanceEnvConf.ServerConf.BillingPrivateKey
 		var err error
 
 		InstanceBillingManager, err = eeBilling.NewClient(serverURL, apiKey)

+ 12 - 12
dashboard/src/main/home/project-settings/ProjectSettings.tsx

@@ -71,12 +71,12 @@ class ProjectSettings extends Component<PropsType, StateType> {
     });
 
     if (this.props.isAuthorized("settings", "", ["get", "delete"])) {
-      if (this.context?.hasBillingEnabled) {
-        tabOptions.push({
-          value: "billing",
-          label: "Billing",
-        });
-      }
+      // if (this.context?.hasBillingEnabled) {
+      //   tabOptions.push({
+      //     value: "billing",
+      //     label: "Billing",
+      //   });
+      // }
 
       if (currentProject?.api_tokens_enabled) {
         tabOptions.push({
@@ -104,12 +104,12 @@ class ProjectSettings extends Component<PropsType, StateType> {
       return <InvitePage />;
     }
 
-    if (
-      this.state.currentTab === "billing" &&
-      this.context?.hasBillingEnabled
-    ) {
-      return <BillingPage />;
-    }
+    // if (
+    //   this.state.currentTab === "billing" &&
+    //   this.context?.hasBillingEnabled
+    // ) {
+    //   return <BillingPage />;
+    // }
 
     if (this.state.currentTab === "manage-access") {
       return <InvitePage />;

+ 15 - 43
ee/billing/client.go

@@ -221,47 +221,19 @@ const (
 )
 
 func (c *Client) ParseProjectUsageFromWebhook(payload []byte) (*cemodels.ProjectUsage, error) {
-	// TODO: parse webhook model
-	return nil, nil
-
-	// subscription := &SubscriptionWebhookRequest{}
-
-	// err := json.Unmarshal(payload, subscription)
-
-	// if err != nil {
-	// 	return nil, err
-	// }
-
-	// // if event type is not subscription, return wrong webhook event type error
-	// if subscription.EventType != "subscription" {
-	// 	return nil, nil
-	// }
-
-	// // get the project id linked to that team
-	// projBilling, err := c.repo.ProjectBilling().ReadProjectBillingByTeamID(subscription.TeamID)
-
-	// if err != nil {
-	// 	return nil, err
-	// }
-
-	// usage := &cemodels.ProjectUsage{
-	// 	ProjectID: projBilling.ProjectID,
-	// }
-
-	// for _, feature := range subscription.Plan.Features {
-	// 	// look for slug of "cpus" and "memory"
-	// 	maxLimit := uint(feature.FeatureSpec.MaxLimit)
-	// 	switch feature.Feature.Slug {
-	// 	case FeatureSlugCPU:
-	// 		usage.ResourceCPU = maxLimit
-	// 	case FeatureSlugMemory:
-	// 		usage.ResourceMemory = 1000 * maxLimit
-	// 	case FeatureSlugClusters:
-	// 		usage.Clusters = maxLimit
-	// 	case FeatureSlugUsers:
-	// 		usage.Users = maxLimit
-	// 	}
-	// }
-
-	// return usage, nil
+	usageData := &APIWebhookRequest{}
+
+	err := json.Unmarshal(payload, usageData)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return &cemodels.ProjectUsage{
+		ProjectID:      usageData.ProjectID,
+		ResourceCPU:    usageData.CPU,
+		ResourceMemory: usageData.Memory * 1000,
+		Clusters:       usageData.Clusters,
+		Users:          usageData.Users,
+	}, nil
 }

+ 9 - 0
ee/billing/types.go

@@ -13,3 +13,12 @@ type DeleteCustomerRequest struct {
 	UserID    uint `json:"user_id" form:"required"`
 	ProjectID uint `json:"project_id" form:"required"`
 }
+
+type APIWebhookRequest struct {
+	ProjectID uint `json:"project_id" form:"required"`
+
+	Clusters uint `json:"clusters" form:"required"`
+	Users    uint `json:"users" form:"required"`
+	CPU      uint `json:"cpu" form:"required"`
+	Memory   uint `json:"memory" form:"required"`
+}

+ 4 - 4
internal/billing/billing.go

@@ -11,10 +11,10 @@ type BillingManager interface {
 	// CreateTeam creates the concept of a billing "team". This is currently a one-to-one
 	// mapping with projects, but this may change in the future (i.e. multiple projects
 	// per same team)
-	CreateTeam(proj *models.Project) (teamID string, err error)
+	CreateTeam(user *models.User, proj *models.Project) (teamID string, err error)
 
 	// DeleteTeam deletes a billing team.
-	DeleteTeam(proj *models.Project) (err error)
+	DeleteTeam(user *models.User, proj *models.Project) (err error)
 
 	// ParseProjectUsageFromWebhook parses the project usage from a webhook payload sent
 	// from a billing agent
@@ -27,11 +27,11 @@ type BillingManager interface {
 // NoopBillingManager performs no billing operations
 type NoopBillingManager struct{}
 
-func (n *NoopBillingManager) CreateTeam(proj *models.Project) (teamID string, err error) {
+func (n *NoopBillingManager) CreateTeam(user *models.User, proj *models.Project) (teamID string, err error) {
 	return fmt.Sprintf("%d", proj.ID), nil
 }
 
-func (n *NoopBillingManager) DeleteTeam(proj *models.Project) (err error) {
+func (n *NoopBillingManager) DeleteTeam(user *models.User, proj *models.Project) (err error) {
 	return nil
 }