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

block infra upgrades when an operation is in progress

Alexander Belanger 4 лет назад
Родитель
Сommit
c20f1eb781

+ 18 - 0
api/server/handlers/infra/delete.go

@@ -2,6 +2,7 @@ package infra
 
 import (
 	"context"
+	"fmt"
 	"net/http"
 
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -46,6 +47,23 @@ func (c *InfraDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// if the last operation is in a "starting" state, block apply
+	if lastOperation.Status == "starting" {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			fmt.Errorf("Operation currently in progress. Please try again when latest operation has completed."),
+			http.StatusBadRequest,
+		))
+
+		return
+	}
+
 	// mark the infra as destroying
 	infra.Status = types.StatusDestroying
 

+ 17 - 6
api/server/handlers/infra/retry_create.go

@@ -63,14 +63,25 @@ func (c *InfraRetryCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
+	lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// if the last operation is in a "starting" state, block apply
+	if lastOperation.Status == "starting" {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			fmt.Errorf("Operation currently in progress. Please try again when latest operation has completed."),
+			http.StatusBadRequest,
+		))
+
+		return
+	}
+
 	// if the values are nil, get the last applied values and marshal them
 	if req.Values == nil || len(req.Values) == 0 {
-		lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
-
-		if err != nil {
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
-		}
 
 		rawValues := lastOperation.LastApplied
 

+ 18 - 0
api/server/handlers/infra/retry_delete.go

@@ -2,6 +2,7 @@ package infra
 
 import (
 	"context"
+	"fmt"
 	"net/http"
 
 	"github.com/porter-dev/porter/api/server/handlers"
@@ -41,6 +42,23 @@ func (c *InfraRetryDeleteHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
 		return
 	}
 
+	lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// if the last operation is in a "starting" state, block apply
+	if lastOperation.Status == "starting" {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			fmt.Errorf("Operation currently in progress. Please try again when latest operation has completed."),
+			http.StatusBadRequest,
+		))
+
+		return
+	}
+
 	// call apply on the provisioner service
 	resp, err := c.Config().ProvisionerClient.Delete(context.Background(), proj.ID, infra.ID, &ptypes.DeleteBaseRequest{
 		OperationKind: "retry_delete",

+ 17 - 7
api/server/handlers/infra/update.go

@@ -63,15 +63,25 @@ func (c *InfraUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	// if the values are nil, get the last applied values and marshal them
-	if req.Values == nil || len(req.Values) == 0 {
-		lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
+	lastOperation, err := c.Repo().Infra().GetLatestOperation(infra)
 
-		if err != nil {
-			c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
-			return
-		}
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	// if the last operation is in a "starting" state, block apply
+	if lastOperation.Status == "starting" {
+		c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
+			fmt.Errorf("Operation currently in progress. Please try again when latest operation has completed."),
+			http.StatusBadRequest,
+		))
+
+		return
+	}
 
+	// if the values are nil, get the last applied values and marshal them
+	if req.Values == nil || len(req.Values) == 0 {
 		rawValues := lastOperation.LastApplied
 
 		err = json.Unmarshal(rawValues, &req.Values)