|
| 1 | +package io.dataline.server.handlers; |
| 2 | + |
| 3 | +import io.dataline.api.model.*; |
| 4 | +import io.dataline.config.DestinationConnectionImplementation; |
| 5 | +import io.dataline.config.persistence.ConfigNotFoundException; |
| 6 | +import io.dataline.config.persistence.ConfigPersistence; |
| 7 | +import io.dataline.config.persistence.JsonValidationException; |
| 8 | +import io.dataline.config.persistence.PersistenceConfigType; |
| 9 | +import io.dataline.server.errors.KnownException; |
| 10 | +import io.dataline.server.validation.IntegrationSchemaValidation; |
| 11 | +import java.util.List; |
| 12 | +import java.util.UUID; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public class DestinationImplementationsHandler { |
| 16 | + private final ConfigPersistence configPersistence; |
| 17 | + private final IntegrationSchemaValidation validator; |
| 18 | + |
| 19 | + public DestinationImplementationsHandler(ConfigPersistence configPersistence) { |
| 20 | + this.configPersistence = configPersistence; |
| 21 | + this.validator = new IntegrationSchemaValidation(configPersistence); |
| 22 | + } |
| 23 | + |
| 24 | + public DestinationImplementationRead createDestinationImplementation( |
| 25 | + DestinationImplementationCreate destinationImplementationCreate) { |
| 26 | + // validate configuration |
| 27 | + validateDestinationImplementation( |
| 28 | + destinationImplementationCreate.getDestinationSpecificationId(), |
| 29 | + destinationImplementationCreate.getConnectionConfiguration()); |
| 30 | + |
| 31 | + // persist |
| 32 | + final UUID destinationImplementationId = UUID.randomUUID(); |
| 33 | + persistDestinationConnectionImplementation( |
| 34 | + destinationImplementationCreate.getDestinationSpecificationId(), |
| 35 | + destinationImplementationCreate.getWorkspaceId(), |
| 36 | + destinationImplementationId, |
| 37 | + destinationImplementationCreate.getConnectionConfiguration()); |
| 38 | + |
| 39 | + // read configuration from db |
| 40 | + return getDestinationImplementationInternal(destinationImplementationId); |
| 41 | + } |
| 42 | + |
| 43 | + public DestinationImplementationRead updateDestinationImplementation( |
| 44 | + DestinationImplementationUpdate destinationImplementationUpdate) { |
| 45 | + // get existing implementation |
| 46 | + final DestinationImplementationRead persistedDestinationImplementation = |
| 47 | + getDestinationImplementationInternal( |
| 48 | + destinationImplementationUpdate.getDestinationImplementationId()); |
| 49 | + |
| 50 | + // validate configuration |
| 51 | + validateDestinationImplementation( |
| 52 | + persistedDestinationImplementation.getDestinationSpecificationId(), |
| 53 | + destinationImplementationUpdate.getConnectionConfiguration()); |
| 54 | + |
| 55 | + // persist |
| 56 | + persistDestinationConnectionImplementation( |
| 57 | + persistedDestinationImplementation.getDestinationSpecificationId(), |
| 58 | + persistedDestinationImplementation.getWorkspaceId(), |
| 59 | + destinationImplementationUpdate.getDestinationImplementationId(), |
| 60 | + destinationImplementationUpdate.getConnectionConfiguration()); |
| 61 | + |
| 62 | + // read configuration from db |
| 63 | + return getDestinationImplementationInternal( |
| 64 | + destinationImplementationUpdate.getDestinationImplementationId()); |
| 65 | + } |
| 66 | + |
| 67 | + public DestinationImplementationRead getDestinationImplementation( |
| 68 | + DestinationImplementationIdRequestBody destinationImplementationIdRequestBody) { |
| 69 | + |
| 70 | + return getDestinationImplementationInternal( |
| 71 | + destinationImplementationIdRequestBody.getDestinationImplementationId()); |
| 72 | + } |
| 73 | + |
| 74 | + public DestinationImplementationReadList listDestinationImplementationsForWorkspace( |
| 75 | + WorkspaceIdRequestBody workspaceIdRequestBody) { |
| 76 | + try { |
| 77 | + |
| 78 | + final List<DestinationImplementationRead> reads = |
| 79 | + configPersistence |
| 80 | + .getConfigs( |
| 81 | + PersistenceConfigType.DESTINATION_CONNECTION_IMPLEMENTATION, |
| 82 | + DestinationConnectionImplementation.class) |
| 83 | + .stream() |
| 84 | + .filter( |
| 85 | + destinationConnectionImplementation -> |
| 86 | + destinationConnectionImplementation |
| 87 | + .getWorkspaceId() |
| 88 | + .equals(workspaceIdRequestBody.getWorkspaceId())) |
| 89 | + .map(this::toDestinationImplementationRead) |
| 90 | + .collect(Collectors.toList()); |
| 91 | + |
| 92 | + final DestinationImplementationReadList destinationImplementationReadList = |
| 93 | + new DestinationImplementationReadList(); |
| 94 | + destinationImplementationReadList.setDestinations(reads); |
| 95 | + return destinationImplementationReadList; |
| 96 | + } catch (JsonValidationException e) { |
| 97 | + throw new KnownException( |
| 98 | + 422, |
| 99 | + String.format( |
| 100 | + "Attempted to retrieve a configuration does not fulfill the specification. Errors: %s", |
| 101 | + e.getMessage())); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private DestinationImplementationRead getDestinationImplementationInternal( |
| 106 | + UUID destinationImplementationId) { |
| 107 | + // read configuration from db |
| 108 | + final DestinationConnectionImplementation retrievedDestinationConnectionImplementation; |
| 109 | + try { |
| 110 | + retrievedDestinationConnectionImplementation = |
| 111 | + configPersistence.getConfig( |
| 112 | + PersistenceConfigType.DESTINATION_CONNECTION_IMPLEMENTATION, |
| 113 | + destinationImplementationId.toString(), |
| 114 | + DestinationConnectionImplementation.class); |
| 115 | + } catch (JsonValidationException e) { |
| 116 | + throw new KnownException( |
| 117 | + 422, |
| 118 | + String.format( |
| 119 | + "The provided configuration does not fulfill the specification. Errors: %s", |
| 120 | + e.getMessage())); |
| 121 | + } catch (ConfigNotFoundException e) { |
| 122 | + throw new KnownException( |
| 123 | + 422, |
| 124 | + String.format( |
| 125 | + "Could not find destination specification: %s.", destinationImplementationId)); |
| 126 | + } |
| 127 | + |
| 128 | + return toDestinationImplementationRead(retrievedDestinationConnectionImplementation); |
| 129 | + } |
| 130 | + |
| 131 | + private void validateDestinationImplementation( |
| 132 | + UUID destinationConnectionSpecificationId, Object implementation) { |
| 133 | + try { |
| 134 | + validator.validateDestinationConnectionConfiguration( |
| 135 | + destinationConnectionSpecificationId, implementation); |
| 136 | + } catch (JsonValidationException e) { |
| 137 | + throw new KnownException( |
| 138 | + 422, |
| 139 | + String.format( |
| 140 | + "The provided configuration does not fulfill the specification. Errors: %s", |
| 141 | + e.getMessage())); |
| 142 | + } catch (ConfigNotFoundException e) { |
| 143 | + throw new KnownException( |
| 144 | + 422, |
| 145 | + String.format( |
| 146 | + "Could not find destination specification: %s.", |
| 147 | + destinationConnectionSpecificationId)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void persistDestinationConnectionImplementation( |
| 152 | + UUID destinationSpecificationId, |
| 153 | + UUID workspaceId, |
| 154 | + UUID destinationImplementationId, |
| 155 | + Object configuration) { |
| 156 | + final DestinationConnectionImplementation destinationConnectionImplementation = |
| 157 | + new DestinationConnectionImplementation(); |
| 158 | + destinationConnectionImplementation.setDestinationSpecificationId(destinationSpecificationId); |
| 159 | + destinationConnectionImplementation.setWorkspaceId(workspaceId); |
| 160 | + destinationConnectionImplementation.setDestinationImplementationId(destinationImplementationId); |
| 161 | + destinationConnectionImplementation.setConfiguration(configuration); |
| 162 | + |
| 163 | + configPersistence.writeConfig( |
| 164 | + PersistenceConfigType.DESTINATION_CONNECTION_IMPLEMENTATION, |
| 165 | + destinationImplementationId.toString(), |
| 166 | + destinationConnectionImplementation); |
| 167 | + } |
| 168 | + |
| 169 | + private DestinationImplementationRead toDestinationImplementationRead( |
| 170 | + DestinationConnectionImplementation destinationConnectionImplementation) { |
| 171 | + final DestinationImplementationRead destinationImplementationRead = |
| 172 | + new DestinationImplementationRead(); |
| 173 | + destinationImplementationRead.setDestinationImplementationId( |
| 174 | + destinationConnectionImplementation.getDestinationImplementationId()); |
| 175 | + destinationImplementationRead.setWorkspaceId( |
| 176 | + destinationConnectionImplementation.getWorkspaceId()); |
| 177 | + destinationImplementationRead.setDestinationSpecificationId( |
| 178 | + destinationConnectionImplementation.getDestinationSpecificationId()); |
| 179 | + destinationImplementationRead.setConnectionConfiguration( |
| 180 | + destinationConnectionImplementation.getConfiguration()); |
| 181 | + |
| 182 | + return destinationImplementationRead; |
| 183 | + } |
| 184 | +} |
0 commit comments