Skip to content
Snippets Groups Projects
Commit 3b53c0f8 authored by Martin Weise's avatar Martin Weise
Browse files

fixed some deletion bugs, merge should now be complete

parent 2b40e18e
No related branches found
No related tags found
2 merge requests!81New stable release,!43Merge dev to master
Showing with 51 additions and 28 deletions
......@@ -81,22 +81,8 @@ public class ContainerEndpoint {
public ResponseEntity<ContainerDto> findById(@NotNull @PathVariable Long id) throws DockerClientException, ContainerNotFoundException {
final Container container = containerService.getById(id);
final ContainerDto containerDto = containerMapper.containerToContainerDto(container);
try {
containerService.findIpAddresses(container.getHash())
.forEach((key, value) -> containerDto.setIpAddress(IpAddressDto.builder()
.ipv4(value)
.build()));
} catch (ContainerNotRunningException e) {
throw new DockerClientException("Could not get container IP", e);
}
final ContainerStateDto stateDto = containerService.getContainerState(container.getHash());
try {
containerDto.setState(stateDto);
} catch (NullPointerException e) {
throw new DockerClientException("Could not get container state");
}
return ResponseEntity.ok()
.body(containerDto);
.body(containerService.packInspectResponse(container, containerDto));
}
@Transactional
......
......@@ -3,6 +3,7 @@ package at.tuwien.service;
import at.tuwien.api.container.ContainerCreateRequestDto;
import at.tuwien.api.container.ContainerDto;
import at.tuwien.api.container.ContainerStateDto;
import at.tuwien.api.container.network.IpAddressDto;
import at.tuwien.entities.container.Container;
import at.tuwien.entities.container.image.ContainerImage;
import at.tuwien.exception.*;
......@@ -25,6 +26,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.SocketUtils;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
......@@ -139,6 +141,33 @@ public class ContainerService {
return container.get();
}
/**
* Packs the container state into a response dto, return less information when the container is not running
*
* @param container The container
* @param out The response dto
* @return Information about the container
* @throws DockerClientException Upon failure to communicate with the Docker daemon
*/
public ContainerDto packInspectResponse(Container container, ContainerDto out) throws DockerClientException {
final ContainerStateDto stateDto = getContainerState(container.getHash());
try {
out.setState(stateDto);
} catch (NullPointerException e) {
throw new DockerClientException("Could not get container state");
}
try {
findIpAddresses(container.getHash())
.forEach((key, value) -> out.setIpAddress(IpAddressDto.builder()
.ipv4(value)
.build()));
} catch (ContainerNotRunningException e) {
log.warn("could not get container ip: {}", e.getMessage());
return out;
}
return out;
}
@Transactional
public List<Container> getAll() {
return containerRepository.findAll();
......@@ -173,7 +202,6 @@ public class ContainerService {
}
final Map<String, String> networks = new HashMap<>();
if (!response.getState().getRunning()) {
log.warn("cannot get IPv4 of container that is not running");
throw new ContainerNotRunningException("container is not running");
}
response.getNetworkSettings()
......@@ -195,7 +223,7 @@ public class ContainerService {
log.error("docker client failed {}", e.getMessage());
throw new DockerClientException("docker client failed", e);
}
log.debug("received container state {}", response);
log.debug("received container state {}", response.getState());
final ContainerDto dto = containerMapper.inspectContainerResponseToContainerDto(response);
return dto.getState();
}
......
package at.tuwien.entities.container;
import at.tuwien.entities.container.image.ContainerImage;
import at.tuwien.entities.database.Database;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
......@@ -9,6 +10,7 @@ import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.time.Instant;
import java.util.List;
@Data
@Entity
......@@ -53,9 +55,14 @@ public class Container {
private Integer port;
@ToString.Include
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private ContainerImage image;
// @ToString.Include
// @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
// private List<Database> databases;
@Column(nullable = false, updatable = false)
@CreatedDate
private Instant created;
......
......@@ -34,7 +34,7 @@ public class Database {
)
private Long id;
@ToString.Include
@ToString.Exclude
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Container container;
......@@ -46,7 +46,7 @@ public class Database {
@Column(nullable = false)
private String internalName;
@ToString.Exclude
@ToString.Include
@OneToMany(mappedBy = "id", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Table> tables;
......
......@@ -48,7 +48,7 @@ public class Table {
@Column(nullable = false, unique = true)
private String internalName;
@ToString.Include
@ToString.Exclude
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "tdbid", insertable = false, updatable = false)
private Database database;
......
......@@ -12,7 +12,7 @@ import java.time.Instant;
import java.util.List;
@Data
@Entity
/*@Entity // not yet in metadata db */
@Builder
@AllArgsConstructor
@NoArgsConstructor
......
......@@ -190,8 +190,8 @@ public class PostgresService extends JdbcConnector {
final PreparedStatement statement = getDeleteStatement(getConnection(table.getDatabase()), table);
statement.execute();
} catch (SQLException e) {
log.error("The SQL statement seems to contain invalid syntax");
throw new TableMalformedException("The SQL statement seems to contain invalid syntax", e);
log.error("The SQL statement seems to contain invalid syntax or table not existing");
throw new TableMalformedException("The SQL statement seems to contain invalid syntax or table not existing", e);
}
}
......@@ -204,8 +204,8 @@ public class PostgresService extends JdbcConnector {
try {
return connection.prepareStatement(deleteQuery.toString());
} catch (SQLException e) {
log.error("invalid syntax: {}", e.getMessage());
throw new DataProcessingException("invalid syntax", e);
log.error("invalid syntax or not existing table: {}", e.getMessage());
throw new DataProcessingException("invalid syntax or not existing table", e);
}
}
......
......@@ -66,14 +66,16 @@ public class TableService {
log.error("Unable to find database {}", databaseId);
throw new DatabaseNotFoundException("Unable to find database.");
}
return database.get().getTables();
final List<Table> tables = tableRepository.findByDatabase(database.get());
log.debug("found tables {} in database: {}", tables, database.get());
return tables;
}
@Transactional
public void delete(Long databaseId, Long tableId) throws TableNotFoundException, DatabaseConnectionException, TableMalformedException, DataProcessingException, DatabaseNotFoundException, ImageNotSupportedException {
final Table table = findById(databaseId, tableId);
postgresService.deleteTable(table);
tableRepository.deleteById(tableId);
tableRepository.delete(table);
}
@Transactional
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment