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

add integrations endpoints

Alexander Belanger 4 жил өмнө
parent
commit
e10d909d95

+ 27 - 0
api/server/handlers/metadata/list_cluster_ints.go

@@ -0,0 +1,27 @@
+package metadata
+
+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/config"
+	"github.com/porter-dev/porter/api/types"
+)
+
+type ListClusterIntegrationsHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListClusterIntegrationsHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListClusterIntegrationsHandler {
+	return &ListClusterIntegrationsHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (v *ListClusterIntegrationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	v.WriteResult(w, r, types.PorterClusterIntegrations)
+}

+ 27 - 0
api/server/handlers/metadata/list_helm_repo_ints.go

@@ -0,0 +1,27 @@
+package metadata
+
+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/config"
+	"github.com/porter-dev/porter/api/types"
+)
+
+type ListHelmRepoIntegrationsHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListHelmRepoIntegrationsHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListHelmRepoIntegrationsHandler {
+	return &ListHelmRepoIntegrationsHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (v *ListHelmRepoIntegrationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	v.WriteResult(w, r, types.PorterHelmRepoIntegrations)
+}

+ 27 - 0
api/server/handlers/metadata/list_registry_ints.go

@@ -0,0 +1,27 @@
+package metadata
+
+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/config"
+	"github.com/porter-dev/porter/api/types"
+)
+
+type ListRegistryIntegrationsHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewListRegistryIntegrationsHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListRegistryIntegrationsHandler {
+	return &ListRegistryIntegrationsHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (v *ListRegistryIntegrationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	v.WriteResult(w, r, types.PorterRegistryIntegrations)
+}

+ 69 - 0
api/server/router/base.go

@@ -50,6 +50,75 @@ func GetBaseRoutes(
 		Router:   r,
 	})
 
