diff --git a/src/main/java/fr/insee/genesis/controller/rest/RundeckExecutionController.java b/src/main/java/fr/insee/genesis/controller/rest/RundeckExecutionController.java new file mode 100644 index 0000000..95ca9a8 --- /dev/null +++ b/src/main/java/fr/insee/genesis/controller/rest/RundeckExecutionController.java @@ -0,0 +1,42 @@ +package fr.insee.genesis.controller.rest; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; +import fr.insee.genesis.domain.ports.api.RundeckExecutionApiPort; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +@RequestMapping(path = "/rundeck-execution") +@Controller +@Slf4j +public class RundeckExecutionController { + + private final RundeckExecutionApiPort rundeckExecutionApiPort; + + @Autowired + public RundeckExecutionController(RundeckExecutionApiPort rundeckExecutionApiPort) { + this.rundeckExecutionApiPort = rundeckExecutionApiPort; + } + + @Operation(summary = "Register a Rundeck execution") + @PostMapping(path = "/save") + public ResponseEntity addRundeckExecution( + @Parameter(description = "Survey name to call Kraftwerk on") @RequestBody RundeckExecution rundeckExecution + ){ + try{ + rundeckExecutionApiPort.addExecution(rundeckExecution); + log.info("{} job saved", rundeckExecution.getJob().getName()); + } catch(Exception e){ + log.info("Rundeck execution was not saved in database"); + return ResponseEntity.internalServerError().build(); + } + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/fr/insee/genesis/domain/model/rundeck/DateStarted.java b/src/main/java/fr/insee/genesis/domain/model/rundeck/DateStarted.java new file mode 100644 index 0000000..e0386b5 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/model/rundeck/DateStarted.java @@ -0,0 +1,13 @@ +package fr.insee.genesis.domain.model.rundeck; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DateStarted { + + private long unixtime; + private String date; + +} diff --git a/src/main/java/fr/insee/genesis/domain/model/rundeck/Job.java b/src/main/java/fr/insee/genesis/domain/model/rundeck/Job.java new file mode 100644 index 0000000..47da484 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/model/rundeck/Job.java @@ -0,0 +1,20 @@ +package fr.insee.genesis.domain.model.rundeck; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Job { + + @JsonProperty("id") + private String idJob; + private long averageDuration; + private String name; + private String group; + private String project; + private String description; + private String href; + private String permalink; +} diff --git a/src/main/java/fr/insee/genesis/domain/model/rundeck/RundeckExecution.java b/src/main/java/fr/insee/genesis/domain/model/rundeck/RundeckExecution.java new file mode 100644 index 0000000..2736762 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/model/rundeck/RundeckExecution.java @@ -0,0 +1,27 @@ +package fr.insee.genesis.domain.model.rundeck; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RundeckExecution { + + @JsonProperty("id") + private long idExecution; + private String href; + private String permalink; + private String status; + private String project; + private String executionType; + private String user; + + @JsonProperty("date-started") + private DateStarted dateStarted; + + private Job job; + private String description; + private String argstring; + private String serverUUID; +} diff --git a/src/main/java/fr/insee/genesis/domain/ports/api/RundeckExecutionApiPort.java b/src/main/java/fr/insee/genesis/domain/ports/api/RundeckExecutionApiPort.java new file mode 100644 index 0000000..9124685 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/ports/api/RundeckExecutionApiPort.java @@ -0,0 +1,9 @@ +package fr.insee.genesis.domain.ports.api; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; + +public interface RundeckExecutionApiPort { + + void addExecution(RundeckExecution rundeckExecution); + +} diff --git a/src/main/java/fr/insee/genesis/domain/ports/spi/RundeckExecutionPersistencePort.java b/src/main/java/fr/insee/genesis/domain/ports/spi/RundeckExecutionPersistencePort.java new file mode 100644 index 0000000..bbdbdb8 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/ports/spi/RundeckExecutionPersistencePort.java @@ -0,0 +1,8 @@ +package fr.insee.genesis.domain.ports.spi; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; + +public interface RundeckExecutionPersistencePort { + + void save(RundeckExecution rundeckExecution); +} diff --git a/src/main/java/fr/insee/genesis/domain/service/rundeck/RundeckExecutionService.java b/src/main/java/fr/insee/genesis/domain/service/rundeck/RundeckExecutionService.java new file mode 100644 index 0000000..06d8456 --- /dev/null +++ b/src/main/java/fr/insee/genesis/domain/service/rundeck/RundeckExecutionService.java @@ -0,0 +1,26 @@ +package fr.insee.genesis.domain.service.rundeck; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; +import fr.insee.genesis.domain.ports.api.RundeckExecutionApiPort; +import fr.insee.genesis.domain.ports.spi.RundeckExecutionPersistencePort; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class RundeckExecutionService implements RundeckExecutionApiPort { + @Qualifier("rundeckExecutionMongoAdapter") + private final RundeckExecutionPersistencePort rundeckExecutionPersistencePort; + + @Autowired + public RundeckExecutionService(RundeckExecutionPersistencePort rundeckExecutionPersistencePort) { + this.rundeckExecutionPersistencePort = rundeckExecutionPersistencePort; + } + + @Override + public void addExecution(RundeckExecution rundeckExecution) { + rundeckExecutionPersistencePort.save(rundeckExecution); + } +} diff --git a/src/main/java/fr/insee/genesis/infrastructure/adapter/RundeckExecutionMongoAdapter.java b/src/main/java/fr/insee/genesis/infrastructure/adapter/RundeckExecutionMongoAdapter.java new file mode 100644 index 0000000..9903e33 --- /dev/null +++ b/src/main/java/fr/insee/genesis/infrastructure/adapter/RundeckExecutionMongoAdapter.java @@ -0,0 +1,30 @@ +package fr.insee.genesis.infrastructure.adapter; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; +import fr.insee.genesis.domain.ports.spi.RundeckExecutionPersistencePort; +import fr.insee.genesis.infrastructure.mappers.RundeckExecutionDocumentMapper; +import fr.insee.genesis.infrastructure.repository.RundeckExecutionDBRepository; +import fr.insee.genesis.infrastructure.repository.ScheduleMongoDBRepository; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +@Service +@Qualifier("rundeckExecutionMongoAdapter") +@Slf4j +public class RundeckExecutionMongoAdapter implements RundeckExecutionPersistencePort { + + private final RundeckExecutionDBRepository rundeckExecutionDBRepository; + + @Autowired + public RundeckExecutionMongoAdapter(RundeckExecutionDBRepository rundeckExecutionDBRepository) { + this.rundeckExecutionDBRepository = rundeckExecutionDBRepository; + } + + @Override + public void save(RundeckExecution rundeckExecution) { + rundeckExecutionDBRepository.insert(RundeckExecutionDocumentMapper.INSTANCE.modelToDocument(rundeckExecution)); + } +} diff --git a/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/Job.java b/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/Job.java new file mode 100644 index 0000000..4f4a2df --- /dev/null +++ b/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/Job.java @@ -0,0 +1,12 @@ +package fr.insee.genesis.infrastructure.document.rundeck; + +import lombok.Data; + +@Data +public class Job { + + private String idJob; + private long averageDuration; + private String name; + private String project; +} diff --git a/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/RundeckExecutionDocument.java b/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/RundeckExecutionDocument.java new file mode 100644 index 0000000..8e2b8ad --- /dev/null +++ b/src/main/java/fr/insee/genesis/infrastructure/document/rundeck/RundeckExecutionDocument.java @@ -0,0 +1,23 @@ +package fr.insee.genesis.infrastructure.document.rundeck; + +import com.fasterxml.jackson.annotation.JsonProperty; +import fr.insee.genesis.domain.model.rundeck.DateStarted; +import lombok.*; +import org.springframework.data.mongodb.core.mapping.Document; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Document(collection= "rundeckExecutions") +public class RundeckExecutionDocument { + + private long idExecution; + private String status; + private String project; + private String user; + + @JsonProperty("date-started") + private DateStarted dateStarted; + + private Job job; +} diff --git a/src/main/java/fr/insee/genesis/infrastructure/mappers/RundeckExecutionDocumentMapper.java b/src/main/java/fr/insee/genesis/infrastructure/mappers/RundeckExecutionDocumentMapper.java new file mode 100644 index 0000000..f0847f0 --- /dev/null +++ b/src/main/java/fr/insee/genesis/infrastructure/mappers/RundeckExecutionDocumentMapper.java @@ -0,0 +1,16 @@ +package fr.insee.genesis.infrastructure.mappers; + +import fr.insee.genesis.domain.model.rundeck.RundeckExecution; +import fr.insee.genesis.infrastructure.document.rundeck.RundeckExecutionDocument; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface RundeckExecutionDocumentMapper { + + RundeckExecutionDocumentMapper INSTANCE = Mappers.getMapper(RundeckExecutionDocumentMapper.class); + + RundeckExecution documentToModel(RundeckExecutionDocument rundeckExecutionDocument); + + RundeckExecutionDocument modelToDocument(RundeckExecution rundeckExecution); +} diff --git a/src/main/java/fr/insee/genesis/infrastructure/repository/RundeckExecutionDBRepository.java b/src/main/java/fr/insee/genesis/infrastructure/repository/RundeckExecutionDBRepository.java new file mode 100644 index 0000000..2983568 --- /dev/null +++ b/src/main/java/fr/insee/genesis/infrastructure/repository/RundeckExecutionDBRepository.java @@ -0,0 +1,9 @@ +package fr.insee.genesis.infrastructure.repository; + +import fr.insee.genesis.infrastructure.document.rundeck.RundeckExecutionDocument; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface RundeckExecutionDBRepository extends MongoRepository { +}