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

fixed some deletion bugs, merge should now be complete

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