Anukul Sangwan 4 лет назад
Родитель
Сommit
9e8eb0aaa3

+ 3 - 14
api/client/integration.go

@@ -11,23 +11,12 @@ import (
 	ints "github.com/porter-dev/porter/internal/models/integrations"
 )
 
-// CreateAWSIntegrationRequest represents the accepted fields for creating
-// an aws integration
-type CreateAWSIntegrationRequest struct {
-	AWSRegion          string `json:"aws_region"`
-	AWSAccessKeyID     string `json:"aws_access_key_id"`
-	AWSSecretAccessKey string `json:"aws_secret_access_key"`
-}
-
-// CreateAWSIntegrationResponse is the resulting integration after creation
-type CreateAWSIntegrationResponse ints.AWSIntegrationExternal
-
 // CreateAWSIntegration creates an AWS integration with the given request options
 func (c *Client) CreateAWSIntegration(
 	ctx context.Context,
 	projectID uint,
-	createAWS *CreateAWSIntegrationRequest,
-) (*CreateAWSIntegrationResponse, error) {
+	createAWS *types.CreateAWSRequest,
+) (*types.CreateAWSResponse, error) {
 	data, err := json.Marshal(createAWS)
 
 	if err != nil {
@@ -45,7 +34,7 @@ func (c *Client) CreateAWSIntegration(
 	}
 
 	req = req.WithContext(ctx)
-	bodyResp := &CreateAWSIntegrationResponse{}
+	bodyResp := &types.CreateAWSResponse{}
 
 	if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
 		if httpErr != nil {

+ 64 - 0
api/server/handlers/project_integrations/create_aws.go

@@ -0,0 +1,64 @@
+package project_integration
+
+import (
+	"net/http"
+
+	"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"
+	"github.com/porter-dev/porter/api/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/models"
+	ints "github.com/porter-dev/porter/internal/models/integrations"
+)
+
+type CreateAWSHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewCreateAWSHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *CreateAWSHandler {
+	return &CreateAWSHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+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)
+
+	request := &types.CreateAWSRequest{}
+
+	if ok := p.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	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
+	}
+
+	res := types.CreateAWSResponse{
+		AWSIntegration: aws.ToAWSIntegrationType(),
+	}
+
+	p.WriteResult(w, r, res)
+}
+
+func CreateAWSIntegration(request *types.CreateAWSRequest, projectID, userID uint) *ints.AWSIntegration {
+	return &ints.AWSIntegration{
+		UserID:             userID,
+		ProjectID:          projectID,
+		AWSRegion:          request.AWSRegion,
+		AWSClusterID:       []byte(request.AWSClusterID),
+		AWSAccessKeyID:     []byte(request.AWSAccessKeyID),
+		AWSSecretAccessKey: []byte(request.AWSSecretAccessKey),
+	}
+}

+ 29 - 1
api/server/router/project_integration.go

@@ -79,7 +79,7 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
-	// POST /api/projects/{project_id}/integrations/basic -> project_integrations.NewCreateBasic
+	// POST /api/projects/{project_id}/integrations/basic -> project_integrations.NewCreateBasicHandler
 	createBasicEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbCreate,
@@ -107,5 +107,33 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
+	// POST /api/projects/{project_id}/integrations/aws -> project_integrations.NewCreateAWSHandler
+	createAWSEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/aws",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	createAWSHandler := project_integration.NewCreateAWSHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: createAWSEndpoint,
+		Handler:  createAWSHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 24 - 0
api/types/project_integration.go

@@ -44,3 +44,27 @@ type CreateBasicRequest struct {
 type CreateBasicResponse struct {
 	*BasicIntegration
 }
+
+type AWSIntegration struct {
+	ID uint `json:"id"`
+
+	// The id of the user that linked this auth mechanism
+	UserID uint `json:"user_id"`
+
+	// The project that this integration belongs to
+	ProjectID uint `json:"project_id"`
+
+	// The AWS arn this is integration is linked to
+	AWSArn string `json:"aws_arn"`
+}
+
+type CreateAWSRequest struct {
+	AWSRegion          string `json:"aws_region"`
+	AWSClusterID       string `json:"aws_cluster_id"`
+	AWSAccessKeyID     string `json:"aws_access_key_id"`
+	AWSSecretAccessKey string `json:"aws_secret_access_key"`
+}
+
+type CreateAWSResponse struct {
+	*AWSIntegration
+}

+ 3 - 2
cli/cmd/connect/ecr.go

@@ -3,6 +3,7 @@ package connect
 import (
 	"context"
 	"fmt"
+	"github.com/porter-dev/porter/api/types"
 	"strings"
 	"time"
 
@@ -60,7 +61,7 @@ Would you like to proceed? %s `,
 		integration, err := client.CreateAWSIntegration(
 			context.Background(),
 			projectID,
-			&api.CreateAWSIntegrationRequest{
+			&types.CreateAWSRequest{
 				AWSAccessKeyID:     creds.AWSAccessKeyID,
 				AWSSecretAccessKey: creds.AWSSecretAccessKey,
 				AWSRegion:          region,
@@ -107,7 +108,7 @@ func ecrManual(
 	integration, err := client.CreateAWSIntegration(
 		context.Background(),
 		projectID,
-		&api.CreateAWSIntegrationRequest{
+		&types.CreateAWSRequest{
 			AWSAccessKeyID:     accessKeyID,
 			AWSSecretAccessKey: secretKey,
 			AWSRegion:          region,

+ 1 - 1
docs/developing/backend-refactor-status.md

@@ -67,7 +67,7 @@
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/eks/destroy`                                                    |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/gke/destroy`                                                    |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/test/destroy`                                                   |             |                 |             |                  |
-| <li>- [ ] `POST /api/projects/{project_id}/integrations/aws`                                                                |             |                 |             |                  |
+| <li>- [x] `POST /api/projects/{project_id}/integrations/aws`                                                                | AS          |                 | yes         |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/aws/{aws_integration_id}/overwrite`                                 |             |                 |             |                  |
 | <li>- [x] `POST /api/projects/{project_id}/integrations/basic`                                                              | AS          |                 | yes         |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/gcp`                                                                |             |                 |             |                  |

+ 4 - 18
internal/models/integrations/aws.go

@@ -8,7 +8,8 @@ import (
 
 	"github.com/aws/aws-sdk-go/aws/credentials"
 	"github.com/aws/aws-sdk-go/aws/session"
-	token "sigs.k8s.io/aws-iam-authenticator/pkg/token"
+	"github.com/porter-dev/porter/api/types"
+	"sigs.k8s.io/aws-iam-authenticator/pkg/token"
 )
 
 // AWSIntegration is an auth mechanism that uses a AWS IAM user to
@@ -46,23 +47,8 @@ type AWSIntegration struct {
 	AWSSessionToken []byte `json:"aws_session_token"`
 }
 
-// AWSIntegrationExternal is a AWSIntegration to be shared over REST
-type AWSIntegrationExternal struct {
-	ID uint `json:"id"`
-
-	// The id of the user that linked this auth mechanism
-	UserID uint `json:"user_id"`
-
-	// The project that this integration belongs to
-	ProjectID uint `json:"project_id"`
-
-	// The AWS arn this is integration is linked to
-	AWSArn string `json:"aws_arn"`
-}
-
-// Externalize generates an external KubeIntegration to be shared over REST
-func (a *AWSIntegration) Externalize() *AWSIntegrationExternal {
-	return &AWSIntegrationExternal{
+func (a *AWSIntegration) ToAWSIntegrationType() *types.AWSIntegration {
+	return &types.AWSIntegration{
 		ID:        a.ID,
 		UserID:    a.UserID,
 		ProjectID: a.ProjectID,