SchemaPlugin.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 DefaultConnectionSchemaParser from '../default/SchemaPlugin'
  18. const customSort = (fields: Field[]) => {
  19. const sortPriority = {
  20. name: 1,
  21. description: 2,
  22. username: 3,
  23. password: 4,
  24. auth_url: 5,
  25. project_name: 6,
  26. glance_api_version: 7,
  27. identity_api_version: 8,
  28. project_domain: 9,
  29. user_domain: 10,
  30. }
  31. fields.sort((a, b) => {
  32. if (sortPriority[a.name] && sortPriority[b.name]) {
  33. return sortPriority[a.name] - sortPriority[b.name]
  34. }
  35. if (sortPriority[a.name]) {
  36. return -1
  37. }
  38. if (sortPriority[b.name]) {
  39. return 1
  40. }
  41. return a.name.localeCompare(b.name)
  42. })
  43. return fields
  44. }
  45. export default class ConnectionSchemaParser {
  46. static parseSchemaToFields(schema: Schema): Field[] {
  47. let fields = DefaultConnectionSchemaParser.parseSchemaToFields(schema)
  48. let identityField = fields.find(f => f.name === 'identity_api_version')
  49. if (identityField && !identityField.default) {
  50. identityField.default = identityField.minimum
  51. }
  52. let createInputChoice = (name: string, field1Name: string, field2Name: string) => {
  53. let field1 = fields.find(f => f.name === field1Name)
  54. let field2 = fields.find(f => f.name === field2Name)
  55. if (field1 && field2) {
  56. field1.label = 'Name'
  57. field2.label = 'ID'
  58. let field: Field = {
  59. name,
  60. type: 'input-choice',
  61. items: [field1, field2],
  62. }
  63. fields.push(field)
  64. }
  65. }
  66. createInputChoice('project_domain', 'project_domain_name', 'project_domain_id')
  67. createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
  68. customSort(fields)
  69. return fields
  70. }
  71. static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
  72. delete data.project_domain
  73. delete data.user_domain
  74. let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
  75. return payload
  76. }
  77. }