release.go 9.1 KB

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