Explorar el Código

e2e flow working, need to revamp new cluster creation for duplicates

Stefan McShane hace 3 años
padre
commit
f4a980be90

+ 4 - 1
api/server/handlers/project/create_cluster.go

@@ -1,6 +1,7 @@
 package project
 
 import (
+	"encoding/base64"
 	"encoding/json"
 	"fmt"
 	"net/http"
@@ -47,6 +48,7 @@ func (c *CreateClusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 			ProvisionedBy:                     "CAPI",
 			CloudProvider:                     "AWS",
 			CloudProviderCredentialIdentifier: capiClusterReq.CloudProviderCredentialsID,
+			Name:                              capiClusterReq.ClusterSettings.ClusterName,
 		}
 		cl, err := c.Config().Repo.Cluster().CreateCluster(&dbCluster)
 		if err != nil {
@@ -63,11 +65,12 @@ func (c *CreateClusterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
 		return
 	}
+	b64 := base64.StdEncoding.EncodeToString(by)
 
 	capiConfig := models.CAPIConfig{
 		ClusterID:         int(capiClusterReq.ClusterID),
 		ProjectID:         int(capiClusterReq.ProjectID),
-		Base64RequestJSON: string(by),
+		Base64RequestJSON: string(b64),
 	}
 
 	_, err = c.Config().Repo.CAPIConfigRepository().Insert(ctx, capiConfig)

+ 21 - 2
api/server/handlers/project_integration/create_aws.go

@@ -1,8 +1,11 @@
 package project_integration
 
 import (
+	"fmt"
 	"net/http"
 
+	"github.com/bufbuild/connect-go"
+	porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
 	"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"
@@ -29,9 +32,9 @@ func NewCreateAWSHandler(
 func (p *CreateAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	user, _ := r.Context().Value(types.UserScope).(*models.User)
 	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+	ctx := r.Context()
 
 	request := &types.CreateAWSRequest{}
-
 	if ok := p.DecodeAndValidate(w, r, request); !ok {
 		return
 	}
@@ -39,7 +42,6 @@ func (p *CreateAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	aws := CreateAWSIntegration(request, project.ID, user.ID)
 
 	aws, err := p.Repo().AWSIntegration().CreateAWSIntegration(aws)
-
 	if err != nil {
 		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
 		return
@@ -49,6 +51,23 @@ func (p *CreateAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		AWSIntegration: aws.ToAWSIntegrationType(),
 	}
 
+	if !p.Config().DisableCAPIProvisioner {
+		credReq := porterv1.CreateAssumeRoleChainRequest{
+			ProjectId:       int64(project.ID),
+			SourceArn:       "arn:aws:iam::108458755588:role/CAPIManagement", // hard coded as this is the final hop for a CAPI cluster
+			TargetAccessId:  request.AWSAccessKeyID,
+			TargetSecretKey: request.AWSSecretAccessKey,
+		}
+		credResp, err := p.Config().ClusterControlPlaneClient.CreateAssumeRoleChain(ctx, connect.NewRequest(&credReq))
+		if err != nil {
+			e := fmt.Errorf("unable to create CAPI required credential: %w", err)
+			p.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+			return
+		}
+		res.CloudProviderCredentialIdentifier = credResp.Msg.TargetArn
+		fmt.Println("stefan", credResp.Msg.ProjectId, credResp.Msg.TargetArn)
+	}
+
 	p.WriteResult(w, r, res)
 }
 

+ 1 - 0
api/types/project_integration.go

@@ -85,6 +85,7 @@ type CreateAWSRequest struct {
 
 type CreateAWSResponse struct {
 	*AWSIntegration
+	CloudProviderCredentialIdentifier string `json:"cloud_provider_credentials_id"`
 }
 
 type OverwriteAWSRequest struct {

+ 4 - 4
dashboard/src/components/CredentialsForm.tsx

@@ -82,7 +82,7 @@ const CredentialsForm: React.FC<Props> = ({
       )
       .then(({ data }) => {
         setCreateStatus("successful");
-        proceed(data.id);
+        proceed(data.cloud_provider_credentials_id);
       })
       .catch((err) => {
         console.error(err);
@@ -98,7 +98,7 @@ const CredentialsForm: React.FC<Props> = ({
             {
               awsCredentials.map((cred: AWSCredential, i: number) => {
                 return (
-                  <Credential 
+                  <Credential
                     key={cred.id}
                     isSelected={cred.id === selectedCredentials?.id}
                     onClick={() => {
@@ -143,7 +143,7 @@ const CredentialsForm: React.FC<Props> = ({
               </CloseButton>
             )
           }
-          <InputRow 
+          <InputRow
             type="string"
             value={awsAccessKeyID}
             setValue={(e: string) => setAWSAccessKeyID(e)}
@@ -151,7 +151,7 @@ const CredentialsForm: React.FC<Props> = ({
             placeholder="ex: AKIAIOSFODNN7EXAMPLE"
             isRequired
           />
-          <InputRow 
+          <InputRow
             type="password"
             value={awsSecretAccessKey}
             setValue={(e: string) => setAWSSecretAccessKey(e)}

+ 4 - 0
internal/repository/gorm/capi_config.go

@@ -3,6 +3,7 @@ package gorm
 import (
 	"context"
 
+	"github.com/google/uuid"
 	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/repository"
 	"gorm.io/gorm"
@@ -20,6 +21,9 @@ func NewCAPIConfigRepository(db *gorm.DB) repository.CAPIConfigRepository {
 
 // Insert creates a new record in the capi_configs table
 func (cr CAPIConfigRepository) Insert(ctx context.Context, conf models.CAPIConfig) (models.CAPIConfig, error) {
+	if conf.ID == uuid.Nil {
+		conf.ID = uuid.New()
+	}
 	tx := cr.db.Create(&conf)
 	if tx.Error != nil {
 		return conf, tx.Error