+	// GET /api/integrations/cluster -> metadata.NewListClusterIntegrationsHandler
+	listClusterIntsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/integrations/cluster",
+			},
+		},
+	)
+
+	listClusterIntsHandler := metadata.NewListClusterIntegrationsHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listClusterIntsEndpoint,
+		Handler:  listClusterIntsHandler,
+		Router:   r,
+	})
+
+	// GET /api/integrations/registry -> metadata.NewListRegistryIntegrationsHandler
+	listRegistryIntsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/integrations/registry",
+			},
+		},
+	)
+
+	listRegistryIntsHandler := metadata.NewListRegistryIntegrationsHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listRegistryIntsEndpoint,
+		Handler:  listRegistryIntsHandler,
+		Router:   r,
+	})
+
+	// GET /api/integrations/helm -> metadata.NewListHelmRepoIntegrationsHandler
+	listHelmRepoIntsEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/integrations/helm",
+			},
+		},
+	)
+
+	listHelmRepoIntsHandler := metadata.NewListHelmRepoIntegrationsHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listHelmRepoIntsEndpoint,
+		Handler:  listHelmRepoIntsHandler,
+		Router:   r,
+	})
+
 	// POST /api/users -> user.NewUserCreateHandler
 	createUserEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{

+ 70 - 0
api/types/integrations.go

@@ -0,0 +1,70 @@
+package types
+
+// PorterIntegration is a supported integration service, specifying an auth
+// mechanism and the category of integration. A single service can have multiple
+// auth mechanisms. For example, a GKE integration can have both an "oauth" mechanism
+// and a "gcp" mechanism:
+//
+// PorterIntegration{
+// 	AuthMechanism: "oauth",
+// 	Category: "cluster",
+// 	Service: GKE,
+// }
+//
+// PorterIntegration{
+// 	AuthMechanism: "gcp",
+// 	Category: "cluster",
+// 	Service: GKE,
+// }
+type PorterIntegration struct {
+	AuthMechanism string `json:"auth_mechanism"`
+	Category      string `json:"category"`
+	Service       string `json:"service"`
+}
+
+// PorterClusterIntegrations are the supported cluster integrations
+var PorterClusterIntegrations = []PorterIntegration{
+	{
+		AuthMechanism: "gcp",
+		Category:      "cluster",
+		Service:       string(GKE),
+	},
+	{
+		AuthMechanism: "aws",
+		Category:      "cluster",
+		Service:       string(EKS),
+	},
+	{
+		AuthMechanism: "kube",
+		Category:      "cluster",
+		Service:       string(Kube),
+	},
+}
+
+// PorterRegistryIntegrations are the supported registry integrations
+var PorterRegistryIntegrations = []PorterIntegration{
+	{
+		AuthMechanism: "gcp",
+		Category:      "registry",
+		Service:       string(GCR),
+	},
+	{
+		AuthMechanism: "aws",
+		Category:      "registry",
+		Service:       string(ECR),
+	},
+	{
+		AuthMechanism: "basic",
+		Category:      "registry",
+		Service:       string(DockerHub),
+	},
+}
+
+// PorterHelmRepoIntegrations are the supported helm repo integrations
+var PorterHelmRepoIntegrations = []PorterIntegration{
+	{
+		AuthMechanism: "basic",
+		Category:      "helm",
+		Service:       "helmrepo",
+	},
+}

+ 1 - 1
dashboard/src/main/home/modals/IntegrationsModal.tsx

@@ -45,7 +45,7 @@ export default class IntegrationsModal extends Component<PropsType, StateType> {
           integrationList[integration.service] &&
           integrationList[integration.service].icon;
         let disabled =
-          integration.service === "kube" || integration.service === "docker";
+          integration.service === "kube" || integration.service === "dockerhub";
         return (
           <IntegrationOption
             key={i}

+ 1 - 1
dashboard/src/shared/common.tsx

@@ -49,7 +49,7 @@ export const integrationList: any = {
       "https://uxwing.com/wp-content/themes/uxwing/download/10-brands-and-social-media/kubernetes.png",
     label: "Upload Kubeconfig",
   },
-  docker: {
+  dockerhub: {
     icon:
       "https://cdn4.iconfinder.com/data/icons/logos-and-brands/512/97_Docker_logo_logos-512.png",
     label: "Docker Hub",

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

@@ -6,15 +6,15 @@
 | <li>- [x] `GET /api/cli/login/exchange`                                                                                     | AB          |                 | yes         |                  |
 | <li>- [x] `GET /api/email/verify/finalize`                                                                                  | AB          |                 |             |                  |
 | <li>- [x] `POST /api/email/verify/initiate`                                                                                 | AB          |                 |             | yes              |
-| <li>- [ ] `GET /api/integrations/cluster`                                                                                   |             |                 |             |                  |
+| <li>- [X] `GET /api/integrations/cluster`                                                                                   | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/github-app/access`                                                                         | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/github-app/authorize`                                                                      | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/github-app/install`                                                                        | AB          |                 |             |                  |
 | <li>- [X] `GET /api/integrations/github-app/oauth`                                                                          | AB          |                 |             |                  |
 | <li>- [X] `POST /api/integrations/github-app/webhook`                                                                       | AB          |                 |             |                  |
-| <li>- [ ] `GET /api/integrations/helm`                                                                                      |             |                 |             |                  |
-| <li>- [ ] `GET /api/integrations/registry`                                                                                  |             |                 |             |                  |
-| <li>- [ ] `GET /api/integrations/repo`                                                                                      |             |                 |             |                  |
+| <li>- [X] `GET /api/integrations/helm`                                                                                      | AB          |                 |             |                  |
+| <li>- [X] `GET /api/integrations/registry`                                                                                  | AB          |                 |             |                  |
+| <li>- [X] `GET /api/integrations/repo`                                                                                      | N/A         |                 |             |                  |
 | <li>- [ ] `GET /api/livez`                                                                                                  |             |                 |             |                  |
 | <li>- [x] `POST /api/login`                                                                                                 | AB          |                 |             | yes              |
 | <li>- [x] `POST /api/logout`                                                                                                | AB          |                 |             | yes              |

+ 1 - 1
internal/kubernetes/agent.go

@@ -1169,7 +1169,7 @@ func (a *Agent) CreateImagePullSecrets(
 			return nil, err
 		}
 
-		secretName := fmt.Sprintf("porter-%s-%d", val.Externalize().Service, val.ID)
+		secretName := fmt.Sprintf("porter-%s-%d", val.ToRegistryType().Service, val.ID)
 
 		secret, err := a.Clientset.CoreV1().Secrets(namespace).Get(
 			context.TODO(),

+ 19 - 24
internal/models/gitrepo.go

@@ -2,7 +2,6 @@ package models
 
 import (
 	"github.com/porter-dev/porter/api/types"
-	"github.com/porter-dev/porter/internal/models/integrations"
 	"gorm.io/gorm"
 )
 
@@ -21,29 +20,25 @@ type GitRepo struct {
 	OAuthIntegrationID uint
 }
 
-// GitRepoExternal is a repository to be shared over REST
-type GitRepoExternal struct {
-	ID uint `json:"id"`
-
-	// The project that this integration belongs to
-	ProjectID uint `json:"project_id"`
-
-	// The username/organization that this repo integration is linked to
-	RepoEntity string `json:"repo_entity"`
-
-	// The integration service for this git repo
-	Service integrations.IntegrationService `json:"service"`
-}
-
-// Externalize generates an external Repo to be shared over REST
-func (r *GitRepo) Externalize() *GitRepoExternal {
-	return &GitRepoExternal{
-		ID:         r.Model.ID,
-		ProjectID:  r.ProjectID,
-		RepoEntity: r.RepoEntity,
-		Service:    integrations.Github,
-	}
-}
+// // GitRepoExternal is a repository to be shared over REST
+// type GitRepoExternal struct {
+// 	ID uint `json:"id"`
+
+// 	// The project that this integration belongs to
+// 	ProjectID uint `json:"project_id"`
+
+// 	// The username/organization that this repo integration is linked to
+// 	RepoEntity string `json:"repo_entity"`
+// }
+
+// // Externalize generates an external Repo to be shared over REST
+// func (r *GitRepo) Externalize() *GitRepoExternal {
+// 	return &GitRepoExternal{
+// 		ID:         r.Model.ID,
+// 		ProjectID:  r.ProjectID,
+// 		RepoEntity: r.RepoEntity,
+// 	}
+// }
 
 // GitActionConfig is a configuration for release's CI integration via
 // Github Actions

+ 0 - 24
internal/models/helm_repo.go

@@ -44,30 +44,6 @@ type HelmRepoExternal struct {
 	Name string `json:"name"`
 
 	RepoURL string `json:"repo_name"`
-
-	// The integration service for this registry
-	Service integrations.IntegrationService `json:"service"`
-}
-
-// Externalize generates an external Registry to be shared over REST
-func (hr *HelmRepo) Externalize() *HelmRepoExternal {
-	var serv integrations.IntegrationService
-
-	if hr.BasicAuthIntegrationID != 0 {
-		serv = integrations.HelmRepo
-	} else if hr.AWSIntegrationID != 0 {
-		serv = integrations.S3
-	} else if hr.GCPIntegrationID != 0 {
-		serv = integrations.GCS
-	}
-
-	return &HelmRepoExternal{
-		ID:        hr.ID,
-		ProjectID: hr.ProjectID,
-		Name:      hr.Name,
-		RepoURL:   hr.RepoURL,
-		Service:   serv,
-	}
 }
 
 // ToHelmRepoType generates an external HelmRepo to be shared over REST

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

@@ -56,20 +56,6 @@ func (a *AWSIntegration) ToAWSIntegrationType() *types.AWSIntegration {
 	}
 }
 
-// ToProjectIntegration converts an aws integration to a project integration
-func (a *AWSIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            a.ID,
-		ProjectID:     a.ProjectID,
-		AuthMechanism: "aws",
-		Category:      category,
-		Service:       service,
-	}
-}
-
 // GetSession retrieves an AWS session to use based on the access key and secret
 // access key
 func (a *AWSIntegration) GetSession() (*session.Session, error) {

+ 0 - 14
internal/models/integrations/basic.go

@@ -32,17 +32,3 @@ func (b *BasicIntegration) ToBasicIntegrationType() *types.BasicIntegration {
 		ProjectID: b.ProjectID,
 	}
 }
-
-// ToProjectIntegration converts an oauth integration to a project integration
-func (b *BasicIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            b.ID,
-		ProjectID:     b.ProjectID,
-		AuthMechanism: "basic",
-		Category:      category,
-		Service:       service,
-	}
-}

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

@@ -3,6 +3,7 @@ package integrations
 import (
 	"context"
 	"encoding/json"
+
 	"github.com/porter-dev/porter/api/types"
 
 	"golang.org/x/oauth2"
@@ -48,20 +49,6 @@ func (g *GCPIntegration) ToGCPIntegrationType() *types.GCPIntegration {
 	}
 }
 
-// ToProjectIntegration converts a gcp integration to a project integration
-func (g *GCPIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            g.ID,
-		ProjectID:     g.ProjectID,
-		AuthMechanism: "gcp",
-		Category:      category,
-		Service:       service,
-	}
-}
-
 // GetBearerToken retrieves a bearer token for a GCP account
 func (g *GCPIntegration) GetBearerToken(
 	getTokenCache GetTokenCacheFunc,

+ 0 - 121
internal/models/integrations/integration.go

@@ -1,121 +0,0 @@
-package integrations
-
-// IntegrationService is the name of a third-party service
-type IntegrationService string
-
-// The list of supported third-party services
-const (
-	GKE       IntegrationService = "gke"
-	DOKS      IntegrationService = "doks"
-	GCS       IntegrationService = "gcs"
-	S3        IntegrationService = "s3"
-	HelmRepo  IntegrationService = "helm"
-	EKS       IntegrationService = "eks"
-	Kube      IntegrationService = "kube"
-	GCR       IntegrationService = "gcr"
-	ECR       IntegrationService = "ecr"
-	DOCR      IntegrationService = "docr"
-	Github    IntegrationService = "github"
-	DockerHub IntegrationService = "dockerhub"
-	Docker    IntegrationService = "docker"
-)
-
-// PorterIntegration is a supported integration service, specifying an auth
-// mechanism and the category of integration. A single service can have multiple
-// auth mechanisms. For example, a GKE integration can have both an "oauth" mechanism
-// and a "gcp" mechanism:
-//
-// PorterIntegration{
-// 	AuthMechanism: "oauth",
-// 	Category: "cluster",
-// 	Service: GKE,
-// }
-//
-// PorterIntegration{
-// 	AuthMechanism: "gcp",
-// 	Category: "cluster",
-// 	Service: GKE,
-// }
-type PorterIntegration struct {
-	AuthMechanism string             `json:"auth_mechanism"`
-	Category      string             `json:"category"`
-	Service       IntegrationService `json:"service"`
-}
-
-// PorterClusterIntegrations are the supported cluster integrations
-var PorterClusterIntegrations = []PorterIntegration{
-	PorterIntegration{
-		AuthMechanism: "gcp",
-		Category:      "cluster",
-		Service:       GKE,
-	},
-	PorterIntegration{
-		AuthMechanism: "aws",
-		Category:      "cluster",
-		Service:       EKS,
-	},
-	PorterIntegration{
-		AuthMechanism: "kube",
-		Category:      "cluster",
-		Service:       Kube,
-	},
-}
-
-// PorterRegistryIntegrations are the supported registry integrations
-var PorterRegistryIntegrations = []PorterIntegration{
-	PorterIntegration{
-		AuthMechanism: "gcp",
-		Category:      "registry",
-		Service:       GCR,
-	},
-	PorterIntegration{
-		AuthMechanism: "aws",
-		Category:      "registry",
-		Service:       ECR,
-	},
-	PorterIntegration{
-		AuthMechanism: "oauth",
-		Category:      "registry",
-		Service:       Docker,
-	},
-}
-
-// PorterHelmRepoIntegrations are the supported helm repo integrations
-var PorterHelmRepoIntegrations = []PorterIntegration{
-	PorterIntegration{
-		AuthMechanism: "basic",
-		Category:      "helm",
-		Service:       HelmRepo,
-	},
-	PorterIntegration{
-		AuthMechanism: "gcp",
-		Category:      "helm",
-		Service:       GCS,
-	},
-	PorterIntegration{
-		AuthMechanism: "aws",
-		Category:      "helm",
-		Service:       S3,
-	},
-}
-
-// PorterGitRepoIntegrations are the supported git repo integrations
-var PorterGitRepoIntegrations = []PorterIntegration{
-	PorterIntegration{
-		AuthMechanism: "oauth",
-		Category:      "repo",
-		Service:       Github,
-	},
-}
-
-// ProjectIntegration is the top-level integration object for various integrations.
-// Although the integrations are stored in the DB by auth mechanism, the integrations
-// are cast to a ProjectIntegration for consolidation before passing on to the client.
-type ProjectIntegration struct {
-	ID        uint `json:"id"`
-	ProjectID uint `json:"project_id"`
-
-	AuthMechanism string             `json:"auth_mechanism"`
-	Category      string             `json:"category"`
-	Service       IntegrationService `json:"service"`
-}

+ 0 - 14
internal/models/integrations/kube.go

@@ -69,17 +69,3 @@ func (k *KubeIntegration) Externalize() *KubeIntegrationExternal {
 		ProjectID: k.ProjectID,
 	}
 }
-
-// ToProjectIntegration converts a gcp integration to a project integration
-func (k *KubeIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            k.ID,
-		ProjectID:     k.ProjectID,
-		AuthMechanism: "kube",
-		Category:      category,
-		Service:       service,
-	}
-}

+ 2 - 15
internal/models/integrations/oauth.go

@@ -1,9 +1,10 @@
 package integrations
 
 import (
+	"time"
+
 	"github.com/porter-dev/porter/api/types"
 	"gorm.io/gorm"
-	"time"
 )
 
 // SharedOAuthModel stores general fields needed for OAuth Integration
@@ -61,17 +62,3 @@ func (o *OAuthIntegration) ToOAuthIntegrationType() *types.OAuthIntegration {
 		ProjectID: o.ProjectID,
 	}
 }
-
-// ToProjectIntegration converts an oauth integration to a project integration
-func (o *OAuthIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            o.ID,
-		ProjectID:     o.ProjectID,
-		AuthMechanism: "oauth",
-		Category:      category,
-		Service:       service,
-	}
-}

+ 0 - 14
internal/models/integrations/oidc.go

@@ -74,17 +74,3 @@ func (o *OIDCIntegration) Externalize() *OIDCIntegrationExternal {
 		ProjectID: o.ProjectID,
 	}
 }
-
-// ToProjectIntegration converts a gcp integration to a project integration
-func (o *OIDCIntegration) ToProjectIntegration(
-	category string,
-	service IntegrationService,
-) *ProjectIntegration {
-	return &ProjectIntegration{
-		ID:            o.ID,
-		ProjectID:     o.ProjectID,
-		AuthMechanism: "oidc",
-		Category:      category,
-		Service:       service,
-	}
-}

+ 0 - 34
internal/models/registry.go

@@ -51,44 +51,10 @@ type RegistryExternal struct {
 	// URL of the registry
 	URL string `json:"url"`
 
-	// The integration service for this registry
-	Service integrations.IntegrationService `json:"service"`
-
 	// The infra id, if registry was provisioned with Porter
 	InfraID uint `json:"infra_id"`
 }
 
-// Externalize generates an external Registry to be shared over REST
-func (r *Registry) Externalize() *RegistryExternal {
-	var serv integrations.IntegrationService
-
-	if r.AWSIntegrationID != 0 {
-		serv = integrations.ECR
-	} else if r.GCPIntegrationID != 0 {
-		serv = integrations.GCR
-	} else if r.DOIntegrationID != 0 {
-		serv = integrations.DOCR
-	} else if strings.Contains(r.URL, "index.docker.io") {
-		serv = integrations.DockerHub
-	}
-
-	uri := r.URL
-
-	// remove the protocol
-	if splStr := strings.Split(uri, "://"); len(splStr) > 1 {
-		uri = splStr[1]
-	}
-
-	return &RegistryExternal{
-		ID:        r.ID,
-		ProjectID: r.ProjectID,
-		Name:      r.Name,
-		URL:       uri,
-		Service:   serv,
-		InfraID:   r.InfraID,
-	}
-}
-
 func (r *Registry) ToRegistryType() *types.Registry {
 	var serv types.RegistryService