SchemaPlugin.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_name: 9,
  29. project_domain_id: 10,
  30. user_domain_name: 11,
  31. user_domain_id: 12,
  32. }
  33. fields.sort((a, b) => {
  34. if (sortPriority[a.name] && sortPriority[b.name]) {
  35. return sortPriority[a.name] - sortPriority[b.name]
  36. }
  37. if (sortPriority[a.name]) {
  38. return -1
  39. }
  40. if (sortPriority[b.name]) {
  41. return 1
  42. }
  43. return a.name.localeCompare(b.name)
  44. })
  45. return fields
  46. }
  47. export default class ConnectionSchemaParser {
  48. static parseSchemaToFields(schema: Schema): Field[] {
  49. let fields = DefaultConnectionSchemaParser.parseSchemaToFields(schema)
  50. let identityField = fields.find(f => f.name === 'identity_api_version')
  51. if (identityField && !identityField.default) {
  52. identityField.default = identityField.minimum
  53. }
  54. customSort(fields)
  55. let projectDomainField = fields.find(f => f.name === 'project_domain_name')
  56. let projectDomainIdField = fields.find(f => f.name === 'project_domain_id')
  57. let userDomainField = fields.find(f => f.name === 'user_domain_name')
  58. let userDomainIdField = fields.find(f => f.name === 'user_domain_id')
  59. let glanceApiVersionField = fields.find(f => f.name === 'glance_api_version')
  60. let identityApiVersionField = fields.find(f => f.name === 'identity_api_version')
  61. let isBasicFunc = (apiVersion: number) => apiVersion > 2
  62. if (
  63. projectDomainField &&
  64. userDomainField &&
  65. glanceApiVersionField &&
  66. identityApiVersionField &&
  67. userDomainIdField &&
  68. projectDomainIdField
  69. ) {
  70. projectDomainField.isBasic = isBasicFunc
  71. projectDomainIdField.isBasic = isBasicFunc
  72. userDomainField.isBasic = isBasicFunc
  73. userDomainIdField.isBasic = isBasicFunc
  74. glanceApiVersionField.isBasic = true
  75. glanceApiVersionField.isBasic = true
  76. }
  77. return fields
  78. }
  79. static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
  80. let payload = DefaultConnectionSchemaParser.parseFieldsToPayload(data, schema)
  81. return payload
  82. }
  83. }