Add monitor deploy service

This commit is contained in:
Konstantinos Triantafyllou 2024-03-06 01:27:38 +02:00
parent 8918826766
commit 76c39d6eb3
6 changed files with 80 additions and 139 deletions

View File

@ -17,21 +17,20 @@ import java.util.*;
public class EmailController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private EmailSender emailSender;
private final EmailSender emailSender;
private final VerifyRecaptcha verifyRecaptcha;
@Autowired
private VerifyRecaptcha verifyRecaptcha;
public EmailController(EmailSender emailSender, VerifyRecaptcha verifyRecaptcha) {
this.emailSender = emailSender;
this.verifyRecaptcha = verifyRecaptcha;
}
@RequestMapping(value = "/contact", method = RequestMethod.POST)
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
verifyRecaptcha.processResponse(form.getRecaptcha());
Email email = form.getEmail();
ArrayList<String> sendTo = new ArrayList<>();
for(String userMail: email.getRecipients()){
sendTo.add(userMail);
}
return emailSender.send(sendTo, email.getSubject(), email.getBody(), false);
return emailSender.send(email.getRecipients(), email.getSubject(), email.getBody(), false);
}
@RequestMapping(value = "/sendMail", method = RequestMethod.POST)
@ -40,7 +39,7 @@ public class EmailController {
String successString = "success";
String failureString = "failure";
Map<String, ArrayList<String>> mailResults = new HashMap<>();
boolean bcc = (optional.isPresent())?optional.get():true;
boolean bcc = optional.orElse(true);
for(String userMail:email.getRecipients()){
ArrayList<String> sendTo = new ArrayList<>();
sendTo.add(userMail);

View File

@ -45,11 +45,7 @@ public class MonitorController {
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public Boolean deletePortals(@RequestBody List<String> portals) {
for (String id: portals) {
String pid = portalService.deletePortal(id);
// layoutService.deleteByPid(pid);
}
portals.forEach(id -> portalService.deletePortal(id));
return true;
}
}

View File

@ -1,25 +1,14 @@
package eu.dnetlib.uoamonitorservice.controllers;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DBObject;
import eu.dnetlib.uoamonitorservice.application.UoaMonitorServiceApplication;
import eu.dnetlib.uoamonitorservice.configuration.GlobalVars;
import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection;
import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig;
import eu.dnetlib.uoamonitorservice.service.MonitorDeployService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@ -28,14 +17,11 @@ import java.util.Map;
public class MonitorLibraryCheckDeployController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private MongoConnection mongoConnection;
private final MonitorDeployService service;
@Autowired
private MongoConfig mongoConfig;
@Autowired
private GlobalVars globalVars;
public MonitorLibraryCheckDeployController(MonitorDeployService service) {
this.service = service;
}
@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET)
public String hello() {
@ -46,33 +32,6 @@ public class MonitorLibraryCheckDeployController {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
public Map<String, String> checkEverything() {
Map<String, String> response = new HashMap<>();
MongoTemplate mt = mongoConnection.getMongoTemplate();
DBObject ping = new BasicDBObject("ping", "1");
try {
CommandResult answer = mt.getDb().command(ping);
response.put("Mongo try: error", answer.getErrorMessage());
} catch (Exception e) {
response.put("Mongo catch: error", e.getMessage());
}
response.put("monitorservice.mongodb.database", mongoConfig.getDatabase());
response.put("monitorservice.mongodb.host", mongoConfig.getHost());
response.put("monitorservice.mongodb.port", mongoConfig.getPort()+"");
response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]");
response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]");
if(globalVars.date != null) {
response.put("Date of deploy", globalVars.date.toString());
}
if(globalVars.getBuildDate() != null) {
response.put("Date of build", globalVars.getBuildDate());
}
if(globalVars.getVersion() != null) {
response.put("Version", globalVars.getVersion());
}
return response;
return this.service.checkEverything();
}
}

View File

