ソースを参照

add random hash to infra

Alexander Belanger 5 年 前
コミット
b0bf74885e
3 ファイル変更29 行追加9 行削除
  1. 5 0
      internal/forms/infra.go
  2. 9 4
      internal/models/infra.go
  3. 15 5
      server/api/provision_handler.go

+ 5 - 0
internal/forms/infra.go

@@ -1,9 +1,12 @@
 package forms
 
 import (
+	cmdutils "github.com/porter-dev/porter/cli/cmd/utils"
 	"github.com/porter-dev/porter/internal/models"
 )
 
+const randCharset string = "abcdefghijklmnopqrstuvwxyz1234567890"
+
 // CreateECRInfra represents the accepted values for creating an
 // ECR infra via the provisioning container
 type CreateECRInfra struct {
@@ -17,6 +20,7 @@ func (ce *CreateECRInfra) ToAWSInfra() (*models.AWSInfra, error) {
 	return &models.AWSInfra{
 		Kind:             models.AWSInfraECR,
 		ProjectID:        ce.ProjectID,
+		Suffix:           cmdutils.StringWithCharset(6, randCharset),
 		Status:           models.StatusCreating,
 		AWSIntegrationID: ce.AWSIntegrationID,
 	}, nil
@@ -35,6 +39,7 @@ func (ce *CreateEKSInfra) ToAWSInfra() (*models.AWSInfra, error) {
 	return &models.AWSInfra{
 		Kind:             models.AWSInfraEKS,
 		ProjectID:        ce.ProjectID,
+		Suffix:           cmdutils.StringWithCharset(6, randCharset),
 		Status:           models.StatusCreating,
 		AWSIntegrationID: ce.AWSIntegrationID,
 	}, nil

+ 9 - 4
internal/models/infra.go

@@ -13,9 +13,11 @@ type InfraStatus string
 
 // The allowed statuses
 const (
-	StatusCreating InfraStatus = "creating"
-	StatusCreated  InfraStatus = "created"
-	StatusError    InfraStatus = "error"
+	StatusCreating   InfraStatus = "creating"
+	StatusCreated    InfraStatus = "created"
+	StatusError      InfraStatus = "error"
+	StatusDestroying InfraStatus = "destroying"
+	StatusDestroyed  InfraStatus = "destroyed"
 )
 
 // AWSInfraKind is the kind that aws infra can be
@@ -35,6 +37,9 @@ type AWSInfra struct {
 	// The type of infra that was provisioned
 	Kind AWSInfraKind `json:"kind"`
 
+	// A random 6-byte suffix to ensure workspace/stream ids are unique
+	Suffix string
+
 	// The project that this infra belongs to
 	ProjectID uint `json:"project_id"`
 
@@ -71,7 +76,7 @@ func (ai *AWSInfra) Externalize() *AWSInfraExternal {
 
 // GetID returns the unique id for this infra
 func (ai *AWSInfra) GetID() string {
-	return fmt.Sprintf("%s-%d-%d", ai.Kind, ai.ProjectID, ai.ID)
+	return fmt.Sprintf("%s-%d-%d-%s", ai.Kind, ai.ProjectID, ai.ID, ai.Suffix)
 }
 
 // ParseWorkspaceID returns the (kind, projectID, infraID)

+ 15 - 5
server/api/provision_handler.go

@@ -2,7 +2,6 @@ package api
 
 import (
 	"encoding/json"
-	"fmt"
 	"net/http"
 	"strconv"
 
@@ -206,11 +205,22 @@ func (app *App) HandleProvisionAWSEKSInfra(w http.ResponseWriter, r *http.Reques
 // HandleGetProvisioningLogs returns real-time logs of the provisioning process via websockets
 func (app *App) HandleGetProvisioningLogs(w http.ResponseWriter, r *http.Request) {
 	// get path parameters
-	kind := chi.URLParam(r, "kind")
-	projectID := chi.URLParam(r, "project_id")
-	infraID := chi.URLParam(r, "infra_id")
+	infraID, err := strconv.ParseUint(chi.URLParam(r, "infra_id"), 10, 64)
 
-	streamName := fmt.Sprintf("%s-%s-%s", kind, projectID, infraID)
+	if err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	// read infra to get id
+	infra, err := app.Repo.AWSInfra.ReadAWSInfra(uint(infraID))
+
+	if err != nil {
+		app.handleErrorDataRead(err, w)
+		return
+	}
+
+	streamName := infra.GetID()
 
 	upgrader.CheckOrigin = func(r *http.Request) bool { return true }