ConnectionSchemaPlugin.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 type { Schema } from '../../../types/Schema'
  16. import type { Field } from '../../../types/Field'
  17. import type { Endpoint } from '../../../types/Endpoint'
  18. import DefaultConnectionSchemaParser from '../default/ConnectionSchemaPlugin'
  19. const customSort = (fields: Field[]) => {
  20. const sortPriority = {
  21. name: 1,
  22. description: 2,
  23. username: 3,
  24. password: 4,
  25. auth_url: 5,
  26. project_name: 6,
  27. glance_api_version: 7,
  28. identity_api_version: 8,
  29. project_domain: 9,
  30. user_domain: 10,
  31. }
  32. fields.sort((a, b) => {
  33. if (sortPriority[a.name] && sortPriority[b.name]) {
  34. return sortPriority[a.name] - sortPriority[b.name]
  35. }
  36. if (sortPriority[a.name]) {
  37. return -1
  38. }
  39. if (sortPriority[b.name]) {
  40. return 1
  41. }
  42. return a.name.localeCompare(b.name)
  43. })
  44. return fields
  45. }
  46. export default class ConnectionSchemaParser {
  47. static parseSchemaToFields(schema: Schema): Field[] {
  48. let fields = DefaultConnectionSchemaParser.parseSchemaToFields(schema)
  49. let identityField = fields.find(f => f.name === 'identity_api_version')
  50. if (identityField && !identityField.default) {
  51. identityField.default = identityField.minimum
  52. }
  53. let createInputChoice = (name: string, field1Name: string, field2Name: string) => {
  54. let field1 = fields.find(f => f.name === field1Name)
  55. let field2 = fields.find(f => f.name === field2Name)
  56. if (field1 && field2) {
  57. field1.label = 'Name'
  58. field2.label = 'ID'
  59. let field: Field = {
  60. name,
  61. type: 'input-choice',
  62. items: [field1, field2],
  63. }
  64. fields.push(field)
  65. }
  66. }
  67. createInputChoice('project_domain', 'project_domain_name', 'project_domain_id')
  68. createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
  69. customSort(fields)
  70. fields.push({
  71. name: 'openstack_use_current_user',
  72. type: 'boolean',
  73. })
  74. return fields
  75. }
  76. static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
  77. if (data.openstack_use_current_user) {
  78. return {
  79. name: data.name,
  80. description: data.description,
  81. connection_info: {},
  82. }
  83. }
  84. delete data.project_domain
  85. delete data.user_domain
  86. let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
  87. return payload
  88. }
  89. static parseConnectionResponse(endpoint: Endpoint) {
  90. if (!endpoint.connection_info || Object.keys(endpoint.connection_info).length === 0) {
  91. return {
  92. openstack_use_current_user: true,
  93. ...endpoint,
  94. }
  95. }
  96. return endpoint
  97. }
  98. }