InstanceSource.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import Api from '../utils/ApiCaller'
  16. import type { Instance } from '../types/Instance'
  17. import { servicesUrl } from '../config'
  18. class InstanceSource {
  19. static loadInstancesChunk(
  20. endpointId: string,
  21. chunkSize: number,
  22. lastInstanceId?: string,
  23. cancelId?: string,
  24. searchText?: string
  25. ): Promise<Instance[]> {
  26. let url = `${servicesUrl.coriolis}/${Api.projectId}/endpoints/${endpointId}/instances`
  27. url = `${url}?limit=${chunkSize}`
  28. if (lastInstanceId) {
  29. url = `${url}&marker=${lastInstanceId}`
  30. }
  31. if (searchText) {
  32. url = `${url}&name=${searchText}`
  33. }
  34. return Api.send({ url, cancelId }).then(response => {
  35. return response.data.instances
  36. })
  37. }
  38. static loadInstances(endpointId: string): Promise<Instance[]> {
  39. Api.cancelRequests(endpointId)
  40. let url = `${servicesUrl.coriolis}/${Api.projectId}/endpoints/${endpointId}/instances`
  41. return Api.send({ url, cancelId: endpointId }).then(response => {
  42. return response.data.instances
  43. })
  44. }
  45. static loadInstanceDetails(endpointId: string, instanceName: string, reqId: number, quietError?: boolean): Promise<{ instance: Instance, reqId: number }> {
  46. return Api.send({
  47. url: `${servicesUrl.coriolis}/${Api.projectId}/endpoints/${endpointId}/instances/${btoa(instanceName)}`,
  48. cancelId: `instanceDetail-${reqId}`,
  49. quietError,
  50. }).then(response => {
  51. return { instance: response.data.instance, reqId }
  52. })
  53. }
  54. static cancelInstancesDetailsRequests(reqId: number) {
  55. Api.cancelRequests(`instanceDetail-${reqId}`)
  56. }
  57. }
  58. export default InstanceSource