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

add endpoints for getting credentials

Alexander Belanger 4 жил өмнө
parent
commit
54553cfb85

+ 6 - 1
api/server/handlers/project_integration/create_aws.go

@@ -53,7 +53,7 @@ func (p *CreateAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 }
 
 func CreateAWSIntegration(request *types.CreateAWSRequest, projectID, userID uint) *ints.AWSIntegration {
-	return &ints.AWSIntegration{
+	resp := &ints.AWSIntegration{
 		UserID:             userID,
 		ProjectID:          projectID,
 		AWSRegion:          request.AWSRegion,
@@ -61,4 +61,9 @@ func CreateAWSIntegration(request *types.CreateAWSRequest, projectID, userID uin
 		AWSAccessKeyID:     []byte(request.AWSAccessKeyID),
 		AWSSecretAccessKey: []byte(request.AWSSecretAccessKey),
 	}
+
+	// attempt to populate the ARN
+	resp.PopulateAWSArn()
+
+	return resp
 }

+ 44 - 0
api/server/handlers/project_integration/list_aws.go

@@ -0,0 +1,44 @@
+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 ListAWSHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListAWSHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListAWSHandler {
+	return &ListAWSHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (p *ListAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	awsInts, err := p.Repo().AWSIntegration().ListAWSIntegrationsByProjectID(project.ID)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	var res types.ListAWSResponse = make([]*types.AWSIntegration, 0)
+
+	for _, awsInt := range awsInts {
+		res = append(res, awsInt.ToAWSIntegrationType())
+	}
+
+	p.WriteResult(w, r, res)
+}

+ 46 - 0
api/server/handlers/project_integration/list_do.go

@@ -0,0 +1,46 @@
+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 ListDOHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListDOHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListDOHandler {
+	return &ListDOHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (p *ListDOHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	oauthInts, err := p.Repo().OAuthIntegration().ListOAuthIntegrationsByProjectID(project.ID)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	var res types.ListOAuthResponse = make([]*types.OAuthIntegration, 0)
+
+	for _, oauthInt := range oauthInts {
+		if oauthInt.Client == types.OAuthDigitalOcean {
+			res = append(res, oauthInt.ToOAuthIntegrationType())
+		}
+	}
+
+	p.WriteResult(w, r, res)
+}

+ 44 - 0
api/server/handlers/project_integration/list_gcp.go

@@ -0,0 +1,44 @@
+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 ListGCPHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListGCPHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListGCPHandler {
+	return &ListGCPHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (p *ListGCPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	gcpInts, err := p.Repo().GCPIntegration().ListGCPIntegrationsByProjectID(project.ID)
+
+	if err != nil {
+		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	var res types.ListGCPResponse = make([]*types.GCPIntegration, 0)
+
+	for _, gcpInt := range gcpInts {
+		res = append(res, gcpInt.ToGCPIntegrationType())
+	}
+
+	p.WriteResult(w, r, res)
+}

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

@@ -79,6 +79,33 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/integrations/do -> project_integration.NewListDOHandler
+	listDOEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/do",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	listDOHandler := project_integration.NewListDOHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listDOEndpoint,
+		Handler:  listDOHandler,
+		Router:   r,
+	})
+
 	// POST /api/projects/{project_id}/integrations/basic -> project_integration.NewCreateBasicHandler
 	createBasicEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
@@ -135,6 +162,33 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/integrations/aws -> project_integration.NewListAWSHandler
+	listAWSEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/aws",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	listAWSHandler := project_integration.NewListAWSHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listAWSEndpoint,
+		Handler:  listAWSHandler,
+		Router:   r,
+	})
+
 	// POST /api/projects/{project_id}/integrations/aws/overwrite -> project_integration.NewOverwriteAWSHandler
 	overwriteAWSEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
@@ -191,5 +245,32 @@ func getProjectIntegrationRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/integrations/gcp -> project_integration.NewListGCPHandler
+	listGCPEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/gcp",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	listGCPHandler := project_integration.NewListGCPHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listGCPEndpoint,
+		Handler:  listGCPHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 12 - 0
api/types/project_integration.go

@@ -1,5 +1,7 @@
 package types
 
+import "time"
+
 // The supported oauth mechanism clients
 const (
 	OAuthGithub       OAuthIntegrationClient = "github"
@@ -12,6 +14,8 @@ type OAuthIntegrationClient string
 
 // OAuthIntegration is an OAuthIntegration to be shared over REST
 type OAuthIntegration struct {
+	CreatedAt time.Time `json:"created_at"`
+
 	ID uint `json:"id"`
 
 	// The name of the auth mechanism
@@ -46,6 +50,8 @@ type CreateBasicResponse struct {
 }
 
 type AWSIntegration struct {
+	CreatedAt time.Time `json:"created_at"`
+
 	ID uint `json:"id"`
 
 	// The id of the user that linked this auth mechanism
@@ -58,6 +64,8 @@ type AWSIntegration struct {
 	AWSArn string `json:"aws_arn"`
 }
 
+type ListAWSResponse []*AWSIntegration
+
 type CreateAWSRequest struct {
 	AWSRegion          string `json:"aws_region"`
 	AWSClusterID       string `json:"aws_cluster_id"`
@@ -81,6 +89,8 @@ type OverwriteAWSResponse struct {
 }
 
 type GCPIntegration struct {
+	CreatedAt time.Time `json:"created_at"`
+
 	ID uint `json:"id"`
 
 	// The id of the user that linked this auth mechanism
@@ -96,6 +106,8 @@ type GCPIntegration struct {
 	GCPUserEmail string `json:"gcp-user-email"`
 }
 
+type ListGCPResponse []*GCPIntegration
+
 type CreateGCPRequest struct {
 	GCPKeyData   string `json:"gcp_key_data" form:"required"`
 	GCPProjectID string `json:"gcp_project_id"`

+ 1 - 0
internal/models/integrations/aws.go

@@ -49,6 +49,7 @@ type AWSIntegration struct {
 
 func (a *AWSIntegration) ToAWSIntegrationType() *types.AWSIntegration {
 	return &types.AWSIntegration{
+		CreatedAt: a.CreatedAt,
 		ID:        a.ID,
 		UserID:    a.UserID,
 		ProjectID: a.ProjectID,

+ 1 - 0
internal/models/integrations/gcp.go

@@ -41,6 +41,7 @@ type GCPIntegration struct {
 
 func (g *GCPIntegration) ToGCPIntegrationType() *types.GCPIntegration {
 	return &types.GCPIntegration{
+		CreatedAt:    g.CreatedAt,
 		ID:           g.ID,
 		UserID:       g.UserID,
 		ProjectID:    g.ProjectID,

+ 1 - 0
internal/models/integrations/oauth.go

@@ -56,6 +56,7 @@ type GithubAppOAuthIntegration struct {
 // ToOAuthIntegrationType generates an external OAuthIntegration to be shared over REST
 func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
 	return &types.OAuthIntegration{
+		CreatedAt: o.CreatedAt,
 		ID:        o.ID,
 		Client:    o.Client,
 		UserID:    o.UserID,