api.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import axios from 'axios';
  2. import { baseApi } from './baseApi';
  3. import { StorageType } from './types';
  4. /**
  5. * Generic api call format
  6. * @param {string} token - Bearer token.
  7. * @param {Object} params - Body params.
  8. * @param {Object} pathParams - Path params.
  9. * @param {(err: Object, res: Object) => void} callback - Callback function.
  10. */
  11. const checkAuth = baseApi('GET', '/api/auth/check');
  12. const registerUser = baseApi<{
  13. email: string,
  14. password: string
  15. }>('POST', '/api/users');
  16. const logInUser = baseApi<{
  17. email: string,
  18. password: string
  19. }>('POST', '/api/login');
  20. const logOutUser = baseApi('POST', '/api/logout');
  21. const getUser = baseApi<{}, { id: number }>('GET', pathParams => {
  22. return `/api/users/${pathParams.id}`;
  23. });
  24. const updateUser = baseApi<{
  25. rawKubeConfig?: string,
  26. allowedContexts?: string[]
  27. }, { id: number }>('PUT', pathParams => {
  28. return `/api/users/${pathParams.id}`;
  29. });
  30. const getClusters = baseApi<{}, { id: number }>('GET', pathParams => {
  31. return `/api/projects/${pathParams.id}/clusters`;
  32. });
  33. const getCharts = baseApi<{
  34. namespace: string,
  35. cluster_id: number,
  36. storage: StorageType,
  37. limit: number,
  38. skip: number,
  39. byDate: boolean,
  40. statusFilter: string[]
  41. }, { id: number }>('GET', pathParams => {
  42. return `/api/projects/${pathParams.id}/releases`;
  43. });
  44. const getChart = baseApi<{
  45. namespace: string,
  46. cluster_id: number,
  47. storage: StorageType
  48. }, { id: number, name: string, revision: number }>('GET', pathParams => {
  49. return `/api/projects/${pathParams.id}/releases/${pathParams.name}/${pathParams.revision}`;
  50. });
  51. const getChartComponents = baseApi<{
  52. namespace: string,
  53. cluster_id: number,
  54. storage: StorageType
  55. }, { id: number, name: string, revision: number }>('GET', pathParams => {
  56. return `/api/projects/${pathParams.id}/releases/${pathParams.name}/${pathParams.revision}/components`;
  57. });
  58. const getChartControllers = baseApi<{
  59. namespace: string,
  60. cluster_id: number,
  61. storage: StorageType
  62. }, { id: number, name: string, revision: number }>('GET', pathParams => {
  63. return `/api/projects/${pathParams.id}/releases/${pathParams.name}/${pathParams.revision}/controllers`;
  64. });
  65. const getNamespaces = baseApi<{
  66. cluster_id: number,
  67. }, { id: number }>('GET', pathParams => {
  68. return `/api/projects/${pathParams.id}/k8s/namespaces`;
  69. });
  70. const getMatchingPods = baseApi<{
  71. cluster_id: number,
  72. selectors: string[]
  73. }, { id: number }>('GET', pathParams => {
  74. return `/api/projects/${pathParams.id}/k8s/pods`;
  75. });
  76. const getIngress = baseApi<{
  77. cluster_id: number,
  78. }, { name: string, namespace: string, id: number }>('GET', pathParams => {
  79. return `/api/projects/${pathParams.id}/k8s/${pathParams.namespace}/ingress/${pathParams.name}`;
  80. });
  81. const getRevisions = baseApi<{
  82. namespace: string,
  83. cluster_id: number,
  84. storage: StorageType
  85. }, { id: number, name: string }>('GET', pathParams => {
  86. return `/api/projects/${pathParams.id}/releases/${pathParams.name}/history`;
  87. });
  88. const rollbackChart = baseApi<{
  89. namespace: string,
  90. storage: StorageType,
  91. revision: number
  92. }, {
  93. id: number,
  94. name: string,
  95. cluster_id: number,
  96. }>('POST', pathParams => {
  97. let { id, name, cluster_id } = pathParams;
  98. return `/api/projects/${id}/releases/${name}/rollback?cluster_id=${cluster_id}`;
  99. });
  100. const upgradeChartValues = baseApi<{
  101. namespace: string,
  102. storage: StorageType,
  103. values: string
  104. }, {
  105. id: number,
  106. name: string,
  107. cluster_id: number,
  108. }>('POST', pathParams => {
  109. let { id, name, cluster_id } = pathParams;
  110. return `/api/projects/${id}/releases/${name}/upgrade?cluster_id=${cluster_id}`;
  111. });
  112. const getTemplates = baseApi('GET', '/api/templates');
  113. const getTemplateInfo = baseApi<{}, { name: string, version: string }>('GET', pathParams => {
  114. return `/api/templates/${pathParams.name}/${pathParams.version}`;
  115. });
  116. const getRepos = baseApi<{}, { id: number }>('GET', pathParams => {
  117. return `/api/projects/${pathParams.id}/repos`;
  118. });
  119. const getBranches = baseApi<{}, { kind: string, repo: string }>('GET', pathParams => {
  120. return `/api/repos/${pathParams.kind}/${pathParams.repo}/branches`;
  121. });
  122. const getBranchContents = baseApi<{
  123. dir: string
  124. }, {
  125. kind: string,
  126. repo: string,
  127. branch: string
  128. }>('GET', pathParams => {
  129. return `/api/repos/github/${pathParams.repo}/${pathParams.branch}/contents`;
  130. });
  131. const getProjects = baseApi<{}, { id: number }>('GET', pathParams => {
  132. return `/api/users/${pathParams.id}/projects`;
  133. });
  134. const getReleaseToken = baseApi<{
  135. namespace: string,
  136. cluster_id: number,
  137. storage: StorageType,
  138. }, { name: string, id: number }>('GET', pathParams => {
  139. return `/api/projects/${pathParams.id}/releases/${pathParams.name}/webhook_token`;
  140. });
  141. const createProject = baseApi<{ name: string }, {}>('POST', pathParams => {
  142. return `/api/projects`;
  143. });
  144. const deleteProject = baseApi<{}, { id: number }>('DELETE', pathParams => {
  145. return `/api/projects/${pathParams.id}`;
  146. });
  147. const deployTemplate = baseApi<{
  148. templateName: string,
  149. imageURL: string,
  150. formValues: any,
  151. storage: StorageType,
  152. namespace: string,
  153. name: string,
  154. }, {
  155. id: number,
  156. cluster_id: number,
  157. name: string,
  158. version: string
  159. }>('POST', pathParams => {
  160. let { cluster_id, id, name, version } = pathParams;
  161. return `/api/projects/${id}/deploy/${name}/${version}?cluster_id=${cluster_id}`;
  162. });
  163. const getClusterIntegrations = baseApi('GET', '/api/integrations/cluster');
  164. const getRegistryIntegrations = baseApi('GET', '/api/integrations/registry');
  165. const getRepoIntegrations = baseApi('GET', '/api/integrations/repo');
  166. const getProjectClusters = baseApi<{}, { id: number }>('GET', pathParams => {
  167. return `/api/projects/${pathParams.id}/clusters`;
  168. });
  169. const getProjectRegistries = baseApi<{}, { id: number }>('GET', pathParams => {
  170. return `/api/projects/${pathParams.id}/registries`;
  171. });
  172. const getProjectRepos = baseApi<{}, { id: number }>('GET', pathParams => {
  173. return `/api/projects/${pathParams.id}/repos`;
  174. });
  175. const createAWSIntegration = baseApi<{
  176. aws_region: string,
  177. aws_cluster_id?: string,
  178. aws_access_key_id: string,
  179. aws_secret_access_key: string,
  180. }, { id: number }>('POST', pathParams => {
  181. return `/api/projects/${pathParams.id}/integrations/aws`;
  182. });
  183. const provisionECR = baseApi<{
  184. ecr_name: string,
  185. aws_integration_id: string,
  186. }, { id: number }>('POST', pathParams => {
  187. return `/api/projects/${pathParams.id}/provision/ecr`;
  188. });
  189. const provisionEKS = baseApi<{
  190. eks_name: string,
  191. aws_integration_id: string,
  192. }, { id: number }>('POST', pathParams => {
  193. return `/api/projects/${pathParams.id}/provision/eks`;
  194. });
  195. const createECR = baseApi<{
  196. name: string,
  197. aws_integration_id: string,
  198. }, { id: number }>('POST', pathParams => {
  199. return `/api/projects/${pathParams.id}/registries`;
  200. });
  201. const getImageRepos = baseApi<{
  202. }, {
  203. project_id: number,
  204. registry_id: number
  205. }>('GET', pathParams => {
  206. return `/api/projects/${pathParams.project_id}/registries/${pathParams.registry_id}/repositories`;
  207. });
  208. const getImageTags = baseApi<{
  209. }, {
  210. project_id: number,
  211. registry_id: number,
  212. repo_name: string,
  213. }>('GET', pathParams => {
  214. return `/api/projects/${pathParams.project_id}/registries/${pathParams.registry_id}/repositories/${pathParams.repo_name}`;
  215. });
  216. const linkGithubProject = baseApi<{
  217. }, {
  218. project_id: number,
  219. }>('GET', pathParams => {
  220. return `/api/oauth/projects/${pathParams.project_id}/github`;
  221. });
  222. const getGitRepos = baseApi<{
  223. }, {
  224. project_id: number,
  225. }>('GET', pathParams => {
  226. return `/api/projects/${pathParams.project_id}/gitrepos`;
  227. });
  228. const getInfra = baseApi<{
  229. }, {
  230. project_id: number,
  231. }>('GET', pathParams => {
  232. return `/api/projects/${pathParams.project_id}/infra`;
  233. });
  234. const destroyCluster = baseApi<{
  235. eks_name: string,
  236. }, {
  237. project_id: number,
  238. infra_id: number,
  239. }>('POST', pathParams => {
  240. return `/api/projects/${pathParams.project_id}/infra/${pathParams.infra_id}/eks/destroy`;
  241. });
  242. const deleteCluster = baseApi<{
  243. }, {
  244. project_id: number,
  245. cluster_id: number,
  246. }>('DELETE', pathParams => {
  247. return `/api/projects/${pathParams.project_id}/clusters/${pathParams.cluster_id}`;
  248. })
  249. // Bundle export to allow default api import (api.<method> is more readable)
  250. export default {
  251. deleteCluster,
  252. destroyCluster,
  253. getInfra,
  254. linkGithubProject,
  255. getGitRepos,
  256. checkAuth,
  257. registerUser,
  258. logInUser,
  259. logOutUser,
  260. getRepos,
  261. getUser,
  262. updateUser,
  263. getClusters,
  264. getCharts,
  265. getChart,
  266. getChartComponents,
  267. getChartControllers,
  268. getNamespaces,
  269. getMatchingPods,
  270. getIngress,
  271. getRevisions,
  272. rollbackChart,
  273. upgradeChartValues,
  274. getTemplates,
  275. getTemplateInfo,
  276. getBranches,
  277. getBranchContents,
  278. getProjects,
  279. getReleaseToken,
  280. createProject,
  281. deleteProject,
  282. deployTemplate,
  283. getClusterIntegrations,
  284. getRegistryIntegrations,
  285. getRepoIntegrations,
  286. getProjectClusters,
  287. getProjectRegistries,
  288. getProjectRepos,
  289. createAWSIntegration,
  290. provisionECR,
  291. provisionEKS,
  292. createECR,
  293. getImageRepos,
  294. getImageTags,
  295. }