Procházet zdrojové kódy

Simplify metronome customer creation

Mauricio Araujo před 2 roky
rodič
revize
3908b4f6c4

+ 6 - 23
api/server/handlers/billing/customer.go

@@ -59,33 +59,16 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
 	if proj.UsageID == uuid.Nil {
 		// Create Metronome customer and add to starter plan
 		if c.Config().ServerConf.MetronomeAPIKey != "" && c.Config().ServerConf.PorterCloudPlanID != "" && proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) {
-			// Create Metronome Customer
-			if c.Config().ServerConf.MetronomeAPIKey != "" {
-				usageID, err := c.Config().BillingManager.MetronomeClient.CreateCustomer(user.CompanyName, proj.Name, proj.ID, proj.BillingID)
-				if err != nil {
-					err = telemetry.Error(ctx, span, err, "error creating billing customer")
-					c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-					return
-				}
-				proj.UsageID = usageID
-			}
-
-			porterCloudPlanID, err := uuid.Parse(c.Config().ServerConf.PorterCloudPlanID)
-			if err != nil {
-				err = telemetry.Error(ctx, span, err, "error parsing starter plan id")
-				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-				return
-			}
-
-			// Add to starter plan
-			customerPlanID, err := c.Config().BillingManager.MetronomeClient.AddCustomerPlan(proj.UsageID, porterCloudPlanID)
+			customerPlanID, err := c.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(user.CompanyName, proj.Name, proj.ID, proj.BillingID, c.Config().ServerConf.PorterCloudPlanID)
 			if err != nil {
-				err = telemetry.Error(ctx, span, err, "error adding customer to starter plan")
+				err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
 				c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-				return
 			}
 			proj.UsagePlanID = customerPlanID
-			shouldUpdate = true
+			telemetry.WithAttributes(span,
+				telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
+				telemetry.AttributeKV{Key: "usage-plan-id", Value: proj.UsagePlanID},
+			)
 		}
 	}
 

+ 7 - 6
api/server/handlers/cluster/install_agent.go

@@ -106,11 +106,13 @@ func (c *InstallAgentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 
 	porterAgentValues := map[string]interface{}{
 		"agent": map[string]interface{}{
-			"porterHost":  c.Config().ServerConf.ServerURL,
-			"porterPort":  "443",
-			"porterToken": encoded,
-			"clusterID":   fmt.Sprintf("%d", cluster.ID),
-			"projectID":   fmt.Sprintf("%d", proj.ID),
+			"porterHost":    c.Config().ServerConf.ServerURL,
+			"porterPort":    "443",
+			"porterToken":   encoded,
+			"clusterID":     fmt.Sprintf("%d", cluster.ID),
+			"projectID":     fmt.Sprintf("%d", proj.ID),
+			"prometheusURL": c.Config().ServerConf.PrometheusUrl,
+			"metronomeKey":  c.Config().ServerConf.MetronomeAPIKey,
 		},
 		"loki": map[string]interface{}{},
 	}
@@ -185,7 +187,6 @@ func checkAndDeleteOlderAgent(k8sAgent *kubernetes.Agent, helmAgent *helm.Agent)
 	}
 
 	_, err = helmAgent.UninstallChart(context.Background(), "porter-agent")
-
 	if err != nil {
 		return err
 	}

+ 11 - 34
api/server/handlers/project/create.go

@@ -3,7 +3,6 @@ package project
 import (
 	"net/http"
 
-	"github.com/google/uuid"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
@@ -81,7 +80,6 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 	}
 
 	// Create Stripe Customer
-	var shouldUpdate bool
 	if p.Config().ServerConf.StripeSecretKey != "" && p.Config().ServerConf.StripePublishableKey != "" {
 		// Create billing customer for project and set the billing ID
 		billingID, err := p.Config().BillingManager.StripeClient.CreateCustomer(ctx, user.Email, proj)
@@ -91,8 +89,6 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 			return
 		}
 		proj.BillingID = billingID
