Explorar el Código

add url to registry create

Alexander Belanger hace 5 años
padre
commit
ebfa51c251

+ 31 - 3
internal/forms/registry.go

@@ -1,6 +1,7 @@
 package forms
 
 import (
+	"github.com/aws/aws-sdk-go/service/ecr"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/repository"
 )
@@ -16,13 +17,40 @@ type CreateRegistry struct {
 }
 
 // ToRegistry converts the form to a gorm registry model
-func (cr *CreateRegistry) ToRegistry() (*models.Registry, error) {
-	return &models.Registry{
+func (cr *CreateRegistry) ToRegistry(repo repository.Repository) (*models.Registry, error) {
+	registry := &models.Registry{
 		Name:             cr.Name,
 		ProjectID:        cr.ProjectID,
+		URL:              cr.URL,
 		GCPIntegrationID: cr.GCPIntegrationID,
 		AWSIntegrationID: cr.AWSIntegrationID,
-	}, nil
+	}
+
+	if registry.URL == "" && registry.AWSIntegrationID != 0 {
+		awsInt, err := repo.AWSIntegration.ReadAWSIntegration(registry.AWSIntegrationID)
+
+		if err != nil {
+			return nil, err
+		}
+
+		sess, err := awsInt.GetSession()
+
+		if err != nil {
+			return nil, err
+		}
+
+		ecrSvc := ecr.New(sess)
+
+		output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
+
+		if err != nil {
+			return nil, err
+		}
+
+		registry.URL = *output.AuthorizationData[0].ProxyEndpoint
+	}
+
+	return registry, nil
 }
 
 // UpdateRegistryForm represents the accepted values for updating a

+ 24 - 1
internal/kubernetes/provisioner/global_stream.go

@@ -7,6 +7,7 @@ import (
 	"fmt"
 	"regexp"
 
+	"github.com/aws/aws-sdk-go/service/ecr"
 	"github.com/porter-dev/porter/internal/repository"
 
 	redis "github.com/go-redis/redis/v8"
@@ -141,7 +142,29 @@ func GlobalStreamListener(
 						json.Unmarshal([]byte(dataString), reg)
 					}
 
-					reg, err := repo.Registry.CreateRegistry(reg)
+					awsInt, err := repo.AWSIntegration.ReadAWSIntegration(reg.AWSIntegrationID)
+
+					if err != nil {
+						continue
+					}
+
+					sess, err := awsInt.GetSession()
+
+					if err != nil {
+						continue
+					}
+
+					ecrSvc := ecr.New(sess)
+
+					output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
+
+					if err != nil {
+						continue
+					}
+
+					reg.URL = *output.AuthorizationData[0].ProxyEndpoint
+
+					reg, err = repo.Registry.CreateRegistry(reg)
 
 					if err != nil {
 						continue

+ 1 - 29
server/api/registry_handler.go

@@ -41,41 +41,13 @@ func (app *App) HandleCreateRegistry(w http.ResponseWriter, r *http.Request) {
 	}
 
 	// convert the form to a registry
-	registry, err := form.ToRegistry()
+	registry, err := form.ToRegistry(*app.Repo)
 
 	if err != nil {
 		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
 		return
 	}
 
-	// if the registry is ECR and URL is not set, get the registry url
-	if registry.URL == "" && registry.AWSIntegrationID != 0 {
-		awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(registry.AWSIntegrationID)
-
-		if err != nil {
-			app.handleErrorDataRead(err, w)
-			return
-		}
-
-		sess, err := awsInt.GetSession()
-
-		if err != nil {
-			app.handleErrorDataRead(err, w)
-			return
-		}
-
-		ecrSvc := ecr.New(sess)
-
-		output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
-
-		if err != nil {
-			app.handleErrorDataRead(err, w)
-			return
-		}
-
-		registry.URL = *output.AuthorizationData[0].ProxyEndpoint
-	}
-
 	// handle write to the database
 	registry, err = app.Repo.Registry.CreateRegistry(registry)