stack.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 id
  52. // in: path
  53. // required: true
  54. // minimum: 1
  55. RevisionID string `json:"revision_id"`
  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}/revisions -> stack.NewStackListRevisionsHandler
  243. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/revisions listStackRevisions
  244. //
  245. // Lists revisions in a stack. A max of 100 revisions will be returned, sorted from most recent to least recent.
  246. //
  247. // ---
  248. // produces:
  249. // - application/json
  250. // summary: List stack revisions
  251. // tags:
  252. // - Stacks
  253. // parameters:
  254. // - name: project_id
  255. // - name: cluster_id
  256. // - name: namespace
  257. // - name: stack_id
  258. // responses:
  259. // '200':
  260. // description: Successfully listed stack revisions
  261. // schema:
  262. // $ref: '#/definitions/ListStackRevisionsResponse'
  263. // '403':
  264. // description: Forbidden
  265. listRevisionsEndpoint := factory.NewAPIEndpoint(
  266. &types.APIRequestMetadata{
  267. Verb: types.APIVerbGet,
  268. Method: types.HTTPVerbGet,
  269. Path: &types.Path{
  270. Parent: basePath,
  271. RelativePath: relPath + "/{stack_id}/revisions",
  272. },
  273. Scopes: []types.PermissionScope{
  274. types.UserScope,
  275. types.ProjectScope,
  276. types.ClusterScope,
  277. types.NamespaceScope,
  278. types.StackScope,
  279. },
  280. },
  281. )
  282. listRevisionsHandler := stack.NewStackListRevisionsHandler(
  283. config,
  284. factory.GetResultWriter(),
  285. )
  286. routes = append(routes, &router.Route{
  287. Endpoint: listRevisionsEndpoint,
  288. Handler: listRevisionsHandler,
  289. Router: r,
  290. })
  291. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/{revision_id} -> stack.NewStackGetRevisionHandler
  292. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/{revision_id} getStackRevision
  293. //
  294. // Gets a stack revision
  295. //
  296. // ---
  297. // produces:
  298. // - application/json
  299. // summary: Get a stack revision
  300. // tags:
  301. // - Stacks
  302. // parameters:
  303. // - name: project_id
  304. // - name: cluster_id
  305. // - name: namespace
  306. // - name: stack_id
  307. // - name: revision_id
  308. // responses:
  309. // '200':
  310. // description: Successfully got the stack revision
  311. // schema:
  312. // $ref: '#/definitions/StackRevision'
  313. // '403':
  314. // description: Forbidden
  315. getRevisionEndpoint := factory.NewAPIEndpoint(
  316. &types.APIRequestMetadata{
  317. Verb: types.APIVerbGet,
  318. Method: types.HTTPVerbGet,
  319. Path: &types.Path{
  320. Parent: basePath,
  321. RelativePath: relPath + "/{stack_id}/{stack_revision_number}",
  322. },
  323. Scopes: []types.PermissionScope{
  324. types.UserScope,
  325. types.ProjectScope,
  326. types.ClusterScope,
  327. types.NamespaceScope,
  328. types.StackScope,
  329. },
  330. },
  331. )
  332. getRevisionHandler := stack.NewStackGetRevisionHandler(
  333. config,
  334. factory.GetResultWriter(),
  335. )
  336. routes = append(routes, &router.Route{
  337. Endpoint: getRevisionEndpoint,
  338. Handler: getRevisionHandler,
  339. Router: r,
  340. })
  341. // PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source -> stack.NewStackPutSourceConfig
  342. // swagger:operation PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source putStackSource
  343. //
  344. // Updates a stack's source configuration
  345. //
  346. // ---
  347. // produces:
  348. // - application/json
  349. // summary: Update source configuration
  350. // tags:
  351. // - Stacks
  352. // parameters:
  353. // - name: project_id
  354. // - name: cluster_id
  355. // - name: namespace
  356. // - name: stack_id
  357. // - in: body
  358. // name: PutStackSourceConfigRequest
  359. // description: The source configurations to update
  360. // schema:
  361. // $ref: '#/definitions/PutStackSourceConfigRequest'
  362. // responses:
  363. // '200':
  364. // description: Successfully updated the source configuration
  365. // schema:
  366. // $ref: '#/definitions/Stack'
  367. // '403':
  368. // description: Forbidden
  369. putSourceEndpoint := factory.NewAPIEndpoint(
  370. &types.APIRequestMetadata{
  371. Verb: types.APIVerbUpdate,
  372. Method: types.HTTPVerbPut,
  373. Path: &types.Path{
  374. Parent: basePath,
  375. RelativePath: relPath + "/{stack_id}/source",
  376. },
  377. Scopes: []types.PermissionScope{
  378. types.UserScope,
  379. types.ProjectScope,
  380. types.ClusterScope,
  381. types.NamespaceScope,
  382. types.StackScope,
  383. },
  384. },
  385. )
  386. putSourceHandler := stack.NewStackPutSourceConfigHandler(
  387. config,
  388. factory.GetDecoderValidator(),
  389. factory.GetResultWriter(),
  390. )
  391. routes = append(routes, &router.Route{
  392. Endpoint: putSourceEndpoint,
  393. Handler: putSourceHandler,
  394. Router: r,
  395. })
  396. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/rollback -> stack.NewStackRollbackHandler
  397. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/rollback rollbackStack
  398. //
  399. // Performs a rollback for a stack
  400. //
  401. // ---
  402. // produces:
  403. // - application/json
  404. // summary: Rollback stack
  405. // tags:
  406. // - Stacks
  407. // parameters:
  408. // - name: project_id
  409. // - name: cluster_id
  410. // - name: namespace
  411. // - name: stack_id
  412. // - in: body
  413. // name: StackRollbackRequest
  414. // description: The target revision to roll back to
  415. // schema:
  416. // $ref: '#/definitions/StackRollbackRequest'
  417. // responses:
  418. // '200':
  419. // description: Successfully rolled the stack back
  420. // schema:
  421. // $ref: '#/definitions/Stack'
  422. // '403':
  423. // description: Forbidden
  424. rollbackEndpoint := factory.NewAPIEndpoint(
  425. &types.APIRequestMetadata{
  426. Verb: types.APIVerbUpdate,
  427. Method: types.HTTPVerbPost,
  428. Path: &types.Path{
  429. Parent: basePath,
  430. RelativePath: relPath + "/{stack_id}/rollback",
  431. },
  432. Scopes: []types.PermissionScope{
  433. types.UserScope,
  434. types.ProjectScope,
  435. types.ClusterScope,
  436. types.NamespaceScope,
  437. types.StackScope,
  438. },
  439. },
  440. )
  441. rollbackHandler := stack.NewStackRollbackHandler(
  442. config,
  443. factory.GetDecoderValidator(),
  444. factory.GetResultWriter(),
  445. )
  446. routes = append(routes, &router.Route{
  447. Endpoint: rollbackEndpoint,
  448. Handler: rollbackHandler,
  449. Router: r,
  450. })
  451. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackDeleteHandler
  452. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} deleteStack
  453. //
  454. // Deletes a stack
  455. //
  456. // ---
  457. // produces:
  458. // - application/json
  459. // summary: Delete a stack
  460. // tags:
  461. // - Stacks
  462. // parameters:
  463. // - name: project_id
  464. // - name: cluster_id
  465. // - name: namespace
  466. // - name: stack_id
  467. // responses:
  468. // '200':
  469. // description: Successfully deleted the stack
  470. // '403':
  471. // description: Forbidden
  472. deleteEndpoint := factory.NewAPIEndpoint(
  473. &types.APIRequestMetadata{
  474. Verb: types.APIVerbDelete,
  475. Method: types.HTTPVerbDelete,
  476. Path: &types.Path{
  477. Parent: basePath,
  478. RelativePath: relPath + "/{stack_id}",
  479. },
  480. Scopes: []types.PermissionScope{
  481. types.UserScope,
  482. types.ProjectScope,
  483. types.ClusterScope,
  484. types.NamespaceScope,
  485. types.StackScope,
  486. },
  487. },
  488. )
  489. deleteHandler := stack.NewStackDeleteHandler(
  490. config,
  491. factory.GetResultWriter(),
  492. )
  493. routes = append(routes, &router.Route{
  494. Endpoint: deleteEndpoint,
  495. Handler: deleteHandler,
  496. Router: r,
  497. })
  498. return routes, newPath
  499. }