Просмотр исходного кода

Merge branch 'nico/new-onboarding-flow' of https://github.com/porter-dev/porter into nico/new-onboarding-flow

jusrhee 4 лет назад
Родитель
Сommit
0098bfa828

+ 2 - 1
api/types/project.go

@@ -63,7 +63,8 @@ type DeleteRoleResponse struct {
 }
 
 type GetBillingTokenResponse struct {
-	Token string `json:"token"`
+	Token  string `json:"token"`
+	TeamID string `json:"team_id"`
 }
 
 type GetProjectBillingResponse struct {

+ 10 - 1
dashboard/src/components/ProvisionerStatus.tsx

@@ -98,11 +98,20 @@ const ProvisionerStatus: React.FC<Props> = ({ modules }) => {
 
       if (val.global_errors) {
         for (let globalErr of val.global_errors) {
-          errors.push("Global error: " + globalErr.error_context);
+          errors.push(globalErr.error_context);
           hasError = true;
         }
       }
 
+      // remove duplicate errors
+      errors = errors.filter(
+        (error, index, self) =>
+          index ===
+          self.findIndex(
+            (e) => e === error || e.includes(error) || error.includes(e)
+          )
+      );
+
       const width =
         val.status == "created"
           ? 100

+ 1 - 6
dashboard/src/main/home/onboarding/steps/ProvisionResources/forms/SharedStatus.tsx

@@ -82,12 +82,6 @@ export const SharedStatus: React.FC<{
       ...globalErrors,
     ];
 
-    // remove duplicate global errors
-    tfModules[index].global_errors = tfModules[index].global_errors.filter(
-      (error, index, self) =>
-        index === self.findIndex((e) => e.error_context === error.error_context)
-    );
-
     setTFModules([...tfModules]);
   };
 
@@ -111,6 +105,7 @@ export const SharedStatus: React.FC<{
       ) {
         setInfraStatus({
           hasError: true,
+          description: "Encountered error while provisioning",
         });
         return;
       }

+ 4 - 1
dashboard/src/main/home/project-settings/BillingPage.tsx

@@ -5,6 +5,7 @@ import { Context } from "shared/Context";
 
 function BillingPage() {
   const [customerToken, setCustomerToken] = useState("");
+  const [teamID, setTeamID] = useState("");
   const { currentProject, setCurrentError, queryUsage } = useContext(Context);
 
   useEffect(() => {
@@ -14,7 +15,9 @@ function BillingPage() {
       .then((res) => {
         if (isSubscripted) {
           const token = res?.data?.token;
+          const teamID = res?.data?.team_id;
           setCustomerToken(token);
+          setTeamID(teamID);
         }
       })
       .catch((err) => {
@@ -28,7 +31,7 @@ function BillingPage() {
 
   return (
     <div style={{ height: "1000px" }}>
-      <CustomerProvider token={customerToken}>
+      <CustomerProvider token={customerToken} teamId={teamID}>
         <PlanSelect
           theme={{
             base: {

+ 3 - 2
ee/api/server/handlers/billing/get_token.go

@@ -52,7 +52,7 @@ func (c *BillingGetTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		}
 	}
 
-	token, err := c.Config().BillingManager.GetIDToken(proj.ID, user)
+	token, teamID, err := c.Config().BillingManager.GetIDToken(proj, user)
 
 	if err != nil {
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
@@ -60,6 +60,7 @@ func (c *BillingGetTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 	}
 
 	c.WriteResult(w, r, &types.GetBillingTokenResponse{
-		Token: token,
+		Token:  token,
+		TeamID: teamID,
 	})
 }

+ 15 - 12
ee/billing/ironplans.go

@@ -186,13 +186,16 @@ func (c *Client) RemoveUserFromTeam(role *cemodels.Role) error {
 }
 
 // GetIDToken gets an id token for a user in a project, creating the ID token if necessary
-func (c *Client) GetIDToken(projectID uint, user *cemodels.User) (token string, err error) {
-	// attempt to read the user billing data from the
-	userBilling, err := c.repo.UserBilling().ReadUserBilling(projectID, user.ID)
+func (c *Client) GetIDToken(proj *cemodels.Project, user *cemodels.User) (token string, teamID string, err error) {
+	// attempt to get a team ID for the project
+	teamID, err = c.GetTeamID(proj)
+
+	// attempt to read the user billing data from the project
+	userBilling, err := c.repo.UserBilling().ReadUserBilling(proj.ID, user.ID)
 	notFound := errors.Is(err, gorm.ErrRecordNotFound)
 
 	if !notFound && err != nil {
-		return "", err
+		return "", "", err
 	}
 
 	if !notFound {
@@ -204,14 +207,14 @@ func (c *Client) GetIDToken(projectID uint, user *cemodels.User) (token string,
 
 			// if JWT token has not expired, return the token
 			if !isTokExpired {
-				return token, nil
+				return token, teamID, nil
 			}
 		}
 	}
 
 	req := &CreateIDTokenRequest{
 		Email:  user.Email,
-		UserID: fmt.Sprintf("%d-%d", projectID, user.ID),
+		UserID: fmt.Sprintf("%d-%d", proj.ID, user.ID),
 	}
 
 	resp := &CreateIDTokenResponse{}
@@ -219,38 +222,38 @@ func (c *Client) GetIDToken(projectID uint, user *cemodels.User) (token string,
 	err = c.postRequest("/customers/v1/token", req, resp)
 
 	if err != nil {
-		return "", err
+		return "", "", err
 	}
 
 	token = resp.Token
 
 	if notFound {
 		_, err := c.repo.UserBilling().CreateUserBilling(&models.UserBilling{
-			ProjectID: projectID,
+			ProjectID: proj.ID,
 			UserID:    user.ID,
 			Token:     []byte(token),
 		})
 
 		if err != nil {
-			return "", err
+			return "", "", err
 		}
 	} else {
 		_, err := c.repo.UserBilling().UpdateUserBilling(&models.UserBilling{
 			Model: &gorm.Model{
 				ID: userBilling.ID,
 			},
-			ProjectID:  projectID,
+			ProjectID:  proj.ID,
 			UserID:     user.ID,
 			Token:      []byte(token),
 			TeammateID: userBilling.TeammateID,
 		})
 
 		if err != nil {
-			return "", err
+			return "", "", err
 		}
 	}
 
-	return token, nil
+	return token, teamID, nil
 }
 
 // VerifySignature verifies a webhook signature based on hmac protocol

+ 3 - 3
internal/billing/billing.go

@@ -32,7 +32,7 @@ type BillingManager interface {
 
 	// GetIDToken retrieves a billing token for a user. The billing token can be exchanged
 	// to view billing information.
-	GetIDToken(projectID uint, user *models.User) (token string, err error)
+	GetIDToken(proj *models.Project, user *models.User) (token string, teamID string, err error)
 
 	// ParseProjectUsageFromWebhook parses the project usage from a webhook payload sent
 	// from a billing agent
@@ -69,8 +69,8 @@ func (n *NoopBillingManager) RemoveUserFromTeam(role *models.Role) error {
 	return nil
 }
 
-func (n *NoopBillingManager) GetIDToken(projectID uint, user *models.User) (token string, err error) {
-	return "", nil
+func (n *NoopBillingManager) GetIDToken(proj *models.Project, user *models.User) (token string, teamID string, err error) {
+	return "", "", nil
 }
 
 func (n *NoopBillingManager) ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, error) {