add openapi defs

This commit is contained in:
Vistaar Juneja 2023-08-22 13:34:49 +01:00
parent 2870aaaabc
commit 887670bd7d
12 changed files with 388 additions and 7 deletions

View File

@ -104,7 +104,9 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
logStream := livelog.ProvideLogStream(config)
logsController := logs2.ProvideController(db, authorizer, executionStore, pipelineStore, stageStore, stepStore, logStore, logStream, spaceStore)
secretStore := database.ProvideSecretStore(db)
spaceController := space.ProvideController(db, provider, pathUID, authorizer, pathStore, pipelineStore, secretStore, spaceStore, repoStore, principalStore, repoController, membershipStore)
connectorStore := database.ProvideConnectorStore(db)
templateStore := database.ProvideTemplateStore(db)
spaceController := space.ProvideController(db, provider, pathUID, authorizer, pathStore, pipelineStore, secretStore, connectorStore, templateStore, spaceStore, repoStore, principalStore, repoController, membershipStore)
pipelineController := pipeline.ProvideController(db, pathUID, pathStore, repoStore, authorizer, pipelineStore, spaceStore)
encrypter, err := encrypt.ProvideEncrypter(config)
if err != nil {
@ -113,9 +115,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
secretController := secret.ProvideController(db, pathUID, pathStore, encrypter, secretStore, authorizer, spaceStore)
triggerStore := database.ProvideTriggerStore(db)
triggerController := trigger.ProvideController(db, authorizer, triggerStore, pipelineStore, spaceStore)
connectorStore := database.ProvideConnectorStore(db)
connectorController := connector.ProvideController(db, pathUID, connectorStore, authorizer, spaceStore)
templateStore := database.ProvideTemplateStore(db)
templateController := template.ProvideController(db, pathUID, pathStore, templateStore, authorizer, spaceStore)
pullReqStore := database.ProvidePullReqStore(db, principalInfoCache)
pullReqActivityStore := database.ProvidePullReqActivityStore(db, principalInfoCache)

View File

@ -22,6 +22,8 @@ type Controller struct {
pathStore store.PathStore
pipelineStore store.PipelineStore
secretStore store.SecretStore
connectorStore store.ConnectorStore
templateStore store.TemplateStore
spaceStore store.SpaceStore
repoStore store.RepoStore
principalStore store.PrincipalStore
@ -32,8 +34,9 @@ type Controller struct {
func NewController(db *sqlx.DB, urlProvider *url.Provider,
uidCheck check.PathUID, authorizer authz.Authorizer,
pathStore store.PathStore, pipelineStore store.PipelineStore, secretStore store.SecretStore,
spaceStore store.SpaceStore, repoStore store.RepoStore, principalStore store.PrincipalStore,
repoCtrl *repo.Controller, membershipStore store.MembershipStore,
connectorStore store.ConnectorStore, templateStore store.TemplateStore, spaceStore store.SpaceStore,
repoStore store.RepoStore, principalStore store.PrincipalStore, repoCtrl *repo.Controller,
membershipStore store.MembershipStore,
) *Controller {
return &Controller{
db: db,
@ -43,6 +46,8 @@ func NewController(db *sqlx.DB, urlProvider *url.Provider,
pathStore: pathStore,
pipelineStore: pipelineStore,
secretStore: secretStore,
connectorStore: connectorStore,
templateStore: templateStore,
spaceStore: spaceStore,
repoStore: repoStore,
principalStore: principalStore,

View File

@ -0,0 +1,54 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"context"
"fmt"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
// ListSecrets lists the connectors in a space.
func (c *Controller) ListConnectors(
ctx context.Context,
session *auth.Session,
spaceRef string,
filter types.ListQueryFilter,
) ([]*types.Connector, int64, error) {
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
if err != nil {
return nil, 0, fmt.Errorf("failed to find parent space: %w", err)
}
err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionSecretView, false)
if err != nil {
return nil, 0, fmt.Errorf("could not authorize: %w", err)
}
var count int64
var connectors []*types.Connector
err = dbtx.New(c.db).WithTx(ctx, func(ctx context.Context) (err error) {
count, err = c.connectorStore.Count(ctx, space.ID, filter)
if err != nil {
return fmt.Errorf("failed to count child executions: %w", err)
}
connectors, err = c.connectorStore.List(ctx, space.ID, filter)
if err != nil {
return fmt.Errorf("failed to list child executions: %w", err)
}
return
}, dbtx.TxDefaultReadOnly)
if err != nil {
return connectors, count, fmt.Errorf("failed to list connectors: %w", err)
}
return connectors, count, nil
}

View File

@ -0,0 +1,54 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"context"
"fmt"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
// ListTemplates lists the templates in a space.
func (c *Controller) ListTemplates(
ctx context.Context,
session *auth.Session,
spaceRef string,
filter types.ListQueryFilter,
) ([]*types.Template, int64, error) {
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
if err != nil {
return nil, 0, fmt.Errorf("failed to find parent space: %w", err)
}
err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionTemplateView, false)
if err != nil {
return nil, 0, fmt.Errorf("could not authorize: %w", err)
}
var count int64
var templates []*types.Template
err = dbtx.New(c.db).WithTx(ctx, func(ctx context.Context) (err error) {
count, err = c.templateStore.Count(ctx, space.ID, filter)
if err != nil {
return fmt.Errorf("failed to count child executions: %w", err)
}
templates, err = c.templateStore.List(ctx, space.ID, filter)
if err != nil {
return fmt.Errorf("failed to list child executions: %w", err)
}
return
}, dbtx.TxDefaultReadOnly)
if err != nil {
return templates, count, fmt.Errorf("failed to list templates: %w", err)
}
return templates, count, nil
}

View File

@ -22,10 +22,11 @@ var WireSet = wire.NewSet(
func ProvideController(db *sqlx.DB, urlProvider *url.Provider, uidCheck check.PathUID, authorizer authz.Authorizer,
pathStore store.PathStore, pipelineStore store.PipelineStore, secretStore store.SecretStore,
connectorStore store.ConnectorStore, templateStore store.TemplateStore,
spaceStore store.SpaceStore, repoStore store.RepoStore, principalStore store.PrincipalStore,
repoCtrl *repo.Controller, membershipStore store.MembershipStore,
) *Controller {
return NewController(db, urlProvider, uidCheck, authorizer,
pathStore, pipelineStore, secretStore, spaceStore, repoStore,
principalStore, repoCtrl, membershipStore)
pathStore, pipelineStore, secretStore, connectorStore, templateStore,
spaceStore, repoStore, principalStore, repoCtrl, membershipStore)
}

View File

@ -0,0 +1,35 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"net/http"
"github.com/harness/gitness/internal/api/controller/space"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
)
func HandleListConnectors(spaceCtrl *space.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
spaceRef, err := request.GetSpaceRefFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
filter := request.ParseListQueryFilterFromRequest(r)
ret, totalCount, err := spaceCtrl.ListSecrets(ctx, session, spaceRef, filter)
if err != nil {
render.TranslatedUserError(w, err)
return
}
render.Pagination(r, w, filter.Page, filter.Size, int(totalCount))
render.JSON(w, http.StatusOK, ret)
}
}

View File

@ -0,0 +1,35 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package space
import (
"net/http"
"github.com/harness/gitness/internal/api/controller/space"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
)
func HandleListTemplates(spaceCtrl *space.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
spaceRef, err := request.GetSpaceRefFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
filter := request.ParseListQueryFilterFromRequest(r)
ret, totalCount, err := spaceCtrl.ListTemplates(ctx, session, spaceRef, filter)
if err != nil {
render.TranslatedUserError(w, err)
return
}
render.Pagination(r, w, filter.Page, filter.Size, int(totalCount))
render.JSON(w, http.StatusOK, ret)
}
}

View File

@ -0,0 +1,79 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package openapi
import (
"net/http"
"github.com/harness/gitness/internal/api/controller/connector"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/types"
"github.com/swaggest/openapi-go/openapi3"
)
type createConnectorRequest struct {
connector.CreateInput
}
type connectorRequest struct {
Ref string `path:"connector_ref"`
}
type getConnectorRequest struct {
connectorRequest
}
type updateConnectorRequest struct {
connectorRequest
connector.UpdateInput
}
func connectorOperations(reflector *openapi3.Reflector) {
opCreate := openapi3.Operation{}
opCreate.WithTags("connector")
opCreate.WithMapOfAnything(map[string]interface{}{"operationId": "createConnector"})
_ = reflector.SetRequest(&opCreate, new(createConnectorRequest), http.MethodPost)
_ = reflector.SetJSONResponse(&opCreate, new(types.Connector), http.StatusCreated)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodPost, "/connectors", opCreate)
opFind := openapi3.Operation{}
opFind.WithTags("connector")
opFind.WithMapOfAnything(map[string]interface{}{"operationId": "findConnector"})
_ = reflector.SetRequest(&opFind, new(getConnectorRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opFind, new(types.Connector), http.StatusOK)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/connectors/{connector_ref}", opFind)
opDelete := openapi3.Operation{}
opDelete.WithTags("connector")
opDelete.WithMapOfAnything(map[string]interface{}{"operationId": "deleteConnector"})
_ = reflector.SetRequest(&opDelete, new(getConnectorRequest), http.MethodDelete)
_ = reflector.SetJSONResponse(&opDelete, nil, http.StatusNoContent)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodDelete, "/connectors/{connector_ref}", opDelete)
opUpdate := openapi3.Operation{}
opUpdate.WithTags("connector")
opUpdate.WithMapOfAnything(map[string]interface{}{"operationId": "updateConnector"})
_ = reflector.SetRequest(&opUpdate, new(updateConnectorRequest), http.MethodPatch)
_ = reflector.SetJSONResponse(&opUpdate, new(types.Connector), http.StatusOK)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodPatch, "/connectors/{connector_ref}", opUpdate)
}