-		shouldUpdate = true
-
 		telemetry.WithAttributes(span,
 			telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
 			telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
@@ -102,42 +98,23 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 
 	// Create Metronome customer and add to starter plan
 	if p.Config().ServerConf.MetronomeAPIKey != "" && p.Config().ServerConf.PorterCloudPlanID != "" && proj.GetFeatureFlag(models.MetronomeEnabled, p.Config().LaunchDarklyClient) {
-		// Create Metronome Customer
-		if p.Config().ServerConf.MetronomeAPIKey != "" {
-			usageID, err := p.Config().BillingManager.MetronomeClient.CreateCustomer(user.CompanyName, proj.Name, proj.ID, proj.BillingID)
-			if err != nil {
-				err = telemetry.Error(ctx, span, err, "error creating billing customer")
-				p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-				return
-			}
-			proj.UsageID = usageID
-			shouldUpdate = true
-		}
-
-		porterCloudPlanID, err := uuid.Parse(p.Config().ServerConf.PorterCloudPlanID)
+		customerPlanID, err := p.Config().BillingManager.MetronomeClient.CreateCustomerWithPlan(user.CompanyName, proj.Name, proj.ID, proj.BillingID, p.Config().ServerConf.PorterCloudPlanID)
 		if err != nil {
-			err = telemetry.Error(ctx, span, err, "error parsing starter plan id")
+			err = telemetry.Error(ctx, span, err, "error creating Metronome customer")
 			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
-		}
-
-		// Add to starter plan
-		customerPlanID, err := p.Config().BillingManager.MetronomeClient.AddCustomerPlan(proj.UsageID, porterCloudPlanID)
-		if err != nil {
-			err = telemetry.Error(ctx, span, err, "error adding customer to starter plan")
-			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
 		}
 		proj.UsagePlanID = customerPlanID
+		telemetry.WithAttributes(span,
+			telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
+			telemetry.AttributeKV{Key: "usage-plan-id", Value: proj.UsagePlanID},
+		)
 	}
 
-	if shouldUpdate {
-		_, err = p.Repo().Project().UpdateProject(proj)
-		if err != nil {
-			err := telemetry.Error(ctx, span, err, "error updating project")
-			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
-		}
+	_, err = p.Repo().Project().UpdateProject(proj)
+	if err != nil {
+		err := telemetry.Error(ctx, span, err, "error updating project")
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
 	}
 
 	// create default project usage restriction

+ 1 - 3
api/server/shared/config/env/envconfs.go

@@ -1,8 +1,6 @@
 package env
 
-import (
-	"time"
-)
+import "time"
 
 // ServerConf is the server configuration
 type ServerConf struct {

+ 1 - 3
api/types/billing.go

@@ -1,8 +1,6 @@
 package types
 
-import (
-	"github.com/google/uuid"
-)
+import "github.com/google/uuid"
 
 // Stripe types
 

+ 19 - 4
internal/billing/metronome.go

@@ -33,8 +33,8 @@ func NewMetronomeClient(metronomeApiKey string) *MetronomeClient {
 	}
 }
 
-// CreateCustomer will create the customer in Metronome
-func (m *MetronomeClient) CreateCustomer(orgName string, projectName string, projectID uint, billingID string) (customerID uuid.UUID, err error) {
+// createCustomer will create the customer in Metronome
+func (m *MetronomeClient) createCustomer(orgName string, projectName string, projectID uint, billingID string) (customerID uuid.UUID, err error) {
 	path := "customers"
 	projIDStr := strconv.FormatUint(uint64(projectID), 10)
 
@@ -61,8 +61,8 @@ func (m *MetronomeClient) CreateCustomer(orgName string, projectName string, pro
 	return result.Data.ID, nil
 }
 
-// AddCustomerPlan will start the customer on the given plan
-func (m *MetronomeClient) AddCustomerPlan(customerID uuid.UUID, planID uuid.UUID) (customerPlanID uuid.UUID, err error) {
+// addCustomerPlan will start the customer on the given plan
+func (m *MetronomeClient) addCustomerPlan(customerID uuid.UUID, planID uuid.UUID) (customerPlanID uuid.UUID, err error) {
 	if customerID == uuid.Nil || planID == uuid.Nil {
 		return customerPlanID, fmt.Errorf("customer or plan id empty")
 	}
@@ -89,6 +89,21 @@ func (m *MetronomeClient) AddCustomerPlan(customerID uuid.UUID, planID uuid.UUID
 	return result.Data.CustomerPlanID, nil
 }
 
+// CreateCustomerWithPlan will create the customer in Metronome and immediately add it to the plan
+func (m *MetronomeClient) CreateCustomerWithPlan(orgName string, projectName string, projectID uint, billingID string, planID string) (customerPlanID uuid.UUID, err error) {
+	porterCloudPlanID, err := uuid.Parse(planID)
+	if err != nil {
+		return customerPlanID, fmt.Errorf("error parsing starter plan id: %w", err)
+	}
+
+	customerID, err := m.createCustomer(orgName, projectName, projectID, billingID)
+	if err != nil {
+		return customerPlanID, err
+	}
+
+	return m.addCustomerPlan(customerID, porterCloudPlanID)
+}
+
 // EndCustomerPlan will immediately end the plan for the given customer
 func (m *MetronomeClient) EndCustomerPlan(customerID uuid.UUID, customerPlanID uuid.UUID) (err error) {
 	if customerID == uuid.Nil || customerPlanID == uuid.Nil {