ProjectSource.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 UserSource from './UserSource'
  17. import { servicesUrl, coriolisUrl } from '../constants'
  18. import type { Project, Role, RoleAssignment } from '../types/Project'
  19. import type { User } from '../types/User'
  20. class ProjectsSource {
  21. static getProjects(): Promise<Project[]> {
  22. return Api.get(servicesUrl.projects).then((response) => {
  23. if (response.data.projects) {
  24. let projects: Project[] = response.data.projects
  25. projects.sort((a, b) => a.name.localeCompare(b.name))
  26. return projects
  27. }
  28. return []
  29. })
  30. }
  31. static getProjectDetails(projectId: string): Promise<Project> {
  32. return Api.get(`${coriolisUrl}identity/projects/${projectId}`).then(response => {
  33. return response.data.project
  34. })
  35. }
  36. static getRoleAssignments(): Promise<RoleAssignment[]> {
  37. return Api.get(`${coriolisUrl}identity/role_assignments?include_names`).then(response => {
  38. let assignments: RoleAssignment[] = response.data.role_assignments
  39. assignments.sort((a1, a2) => a1.role.name.localeCompare(a2.role.name))
  40. return assignments
  41. })
  42. }
  43. static getUsers(projectId: string): Promise<User[]> {
  44. return this.getRoleAssignments().then(assignments => {
  45. const userIds: string[] = assignments
  46. .filter(a => a.scope.project.id === projectId)
  47. .filter((a, i, arr) => arr.findIndex(e => a.user.id === e.user.id) === i)
  48. .map(a => a.user.id)
  49. return Promise.all(userIds.map(id => {
  50. return UserSource.getUserInfo(id)
  51. })).then((users: User[]) => {
  52. users.sort((a, b) => a.name.localeCompare(b.name))
  53. return users
  54. })
  55. })
  56. }
  57. static removeUser(projectId: string, userId: string, roleIds: string[]): Promise<void> {
  58. return Promise.all(roleIds.map(id => {
  59. return Api.send({
  60. url: `${coriolisUrl}identity/projects/${projectId}/users/${userId}/roles/${id}`,
  61. method: 'DELETE',
  62. })
  63. })).then(() => { })
  64. }
  65. static assignUser(projectId: string, userId: string, roleId: string): Promise<void> {
  66. return Api.send({
  67. url: `${coriolisUrl}identity/projects/${projectId}/users/${userId}/roles/${roleId}`,
  68. method: 'PUT',
  69. }).then(() => { })
  70. }
  71. static getRoles(): Promise<Role[]> {
  72. return UserSource.getRoles()
  73. }
  74. static update(projectId: string, project: Project): Promise<Project> {
  75. let data = { project: {} }
  76. if (project.name != null) {
  77. data.project.name = project.name
  78. }
  79. if (project.description != null) {
  80. data.project.description = project.description
  81. }
  82. if (project.description != null) {
  83. data.project.enabled = project.enabled
  84. }
  85. return Api.send({
  86. url: `${coriolisUrl}identity/projects/${projectId}`,
  87. method: 'PATCH',
  88. data,
  89. }).then(response => response.data.project)
  90. }
  91. static delete(projectId: string): Promise<void> {
  92. return Api.send({
  93. url: `${coriolisUrl}identity/projects/${projectId}`,
  94. method: 'DELETE',
  95. }).then(() => { })
  96. }
  97. static add(project: Project, userId: string): Promise<Project> {
  98. let data = { project: {} }
  99. data.project.name = project.name
  100. if (project.enabled != null) {
  101. data.project.enabled = project.enabled
  102. }
  103. if (project.description != null) {
  104. data.project.description = project.description
  105. }
  106. let addedProject: Project
  107. return Api.send({
  108. url: `${coriolisUrl}identity/projects/`,
  109. method: 'POST',
  110. data,
  111. }).then(response => {
  112. addedProject = response.data.project
  113. return UserSource.getAdminRoleId()
  114. }).then(adminRoleId => {
  115. return UserSource.assignUserToProjectWithRole(userId, addedProject.id, adminRoleId)
  116. }).then(() => {
  117. return addedProject
  118. })
  119. }
  120. }
  121. export default ProjectsSource