ConnectionSchemaPlugin.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import type { Schema } from '@src/@types/Schema'
  15. import type { Field } from '@src/@types/Field'
  16. import type { Endpoint } from '@src/@types/Endpoint'
  17. import ConnectionSchemaParserBase from '@src/plugins/default/ConnectionSchemaPlugin'
  18. const customSort = (fields: Field[]) => {
  19. const sortPriority: any = {
  20. name: 1,
  21. mapped_regions: 1.5,
  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 extends ConnectionSchemaParserBase {
  47. override parseSchemaToFields(schema: Schema): Field[] {
  48. const fields = super.parseSchemaToFields(schema)
  49. const identityField = fields.find(f => f.name === 'identity_api_version')
  50. if (identityField && !identityField.default) {
  51. identityField.default = identityField.minimum
  52. }
  53. fields.find(f => f.name === 'ceph_options')?.properties?.forEach(f => { f.name = `ceph_options/${f.name}` })
  54. const createInputChoice = (name: string, field1Name: string, field2Name: string) => {
  55. const field1 = fields.find(f => f.name === field1Name)
  56. const field2 = fields.find(f => f.name === field2Name)
  57. if (field1 && field2) {
  58. field1.label = 'Name'
  59. field2.label = 'ID'
  60. const field: Field = {
  61. name,
  62. type: 'input-choice',
  63. items: [field1, field2],
  64. }
  65. fields.push(field)
  66. }
  67. }
  68. createInputChoice('project_domain', 'project_domain_name', 'project_domain_id')
  69. createInputChoice('user_domain', 'user_domain_name', 'user_domain_id')
  70. customSort(fields)
  71. fields.push({
  72. name: 'openstack_use_current_user',
  73. type: 'boolean',
  74. })
  75. return fields
  76. }
  77. override parseConnectionInfoToPayload(data: { [prop: string]: any }, schema: Schema) {
  78. if (data.openstack_use_current_user) {
  79. return {}
  80. }
  81. // eslint-disable-next-line no-param-reassign
  82. delete data.project_domain
  83. // eslint-disable-next-line no-param-reassign
  84. delete data.user_domain
  85. const payload = super.parseConnectionInfoToPayload(data, schema)
  86. return payload
  87. }
  88. override parseConnectionResponse(endpoint: Endpoint) {
  89. if (!endpoint.connection_info || Object.keys(endpoint.connection_info).length === 0) {
  90. return {
  91. openstack_use_current_user: true,
  92. ...endpoint,
  93. }
  94. }
  95. return endpoint
  96. }
  97. }