Commit c36da2cd authored by Spiros Koulouzis's avatar Spiros Koulouzis

provision with API v0.0 works

parent ba93249a
...@@ -15,8 +15,15 @@ ...@@ -15,8 +15,15 @@
*/ */
package nl.uva.sne.drip.api.service; package nl.uva.sne.drip.api.service;
import java.util.List;
import nl.uva.sne.drip.api.dao.ClusterCredentialsDao; import nl.uva.sne.drip.api.dao.ClusterCredentialsDao;
import nl.uva.sne.drip.commons.v1.types.ClusterCredentials;
import nl.uva.sne.drip.commons.v1.types.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
...@@ -24,15 +31,33 @@ import org.springframework.stereotype.Service; ...@@ -24,15 +31,33 @@ import org.springframework.stereotype.Service;
* @author S. Koulouzis * @author S. Koulouzis
*/ */
@Service @Service
@PreAuthorize("isAuthenticated()")
public class ClusterCredentialService { public class ClusterCredentialService {
@Autowired @Autowired
private ClusterCredentialsDao dao; private ClusterCredentialsDao dao;
/** @PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
* @return the dao public ClusterCredentials findOne(String id) {
*/ return dao.findOne(id);
public ClusterCredentialsDao getDao() { }
return dao;
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public List<ClusterCredentials> findAll() {
return dao.findAll();
}
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ClusterCredentials delete(String id) {
ClusterCredentials cred = dao.findOne(id);
dao.delete(cred);
return cred;
}
public ClusterCredentials save(ClusterCredentials clusterCred) {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String owner = user.getUsername();
clusterCred.setOwner(owner);
return dao.save(clusterCred);
} }
} }
...@@ -40,6 +40,8 @@ import nl.uva.sne.drip.drip.converter.SimplePlanContainer; ...@@ -40,6 +40,8 @@ import nl.uva.sne.drip.drip.converter.SimplePlanContainer;
import org.json.JSONException; import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -131,7 +133,7 @@ public class PlannerService { ...@@ -131,7 +133,7 @@ public class PlannerService {
} }
public String get(String id, String fromat) throws JSONException { public String get(String id, String fromat) throws JSONException {
Plan plan = planDao.findOne(id); Plan plan = findOne(id);
if (plan == null) { if (plan == null) {
throw new NotFoundException(); throw new NotFoundException();
} }
...@@ -139,7 +141,7 @@ public class PlannerService { ...@@ -139,7 +141,7 @@ public class PlannerService {
Map<String, Object> map = plan.getKeyValue(); Map<String, Object> map = plan.getKeyValue();
Set<String> ids = plan.getLoweLevelPlanIDs(); Set<String> ids = plan.getLoweLevelPlanIDs();
for (String lowID : ids) { for (String lowID : ids) {
Map<String, Object> lowLevelMap = planDao.findOne(lowID).getKeyValue(); Map<String, Object> lowLevelMap = findOne(lowID).getKeyValue();
if (lowLevelMap != null) { if (lowLevelMap != null) {
map.putAll(lowLevelMap); map.putAll(lowLevelMap);
} }
...@@ -161,9 +163,10 @@ public class PlannerService { ...@@ -161,9 +163,10 @@ public class PlannerService {
} }
public String getToscaID(String id) { public String getToscaID(String id) {
return planDao.findOne(id).getToscaID(); return findOne(id).getToscaID();
} }
@PostFilter("(filterObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public List<Plan> findAll() { public List<Plan> findAll() {
List<Plan> all = planDao.findAll(); List<Plan> all = planDao.findAll();
List<Plan> topLevel = new ArrayList<>(); List<Plan> topLevel = new ArrayList<>();
...@@ -182,12 +185,17 @@ public class PlannerService { ...@@ -182,12 +185,17 @@ public class PlannerService {
return planDao.save(plan); return planDao.save(plan);
} }
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public Plan findOne(String lowiID) { public Plan findOne(String lowiID) {
return planDao.findOne(lowiID); return planDao.findOne(lowiID);
} }
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public Plan delete(String id) { public Plan delete(String id) {
Plan plan = planDao.findOne(id); Plan plan = planDao.findOne(id);
if (plan == null) {
throw new NotFoundException();
}
planDao.delete(plan); planDao.delete(plan);
return plan; return plan;
} }
......
...@@ -36,6 +36,7 @@ import nl.uva.sne.drip.api.dao.ProvisionInfoDao; ...@@ -36,6 +36,7 @@ import nl.uva.sne.drip.api.dao.ProvisionInfoDao;
import nl.uva.sne.drip.api.exception.BadRequestException; import nl.uva.sne.drip.api.exception.BadRequestException;
import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException; import nl.uva.sne.drip.api.exception.CloudCredentialsNotFoundException;
import nl.uva.sne.drip.api.exception.ExceptionHandler; import nl.uva.sne.drip.api.exception.ExceptionHandler;
import nl.uva.sne.drip.api.exception.NotFoundException;
import nl.uva.sne.drip.api.exception.PlanNotFoundException; import nl.uva.sne.drip.api.exception.PlanNotFoundException;
import nl.uva.sne.drip.api.rpc.DRIPCaller; import nl.uva.sne.drip.api.rpc.DRIPCaller;
import nl.uva.sne.drip.api.rpc.ProvisionerCaller; import nl.uva.sne.drip.api.rpc.ProvisionerCaller;
...@@ -95,6 +96,9 @@ public class ProvisionService { ...@@ -95,6 +96,9 @@ public class ProvisionService {
@PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))") @PostAuthorize("(returnObject.owner == authentication.name) or (hasRole('ROLE_ADMIN'))")
public ProvisionInfo findOne(String id) { public ProvisionInfo findOne(String id) {
ProvisionInfo provisionInfo = dao.findOne(id); ProvisionInfo provisionInfo = dao.findOne(id);
if (provisionInfo == null) {
throw new NotFoundException();
}
return provisionInfo; return provisionInfo;
} }
......
...@@ -53,8 +53,9 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -53,8 +53,9 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
/** /**
* This controller is responsible for deploying a cluster on provisoned resources. * This controller is responsible for deploying a cluster on provisoned
* * resources.
*
* @author S. Koulouzis * @author S. Koulouzis
*/ */
@RestController @RestController
...@@ -75,10 +76,11 @@ public class DeployController { ...@@ -75,10 +76,11 @@ public class DeployController {
private ClusterCredentialService clusterCredentialService; private ClusterCredentialService clusterCredentialService;
/** /**
* Deploys a cluster on a provisioned resources. * Deploys a cluster on a provisioned resources.
* @param provisionID *
* @param provisionID
* @param clusterType * @param clusterType
* @return the id of the cluster credentials * @return the id of the cluster credentials
*/ */
@RequestMapping(value = "/deploy/{id}/", method = RequestMethod.GET, params = {"cluster"}) @RequestMapping(value = "/deploy/{id}/", method = RequestMethod.GET, params = {"cluster"})
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
...@@ -97,7 +99,7 @@ public class DeployController { ...@@ -97,7 +99,7 @@ public class DeployController {
if (name.equals("credential")) { if (name.equals("credential")) {
String value = p.getValue(); String value = p.getValue();
clusterCred.setContents(value); clusterCred.setContents(value);
clusterCredentialService.getDao().save(clusterCred); clusterCredentialService.save(clusterCred);
return clusterCred.getId(); return clusterCred.getId();
} }
} }
...@@ -109,7 +111,8 @@ public class DeployController { ...@@ -109,7 +111,8 @@ public class DeployController {
} }
/** /**
* Gets the cluster credentials. * Gets the cluster credentials.
*
* @param id * @param id
* @return the cluster credentials * @return the cluster credentials
*/ */
...@@ -117,7 +120,7 @@ public class DeployController { ...@@ -117,7 +120,7 @@ public class DeployController {
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
ClusterCredentials get(@PathVariable("id") String id) { ClusterCredentials get(@PathVariable("id") String id) {
ClusterCredentials clusterC = clusterCredentialService.getDao().findOne(id); ClusterCredentials clusterC = clusterCredentialService.findOne(id);
if (clusterC == null) { if (clusterC == null) {
throw new NotFoundException(); throw new NotFoundException();
} }
...@@ -126,13 +129,14 @@ public class DeployController { ...@@ -126,13 +129,14 @@ public class DeployController {
/** /**
* Gets the IDs of all the stored cluster credentials * Gets the IDs of all the stored cluster credentials
* @return a list of all the IDs *
* @return a list of all the IDs
*/ */
@RequestMapping(value = "/ids", method = RequestMethod.GET) @RequestMapping(value = "/ids", method = RequestMethod.GET)
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
List<String> getIds() { List<String> getIds() {
List<ClusterCredentials> all = clusterCredentialService.getDao().findAll(); List<ClusterCredentials> all = clusterCredentialService.findAll();
List<String> ids = new ArrayList<>(all.size()); List<String> ids = new ArrayList<>(all.size());
for (ClusterCredentials pi : all) { for (ClusterCredentials pi : all) {
ids.add(pi.getId()); ids.add(pi.getId());
...@@ -141,17 +145,18 @@ public class DeployController { ...@@ -141,17 +145,18 @@ public class DeployController {
} }
/** /**
* Deletes a cluster credential * Deletes a cluster credential
* @param id. The id of the cluster credential *
* @return the id f the deleted cluster credential * @param id. The id of the cluster credential
* @return the id f the deleted cluster credential
*/ */
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@RolesAllowed({UserService.USER, UserService.ADMIN}) @RolesAllowed({UserService.USER, UserService.ADMIN})
public @ResponseBody public @ResponseBody
String delete(@PathVariable("id") String id) { String delete(@PathVariable("id") String id) {
ClusterCredentials cred = clusterCredentialService.getDao().findOne(id); ClusterCredentials cred = clusterCredentialService.findOne(id);
if (cred != null) { if (cred != null) {
provisionService.delete(id); clusterCredentialService.delete(id);
return "Deleted : " + id; return "Deleted : " + id;
} }
throw new NotFoundException(); throw new NotFoundException();
......
...@@ -24,7 +24,7 @@ import org.springframework.data.annotation.Id; ...@@ -24,7 +24,7 @@ import org.springframework.data.annotation.Id;
* *
* @author S. Koulouzis * @author S. Koulouzis
*/ */
public class ClusterCredentials { public class ClusterCredentials extends OwnedObject{
@Id @Id
private String id; private String id;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment