ConnectionSchemaPlugin.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 {
  15. connectionSchemaToFields,
  16. defaultSchemaToFields,
  17. fieldsToPayload,
  18. generateBaseFields,
  19. } from '../default/ConnectionSchemaPlugin'
  20. import { Endpoint } from '../../@types/Endpoint'
  21. const fieldsToPayloadUseDefaults = (data: any, schema: { properties: any }) => {
  22. const info: any = {}
  23. Object.keys(schema.properties).forEach(fieldName => {
  24. if (typeof data[fieldName] === 'object') {
  25. return
  26. }
  27. if (data[fieldName]) {
  28. info[fieldName] = data[fieldName]
  29. } else if (schema.properties[fieldName].default) {
  30. info[fieldName] = schema.properties[fieldName].default
  31. }
  32. })
  33. return info
  34. }
  35. const azureConnectionParse = (
  36. schema: any,
  37. ) => {
  38. const commonFields = connectionSchemaToFields(schema).filter(f => f.type !== 'object' && f.name !== 'secret_ref' && Object.keys(f).findIndex(k => k === 'enum') === -1)
  39. const subscriptionIdField = commonFields.find(f => f.name === 'subscription_id')
  40. if (subscriptionIdField) {
  41. subscriptionIdField.required = true
  42. }
  43. const getOption = (name: string) => ({
  44. name,
  45. type: 'radio',
  46. fields: [
  47. ...connectionSchemaToFields(schema.properties[name]),
  48. ...commonFields,
  49. ],
  50. })
  51. const radioGroup = {
  52. name: 'login_type',
  53. type: 'radio-group',
  54. items: [getOption('user_credentials'), getOption('service_principal_credentials')],
  55. }
  56. const cloudProfileDropdown = {
  57. name: 'cloud_profile',
  58. type: 'string',
  59. required: true,
  60. ...schema.properties.cloud_profile,
  61. custom_cloud_fields: [
  62. ...defaultSchemaToFields(schema.properties.custom_cloud_properties.properties.endpoints),
  63. ...defaultSchemaToFields(schema.properties.custom_cloud_properties.properties.suffixes),
  64. ],
  65. }
  66. cloudProfileDropdown.custom_cloud_fields.sort(
  67. (a: { required: any; name: string }, b: { required: any; name: any }) => {
  68. if (a.required && !b.required) {
  69. return -1
  70. }
  71. if (!a.required && b.required) {
  72. return 1
  73. }
  74. return a.name.localeCompare(b.name)
  75. },
  76. )
  77. return [radioGroup, cloudProfileDropdown]
  78. }
  79. export default class ConnectionSchemaParser {
  80. static parseSchemaToFields(schema: any) {
  81. let fields = azureConnectionParse(schema)
  82. fields = [
  83. ...generateBaseFields(),
  84. ...fields,
  85. ]
  86. return fields
  87. }
  88. static parseConnectionInfoToPayload(data: any, schema: any) {
  89. const connectionInfo: any = fieldsToPayload(data, schema)
  90. const loginType = data.login_type || 'user_credentials'
  91. connectionInfo[loginType] = fieldsToPayload(data, schema.properties[loginType])
  92. if (!data.cloud_profile) {
  93. connectionInfo.cloud_profile = 'AzureCloud'
  94. }
  95. if (data.cloud_profile === 'CustomCloud') {
  96. connectionInfo.custom_cloud_properties = {
  97. endpoints: {
  98. ...fieldsToPayloadUseDefaults(
  99. data,
  100. schema.properties.custom_cloud_properties.properties.endpoints,
  101. ),
  102. },
  103. suffixes: {
  104. ...fieldsToPayloadUseDefaults(
  105. data,
  106. schema.properties.custom_cloud_properties.properties.suffixes,
  107. ),
  108. },
  109. }
  110. }
  111. if (data.secret_ref) {
  112. connectionInfo.secret_ref = data.secret_ref
  113. }
  114. return connectionInfo
  115. }
  116. static parseConnectionResponse(endpoint: Endpoint) {
  117. return endpoint
  118. }
  119. }