SchemaPlugin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, SchemaProperties, SchemaDefinitions } from '../../../types/Schema'
  16. import type { Field } from '../../../types/Field'
  17. export const defaultSchemaToFields = (schema: SchemaProperties, schemaDefinitions?: ?SchemaDefinitions, parent?: string): any[] => {
  18. let fields = Object.keys(schema.properties).map(fieldName => {
  19. let properties = schema.properties[fieldName]
  20. if (typeof schema.properties[fieldName].$ref === 'string' && schemaDefinitions) {
  21. const definitionName = schema.properties[fieldName].$ref.substr(schema.properties[fieldName].$ref.lastIndexOf('/') + 1)
  22. properties = schemaDefinitions[definitionName]
  23. return {
  24. name: fieldName,
  25. type: properties.type ? properties.type : '',
  26. properties: properties.properties ? defaultSchemaToFields(properties, null, fieldName) : [],
  27. }
  28. }
  29. return {
  30. ...properties,
  31. name: parent ? `${parent}/${fieldName}` : fieldName,
  32. required: schema.required && schema.required.find(k => k === fieldName) ? true : fieldName === 'username' || fieldName === 'password',
  33. }
  34. })
  35. return fields
  36. }
  37. export const connectionSchemaToFields = (schema: SchemaProperties) => {
  38. let fields = defaultSchemaToFields(schema)
  39. let sortPriority = { username: 1, password: 2 }
  40. fields.sort((a, b) => {
  41. if (sortPriority[a.name] && sortPriority[b.name]) {
  42. return sortPriority[a.name] - sortPriority[b.name]
  43. }
  44. if (sortPriority[a.name] || (a.required && !b.required)) {
  45. return -1
  46. }
  47. if (sortPriority[b.name] || (!a.required && b.required)) {
  48. return 1
  49. }
  50. return a.name.localeCompare(b.name)
  51. })
  52. return fields
  53. }
  54. export const generateField = (name: string, label: string, required: boolean = false, type: string = 'string', defaultValue: any = null) => {
  55. let field = {
  56. name,
  57. label,
  58. type,
  59. required,
  60. default: undefined,
  61. }
  62. if (defaultValue) {
  63. field.default = defaultValue
  64. }
  65. return field
  66. }
  67. export const fieldsToPayload = (data: { [string]: mixed }, schema: SchemaProperties) => {
  68. let info = {}
  69. Object.keys(schema.properties).forEach(fieldName => {
  70. if (data[fieldName] && typeof data[fieldName] !== 'object') {
  71. info[fieldName] = data[fieldName]
  72. }
  73. })
  74. return info
  75. }
  76. export default class ConnectionSchemaParser {
  77. static parseSchemaToFields(schema: Schema): Field[] {
  78. let fields = connectionSchemaToFields(schema.oneOf[0])
  79. fields = [
  80. generateField('name', 'Endpoint Name', true),
  81. generateField('description', 'Endpoint Description'),
  82. ...fields,
  83. ]
  84. return fields
  85. }
  86. static parseFieldsToPayload(data: { [string]: mixed }, schema: Schema) {
  87. let payload = {}
  88. payload.name = data.name
  89. payload.description = data.description
  90. payload.connection_info = fieldsToPayload(data, schema.oneOf[0])
  91. if (data.secret_ref) {
  92. payload.connection_info.secret_ref = data.secret_ref
  93. }
  94. return payload
  95. }
  96. }