2
0
Эх сурвалжийг харах

add NewOverwriteAWSHandler

Anukul Sangwan 4 жил өмнө
parent
commit
2a3a0eae61

+ 82 - 0
api/server/handlers/project_integrations/overwrite_aws.go

@@ -0,0 +1,82 @@
+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"
+)
+
+type OverwriteAWSHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewOverwriteAWSHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *OverwriteAWSHandler {
+	return &OverwriteAWSHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+func (p *OverwriteAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	request := &types.OverwriteAWSRequest{}
+
+	if ok := p.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	// read the aws integration by ID and overwrite the access id/secret
+	awsIntegration, err := p.Repo().AWSIntegration().ReadAWSIntegration(project.ID, request.AWSIntegrationID)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	awsIntegration.AWSAccessKeyID = []byte(request.AWSAccessKeyID)
+	awsIntegration.AWSSecretAccessKey = []byte(request.AWSSecretAccessKey)
+
+	// handle write to the database
+	awsIntegration, err = p.Repo().AWSIntegration().OverwriteAWSIntegration(awsIntegration)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	if request.ClusterID > 0 {
+		cluster, err := p.Repo().Cluster().ReadCluster(project.ID, request.ClusterID)
+
+		// clear the token
+		cluster.TokenCache.Token = []byte("")
+
+		cluster, err = p.Repo().Cluster().UpdateClusterTokenCache(&cluster.TokenCache)
+
+		if err != nil {
+			p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+			return
+		}
+	}
+
+	// app.Logger.Info().Msgf("AWS integration overwritten: %d", awsIntegration.ID)
+
+	res := types.OverwriteAWSResponse{
+		AWSIntegration: awsIntegration.ToAWSIntegrationType(),
+	}
+
+	p.WriteResult(w, r, res)
+}

+ 28 - 0
api/server/router/project_integration.go

@@ -135,5 +135,33 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
+	// POST /api/projects/{project_id}/integrations/aws/overwrite -> project_integrations.NewOverwriteAWSHandler
+	overwriteAWSEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/aws/overwrite",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	overwriteAWSHandler := project_integration.NewOverwriteAWSHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: overwriteAWSEndpoint,
+		Handler:  overwriteAWSHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 11 - 0
api/types/project_integration.go

@@ -68,3 +68,14 @@ type CreateAWSRequest struct {
 type CreateAWSResponse struct {
 	*AWSIntegration
 }
+
+type OverwriteAWSRequest struct {
+	AWSIntegrationID   uint   `json:"aws_integration_id,required"`
+	AWSAccessKeyID     string `json:"aws_access_key_id,required"`
+	AWSSecretAccessKey string `json:"aws_secret_access_key,required"`
+	ClusterID          uint   `json:"cluster_id"`
+}
+
+type OverwriteAWSResponse struct {
+	*AWSIntegration
+}

+ 3 - 3
dashboard/src/main/home/cluster-dashboard/dashboard/ClusterSettings.tsx

@@ -18,13 +18,13 @@ const ClusterSettings: React.FC = () => {
       .overwriteAWSIntegration(
         "<token>",
         {
+          aws_integration_id: context.currentCluster.aws_integration_id,
           aws_access_key_id: accessKeyId,
           aws_secret_access_key: secretKey,
+          cluster_id: context.currentCluster.id,
         },
         {
-          projectID: context.currentProject.id,
-          awsIntegrationID: context.currentCluster.aws_integration_id,
-          cluster_id: context.currentCluster.id,
+          project_id: context.currentProject.id,
         }
       )
       .then(({ data }) => {

+ 4 - 4
dashboard/src/shared/api.tsx

@@ -47,16 +47,16 @@ const createAWSIntegration = baseApi<
 
 const overwriteAWSIntegration = baseApi<
   {
+    aws_integration_id: number;
     aws_access_key_id: string;
     aws_secret_access_key: string;
+    cluster_id: number;
   },
   {
-    projectID: number;
-    awsIntegrationID: number;
-    cluster_id: number;
+    project_id: number;
   }
 >("POST", (pathParams) => {
-  return `/api/projects/${pathParams.projectID}/integrations/aws/${pathParams.awsIntegrationID}/overwrite?cluster_id=${pathParams.cluster_id}`;
+  return `/api/projects/${pathParams.project_id}/integrations/aws/overwrite`;
 });
 
 const createDOCR = baseApi<

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

@@ -67,8 +67,8 @@
 | <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>- [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/aws`                                                                | AS          |                 | yes         | yes              |
+| <li>- [x] `POST /api/projects/{project_id}/integrations/aws/{aws_integration_id}/overwrite`                                 | AS          | yes             |             | yes              |
 | <li>- [x] `POST /api/projects/{project_id}/integrations/basic`                                                              | AS          |                 | yes         |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/gcp`                                                                |             |                 |             |                  |
 | <li>- [x] `GET /api/projects/{project_id}/integrations/oauth`                                                               | AS          |                 | yes         | yes              |