release.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 in the namespace denoted by `namespace`. The namespace should belong to the
  83. // cluster denoted by `cluster_id` which itself should belong to the project denoted by `project_id`.
  84. //
  85. // ---
  86. // produces:
  87. // - application/json
  88. // summary: Create a new release
  89. // tags:
  90. // - Releases
  91. // parameters:
  92. // - name: project_id
  93. // - name: cluster_id
  94. // - name: namespace
  95. // - in: body
  96. // name: CreateReleaseRequest
  97. // description: The release to create
  98. // schema:
  99. // $ref: '#/definitions/CreateReleaseRequest'
  100. // responses:
  101. // '201':
  102. // description: Successfully created the release
  103. // '400':
  104. // description: A malformed or bad request
  105. // '403':
  106. // description: Forbidden
  107. // '404':
  108. // description: A subresource was not found
  109. // '409':
  110. // description: A conflict occurred with another external service
  111. // '412':
  112. // description: A precondition failed for the request
  113. createReleaseEndpoint := factory.NewAPIEndpoint(
  114. &types.APIRequestMetadata{
  115. Verb: types.APIVerbCreate,
  116. Method: types.HTTPVerbPost,
  117. Path: &types.Path{
  118. Parent: basePath,
  119. RelativePath: relPath,
  120. },
  121. Scopes: []types.PermissionScope{
  122. types.UserScope,
  123. types.ProjectScope,
  124. types.ClusterScope,
  125. types.NamespaceScope,
  126. },
  127. },
  128. )
  129. createReleaseHandler := release.NewCreateReleaseHandler(
  130. config,
  131. factory.GetDecoderValidator(),
  132. factory.GetResultWriter(),
  133. )
  134. routes = append(routes, &router.Route{
  135. Endpoint: createReleaseEndpoint,
  136. Handler: createReleaseHandler,
  137. Router: r,
  138. })
  139. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} -> release.NewReleaseGetHandler
  140. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} getRelease
  141. //
  142. // Gets the release denoted by the name `name` and its version `version`. The release should belong to the namespace
  143. // denoted by `namespace` which itself should belong to the cluster denoted by `cluster_id` and project
  144. // denoted by `project_id`.
  145. //
  146. // ---
  147. // produces:
  148. // - application/json
  149. // summary: Get a release
  150. // tags:
  151. // - Releases
  152. // parameters:
  153. // - name: project_id
  154. // - name: cluster_id
  155. // - name: namespace
  156. // - name: name
  157. // - name: version
  158. // responses:
  159. // '200':
  160. // description: Successfully got the release
  161. // schema:
  162. // $ref: '#/definitions/GetReleaseResponse'
  163. // '403':
  164. // description: Forbidden
  165. getEndpoint := factory.NewAPIEndpoint(
  166. &types.APIRequestMetadata{
  167. Verb: types.APIVerbGet,
  168. Method: types.HTTPVerbGet,
  169. Path: &types.Path{
  170. Parent: basePath,
  171. RelativePath: relPath + "/{name}/{version}",
  172. },
  173. Scopes: []types.PermissionScope{
  174. types.UserScope,
  175. types.ProjectScope,
  176. types.ClusterScope,
  177. types.NamespaceScope,
  178. types.ReleaseScope,
  179. },
  180. },
  181. )
  182. getHandler := release.NewReleaseGetHandler(
  183. config,
  184. factory.GetResultWriter(),
  185. )
  186. routes = append(routes, &router.Route{
  187. Endpoint: getEndpoint,
  188. Handler: getHandler,
  189. Router: r,
  190. })
  191. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  192. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases listReleases
  193. //
  194. // List all releases in the namespace denoted by `namespace`. The namespace should belong to the cluster
  195. // denoted by `cluster_id` and project denoted by `project_id`.
  196. //
  197. // ---
  198. // produces:
  199. // - application/json
  200. // summary: List releases
  201. // tags:
  202. // - Releases
  203. // responses:
  204. // '201':
  205. // description: Successfully listed releases
  206. // schema:
  207. // $ref: '#/definitions/ListReleasesResponse'
  208. // '403':
  209. // description: Forbidden
  210. listReleasesEndpoint := factory.NewAPIEndpoint(
  211. &types.APIRequestMetadata{
  212. Verb: types.APIVerbGet,
  213. Method: types.HTTPVerbGet,
  214. Path: &types.Path{
  215. Parent: basePath,
  216. RelativePath: relPath,
  217. },
  218. Scopes: []types.PermissionScope{
  219. types.UserScope,
  220. types.ProjectScope,
  221. types.ClusterScope,
  222. types.NamespaceScope,
  223. },
  224. },
  225. )
  226. listReleasesHandler := namespace.NewListReleasesHandler(
  227. config,
  228. factory.GetDecoderValidator(),
  229. factory.GetResultWriter(),
  230. )
  231. routes = append(routes, &router.Route{
  232. Endpoint: listReleasesEndpoint,
  233. Handler: listReleasesHandler,
  234. Router: r,
  235. })
  236. // PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  237. // release.NewUpgradeReleaseHandler
  238. // swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} updateRelease
  239. //
  240. // Upgrades the release with the name denoted by `name` and version denoted by `version`. The release should belong
  241. // to the namespace denoted by `namespace` which itself should belong to the cluster denoted by `cluster_id` and project
  242. // denoted by `project_id`.
  243. //
  244. // ---
  245. // produces:
  246. // - application/json
  247. // summary: Update a release
  248. // tags:
  249. // - Releases
  250. // parameters:
  251. // - name: project_id
  252. // - name: cluster_id
  253. // - name: namespace
  254. // - name: name
  255. // - name: version
  256. // - in: body
  257. // name: UpdateReleaseRequest
  258. // description: The release to update
  259. // schema:
  260. // $ref: '#/definitions/UpdateReleaseRequest'
  261. // responses:
  262. // '200':
  263. // description: Successfully updated the release
  264. // '400':
  265. // description: A malformed or bad request
  266. // '403':
  267. // description: Forbidden
  268. upgradeEndpoint := factory.NewAPIEndpoint(
  269. &types.APIRequestMetadata{
  270. Verb: types.APIVerbUpdate,
  271. Method: types.HTTPVerbPatch,
  272. Path: &types.Path{
  273. Parent: basePath,
  274. RelativePath: relPath + "/{name}/{version}",
  275. },
  276. Scopes: []types.PermissionScope{
  277. types.UserScope,
  278. types.ProjectScope,
  279. types.ClusterScope,
  280. types.NamespaceScope,
  281. types.ReleaseScope,
  282. },
  283. },
  284. )
  285. upgradeHandler := v1Release.NewUpgradeReleaseHandler(
  286. config,
  287. factory.GetDecoderValidator(),
  288. factory.GetResultWriter(),
  289. )
  290. routes = append(routes, &router.Route{
  291. Endpoint: upgradeEndpoint,
  292. Handler: upgradeHandler,
  293. Router: r,
  294. })
  295. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} ->
  296. // release.NewDeleteReleaseHandler
  297. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} deleteRelease
  298. //
  299. // Deletes the release with the name denoted by `name` and version denoted by `version`. The release should belong
  300. // to the namespace denoted by `namespace` which itself should belong to the cluster denoted by `cluster_id` and project
  301. // denoted by `project_id`.
  302. //
  303. // ---
  304. // produces:
  305. // - application/json
  306. // summary: Delete a release
  307. // tags:
  308. // - Releases
  309. // parameters:
  310. // - name: project_id
  311. // - name: cluster_id
  312. // - name: namespace
  313. // - name: name
  314. // - name: version
  315. // responses:
  316. // '200':
  317. // description: Successfully deleted the release
  318. // '403':
  319. // description: Forbidden
  320. deleteEndpoint := factory.NewAPIEndpoint(
  321. &types.APIRequestMetadata{
  322. Verb: types.APIVerbDelete,
  323. Method: types.HTTPVerbDelete,
  324. Path: &types.Path{
  325. Parent: basePath,
  326. RelativePath: relPath,
  327. },
  328. Scopes: []types.PermissionScope{
  329. types.UserScope,
  330. types.ProjectScope,
  331. types.ClusterScope,
  332. types.NamespaceScope,
  333. types.ReleaseScope,
  334. },
  335. },
  336. )
  337. deleteHandler := release.NewDeleteReleaseHandler(
  338. config,
  339. factory.GetDecoderValidator(),
  340. factory.GetResultWriter(),
  341. )
  342. routes = append(routes, &router.Route{
  343. Endpoint: deleteEndpoint,
  344. Handler: deleteHandler,
  345. Router: r,
  346. })
  347. return routes, newPath
  348. }