stack.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package v1
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/stack"
  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. )
  10. // swagger:parameters getStack deleteStack putStackSource rollbackStack
  11. type stackPathParams struct {
  12. // The project id
  13. // in: path
  14. // required: true
  15. // minimum: 1
  16. ProjectID uint `json:"project_id"`
  17. // The cluster id
  18. // in: path
  19. // required: true
  20. // minimum: 1
  21. ClusterID uint `json:"cluster_id"`
  22. // The namespace
  23. // in: path
  24. // required: true
  25. Namespace string `json:"namespace"`
  26. // The stack id
  27. // in: path
  28. // required: true
  29. StackID string `json:"stack_id"`
  30. }
  31. // swagger:parameters getStackRevision
  32. type stackRevisionPathParams struct {
  33. // The project id
  34. // in: path
  35. // required: true
  36. // minimum: 1
  37. ProjectID uint `json:"project_id"`
  38. // The cluster id
  39. // in: path
  40. // required: true
  41. // minimum: 1
  42. ClusterID uint `json:"cluster_id"`
  43. // The namespace
  44. // in: path
  45. // required: true
  46. Namespace string `json:"namespace"`
  47. // The stack id
  48. // in: path
  49. // required: true
  50. StackID string `json:"stack_id"`
  51. // The stack revision number
  52. // in: path
  53. // required: true
  54. // minimum: 1
  55. StackRevisionNumber string `json:"stack_revision_number"`
  56. }
  57. func NewV1StackScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  58. return &router.Registerer{
  59. GetRoutes: GetV1StackScopedRoutes,
  60. Children: children,
  61. }
  62. }
  63. func GetV1StackScopedRoutes(
  64. r chi.Router,
  65. config *config.Config,
  66. basePath *types.Path,
  67. factory shared.APIEndpointFactory,
  68. children ...*router.Registerer,
  69. ) []*router.Route {
  70. routes, projPath := getV1StackRoutes(r, config, basePath, factory)
  71. if len(children) > 0 {
  72. r.Route(projPath.RelativePath, func(r chi.Router) {
  73. for _, child := range children {
  74. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  75. routes = append(routes, childRoutes...)
  76. }
  77. })
  78. }
  79. return routes
  80. }
  81. func getV1StackRoutes(
  82. r chi.Router,
  83. config *config.Config,
  84. basePath *types.Path,
  85. factory shared.APIEndpointFactory,
  86. ) ([]*router.Route, *types.Path) {
  87. relPath := "/stacks"
  88. newPath := &types.Path{
  89. Parent: basePath,
  90. RelativePath: relPath,
  91. }
  92. var routes []*router.Route
  93. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks -> stack.NewStackCreateHandler
  94. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks createStack
  95. //
  96. // Creates a new stack and triggers a deployment for all resources in the stack.
  97. //
  98. // ---
  99. // produces:
  100. // - application/json
  101. // summary: Create a stack
  102. // tags:
  103. // - Stacks
  104. // parameters:
  105. // - name: project_id
  106. // - name: cluster_id
  107. // - name: namespace
  108. // - in: body
  109. // name: CreateStackRequest
  110. // description: The stack to create
  111. // schema:
  112. // $ref: '#/definitions/CreateStackRequest'
  113. // responses:
  114. // '201':
  115. // description: Successfully created the stack
  116. // schema:
  117. // $ref: '#/definitions/Stack'
  118. // '403':
  119. // description: Forbidden
  120. createEndpoint := factory.NewAPIEndpoint(
  121. &types.APIRequestMetadata{
  122. Verb: types.APIVerbCreate,
  123. Method: types.HTTPVerbPost,
  124. Path: &types.Path{
  125. Parent: basePath,
  126. RelativePath: relPath,
  127. },
  128. Scopes: []types.PermissionScope{
  129. types.UserScope,
  130. types.ProjectScope,
  131. types.ClusterScope,
  132. types.NamespaceScope,
  133. },
  134. },
  135. )
  136. createHandler := stack.NewStackCreateHandler(
  137. config,
  138. factory.GetDecoderValidator(),
  139. factory.GetResultWriter(),
  140. )
  141. routes = append(routes, &router.Route{
  142. Endpoint: createEndpoint,
  143. Handler: createHandler,
  144. Router: r,
  145. })
  146. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks -> stack.NewStackListHandler
  147. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks listStacks
  148. //
  149. // Lists stacks in a namespace
  150. //
  151. // ---
  152. // produces:
  153. // - application/json
  154. // summary: List stacks
  155. // tags:
  156. // - Stacks
  157. // parameters:
  158. // - name: project_id
  159. // - name: cluster_id
  160. // - name: namespace
  161. // responses:
  162. // '200':
  163. // description: Successfully listed stacks
  164. // schema:
  165. // $ref: '#/definitions/StackListResponse'
  166. // '403':
  167. // description: Forbidden
  168. listEndpoint := factory.NewAPIEndpoint(
  169. &types.APIRequestMetadata{
  170. Verb: types.APIVerbGet,
  171. Method: types.HTTPVerbGet,
  172. Path: &types.Path{
  173. Parent: basePath,
  174. RelativePath: relPath,
  175. },
  176. Scopes: []types.PermissionScope{
  177. types.UserScope,
  178. types.ProjectScope,
  179. types.ClusterScope,
  180. types.NamespaceScope,
  181. },
  182. },
  183. )
  184. listHandler := stack.NewStackListHandler(
  185. config,
  186. factory.GetResultWriter(),
  187. )
  188. routes = append(routes, &router.Route{
  189. Endpoint: listEndpoint,
  190. Handler: listHandler,
  191. Router: r,
  192. })
  193. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackGetHandler
  194. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} getStack
  195. //
  196. // Gets a stack
  197. //
  198. // ---
  199. // produces:
  200. // - application/json
  201. // summary: Get a stack
  202. // tags:
  203. // - Stacks
  204. // parameters:
  205. // - name: project_id
  206. // - name: cluster_id
  207. // - name: namespace
  208. // - name: stack_id
  209. // responses:
  210. // '200':
  211. // description: Successfully got the stack
  212. // schema:
  213. // $ref: '#/definitions/Stack'
  214. // '403':
  215. // description: Forbidden
  216. getEndpoint := factory.NewAPIEndpoint(
  217. &types.APIRequestMetadata{
  218. Verb: types.APIVerbGet,
  219. Method: types.HTTPVerbGet,
  220. Path: &types.Path{
  221. Parent: basePath,
  222. RelativePath: relPath + "/{stack_id}",
  223. },
  224. Scopes: []types.PermissionScope{
  225. types.UserScope,
  226. types.ProjectScope,
  227. types.ClusterScope,
  228. types.NamespaceScope,
  229. types.StackScope,
  230. },
  231. },
  232. )
  233. getHandler := stack.NewStackGetHandler(
  234. config,
  235. factory.GetResultWriter(),
  236. )
  237. routes = append(routes, &router.Route{
  238. Endpoint: getEndpoint,
  239. Handler: getHandler,
  240. Router: r,
  241. })
  242. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/{stack_revision_number} -> stack.NewStackGetRevisionHandler
  243. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/{stack_revision_number} getStackRevision
  244. //
  245. // Gets a stack revision
  246. //
  247. // ---
  248. // produces:
  249. // - application/json
  250. // summary: Get a stack revision
  251. // tags:
  252. // - Stacks
  253. // parameters:
  254. // - name: project_id
  255. // - name: cluster_id
  256. // - name: namespace
  257. // - name: stack_id
  258. // - name: stack_revision_number
  259. // responses:
  260. // '200':
  261. // description: Successfully got the stack revision
  262. // schema:
  263. // $ref: '#/definitions/StackRevision'
  264. // '403':
  265. // description: Forbidden
  266. getRevisionEndpoint := factory.NewAPIEndpoint(
  267. &types.APIRequestMetadata{
  268. Verb: types.APIVerbGet,
  269. Method: types.HTTPVerbGet,
  270. Path: &types.Path{
  271. Parent: basePath,
  272. RelativePath: relPath + "/{stack_id}/{stack_revision_number}",
  273. },
  274. Scopes: []types.PermissionScope{
  275. types.UserScope,
  276. types.ProjectScope,
  277. types.ClusterScope,
  278. types.NamespaceScope,
  279. types.StackScope,
  280. },
  281. },
  282. )
  283. getRevisionHandler := stack.NewStackGetRevisionHandler(
  284. config,
  285. factory.GetResultWriter(),
  286. )
  287. routes = append(routes, &router.Route{
  288. Endpoint: getRevisionEndpoint,
  289. Handler: getRevisionHandler,
  290. Router: r,
  291. })
  292. // PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source -> stack.NewStackPutSourceConfig
  293. // swagger:operation PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source putStackSource
  294. //
  295. // Updates a stack's source configuration
  296. //
  297. // ---
  298. // produces:
  299. // - application/json
  300. // summary: Update source configuration
  301. // tags:
  302. // - Stacks
  303. // parameters:
  304. // - name: project_id
  305. // - name: cluster_id
  306. // - name: namespace
  307. // - name: stack_id
  308. // - in: body
  309. // name: PutStackSourceConfigRequest
  310. // description: The source configurations to update
  311. // schema:
  312. // $ref: '#/definitions/PutStackSourceConfigRequest'
  313. // responses:
  314. // '200':
  315. // description: Successfully updated the source configuration
  316. // schema:
  317. // $ref: '#/definitions/Stack'
  318. // '403':
  319. // description: Forbidden
  320. putSourceEndpoint := factory.NewAPIEndpoint(
  321. &types.APIRequestMetadata{
  322. Verb: types.APIVerbUpdate,
  323. Method: types.HTTPVerbPut,
  324. Path: &types.Path{
  325. Parent: basePath,
  326. RelativePath: relPath + "/{stack_id}/source",
  327. },
  328. Scopes: []types.PermissionScope{
  329. types.UserScope,
  330. types.ProjectScope,
  331. types.ClusterScope,
  332. types.NamespaceScope,
  333. types.StackScope,
  334. },
  335. },
  336. )
  337. putSourceHandler := stack.NewStackPutSourceConfigHandler(
  338. config,
  339. factory.GetDecoderValidator(),
  340. factory.GetResultWriter(),
  341. )
  342. routes = append(routes, &router.Route{
  343. Endpoint: putSourceEndpoint,
  344. Handler: putSourceHandler,
  345. Router: r,
  346. })
  347. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/rollback -> stack.NewStackRollbackHandler
  348. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/rollback rollbackStack
  349. //
  350. // Performs a rollback for a stack
  351. //
  352. // ---
  353. // produces:
  354. // - application/json
  355. // summary: Rollback stack
  356. // tags:
  357. // - Stacks
  358. // parameters:
  359. // - name: project_id
  360. // - name: cluster_id
  361. // - name: namespace
  362. // - name: stack_id
  363. // - in: body
  364. // name: StackRollbackRequest
  365. // description: The target revision to roll back to
  366. // schema:
  367. // $ref: '#/definitions/StackRollbackRequest'
  368. // responses:
  369. // '200':
  370. // description: Successfully rolled the stack back
  371. // schema:
  372. // $ref: '#/definitions/Stack'
  373. // '403':
  374. // description: Forbidden
  375. rollbackEndpoint := factory.NewAPIEndpoint(
  376. &types.APIRequestMetadata{
  377. Verb: types.APIVerbUpdate,
  378. Method: types.HTTPVerbPost,
  379. Path: &types.Path{
  380. Parent: basePath,
  381. RelativePath: relPath + "/{stack_id}/rollback",
  382. },
  383. Scopes: []types.PermissionScope{
  384. types.UserScope,
  385. types.ProjectScope,
  386. types.ClusterScope,
  387. types.NamespaceScope,
  388. types.StackScope,
  389. },
  390. },
  391. )
  392. rollbackHandler := stack.NewStackRollbackHandler(
  393. config,
  394. factory.GetDecoderValidator(),
  395. factory.GetResultWriter(),
  396. )
  397. routes = append(routes, &router.Route{
  398. Endpoint: rollbackEndpoint,
  399. Handler: rollbackHandler,
  400. Router: r,
  401. })
  402. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackDeleteHandler
  403. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} deleteStack
  404. //
  405. // Deletes a stack
  406. //
  407. // ---
  408. // produces:
  409. // - application/json
  410. // summary: Delete a stack
  411. // tags:
  412. // - Stacks
  413. // parameters:
  414. // - name: project_id
  415. // - name: cluster_id
  416. // - name: namespace
  417. // - name: stack_id
  418. // responses:
  419. // '200':
  420. // description: Successfully deleted the stack
  421. // '403':
  422. // description: Forbidden
  423. deleteEndpoint := factory.NewAPIEndpoint(
  424. &types.APIRequestMetadata{
  425. Verb: types.APIVerbDelete,
  426. Method: types.HTTPVerbDelete,
  427. Path: &types.Path{
  428. Parent: basePath,
  429. RelativePath: relPath + "/{stack_id}",
  430. },
  431. Scopes: []types.PermissionScope{
  432. types.UserScope,
  433. types.ProjectScope,
  434. types.ClusterScope,
  435. types.NamespaceScope,
  436. types.StackScope,
  437. },
  438. },
  439. )
  440. deleteHandler := stack.NewStackDeleteHandler(
  441. config,
  442. factory.GetResultWriter(),
  443. )
  444. routes = append(routes, &router.Route{
  445. Endpoint: deleteEndpoint,
  446. Handler: deleteHandler,
  447. Router: r,
  448. })
  449. return routes, newPath
  450. }