@ -6,6 +6,7 @@ import com.mongodb.DBObject;
import eu.dnetlib.uoamonitorservice.configuration.GlobalVars;
import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection;
import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig;
import eu.dnetlib.uoamonitorservice.service.MonitorDeployService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
@ -24,14 +25,11 @@ import java.util.Map;
public class MonitorServiceCheckDeployController {
private final Logger log = LogManager.getLogger(this.getClass());
@Autowired
private MongoConnection mongoConnection;
private final MonitorDeployService service;
@Autowired
private MongoConfig mongoConfig;
@Autowired
private GlobalVars globalVars;
public MonitorServiceCheckDeployController(MonitorDeployService service) {
this.service = service;
}
@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET)
public String hello() {
@ -42,33 +40,6 @@ public class MonitorServiceCheckDeployController {
@PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
@RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET)
public Map<String, String> checkEverything() {
Map<String, String> response = new HashMap<>();
MongoTemplate mt = mongoConnection.getMongoTemplate();
DBObject ping = new BasicDBObject("ping", "1");
try {
CommandResult answer = mt.getDb().command(ping);
response.put("Mongo try: error", answer.getErrorMessage());
} catch (Exception e) {
response.put("Mongo catch: error", e.getMessage());
}
response.put("monitorservice.mongodb.database", mongoConfig.getDatabase());
response.put("monitorservice.mongodb.host", mongoConfig.getHost());
response.put("monitorservice.mongodb.port", mongoConfig.getPort()+"");
response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]");
response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]");
if(globalVars.date != null) {
response.put("Date of deploy", globalVars.date.toString());
}
if(globalVars.getBuildDate() != null) {
response.put("Date of build", globalVars.getBuildDate());
}
if(globalVars.getVersion() != null) {
response.put("Version", globalVars.getVersion());
}
return response;
return this.service.checkEverything();
}
}

View File

@ -1,43 +0,0 @@
package eu.dnetlib.uoamonitorservice.handlers.utils;
import java.util.ArrayList;
import java.util.List;
public class UserInfo {
String name;
String email;
List<String> edu_person_entitlements = new ArrayList<String>();
@Override
public String toString() {
return "UserInfo{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", edu_person_entitlements=" + edu_person_entitlements +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getEdu_person_entitlements() {
return edu_person_entitlements;
}
public void setEdu_person_entitlements(List<String> edu_person_entitlements) {
this.edu_person_entitlements = edu_person_entitlements;
}
}

View File

@ -0,0 +1,59 @@
package eu.dnetlib.uoamonitorservice.service;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DBObject;
import eu.dnetlib.uoamonitorservice.configuration.GlobalVars;
import eu.dnetlib.uoamonitorservice.configuration.mongo.MongoConnection;
import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class MonitorDeployService {
private final MongoConnection mongoConnection;
private final MongoConfig mongoConfig;
private final GlobalVars globalVars;
@Autowired
public MonitorDeployService(MongoConnection mongoConnection, MongoConfig mongoConfig, GlobalVars globalVars) {
this.mongoConnection = mongoConnection;
this.mongoConfig = mongoConfig;
this.globalVars = globalVars;
}
public Map<String, String> checkEverything() {
Map<String, String> response = new HashMap<>();
MongoTemplate mt = mongoConnection.getMongoTemplate();
DBObject ping = new BasicDBObject("ping", "1");
try {
CommandResult answer = mt.getDb().command(ping);
response.put("Mongo try: error", answer.getErrorMessage());
} catch (Exception e) {
response.put("Mongo catch: error", e.getMessage());
}
response.put("monitorservice.mongodb.database", mongoConfig.getDatabase());
response.put("monitorservice.mongodb.host", mongoConfig.getHost());
response.put("monitorservice.mongodb.port", mongoConfig.getPort() + "");
response.put("monitorservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]");
response.put("monitorservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]");
if (globalVars.date != null) {
response.put("Date of deploy", globalVars.date.toString());
}
if (globalVars.getBuildDate() != null) {
response.put("Date of build", globalVars.getBuildDate());
}
if (globalVars.getVersion() != null) {
response.put("Version", globalVars.getVersion());
}
return response;
}
}