Procházet zdrojové kódy

add NewCreateBasicHandler

Anukul Sangwan před 4 roky
rodič
revize
73c9b3021f

+ 3 - 13
api/client/integration.go

@@ -103,22 +103,12 @@ func (c *Client) CreateGCPIntegration(
 	return bodyResp, nil
 }
 
-// CreateBasicAuthIntegrationRequest represents the accepted fields for creating
-// a "basic auth" integration
-type CreateBasicAuthIntegrationRequest struct {
-	Username string `json:"username"`
-	Password string `json:"password"`
-}
-
-// CreateBasicAuthIntegrationResponse is the resulting integration after creation
-type CreateBasicAuthIntegrationResponse ints.BasicIntegrationExternal
-
 // CreateBasicAuthIntegration creates a "basic auth" integration
 func (c *Client) CreateBasicAuthIntegration(
 	ctx context.Context,
 	projectID uint,
-	createBasic *CreateBasicAuthIntegrationRequest,
-) (*CreateBasicAuthIntegrationResponse, error) {
+	createBasic *types.CreateBasicRequest,
+) (*types.CreateBasicResponse, error) {
 	data, err := json.Marshal(createBasic)
 
 	if err != nil {
@@ -136,7 +126,7 @@ func (c *Client) CreateBasicAuthIntegration(
 	}
 
 	req = req.WithContext(ctx)
-	bodyResp := &CreateBasicAuthIntegrationResponse{}
+	bodyResp := &types.CreateBasicResponse{}
 
 	if httpErr, err := c.sendRequest(req, bodyResp, true); httpErr != nil || err != nil {
 		if httpErr != nil {

+ 62 - 0
api/server/handlers/project_integrations/create_basic.go

@@ -0,0 +1,62 @@
+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 CreateBasicHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewCreateBasicHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *CreateBasicHandler {
+	return &CreateBasicHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+func (p *CreateBasicHandler) 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.CreateBasicRequest{}
+
+	if ok := p.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	basic := CreateBasicIntegration(request.Username, request.Password, project.ID, user.ID)
+
+	basic, err := p.Repo().BasicIntegration().CreateBasicIntegration(basic)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	res := types.CreateBasicResponse{
+		BasicIntegration: basic.ToBasicIntegrationType(),
+	}
+
+	p.WriteResult(w, r, res)
+}
+
+func CreateBasicIntegration(username, password string, projectID, userID uint) *ints.BasicIntegration {
+	return &ints.BasicIntegration{
+		UserID:    userID,
+		ProjectID: projectID,
+		Username:  []byte(username),
+		Password:  []byte(password),
+	}
+}

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

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

+ 19 - 0
api/types/project_integration.go

@@ -25,3 +25,22 @@ type OAuthIntegration struct {
 }
 
 type ListOAuthResponse []*OAuthIntegration
+
+type BasicIntegration 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"`
+}
+
+type CreateBasicRequest struct {
+	Username string `json:"username,required"`
+	Password string `json:"password,required"`
+}
+
+type CreateBasicResponse struct {
+	*BasicIntegration
+}

+ 2 - 1
cli/cmd/connect/dockerhub.go

@@ -3,6 +3,7 @@ package connect
 import (
 	"context"
 	"fmt"
+	"github.com/porter-dev/porter/api/types"
 
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -44,7 +45,7 @@ Token:`)
 	integration, err := client.CreateBasicAuthIntegration(
 		context.Background(),
 		projectID,
-		&api.CreateBasicAuthIntegrationRequest{
+		&types.CreateBasicRequest{
 			Username: username,
 			Password: password,
 		},

+ 2 - 1
cli/cmd/connect/helm.go

@@ -3,6 +3,7 @@ package connect
 import (
 	"context"
 	"fmt"
+	"github.com/porter-dev/porter/api/types"
 	"strings"
 
 	"github.com/fatih/color"
@@ -64,7 +65,7 @@ func Helm(
 	integration, err := client.CreateBasicAuthIntegration(
 		context.Background(),
 		projectID,
-		&api.CreateBasicAuthIntegrationRequest{
+		&types.CreateBasicRequest{
 			Username: username,
 			Password: password,
 		},

+ 2 - 1
cli/cmd/connect/registry.go

@@ -3,6 +3,7 @@ package connect
 import (
 	"context"
 	"fmt"
+	"github.com/porter-dev/porter/api/types"
 
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -44,7 +45,7 @@ Username: `))
 	integration, err := client.CreateBasicAuthIntegration(
 		context.Background(),
 		projectID,
-		&api.CreateBasicAuthIntegrationRequest{
+		&types.CreateBasicRequest{
 			Username: username,
 			Password: password,
 		},

+ 3 - 3
cmd/migrate/keyrotate/helpers_test.go

@@ -5,7 +5,7 @@ import (
 	"testing"
 	"time"
 
-	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/server/shared/config/env"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/adapter"
 	"github.com/porter-dev/porter/internal/models"
@@ -43,7 +43,7 @@ type tester struct {
 func setupTestEnv(tester *tester, t *testing.T) {
 	t.Helper()
 
-	db, err := adapter.New(&shared.DBConf{
+	db, err := adapter.New(&env.DBConf{
 		EncryptionKey: "__random_strong_encryption_key__",
 		SQLLite:       true,
 		SQLLitePath:   tester.dbFileName,
@@ -261,7 +261,7 @@ func initOAuthIntegration(tester *tester, t *testing.T) {
 			AccessToken:  []byte("idtoken"),
 			RefreshToken: []byte("refreshtoken"),
 		},
-		Client:    ints.OAuthGithub,
+		Client:    types.OAuthGithub,
 		ProjectID: tester.initProjects[0].ID,
 		UserID:    tester.initUsers[0].ID,
 	}

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

@@ -46,7 +46,7 @@
 | <li>- [ ] `DELETE /api/projects/{project_id}/clusters/{cluster_id}`                                                         |             |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/clusters/{cluster_id}/node/{node_name}`                                           | AB          |                 |             |                  |
 | <li>- [X] `GET /api/projects/{project_id}/clusters/{cluster_id}/nodes`                                                      | AB          |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/collaborators`                                                                    |             |                 |             |                  |
+| <li>- [x] `GET /api/projects/{project_id}/collaborators`                                                                    | AS          |                 |             | yes              |
 | <li>- [X] `POST /api/projects/{project_id}/delete/{name}`                                                                   | AB          |                 |             |                  |
 | <li>- [X] `POST /api/projects/{project_id}/deploy/addon/{name}/{version}`                                                   | AB          |                 |             |                  |
 | <li>- [X] `POST /api/projects/{project_id}/deploy/{name}/{version}`                                                         | AB          |                 |             |                  |
@@ -69,9 +69,9 @@
 | <li>- [ ] `POST /api/projects/{project_id}/infra/{infra_id}/test/destroy`                                                   |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/aws`                                                                |             |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/aws/{aws_integration_id}/overwrite`                                 |             |                 |             |                  |
-| <li>- [ ] `POST /api/projects/{project_id}/integrations/basic`                                                              |             |                 |             |                  |
+| <li>- [x] `POST /api/projects/{project_id}/integrations/basic`                                                              | AS          |                 | yes         |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/integrations/gcp`                                                                |             |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/integrations/oauth`                                                               |             |                 |             |                  |
+| <li>- [x] `GET /api/projects/{project_id}/integrations/oauth`                                                               | AS          |                 | yes         | yes              |
 | <li>- [x] `GET /api/projects/{project_id}/invites`                                                                          | AS          |                 |             | yes              |
 | <li>- [x] `POST /api/projects/{project_id}/invites`                                                                         | AS          |                 |             | yes              |
 | <li>- [x] `POST /api/projects/{project_id}/invites/{invite_id}`                                                             | AS          |                 |             | yes              |

+ 7 - 15
internal/models/integrations/basic.go

@@ -1,6 +1,10 @@
 package integrations
 
-import "gorm.io/gorm"
+import (
+	"gorm.io/gorm"
+
+	"github.com/porter-dev/porter/api/types"
+)
 
 // BasicIntegration represents a basic auth mechanism via username/password
 type BasicIntegration struct {
@@ -21,20 +25,8 @@ type BasicIntegration struct {
 	Password []byte `json:"password,omitempty"`
 }
 
-// BasicIntegrationExternal is a BasicIntegration to be shared over REST
-type BasicIntegrationExternal 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"`
-}
-
-// Externalize generates an external BasicIntegration to be shared over REST
-func (b *BasicIntegration) Externalize() *BasicIntegrationExternal {
-	return &BasicIntegrationExternal{
+func (b *BasicIntegration) ToBasicIntegrationType() *types.BasicIntegration {
+	return &types.BasicIntegration{
 		ID:        b.ID,
 		UserID:    b.UserID,
 		ProjectID: b.ProjectID,