2
0

env_group.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. package v1
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi/v5"
  5. v1EnvGroup "github.com/porter-dev/porter/api/server/handlers/v1/env_group"
  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 getEnvGroup
  12. type envGroupVersionPathParams 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. // The namespace name
  24. // in: path
  25. // required: true
  26. Namespace string `json:"namespace"`
  27. // The env group name
  28. // in: path
  29. // required: true
  30. Name string `json:"name"`
  31. // The version of the env group. 0 means latest version.
  32. // in: path
  33. // required: true
  34. // minimum: 0
  35. Version uint `json:"version"`
  36. }
  37. // swagger:parameters getEnvGroupAllVersions deleteEnvGroup addReleaseToEnvGroup removeReleaseFromEnvGroup
  38. type envGroupPathParams struct {
  39. // The project id
  40. // in: path
  41. // required: true
  42. // minimum: 1
  43. ProjectID uint `json:"project_id"`
  44. // The cluster id
  45. // in: path
  46. // required: true
  47. // minimum: 1
  48. ClusterID uint `json:"cluster_id"`
  49. // The namespace name
  50. // in: path
  51. // required: true
  52. Namespace string `json:"namespace"`
  53. // The env group name
  54. // in: path
  55. // required: true
  56. Name string `json:"name"`
  57. }
  58. func NewV1EnvGroupScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  59. return &router.Registerer{
  60. GetRoutes: GetV1EnvGroupScopedRoutes,
  61. Children: children,
  62. }
  63. }
  64. func GetV1EnvGroupScopedRoutes(
  65. r chi.Router,
  66. config *config.Config,
  67. basePath *types.Path,
  68. factory shared.APIEndpointFactory,
  69. children ...*router.Registerer,
  70. ) []*router.Route {
  71. routes, projPath := getV1EnvGroupRoutes(r, config, basePath, factory)
  72. if len(children) > 0 {
  73. r.Route(projPath.RelativePath, func(r chi.Router) {
  74. for _, child := range children {
  75. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  76. routes = append(routes, childRoutes...)
  77. }
  78. })
  79. }
  80. return routes
  81. }
  82. func getV1EnvGroupRoutes(
  83. r chi.Router,
  84. config *config.Config,
  85. basePath *types.Path,
  86. factory shared.APIEndpointFactory,
  87. ) ([]*router.Route, *types.Path) {
  88. relPath := "/env_groups"
  89. newPath := &types.Path{
  90. Parent: basePath,
  91. RelativePath: relPath,
  92. }
  93. var routes []*router.Route
  94. // PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups -> namespace.NewCreateEnvGroupHandler
  95. // swagger:operation PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups createOrUpdateEnvGroup
  96. //
  97. // Creates a new env group or updates an existing one in the namespace denoted by `namespace`. The namespace should belong to the cluster
  98. // denoted by `cluster_id`. The cluster should belong to the project denoted by `project_id`.
  99. //
  100. // **Note:** If updating an existing env group, the linked releases with the env group will all be updated as well.
  101. //
  102. // ---
  103. // produces:
  104. // - application/json
  105. // summary: Create or update an env group
  106. // tags:
  107. // - Env groups
  108. // parameters:
  109. // - name: project_id
  110. // - name: cluster_id
  111. // - name: namespace
  112. // - in: body
  113. // name: CreateEnvGroupRequest
  114. // description: The env group to create or update
  115. // schema:
  116. // $ref: '#/definitions/CreateEnvGroupRequest'
  117. // responses:
  118. // '200':
  119. // description: Successfully created a new namespace
  120. // schema:
  121. // $ref: '#/definitions/V1EnvGroupResponse'
  122. // '403':
  123. // description: Forbidden
  124. createOrUpdateEnvGroupEndpoint := factory.NewAPIEndpoint(
  125. &types.APIRequestMetadata{
  126. Verb: types.APIVerbUpdate,
  127. Method: types.HTTPVerbPut,
  128. Path: &types.Path{
  129. Parent: basePath,
  130. RelativePath: relPath,
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. types.ClusterScope,
  136. types.NamespaceScope,
  137. },
  138. },
  139. )
  140. createOrUpdateEnvGroupHandler := v1EnvGroup.NewCreateEnvGroupHandler(
  141. config,
  142. factory.GetDecoderValidator(),
  143. factory.GetResultWriter(),
  144. )
  145. routes = append(routes, &router.Route{
  146. Endpoint: createOrUpdateEnvGroupEndpoint,
  147. Handler: createOrUpdateEnvGroupHandler,
  148. Router: r,
  149. })
  150. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/versions/{version} -> env_group.NewGetEnvGroupHandler
  151. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/versions/{version} getEnvGroup
  152. //
  153. // Gets an env group denoted by `name` and `version` in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  154. // `cluster_id`, which in turn should belong to the project denoted by `project_id`. **Note:** To get the latest version of an env group, set `version` to `0`.
  155. //
  156. // ---
  157. // produces:
  158. // - application/json
  159. // summary: Get an env group
  160. // tags:
  161. // - Env groups
  162. // parameters:
  163. // - name: project_id
  164. // - name: cluster_id
  165. // - name: namespace
  166. // - name: name
  167. // - name: version
  168. // responses:
  169. // '200':
  170. // description: Successfully fetched the env group
  171. // schema:
  172. // $ref: '#/definitions/V1EnvGroupResponse'
  173. // '403':
  174. // description: Forbidden
  175. // '404':
  176. // description: Env group not found
  177. getEnvGroupEndpoint := factory.NewAPIEndpoint(
  178. &types.APIRequestMetadata{
  179. Verb: types.APIVerbGet,
  180. Method: types.HTTPVerbGet,
  181. Path: &types.Path{
  182. Parent: basePath,
  183. RelativePath: fmt.Sprintf("%s/{%s}/versions/{%s}", relPath, types.URLParamEnvGroupName,
  184. types.URLParamEnvGroupVersion),
  185. },
  186. Scopes: []types.PermissionScope{
  187. types.UserScope,
  188. types.ProjectScope,
  189. types.ClusterScope,
  190. types.NamespaceScope,
  191. },
  192. },
  193. )
  194. getEnvGroupHandler := v1EnvGroup.NewGetEnvGroupHandler(
  195. config,
  196. factory.GetDecoderValidator(),
  197. factory.GetResultWriter(),
  198. )
  199. routes = append(routes, &router.Route{
  200. Endpoint: getEnvGroupEndpoint,
  201. Handler: getEnvGroupHandler,
  202. Router: r,
  203. })
  204. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name} -> env_group.NewGetEnvGroupAllVersionsHandler
  205. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name} getEnvGroupAllVersions
  206. //
  207. // Gets all versions of an env group denoted by `name` in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  208. // `cluster_id`, which in turn should belong to the project denoted by `project_id`.
  209. //
  210. // ---
  211. // produces:
  212. // - application/json
  213. // summary: Get all versions of an env group
  214. // tags:
  215. // - Env groups
  216. // parameters:
  217. // - name: project_id
  218. // - name: cluster_id
  219. // - name: namespace
  220. // - name: name
  221. // responses:
  222. // '200':
  223. // description: Successfully fetched the env group
  224. // schema:
  225. // $ref: '#/definitions/V1EnvGroupsAllVersionsResponse'
  226. // '403':
  227. // description: Forbidden
  228. // '404':
  229. // description: Env group not found
  230. getEnvGroupAllVersionsEndpoint := factory.NewAPIEndpoint(
  231. &types.APIRequestMetadata{
  232. Verb: types.APIVerbGet,
  233. Method: types.HTTPVerbGet,
  234. Path: &types.Path{
  235. Parent: basePath,
  236. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamEnvGroupName),
  237. },
  238. Scopes: []types.PermissionScope{
  239. types.UserScope,
  240. types.ProjectScope,
  241. types.ClusterScope,
  242. types.NamespaceScope,
  243. },
  244. },
  245. )
  246. getEnvGroupAllVersionsHandler := v1EnvGroup.NewGetEnvGroupAllVersionsHandler(
  247. config,
  248. factory.GetDecoderValidator(),
  249. factory.GetResultWriter(),
  250. )
  251. routes = append(routes, &router.Route{
  252. Endpoint: getEnvGroupAllVersionsEndpoint,
  253. Handler: getEnvGroupAllVersionsHandler,
  254. Router: r,
  255. })
  256. // GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups -> namespace.NewListEnvGroupsHandler
  257. // swagger:operation GET /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups listAllEnvGroups
  258. //
  259. // Gets all env groups in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  260. // `cluster_id`, which in turn should belong to the project denoted by `project_id`.
  261. //
  262. // ---
  263. // produces:
  264. // - application/json
  265. // summary: Get all env groups
  266. // tags:
  267. // - Env groups
  268. // parameters:
  269. // - name: project_id
  270. // - name: cluster_id
  271. // - name: namespace
  272. // responses:
  273. // '200':
  274. // description: Successfully fetched the env group
  275. // schema:
  276. // $ref: '#/definitions/V1ListAllEnvGroupsResponse'
  277. // '403':
  278. // description: Forbidden
  279. listEnvGroupsEndpoint := factory.NewAPIEndpoint(
  280. &types.APIRequestMetadata{
  281. Verb: types.APIVerbGet,
  282. Method: types.HTTPVerbGet,
  283. Path: &types.Path{
  284. Parent: basePath,
  285. RelativePath: relPath,
  286. },
  287. Scopes: []types.PermissionScope{
  288. types.UserScope,
  289. types.ProjectScope,
  290. types.ClusterScope,
  291. types.NamespaceScope,
  292. },
  293. },
  294. )
  295. listEnvGroupsHandler := v1EnvGroup.NewListEnvGroupsHandler(
  296. config,
  297. factory.GetResultWriter(),
  298. )
  299. routes = append(routes, &router.Route{
  300. Endpoint: listEnvGroupsEndpoint,
  301. Handler: listEnvGroupsHandler,
  302. Router: r,
  303. })
  304. // DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name} -> env_group.NewDeleteEnvGroupHandler
  305. // swagger:operation DELETE /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name} deleteEnvGroup
  306. //
  307. // Deletes the env group denoted by `name` in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  308. // `cluster_id`, which in turn should belong to the project denoted by `project_id`.
  309. //
  310. // ---
  311. // produces:
  312. // - application/json
  313. // summary: Delete an env group
  314. // tags:
  315. // - Env groups
  316. // parameters:
  317. // - name: project_id
  318. // - name: cluster_id
  319. // - name: namespace
  320. // - name: name
  321. // responses:
  322. // '200':
  323. // description: Successfully deleted the env group
  324. // '403':
  325. // description: Forbidden
  326. // '412':
  327. // description: Env group is linked to one or more releases
  328. deleteEnvGroupEndpoint := factory.NewAPIEndpoint(
  329. &types.APIRequestMetadata{
  330. Verb: types.APIVerbDelete,
  331. Method: types.HTTPVerbDelete,
  332. Path: &types.Path{
  333. Parent: basePath,
  334. RelativePath: fmt.Sprintf("%s/{%s}", relPath, types.URLParamEnvGroupName),
  335. },
  336. Scopes: []types.PermissionScope{
  337. types.UserScope,
  338. types.ProjectScope,
  339. types.ClusterScope,
  340. types.NamespaceScope,
  341. },
  342. },
  343. )
  344. deleteEnvGroupHandler := v1EnvGroup.NewDeleteEnvGroupHandler(
  345. config,
  346. factory.GetDecoderValidator(),
  347. factory.GetResultWriter(),
  348. )
  349. routes = append(routes, &router.Route{
  350. Endpoint: deleteEnvGroupEndpoint,
  351. Handler: deleteEnvGroupHandler,
  352. Router: r,
  353. })
  354. // PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/add_release -> env_group.NewAddReleaseToEnvGroupHandler
  355. // swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/add_release addReleaseToEnvGroup
  356. //
  357. // Adds a release to the env group denoted by `name` in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  358. // `cluster_id`, which in turn should belong to the project denoted by `project_id`.
  359. //
  360. // ---
  361. // produces:
  362. // - application/json
  363. // summary: Add a release to an env group
  364. // tags:
  365. // - Env groups
  366. // parameters:
  367. // - name: project_id
  368. // - name: cluster_id
  369. // - name: namespace
  370. // - name: name
  371. // - in: body
  372. // name: V1EnvGroupReleaseRequest
  373. // description: The name of the release to add
  374. // schema:
  375. // $ref: '#/definitions/V1EnvGroupReleaseRequest'
  376. // responses:
  377. // '200':
  378. // description: Successfully added the release
  379. // '403':
  380. // description: Forbidden
  381. // '404':
  382. // description: Env group not found
  383. addReleaseToEnvGroupEndpoint := factory.NewAPIEndpoint(
  384. &types.APIRequestMetadata{
  385. Verb: types.APIVerbUpdate,
  386. Method: types.HTTPVerbPatch,
  387. Path: &types.Path{
  388. Parent: basePath,
  389. RelativePath: fmt.Sprintf("%s/{%s}/add_release", relPath, types.URLParamEnvGroupName),
  390. },
  391. Scopes: []types.PermissionScope{
  392. types.UserScope,
  393. types.ProjectScope,
  394. types.ClusterScope,
  395. types.NamespaceScope,
  396. },
  397. },
  398. )
  399. addReleaseToEnvGroupHandler := v1EnvGroup.NewAddReleaseToEnvGroupHandler(
  400. config,
  401. factory.GetDecoderValidator(),
  402. factory.GetResultWriter(),
  403. )
  404. routes = append(routes, &router.Route{
  405. Endpoint: addReleaseToEnvGroupEndpoint,
  406. Handler: addReleaseToEnvGroupHandler,
  407. Router: r,
  408. })
  409. // PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/remove_release -> env_group.NewRemoveReleaseFromEnvGroupHandler
  410. // swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/env_groups/{name}/remove_release removeReleaseFromEnvGroup
  411. //
  412. // Removes a release from the env group denoted by `name` in the namespace denoted by `namespace`. The namespace should belong to the cluster denoted by
  413. // `cluster_id`, which in turn should belong to the project denoted by `project_id`.
  414. //
  415. // ---
  416. // produces:
  417. // - application/json
  418. // summary: Remove a release from an env group
  419. // tags:
  420. // - Env groups
  421. // parameters:
  422. // - name: project_id
  423. // - name: cluster_id
  424. // - name: namespace
  425. // - name: name
  426. // - in: body
  427. // name: V1EnvGroupReleaseRequest
  428. // description: The name of the release to remove
  429. // schema:
  430. // $ref: '#/definitions/V1EnvGroupReleaseRequest'
  431. // responses:
  432. // '200':
  433. // description: Successfully removed the release
  434. // '403':
  435. // description: Forbidden
  436. // '404':
  437. // description: Env group not found
  438. removeReleaseFromEnvGroupEndpoint := factory.NewAPIEndpoint(
  439. &types.APIRequestMetadata{
  440. Verb: types.APIVerbUpdate,
  441. Method: types.HTTPVerbPatch,
  442. Path: &types.Path{
  443. Parent: basePath,
  444. RelativePath: fmt.Sprintf("%s/{%s}/remove_release", relPath, types.URLParamEnvGroupName),
  445. },
  446. Scopes: []types.PermissionScope{
  447. types.UserScope,
  448. types.ProjectScope,
  449. types.ClusterScope,
  450. types.NamespaceScope,
  451. },
  452. },
  453. )
  454. removeReleaseFromEnvGroupHandler := v1EnvGroup.NewRemoveReleaseFromEnvGroupHandler(
  455. config,
  456. factory.GetDecoderValidator(),
  457. factory.GetResultWriter(),
  458. )
  459. routes = append(routes, &router.Route{
  460. Endpoint: removeReleaseFromEnvGroupEndpoint,
  461. Handler: removeReleaseFromEnvGroupHandler,
  462. Router: r,
  463. })
  464. return routes, newPath
  465. }