schemas.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {
  2. SchemasApi,
  3. Configuration,
  4. NewSchemaSubject,
  5. SchemaSubject,
  6. CompatibilityLevelCompatibilityEnum,
  7. SchemaType,
  8. } from 'generated-sources';
  9. import {
  10. PromiseThunkResult,
  11. ClusterName,
  12. SchemaName,
  13. FailurePayload,
  14. } from 'redux/interfaces';
  15. import { BASE_PARAMS } from 'lib/constants';
  16. import * as actions from 'redux/actions';
  17. import { getResponse } from 'lib/errorHandling';
  18. import { isEqual } from 'lodash';
  19. const apiClientConf = new Configuration(BASE_PARAMS);
  20. export const schemasApiClient = new SchemasApi(apiClientConf);
  21. export const fetchSchemasByClusterName = (
  22. clusterName: ClusterName
  23. ): PromiseThunkResult<void> => async (dispatch) => {
  24. dispatch(actions.fetchSchemasByClusterNameAction.request());
  25. try {
  26. const schemas = await schemasApiClient.getSchemas({ clusterName });
  27. dispatch(actions.fetchSchemasByClusterNameAction.success(schemas));
  28. } catch (e) {
  29. dispatch(actions.fetchSchemasByClusterNameAction.failure());
  30. }
  31. };
  32. export const fetchSchemaVersions = (
  33. clusterName: ClusterName,
  34. subject: SchemaName
  35. ): PromiseThunkResult<void> => async (dispatch) => {
  36. if (!subject) return;
  37. dispatch(actions.fetchSchemaVersionsAction.request());
  38. try {
  39. const versions = await schemasApiClient.getAllVersionsBySubject({
  40. clusterName,
  41. subject,
  42. });
  43. dispatch(actions.fetchSchemaVersionsAction.success(versions));
  44. } catch (e) {
  45. dispatch(actions.fetchSchemaVersionsAction.failure());
  46. }
  47. };
  48. export const createSchema = (
  49. clusterName: ClusterName,
  50. newSchemaSubject: NewSchemaSubject
  51. ): PromiseThunkResult => async (dispatch) => {
  52. dispatch(actions.createSchemaAction.request());
  53. try {
  54. const schema: SchemaSubject = await schemasApiClient.createNewSchema({
  55. clusterName,
  56. newSchemaSubject,
  57. });
  58. dispatch(actions.createSchemaAction.success(schema));
  59. } catch (error) {
  60. const response = await getResponse(error);
  61. const alert: FailurePayload = {
  62. subject: ['schema', newSchemaSubject.subject].join('-'),
  63. title: `Schema ${newSchemaSubject.subject}`,
  64. response,
  65. };
  66. dispatch(actions.createSchemaAction.failure({ alert }));
  67. }
  68. };
  69. export const updateSchemaCompatibilityLevel = (
  70. clusterName: ClusterName,
  71. subject: string,
  72. compatibilityLevel: CompatibilityLevelCompatibilityEnum
  73. ): PromiseThunkResult => async (dispatch) => {
  74. dispatch(actions.updateSchemaCompatibilityLevelAction.request());
  75. try {
  76. await schemasApiClient.updateSchemaCompatibilityLevel({
  77. clusterName,
  78. subject,
  79. compatibilityLevel: {
  80. compatibility: compatibilityLevel,
  81. },
  82. });
  83. dispatch(actions.updateSchemaCompatibilityLevelAction.success());
  84. } catch (error) {
  85. const response = await getResponse(error);
  86. const alert: FailurePayload = {
  87. subject: 'compatibilityLevel',
  88. title: `Compatibility level ${subject}`,
  89. response,
  90. };
  91. dispatch(actions.updateSchemaCompatibilityLevelAction.failure({ alert }));
  92. }
  93. };
  94. export const updateSchema = (
  95. latestSchema: SchemaSubject,
  96. newSchema: string,
  97. newSchemaType: SchemaType,
  98. newCompatibilityLevel: CompatibilityLevelCompatibilityEnum,
  99. clusterName: string,
  100. subject: string
  101. ): PromiseThunkResult => async (dispatch) => {
  102. if (
  103. (newSchema &&
  104. !isEqual(JSON.parse(latestSchema.schema), JSON.parse(newSchema))) ||
  105. newSchemaType !== latestSchema.schemaType
  106. ) {
  107. await dispatch(
  108. createSchema(clusterName, {
  109. ...latestSchema,
  110. schema: newSchema || latestSchema.schema,
  111. schemaType: newSchemaType || latestSchema.schemaType,
  112. })
  113. );
  114. }
  115. if (newCompatibilityLevel !== latestSchema.compatibilityLevel) {
  116. await dispatch(
  117. updateSchemaCompatibilityLevel(
  118. clusterName,
  119. subject,
  120. newCompatibilityLevel
  121. )
  122. );
  123. }
  124. };
  125. export const deleteSchema = (
  126. clusterName: ClusterName,
  127. subject: string
  128. ): PromiseThunkResult => async (dispatch) => {
  129. dispatch(actions.deleteSchemaAction.request());
  130. try {
  131. await schemasApiClient.deleteSchema({
  132. clusterName,
  133. subject,
  134. });
  135. dispatch(actions.deleteSchemaAction.success(subject));
  136. } catch (error) {
  137. const response = await getResponse(error);
  138. const alert: FailurePayload = {
  139. subject: ['schema', subject].join('-'),
  140. title: `Schema ${subject}`,
  141. response,
  142. };
  143. dispatch(actions.deleteSchemaAction.failure({ alert }));
  144. }
  145. };