2
0

cluster.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  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 in the cluster denoted by `cluster_id`. The cluster should belong to the project
  64. // denoted by `project_id`.
  65. //
  66. // ---
  67. // produces:
  68. // - application/json
  69. // summary: Create a new namespace
  70. // tags:
  71. // - Namespaces
  72. // parameters:
  73. // - name: project_id
  74. // - name: cluster_id
  75. // - in: body
  76. // name: CreateNamespaceRequest
  77. // description: The namespace to create
  78. // schema:
  79. // $ref: '#/definitions/CreateNamespaceRequest'
  80. // responses:
  81. // '201':
  82. // description: Successfully created a new namespace
  83. // schema:
  84. // $ref: '#/definitions/NamespaceResponse'
  85. // '403':
  86. // description: Forbidden
  87. // '412':
  88. // description: Namespace already exists
  89. createNamespaceEndpoint := factory.NewAPIEndpoint(
  90. &types.APIRequestMetadata{
  91. Verb: types.APIVerbCreate,
  92. Method: types.HTTPVerbPost,
  93. Path: &types.Path{
  94. Parent: basePath,
  95. RelativePath: relPath + "/namespaces",
  96. },
  97. Scopes: []types.PermissionScope{
  98. types.UserScope,
  99. types.ProjectScope,
  100. types.ClusterScope,
  101. },
  102. },
  103. )
  104. createNamespaceHandler := cluster.NewCreateNamespaceHandler(
  105. config,
  106. factory.GetDecoderValidator(),
  107. factory.GetResultWriter(),
  108. )
  109. routes = append(routes, &router.Route{
  110. Endpoint: createNamespaceEndpoint,
  111. Handler: createNamespaceHandler,
  112. Router: r,
  113. })
  114. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewGetNamespaceHandler
  115. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} getNamespace
  116. //
  117. // Gets a namespace denoted by the name `namespace`. The namespace should belong to the cluster
  118. // denoted by `cluster_id` which itself should belong to the project denoted by `project_id`.
  119. //
  120. // ---
  121. // produces:
  122. // - application/json
  123. // summary: Get a namespace
  124. // tags:
  125. // - Namespaces
  126. // parameters:
  127. // - name: project_id
  128. // - name: cluster_id
  129. // - name: namespace
  130. // responses:
  131. // '200':
  132. // description: Successfully got the namespace
  133. // schema:
  134. // $ref: '#/definitions/NamespaceResponse'
  135. // '403':
  136. // description: Forbidden
  137. // '404':
  138. // description: Not Found
  139. getNamespaceEndpoint := factory.NewAPIEndpoint(
  140. &types.APIRequestMetadata{
  141. Verb: types.APIVerbGet,
  142. Method: types.HTTPVerbGet,
  143. Path: &types.Path{
  144. Parent: basePath,
  145. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  146. },
  147. Scopes: []types.PermissionScope{
  148. types.UserScope,
  149. types.ProjectScope,
  150. types.ClusterScope,
  151. },
  152. },
  153. )
  154. getNamespaceHandler := cluster.NewGetNamespaceHandler(
  155. config,
  156. factory.GetResultWriter(),
  157. )
  158. routes = append(routes, &router.Route{
  159. Endpoint: getNamespaceEndpoint,
  160. Handler: getNamespaceHandler,
  161. Router: r,
  162. })
  163. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewListNamespacesHandler
  164. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces listNamespaces
  165. //
  166. // Lists all namespaces in the cluster denoted by `cluster_id`. The cluster should belong to
  167. // the project denoted by `project_id`.
  168. //
  169. // ---
  170. // produces:
  171. // - application/json
  172. // summary: List all namespaces
  173. // tags:
  174. // - Namespaces
  175. // parameters:
  176. // - name: project_id
  177. // - name: cluster_id
  178. // responses:
  179. // '200':
  180. // description: Successfully listed namespaces
  181. // schema:
  182. // $ref: '#/definitions/ListNamespacesResponse'
  183. // '403':
  184. // description: Forbidden
  185. listNamespacesEndpoint := factory.NewAPIEndpoint(
  186. &types.APIRequestMetadata{
  187. Verb: types.APIVerbGet,
  188. Method: types.HTTPVerbGet,
  189. Path: &types.Path{
  190. Parent: basePath,
  191. RelativePath: relPath + "/namespaces",
  192. },
  193. Scopes: []types.PermissionScope{
  194. types.UserScope,
  195. types.ProjectScope,
  196. types.ClusterScope,
  197. },
  198. },
  199. )
  200. listNamespacesHandler := cluster.NewListNamespacesHandler(
  201. config,
  202. factory.GetResultWriter(),
  203. )
  204. routes = append(routes, &router.Route{
  205. Endpoint: listNamespacesEndpoint,
  206. Handler: listNamespacesHandler,
  207. Router: r,
  208. })
  209. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} -> cluster.NewDeleteNamespaceHandler
  210. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace} deleteNamespace
  211. //
  212. // Deletes a namespace with the name `namespace`. The namespace should belong to the cluster
  213. // denoted by `cluster_id` which itself should belong to the project denoted by `project_id`.
  214. // Note that this endpoint does not indicate if the namespace does not exist.
  215. //
  216. // ---
  217. // produces:
  218. // - application/json
  219. // summary: Delete a namespace
  220. // tags:
  221. // - Namespaces
  222. // parameters:
  223. // - name: project_id
  224. // - name: cluster_id
  225. // - name: namespace
  226. // responses:
  227. // '200':
  228. // description: Successfully deleted namespace
  229. // '403':
  230. // description: Forbidden
  231. deleteNamespaceEndpoint := factory.NewAPIEndpoint(
  232. &types.APIRequestMetadata{
  233. Verb: types.APIVerbDelete,
  234. Method: types.HTTPVerbDelete,
  235. Path: &types.Path{
  236. Parent: basePath,
  237. RelativePath: fmt.Sprintf("%s/namespaces/{%s}", relPath, types.URLParamNamespace),
  238. },
  239. Scopes: []types.PermissionScope{
  240. types.UserScope,
  241. types.ProjectScope,
  242. types.ClusterScope,
  243. },
  244. },
  245. )
  246. deleteNamespaceHandler := cluster.NewDeleteNamespaceHandler(
  247. config,
  248. factory.GetDecoderValidator(),
  249. )
  250. routes = append(routes, &router.Route{
  251. Endpoint: deleteNamespaceEndpoint,
  252. Handler: deleteNamespaceHandler,
  253. Router: r,
  254. })
  255. return routes, newPath
  256. }