release.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package v1
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/namespace"
  5. "github.com/porter-dev/porter/api/server/handlers/release"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. // swagger:parameters getRelease updateRelease deleteRelease
  12. type releasePathParams struct {
  13. // The project id
  14. // in: path
  15. // required: true
  16. // minimum: 1
  17. ProjectID uint `json:"project_id"`
  18. // The registry id
  19. // in: path
  20. // required: true
  21. // minimum: 1
  22. RegistryID uint `json:"registry_id"`
  23. // The namespace name
  24. // in: path
  25. // required: true
  26. Namespace string `json:"namespace"`
  27. // The release name
  28. // in: path
  29. // required: true
  30. Name string `json:"name"`
  31. // The release version (`0` for latest version)
  32. // in: path
  33. // required: true
  34. // minimum: 0
  35. Version uint `json:"version"`
  36. }
  37. // swagger:parameters listReleases
  38. type listReleasesRequest struct {
  39. *namespacePathParams
  40. *types.ListReleasesRequest
  41. }
  42. func NewV1ReleaseScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  43. return &router.Registerer{
  44. GetRoutes: GetV1ReleaseScopedRoutes,
  45. Children: children,
  46. }
  47. }
  48. func GetV1ReleaseScopedRoutes(
  49. r chi.Router,
  50. config *config.Config,
  51. basePath *types.Path,
  52. factory shared.APIEndpointFactory,
  53. children ...*router.Registerer,
  54. ) []*router.Route {
  55. routes, projPath := getV1ReleaseRoutes(r, config, basePath, factory)
  56. if len(children) > 0 {
  57. r.Route(projPath.RelativePath, func(r chi.Router) {
  58. for _, child := range children {
  59. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  60. routes = append(routes, childRoutes...)
  61. }
  62. })
  63. }
  64. return routes
  65. }
  66. func getV1ReleaseRoutes(
  67. r chi.Router,
  68. config *config.Config,
  69. basePath *types.Path,
  70. factory shared.APIEndpointFactory,
  71. ) ([]*router.Route, *types.Path) {
  72. relPath := "/releases"
  73. newPath := &types.Path{
  74. Parent: basePath,
  75. RelativePath: relPath,
  76. }
  77. var routes []*router.Route
  78. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> release.NewCreateReleaseHandler
  79. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases createRelease
  80. //
  81. // Creates a new release
  82. //
  83. // ---
  84. // produces:
  85. // - application/json
  86. // summary: Create a new release
  87. // tags:
  88. // - Releases
  89. // parameters:
  90. // - name: project_id
  91. // - name: cluster_id
  92. // - name: namespace
  93. // - in: body
  94. // name: CreateReleaseRequest
  95. // description: The release to create
  96. // schema:
  97. // $ref: '#/definitions/CreateReleaseRequest'
  98. // responses:
  99. // '201':
  100. // description: Successfully created the release
  101. // '403':
  102. // description: Forbidden
  103. createReleaseEndpoint := factory.NewAPIEndpoint(
  104. &types.APIRequestMetadata{
  105. Verb: types.APIVerbCreate,
  106. Method: types.HTTPVerbPost,
  107. Path: &types.Path{
  108. Parent: basePath,
  109. RelativePath: relPath,
  110. },
  111. Scopes: []types.PermissionScope{
  112. types.UserScope,
  113. types.ProjectScope,
  114. types.ClusterScope,
  115. types.NamespaceScope,
  116. },
  117. },
  118. )
  119. createReleaseHandler := release.NewCreateReleaseHandler(
  120. config,
  121. factory.GetDecoderValidator(),
  122. factory.GetResultWriter(),
  123. )
  124. routes = append(routes, &router.Route{
  125. Endpoint: createReleaseEndpoint,
  126. Handler: createReleaseHandler,
  127. Router: r,
  128. })
  129. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} -> release.NewReleaseGetHandler
  130. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} getRelease
  131. //
  132. // Gets a release
  133. //
  134. // ---
  135. // produces:
  136. // - application/json
  137. // summary: Get a release
  138. // tags:
  139. // - Releases
  140. // parameters:
  141. // - name: project_id
  142. // - name: cluster_id
  143. // - name: namespace
  144. // - name: name
  145. // - name: version
  146. // responses:
  147. // '201':
  148. // description: Successfully got the release
  149. // schema:
  150. // $ref: '#/definitions/GetReleaseResponse'
  151. // '403':
  152. // description: Forbidden
  153. getEndpoint := factory.NewAPIEndpoint(
  154. &types.APIRequestMetadata{
  155. Verb: types.APIVerbGet,
  156. Method: types.HTTPVerbGet,
  157. Path: &types.Path{
  158. Parent: basePath,
  159. RelativePath: relPath + "/{name}/{version}",
  160. },
  161. Scopes: []types.PermissionScope{
  162. types.UserScope,
  163. types.ProjectScope,
  164. types.ClusterScope,
  165. types.NamespaceScope,
  166. types.ReleaseScope,
  167. },
  168. },
  169. )
  170. getHandler := release.NewReleaseGetHandler(
  171. config,
  172. factory.GetResultWriter(),
  173. )
  174. routes = append(routes, &router.Route{
  175. Endpoint: getEndpoint,
  176. Handler: getHandler,
  177. Router: r,
  178. })
  179. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  180. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases listReleases
  181. //
  182. // List releases
  183. //
  184. // ---
  185. // produces:
  186. // - application/json
  187. // summary: List releases
  188. // tags:
  189. // - Releases
  190. // responses:
  191. // '201':
  192. // description: Successfully listed releases
  193. // schema:
  194. // $ref: '#/definitions/ListReleasesResponse'
  195. // '403':
  196. // description: Forbidden
  197. listReleasesEndpoint := factory.NewAPIEndpoint(
  198. &types.APIRequestMetadata{
  199. Verb: types.APIVerbGet,
  200. Method: types.HTTPVerbGet,
  201. Path: &types.Path{
  202. Parent: basePath,
  203. RelativePath: relPath,
  204. },
  205. Scopes: []types.PermissionScope{
  206. types.UserScope,
  207. types.ProjectScope,
  208. types.ClusterScope,
  209. types.NamespaceScope,
  210. },
  211. },
  212. )
  213. listReleasesHandler := namespace.NewListReleasesHandler(
  214. config,
  215. factory.GetDecoderValidator(),
  216. factory.GetResultWriter(),
  217. )
  218. routes = append(routes, &router.Route{
  219. Endpoint: listReleasesEndpoint,
  220. Handler: listReleasesHandler,
  221. Router: r,
  222. })
  223. // PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  224. // release.NewUpgradeReleaseHandler
  225. // swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} updateRelease
  226. //
  227. // Updates a release
  228. //
  229. // ---
  230. // produces:
  231. // - application/json
  232. // summary: Update a release
  233. // tags:
  234. // - Releases
  235. // parameters:
  236. // - name: project_id
  237. // - name: cluster_id
  238. // - name: namespace
  239. // - name: name
  240. // - name: version
  241. // - in: body
  242. // name: UpdateReleaseRequest
  243. // description: The release to update
  244. // schema:
  245. // $ref: '#/definitions/UpdateReleaseRequest'
  246. // responses:
  247. // '200':
  248. // description: Successfully updated the release
  249. // '403':
  250. // description: Forbidden
  251. upgradeEndpoint := factory.NewAPIEndpoint(
  252. &types.APIRequestMetadata{
  253. Verb: types.APIVerbUpdate,
  254. Method: types.HTTPVerbPatch,
  255. Path: &types.Path{
  256. Parent: basePath,
  257. RelativePath: relPath + "/{name}/{version}",
  258. },
  259. Scopes: []types.PermissionScope{
  260. types.UserScope,
  261. types.ProjectScope,
  262. types.ClusterScope,
  263. types.NamespaceScope,
  264. types.ReleaseScope,
  265. },
  266. },
  267. )
  268. upgradeHandler := release.NewUpgradeReleaseHandler(
  269. config,
  270. factory.GetDecoderValidator(),
  271. factory.GetResultWriter(),
  272. )
  273. routes = append(routes, &router.Route{
  274. Endpoint: upgradeEndpoint,
  275. Handler: upgradeHandler,
  276. Router: r,
  277. })
  278. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  279. // release.NewDeleteReleaseHandler
  280. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} deleteRelease
  281. //
  282. // Deletes a release
  283. //
  284. // ---
  285. // produces:
  286. // - application/json
  287. // summary: Delete a release
  288. // tags:
  289. // - Releases
  290. // parameters:
  291. // - name: project_id
  292. // - name: cluster_id
  293. // - name: namespace
  294. // - name: name
  295. // - name: version
  296. // responses:
  297. // '200':
  298. // description: Successfully deleted the release
  299. // '403':
  300. // description: Forbidden
  301. deleteEndpoint := factory.NewAPIEndpoint(
  302. &types.APIRequestMetadata{
  303. Verb: types.APIVerbDelete,
  304. Method: types.HTTPVerbDelete,
  305. Path: &types.Path{
  306. Parent: basePath,
  307. RelativePath: relPath,
  308. },
  309. Scopes: []types.PermissionScope{
  310. types.UserScope,
  311. types.ProjectScope,
  312. types.ClusterScope,
  313. types.NamespaceScope,
  314. types.ReleaseScope,
  315. },
  316. },
  317. )
  318. deleteHandler := release.NewDeleteReleaseHandler(
  319. config,
  320. factory.GetDecoderValidator(),
  321. factory.GetResultWriter(),
  322. )
  323. routes = append(routes, &router.Route{
  324. Endpoint: deleteEndpoint,
  325. Handler: deleteHandler,
  326. Router: r,
  327. })
  328. return routes, newPath
  329. }