View File

@ -166,6 +166,19 @@ func pipelineOperations(reflector *openapi3.Reflector) {
_ = reflector.Spec.AddOperation(http.MethodGet,
"/pipelines/{pipeline_ref}/executions", executionList)
triggerList := openapi3.Operation{}
triggerList.WithTags("pipeline")
triggerList.WithMapOfAnything(map[string]interface{}{"operationId": "listTriggers"})
triggerList.WithParameters(queryParameterQueryRepo, queryParameterPage, queryParameterLimit)
_ = reflector.SetRequest(&triggerList, new(pipelineRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&triggerList, []types.Trigger{}, http.StatusOK)
_ = reflector.SetJSONResponse(&triggerList, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&triggerList, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&triggerList, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&triggerList, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet,
"/pipelines/{pipeline_ref}/triggers", triggerList)
logView := openapi3.Operation{}
logView.WithTags("pipeline")
logView.WithMapOfAnything(map[string]interface{}{"operationId": "viewLogs"})

View File

@ -242,6 +242,30 @@ func spaceOperations(reflector *openapi3.Reflector) {
_ = reflector.SetJSONResponse(&opPipelines, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{space_ref}/pipelines", opPipelines)
opTemplates := openapi3.Operation{}
opTemplates.WithTags("space")
opTemplates.WithMapOfAnything(map[string]interface{}{"operationId": "listTemplates"})
opTemplates.WithParameters(queryParameterQueryRepo, queryParameterPage, queryParameterLimit)
_ = reflector.SetRequest(&opTemplates, new(spaceRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opTemplates, []types.Template{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opTemplates, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opTemplates, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opTemplates, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opTemplates, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{space_ref}/templates", opTemplates)
opConnectors := openapi3.Operation{}
opConnectors.WithTags("space")
opConnectors.WithMapOfAnything(map[string]interface{}{"operationId": "listConnectors"})
opConnectors.WithParameters(queryParameterQueryRepo, queryParameterPage, queryParameterLimit)
_ = reflector.SetRequest(&opConnectors, new(spaceRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opConnectors, []types.Connector{}, http.StatusOK)
_ = reflector.SetJSONResponse(&opConnectors, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opConnectors, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opConnectors, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opConnectors, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/spaces/{space_ref}/connectors", opConnectors)
opSecrets := openapi3.Operation{}
opSecrets.WithTags("space")
opSecrets.WithMapOfAnything(map[string]interface{}{"operationId": "listSecrets"})

View File

@ -0,0 +1,79 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package openapi
import (
"net/http"
"github.com/harness/gitness/internal/api/controller/template"
"github.com/harness/gitness/internal/api/usererror"
"github.com/harness/gitness/types"
"github.com/swaggest/openapi-go/openapi3"
)
type createTemplateRequest struct {
template.CreateInput
}
type templateRequest struct {
Ref string `path:"template_ref"`
}
type getTemplateRequest struct {
templateRequest
}
type updateTemplateRequest struct {
templateRequest
template.UpdateInput
}
func templateOperations(reflector *openapi3.Reflector) {
opCreate := openapi3.Operation{}
opCreate.WithTags("template")
opCreate.WithMapOfAnything(map[string]interface{}{"operationId": "createTemplate"})
_ = reflector.SetRequest(&opCreate, new(createTemplateRequest), http.MethodPost)
_ = reflector.SetJSONResponse(&opCreate, new(types.Template), http.StatusCreated)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opCreate, new(usererror.Error), http.StatusForbidden)
_ = reflector.Spec.AddOperation(http.MethodPost, "/templates", opCreate)
opFind := openapi3.Operation{}
opFind.WithTags("template")
opFind.WithMapOfAnything(map[string]interface{}{"operationId": "findTemplate"})
_ = reflector.SetRequest(&opFind, new(getTemplateRequest), http.MethodGet)
_ = reflector.SetJSONResponse(&opFind, new(types.Template), http.StatusOK)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opFind, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodGet, "/templates/{template_ref}", opFind)
opDelete := openapi3.Operation{}
opDelete.WithTags("template")
opDelete.WithMapOfAnything(map[string]interface{}{"operationId": "deleteTemplate"})
_ = reflector.SetRequest(&opDelete, new(getTemplateRequest), http.MethodDelete)
_ = reflector.SetJSONResponse(&opDelete, nil, http.StatusNoContent)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opDelete, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodDelete, "/templates/{template_ref}", opDelete)
opUpdate := openapi3.Operation{}
opUpdate.WithTags("template")
opUpdate.WithMapOfAnything(map[string]interface{}{"operationId": "updateTemplate"})
_ = reflector.SetRequest(&opUpdate, new(updateTemplateRequest), http.MethodPatch)
_ = reflector.SetJSONResponse(&opUpdate, new(types.Template), http.StatusOK)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodPatch, "/templates/{template_ref}", opUpdate)
}

View File

@ -190,6 +190,8 @@ func setupSpaces(r chi.Router, spaceCtrl *space.Controller) {
r.Get("/service-accounts", handlerspace.HandleListServiceAccounts(spaceCtrl))
r.Get("/pipelines", handlerspace.HandleListPipelines(spaceCtrl))
r.Get("/secrets", handlerspace.HandleListSecrets(spaceCtrl))
r.Get("/connectors", handlerspace.HandleListConnectors(spaceCtrl))
r.Get("/templates", handlerspace.HandleListTemplates(spaceCtrl))
// Child collections
r.Route("/paths", func(r chi.Router) {