stack.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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
  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. func NewV1StackScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  32. return &router.Registerer{
  33. GetRoutes: GetV1StackScopedRoutes,
  34. Children: children,
  35. }
  36. }
  37. func GetV1StackScopedRoutes(
  38. r chi.Router,
  39. config *config.Config,
  40. basePath *types.Path,
  41. factory shared.APIEndpointFactory,
  42. children ...*router.Registerer,
  43. ) []*router.Route {
  44. routes, projPath := getV1StackRoutes(r, config, basePath, factory)
  45. if len(children) > 0 {
  46. r.Route(projPath.RelativePath, func(r chi.Router) {
  47. for _, child := range children {
  48. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  49. routes = append(routes, childRoutes...)
  50. }
  51. })
  52. }
  53. return routes
  54. }
  55. func getV1StackRoutes(
  56. r chi.Router,
  57. config *config.Config,
  58. basePath *types.Path,
  59. factory shared.APIEndpointFactory,
  60. ) ([]*router.Route, *types.Path) {
  61. relPath := "/stacks"
  62. newPath := &types.Path{
  63. Parent: basePath,
  64. RelativePath: relPath,
  65. }
  66. var routes []*router.Route
  67. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks -> stack.NewStackCreateHandler
  68. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks createStack
  69. //
  70. // Creates a stack
  71. //
  72. // ---
  73. // produces:
  74. // - application/json
  75. // summary: Create a stack
  76. // tags:
  77. // - Stacks
  78. // parameters:
  79. // - name: project_id
  80. // - name: cluster_id
  81. // - name: namespace
  82. // - in: body
  83. // name: CreateStackRequest
  84. // description: The stack to create
  85. // schema:
  86. // $ref: '#/definitions/CreateStackRequest'
  87. // responses:
  88. // '201':
  89. // description: Successfully created the stack
  90. // schema:
  91. // $ref: '#/definitions/Stack'
  92. // '403':
  93. // description: Forbidden
  94. createEndpoint := factory.NewAPIEndpoint(
  95. &types.APIRequestMetadata{
  96. Verb: types.APIVerbCreate,
  97. Method: types.HTTPVerbPost,
  98. Path: &types.Path{
  99. Parent: basePath,
  100. RelativePath: relPath,
  101. },
  102. Scopes: []types.PermissionScope{
  103. types.UserScope,
  104. types.ProjectScope,
  105. types.ClusterScope,
  106. types.NamespaceScope,
  107. },
  108. },
  109. )
  110. createHandler := stack.NewStackCreateHandler(
  111. config,
  112. factory.GetDecoderValidator(),
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &router.Route{
  116. Endpoint: createEndpoint,
  117. Handler: createHandler,
  118. Router: r,
  119. })
  120. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackGetHandler
  121. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} getStack
  122. //
  123. // Gets a stack
  124. //
  125. // ---
  126. // produces:
  127. // - application/json
  128. // summary: Get a stack
  129. // tags:
  130. // - Stacks
  131. // parameters:
  132. // - name: project_id
  133. // - name: cluster_id
  134. // - name: namespace
  135. // - name: stack_id
  136. // responses:
  137. // '200':
  138. // description: Successfully got the stack
  139. // schema:
  140. // $ref: '#/definitions/Stack'
  141. // '403':
  142. // description: Forbidden
  143. getEndpoint := factory.NewAPIEndpoint(
  144. &types.APIRequestMetadata{
  145. Verb: types.APIVerbGet,
  146. Method: types.HTTPVerbGet,
  147. Path: &types.Path{
  148. Parent: basePath,
  149. RelativePath: relPath + "/{stack_id}",
  150. },
  151. Scopes: []types.PermissionScope{
  152. types.UserScope,
  153. types.ProjectScope,
  154. types.ClusterScope,
  155. types.NamespaceScope,
  156. types.StackScope,
  157. },
  158. },
  159. )
  160. getHandler := stack.NewStackGetHandler(
  161. config,
  162. factory.GetResultWriter(),
  163. )
  164. routes = append(routes, &router.Route{
  165. Endpoint: getEndpoint,
  166. Handler: getHandler,
  167. Router: r,
  168. })
  169. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} -> stack.NewStackDeleteHandler
  170. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id} deleteStack
  171. //
  172. // Deletes a stack
  173. //
  174. // ---
  175. // produces:
  176. // - application/json
  177. // summary: Delete a stack
  178. // tags:
  179. // - Stacks
  180. // parameters:
  181. // - name: project_id
  182. // - name: cluster_id
  183. // - name: namespace
  184. // - name: stack_id
  185. // responses:
  186. // '200':
  187. // description: Successfully deleted the stack
  188. // '403':
  189. // description: Forbidden
  190. deleteEndpoint := factory.NewAPIEndpoint(
  191. &types.APIRequestMetadata{
  192. Verb: types.APIVerbDelete,
  193. Method: types.HTTPVerbDelete,
  194. Path: &types.Path{
  195. Parent: basePath,
  196. RelativePath: relPath + "/{stack_id}",
  197. },
  198. Scopes: []types.PermissionScope{
  199. types.UserScope,
  200. types.ProjectScope,
  201. types.ClusterScope,
  202. types.NamespaceScope,
  203. types.StackScope,
  204. },
  205. },
  206. )
  207. deleteHandler := stack.NewStackDeleteHandler(
  208. config,
  209. factory.GetResultWriter(),
  210. )
  211. routes = append(routes, &router.Route{
  212. Endpoint: deleteEndpoint,
  213. Handler: deleteHandler,
  214. Router: r,
  215. })
  216. return routes, newPath
  217. }