project.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/server/shared/router"
  8. "github.com/porter-dev/porter/api/types"
  9. v1Template "github.com/porter-dev/porter/api/server/handlers/v1/template"
  10. )
  11. // swagger:parameters createRegistry listRegistries listTemplates
  12. type projectPathParams struct {
  13. // The project id
  14. // in: path
  15. // required: true
  16. // minimum: 1
  17. ProjectID uint `json:"project_id"`
  18. }
  19. // swagger:parameters getTemplate getTemplateUpgradeNotes
  20. type getTemplatePathParams struct {
  21. // The project id
  22. // in: path
  23. // required: true
  24. // minimum: 1
  25. ProjectID uint `json:"project_id"`
  26. // The name of the template
  27. // in: path
  28. // required: true
  29. // type: string
  30. Name string `json:"name"`
  31. // The version of the template
  32. // in: path
  33. // required: true
  34. // type: string
  35. Version string `json:"version"`
  36. }
  37. func NewV1ProjectScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  38. return &router.Registerer{
  39. GetRoutes: GetV1ProjectScopedRoutes,
  40. Children: children,
  41. }
  42. }
  43. func GetV1ProjectScopedRoutes(
  44. r chi.Router,
  45. config *config.Config,
  46. basePath *types.Path,
  47. factory shared.APIEndpointFactory,
  48. children ...*router.Registerer,
  49. ) []*router.Route {
  50. routes, projPath := getV1ProjectRoutes(r, config, basePath, factory)
  51. if len(children) > 0 {
  52. r.Route(projPath.RelativePath, func(r chi.Router) {
  53. for _, child := range children {
  54. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  55. routes = append(routes, childRoutes...)
  56. }
  57. })
  58. }
  59. return routes
  60. }
  61. func getV1ProjectRoutes(
  62. r chi.Router,
  63. config *config.Config,
  64. basePath *types.Path,
  65. factory shared.APIEndpointFactory,
  66. ) ([]*router.Route, *types.Path) {
  67. relPath := "/projects/{project_id}"
  68. newPath := &types.Path{
  69. Parent: basePath,
  70. RelativePath: relPath,
  71. }
  72. var routes []*router.Route
  73. // GET /api/v1/projects/{project_id}/templates -> v1Template.NewTemplateListHandler
  74. // swagger:operation GET /api/v1/projects/{project_id}/templates listTemplates
  75. //
  76. // Lists templates for a given `repo_url`.
  77. //
  78. // ---
  79. // produces:
  80. // - application/json
  81. // summary: List templates
  82. // tags:
  83. // - Templates
  84. // parameters:
  85. // - name: project_id
  86. // - name: repo_url
  87. // in: query
  88. // description: |
  89. // The full path (including scheme) of the Helm registry to list templates from.
  90. // type: string
  91. // responses:
  92. // '200':
  93. // description: Successfully listed templates
  94. // schema:
  95. // $ref: '#/definitions/ListTemplatesResponse'
  96. // '400':
  97. // description: A malformed or bad request
  98. // '403':
  99. // description: Forbidden
  100. listTemplatesEndpoint := factory.NewAPIEndpoint(
  101. &types.APIRequestMetadata{
  102. Verb: types.APIVerbList,
  103. Method: types.HTTPVerbGet,
  104. Path: &types.Path{
  105. Parent: basePath,
  106. RelativePath: fmt.Sprintf("%s/templates", relPath),
  107. },
  108. Scopes: []types.PermissionScope{
  109. types.UserScope,
  110. types.ProjectScope,
  111. },
  112. },
  113. )
  114. listTemplatesRequest := v1Template.NewTemplateListHandler(
  115. config,
  116. factory.GetDecoderValidator(),
  117. factory.GetResultWriter(),
  118. )
  119. routes = append(routes, &router.Route{
  120. Endpoint: listTemplatesEndpoint,
  121. Handler: listTemplatesRequest,
  122. Router: r,
  123. })
  124. // GET /api/v1/projects/{project_id}/templates/{name}/versions/{version} -> v1Template.NewTemplateGetHandler
  125. // swagger:operation GET /api/v1/projects/{project_id}/templates/{name}/versions/{version} getTemplate
  126. //
  127. // Retrieves a given template by a `name` and a `version`
  128. //
  129. // ---
  130. // produces:
  131. // - application/json
  132. // summary: Get template
  133. // tags:
  134. // - Templates
  135. // parameters:
  136. // - name: project_id
  137. // - name: name
  138. // - name: version
  139. // - name: repo_url
  140. // in: query
  141. // description: |
  142. // The full path (including scheme) of the Helm registry to list templates from.
  143. // type: string
  144. // responses:
  145. // '200':
  146. // description: Successfully got the template
  147. // schema:
  148. // $ref: '#/definitions/GetTemplateResponse'
  149. // '400':
  150. // description: A malformed or bad request
  151. // '403':
  152. // description: Forbidden
  153. getTemplateEndpoint := factory.NewAPIEndpoint(
  154. &types.APIRequestMetadata{
  155. Verb: types.APIVerbGet,
  156. Method: types.HTTPVerbGet,
  157. Path: &types.Path{
  158. Parent: basePath,
  159. RelativePath: fmt.Sprintf(
  160. "%s/templates/{%s}/versions/{%s}",
  161. relPath,
  162. types.URLParamTemplateName,
  163. types.URLParamTemplateVersion,
  164. ),
  165. },
  166. Scopes: []types.PermissionScope{
  167. types.UserScope,
  168. types.ProjectScope,
  169. },
  170. },
  171. )
  172. getTemplateRequest := v1Template.NewTemplateGetHandler(
  173. config,
  174. factory.GetDecoderValidator(),
  175. factory.GetResultWriter(),
  176. )
  177. routes = append(routes, &router.Route{
  178. Endpoint: getTemplateEndpoint,
  179. Handler: getTemplateRequest,
  180. Router: r,
  181. })
  182. // GET /api/v1/projects/{project_id}/templates/{name}/versions/{version}/upgrade_notes -> v1Template.NewTemplateGetUpgradeNotesHandler
  183. // swagger:operation GET /api/v1/projects/{project_id}/templates/{name}/versions/{version}/upgrade_notes getTemplateUpgradeNotes
  184. //
  185. // Retrieves a given template by a `name` and a `version`
  186. //
  187. // ---
  188. // produces:
  189. // - application/json
  190. // summary: Get template upgrade notes
  191. // tags:
  192. // - Templates
  193. // parameters:
  194. // - name: project_id
  195. // - name: name
  196. // - name: version
  197. // - name: prev_version
  198. // in: query
  199. // description: |
  200. // The previous version of the templates to generate upgrade notes from.
  201. // type: string
  202. // - name: repo_url
  203. // in: query
  204. // description: |
  205. // The full path (including scheme) of the Helm registry to list templates from.
  206. // type: string
  207. // responses:
  208. // '200':
  209. // description: Successfully got the upgrade notes
  210. // schema:
  211. // $ref: '#/definitions/GetTemplateUpgradeNotesResponse'
  212. // '400':
  213. // description: A malformed or bad request
  214. // '403':
  215. // description: Forbidden
  216. getTemplateUpgradeNotesEndpoint := factory.NewAPIEndpoint(
  217. &types.APIRequestMetadata{
  218. Verb: types.APIVerbGet,
  219. Method: types.HTTPVerbGet,
  220. Path: &types.Path{
  221. Parent: basePath,
  222. RelativePath: fmt.Sprintf(
  223. "/templates/{%s}/versions/{%s}/upgrade_notes",
  224. types.URLParamTemplateName,
  225. types.URLParamTemplateVersion,
  226. ),
  227. },
  228. Scopes: []types.PermissionScope{
  229. types.UserScope,
  230. types.ProjectScope,
  231. },
  232. },
  233. )
  234. getTemplateUpgradeNotesRequest := v1Template.NewTemplateGetUpgradeNotesHandler(
  235. config,
  236. factory.GetDecoderValidator(),
  237. factory.GetResultWriter(),
  238. )
  239. routes = append(routes, &router.Route{
  240. Endpoint: getTemplateUpgradeNotesEndpoint,
  241. Handler: getTemplateUpgradeNotesRequest,
  242. Router: r,
  243. })
  244. return routes, newPath
  245. }