| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- package v1
- import (
- "fmt"
- "github.com/go-chi/chi/v5"
- "github.com/porter-dev/porter/api/server/handlers/registry"
- v1Registry "github.com/porter-dev/porter/api/server/handlers/v1/registry"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/server/shared/router"
- "github.com/porter-dev/porter/api/types"
- )
- // swagger:parameters getRegistry deleteRegistry createRegistryRepository listRegistryRepositories listRegistryImages
- type registryPathParams struct {
- // The project id
- // in: path
- // required: true
- // minimum: 1
- ProjectID uint `json:"project_id"`
- // The registry id
- // in: path
- // required: true
- // minimum: 1
- RegistryID uint `json:"registry_id"`
- }
- func NewV1RegistryScopedRegisterer(children ...*router.Registerer) *router.Registerer {
- return &router.Registerer{
- GetRoutes: GetV1RegistryScopedRoutes,
- Children: children,
- }
- }
- func GetV1RegistryScopedRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- children ...*router.Registerer,
- ) []*router.Route {
- routes, projPath := getV1RegistryRoutes(r, config, basePath, factory)
- if len(children) > 0 {
- r.Route(projPath.RelativePath, func(r chi.Router) {
- for _, child := range children {
- childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
- routes = append(routes, childRoutes...)
- }
- })
- }
- return routes
- }
- func getV1RegistryRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- ) ([]*router.Route, *types.Path) {
- relPath := "/registries"
- newPath := &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- }
- var routes []*router.Route
- // POST /api/v1/projects/{project_id}/registries -> registry.NewRegistryCreateHandler
- // swagger:operation POST /api/v1/projects/{project_id}/registries createRegistry
- //
- // Connects a new image registry to the project denoted by `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: Connect an image registry
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - in: body
- // name: CreateRegistryRequest
- // description: The registry to connect
- // schema:
- // $ref: '#/definitions/CreateRegistryRequest'
- // responses:
- // '201':
- // description: Successfully connected the registry
- // schema:
- // $ref: '#/definitions/CreateRegistryResponse'
- // '400':
- // description: A malformed or bad request
- // '403':
- // description: Forbidden
- // '404':
- // description: A subresource was not found
- createRegistryEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbCreate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- },
- },
- )
- createRegistryHandler := registry.NewRegistryCreateHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: createRegistryEndpoint,
- Handler: createRegistryHandler,
- Router: r,
- })
- // GET /api/v1/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryGetHandler
- // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id} getRegistry
- //
- // Gets an image registry denoted by `registry_id`. The registry should belong to the
- // project denoted by `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: Get an image registry
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // responses:
- // '200':
- // description: Successfully got the registry
- // schema:
- // $ref: '#/definitions/GetRegistryResponse'
- // '403':
- // description: Forbidden
- getEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/{registry_id}",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- getHandler := registry.NewRegistryGetHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getEndpoint,
- Handler: getHandler,
- Router: r,
- })
- // GET /api/v1/projects/{project_id}/registries -> registry.NewRegistryListHandler
- // swagger:operation GET /api/v1/projects/{project_id}/registries listRegistries
- //
- // Lists all registries connected to the project denoted by `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: List image registries
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // responses:
- // '200':
- // description: Successfully listed registries
- // schema:
- // $ref: '#/definitions/ListRegistriesResponse'
- // '403':
- // description: Forbidden
- listRegistriesEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- },
- },
- )
- listRegistriesHandler := registry.NewRegistryListHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listRegistriesEndpoint,
- Handler: listRegistriesHandler,
- Router: r,
- })
- // DELETE /api/v1/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryDeleteHandler
- // swagger:operation DELETE /api/v1/projects/{project_id}/registries/{registry_id} deleteRegistry
- //
- // Deletes a registry denoted by `registry_id`. The registry should belong to
- // the project denoted by `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: Disconnect image registry
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // responses:
- // '200':
- // description: Successfully disconnected image registry
- // '403':
- // description: Forbidden
- deleteEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbDelete,
- Method: types.HTTPVerbDelete,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/{registry_id}",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- deleteHandler := registry.NewRegistryDeleteHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: deleteEndpoint,
- Handler: deleteHandler,
- Router: r,
- })
- // POST /api/v1/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryCreateRepositoryHandler
- // swagger:operation POST /api/v1/projects/{project_id}/registries/{registry_id}/repositories createRegistryRepository
- //
- // Creates an image repository inside the registry specified by `registry_id`. This method **only** creates repositories for ECR-integrated
- // repositories.
- //
- // ---
- // produces:
- // - application/json
- // summary: Create image repository
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // - in: body
- // name: CreateRepositoryRequest
- // description: The repository to create
- // schema:
- // $ref: '#/definitions/CreateRegistryRepositoryRequest'
- // responses:
- // '201':
- // description: Successfully created the image repository
- // '403':
- // description: Forbidden
- createRepositoryEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbCreate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/{registry_id}/repositories",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- createRepositoryHandler := registry.NewRegistryCreateRepositoryHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: createRepositoryEndpoint,
- Handler: createRepositoryHandler,
- Router: r,
- })
- // GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryListRepositoriesHandler
- // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories listRegistryRepositories
- //
- // Lists image repositories inside the image registry denoted by `registry_id`. The registry
- // should belong to the project denoted by `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: List image repositories
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // responses:
- // '200':
- // description: Successfully listed image repositories
- // schema:
- // $ref: '#/definitions/ListRegistryRepositoriesResponse'
- // '403':
- // description: Forbidden
- listRepositoriesEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/{registry_id}/repositories",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- listRepositoriesHandler := registry.NewRegistryListRepositoriesHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listRepositoriesEndpoint,
- Handler: listRepositoriesHandler,
- Router: r,
- })
- // GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories/* -> registry.NewRegistryListImagesHandler
- // swagger:operation GET /api/v1/projects/{project_id}/registries/{registry_id}/repositories/{repository} listRegistryImages
- //
- // Lists all images in the image repository denoted by the name `repository`. The repository should belong
- // to the registry denoted by `registry_id` which should itself belong to the project denoted by
- // `project_id`.
- //
- // ---
- // produces:
- // - application/json
- // summary: List images
- // tags:
- // - Registries
- // parameters:
- // - name: project_id
- // - name: registry_id
- // - name: repository
- // in: path
- // description: The image repository name. Should be of the form REPOSITORY/IMAGE when using Google Artifact Registry.
- // type: string
- // required: true
- // - name: num
- // in: query
- // description: |
- // The number of images to list.
- // For ECR images, a maximum of 1000 is allowed.
- // type: integer
- // required: false
- // minimum: 1
- // - name: next
- // in: query
- // description: The next page string used for pagination, from a previous request.
- // type: string
- // - name: page
- // in: query
- // description: |
- // The page number used for pagination, possibly from a previous request.
- // (**DigitalOcean only**)
- // type: integer
- // minimum: 1
- // responses:
- // '200':
- // description: Successfully listed images
- // schema:
- // $ref: '#/definitions/V1ListImageResponse'
- // '400':
- // description: A malformed or bad request
- // '403':
- // description: Forbidden
- listImagesEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf(
- "%s/{registry_id}/repositories/%s",
- relPath,
- types.URLParamWildcard,
- ),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- listImagesHandler := v1Registry.NewRegistryListImagesHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listImagesEndpoint,
- Handler: listImagesHandler,
- Router: r,
- })
- return routes, newPath
- }
|