Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev rundeck api #132

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Object> 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();
}

}
Original file line number Diff line number Diff line change
@@ -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;

}
20 changes: 20 additions & 0 deletions src/main/java/fr/insee/genesis/domain/model/rundeck/Job.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<RundeckExecutionDocument, String> {
}
Loading