cluster.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/cluster"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. // swagger:parameters createNamespace listNamespaces
  12. type clusterPathParams struct {
  13. // The project id
  14. // in: path
  15. // required: true
  16. // minimum: 1
  17. ProjectID uint `json:"project_id"`
  18. // The cluster id
  19. // in: path
  20. // required: true
  21. // minimum: 1
  22. ClusterID uint `json:"cluster_id"`
  23. }
  24. func NewV1ClusterScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  25. return &router.Registerer{
  26. GetRoutes: GetV1ClusterScopedRoutes,
  27. Children: children,
  28. }
  29. }
  30. func GetV1ClusterScopedRoutes(
  31. r chi.Router,
  32. config *config.Config,
  33. basePath *types.Path,
  34. factory shared.APIEndpointFactory,
  35. children ...*router.Registerer,
  36. ) []*router.Route {
  37. routes, projPath := getV1ClusterRoutes(r, config, basePath, factory)
  38. if len(children) > 0 {
  39. r.Route(projPath.RelativePath, func(r chi.Router) {
  40. for _, child := range children {
  41. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  42. routes = append(routes, childRoutes...)
  43. }
  44. })
  45. }
  46. return routes
  47. }
  48. func getV1ClusterRoutes(
  49. r chi.Router,
  50. config *config.Config,
  51. basePath *types.Path,
  52. factory shared.APIEndpointFactory,
  53. ) ([]*router.Route, *types.Path) {
  54. relPath := "/clusters/{cluster_id}"
  55. newPath := &types.Path{
  56. Parent: basePath,
  57. RelativePath: relPath,
  58. }
  59. var routes []*router.Route
  60. // POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewCreateNamespaceHandler
  61. // swagger:operation POST /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces createNamespace
  62. //
  63. // Creates a new namespace
  64. //
  65. // ---
  66. // produces:
  67. // - application/json
  68. // summary: Create a new namespace
  69. // tags:
  70. // - Namespaces
  71. // parameters:
  72. // - name: project_id
  73. // - name: cluster_id
  74. // - in: body
  75. // name: CreateNamespaceRequest
  76. // description: The namespace to create
  77. // schema:
  78. // $ref: '#/definitions/CreateNamespaceRequest'
  79. // responses:
  80. // '201':
  81. // description: Successfully created a new namespace
  82. // schema:
  83. // $ref: '#/definitions/CreateNamespaceResponse'
  84. // '403':
  85. // description: Forbidden
  86. createNamespaceEndpoint := factory.NewAPIEndpoint(
  87. &types.APIRequestMetadata{
  88. Verb: types.APIVerbCreate,
  89. Method: types.HTTPVerbPost,
  90. Path: &types.Path{
  91. Parent: basePath,
  92. RelativePath: relPath + "/namespaces",
  93. },
  94. Scopes: []types.PermissionScope{
  95. types.UserScope,
  96. types.ProjectScope,
  97. types.ClusterScope,
  98. },
  99. },
  100. )
  101. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  102. config,
  103. factory.GetDecoderValidator(),
  104. factory.GetResultWriter(),
  105. )
  106. routes = append(routes, &router.Route{
  107. Endpoint: createNamespaceEndpoint,
  108. Handler: createNamespaceHandler,
  109. Router: r,
  110. })
  111. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewGetNamespaceHandler
  112. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} getNamespace
  113. //
  114. // Gets a namespace
  115. //
  116. // ---
  117. // produces:
  118. // - application/json
  119. // summary: Get a namespace
  120. // tags:
  121. // - Namespaces
  122. // parameters:
  123. // - name: project_id
  124. // - name: cluster_id
  125. // - name: namespace
  126. // responses:
  127. // '200':
  128. // description: Successfully got the namespace
  129. // schema:
  130. // $ref: '#/definitions/GetNamespaceResponse'
  131. // '403':
  132. // description: Forbidden
  133. // '404':
  134. // description: Not Found
  135. getNamespaceEndpoint := factory.NewAPIEndpoint(
  136. &types.APIRequestMetadata{
  137. Verb: types.APIVerbGet,
  138. Method: types.HTTPVerbGet,
  139. Path: &types.Path{
  140. Parent: basePath,
  141. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  142. },
  143. Scopes: []types.PermissionScope{
  144. types.UserScope,
  145. types.ProjectScope,
  146. types.ClusterScope,
  147. },
  148. },
  149. )
  150. getNamespaceHandler := cluster.NewGetNamespaceHandler(
  151. config,
  152. factory.GetResultWriter(),
  153. )
  154. routes = append(routes, &router.Route{
  155. Endpoint: getNamespaceEndpoint,
  156. Handler: getNamespaceHandler,
  157. Router: r,
  158. })
  159. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewListNamespacesHandler
  160. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces listNamespaces
  161. //
  162. // Lists namespaces
  163. //
  164. // ---
  165. // produces:
  166. // - application/json
  167. // summary: List all namespaces
  168. // tags:
  169. // - Namespaces
  170. // parameters:
  171. // - name: project_id
  172. // - name: cluster_id
  173. // responses:
  174. // '200':
  175. // description: Successfully listed namespaces
  176. // schema:
  177. // $ref: '#/definitions/ListNamespacesResponse'
  178. // '403':
  179. // description: Forbidden
  180. listNamespacesEndpoint := factory.NewAPIEndpoint(
  181. &types.APIRequestMetadata{
  182. Verb: types.APIVerbGet,
  183. Method: types.HTTPVerbGet,
  184. Path: &types.Path{
  185. Parent: basePath,
  186. RelativePath: relPath + "/namespaces",
  187. },
  188. Scopes: []types.PermissionScope{
  189. types.UserScope,
  190. types.ProjectScope,
  191. types.ClusterScope,
  192. },
  193. },
  194. )
  195. listNamespacesHandler := cluster.NewListNamespacesHandler(
  196. config,
  197. factory.GetResultWriter(),
  198. )
  199. routes = append(routes, &router.Route{
  200. Endpoint: listNamespacesEndpoint,
  201. Handler: listNamespacesHandler,
  202. Router: r,
  203. })
  204. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  205. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} deleteNamespace
  206. //
  207. // Deletes a namespace
  208. //
  209. // ---
  210. // produces:
  211. // - application/json
  212. // summary: Delete a namespace
  213. // tags:
  214. // - Namespaces
  215. // parameters:
  216. // - name: project_id
  217. // - name: cluster_id
  218. // - name: namespace
  219. // responses:
  220. // '200':
  221. // description: Successfully deleted namespace
  222. // '403':
  223. // description: Forbidden
  224. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  225. &types.APIRequestMetadata{
  226. Verb: types.APIVerbDelete,
  227. Method: types.HTTPVerbDelete,
  228. Path: &types.Path{
  229. Parent: basePath,
  230. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  231. },
  232. Scopes: []types.PermissionScope{
  233. types.UserScope,
  234. types.ProjectScope,
  235. types.ClusterScope,
  236. },
  237. },
  238. )
  239. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  240. config,
  241. factory.GetDecoderValidator(),
  242. )
  243. routes = append(routes, &router.Route{
  244. Endpoint: deleteNamespaceEndpoint,
  245. Handler: deleteNamespaceHandler,
  246. Router: r,
  247. })
  248. return routes, newPath
  249. }