Return 409 Conflict status if application, autoscaling policy and kubernetes cluster already exists or application already deployed
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/db77fe03 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/db77fe03 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/db77fe03 Branch: refs/heads/master Commit: db77fe036ad8c261c914396722edc8f09768d864 Parents: 477c366 Author: Chamila de Alwis <[email protected]> Authored: Sat Apr 18 00:48:21 2015 +0530 Committer: Chamila de Alwis <[email protected]> Committed: Sat Apr 18 00:48:21 2015 +0530 ---------------------------------------------------------------------- .../common/client/AutoscalerServiceClient.java | 2 +- .../rest/endpoint/api/StratosApiV40Utils.java | 2 +- .../rest/endpoint/api/StratosApiV41.java | 70 ++++++++++++++------ .../rest/endpoint/api/StratosApiV41Utils.java | 2 +- 4 files changed, 54 insertions(+), 22 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/db77fe03/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/client/AutoscalerServiceClient.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/client/AutoscalerServiceClient.java b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/client/AutoscalerServiceClient.java index 3949bec..2b7d0cb 100644 --- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/client/AutoscalerServiceClient.java +++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/client/AutoscalerServiceClient.java @@ -146,7 +146,7 @@ public class AutoscalerServiceClient { stub.deleteApplication(applicationId); } - public boolean deployAutoscalingPolicy(AutoscalePolicy autoScalePolicy) throws RemoteException, + public boolean addAutoscalingPolicy(AutoscalePolicy autoScalePolicy) throws RemoteException, AutoscalerServiceInvalidPolicyExceptionException { return stub.addAutoScalingPolicy(autoScalePolicy); } http://git-wip-us.apache.org/repos/asf/stratos/blob/db77fe03/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java index 6c83add..2cdea8c 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV40Utils.java @@ -160,7 +160,7 @@ public class StratosApiV40Utils { try { autoscalerServiceClient - .deployAutoscalingPolicy(autoscalePolicy); + .addAutoscalingPolicy(autoscalePolicy); } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e); http://git-wip-us.apache.org/repos/asf/stratos/blob/db77fe03/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java index 371c709..4df702f 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41.java @@ -627,12 +627,20 @@ public class StratosApiV41 extends AbstractApi { @Consumes("application/json") @AuthorizationAction("/permission/protected/manage/addApplication") public Response addApplication(ApplicationBean applicationDefinition) throws RestAPIException { - StratosApiV41Utils.addApplication(applicationDefinition, getConfigContext(), getUsername(), getTenantDomain()); + try { + StratosApiV41Utils.addApplication(applicationDefinition, getConfigContext(), getUsername(), getTenantDomain()); - URI url = uriInfo.getAbsolutePathBuilder().path(applicationDefinition.getApplicationId()).build(); - return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), - String.format("Application added successfully: [application] %s", - applicationDefinition.getApplicationId()))).build(); + URI url = uriInfo.getAbsolutePathBuilder().path(applicationDefinition.getApplicationId()).build(); + return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), + String.format("Application added successfully: [application] %s", + applicationDefinition.getApplicationId()))).build(); + } catch (RestAPIException e) { + if (e.getMessage().contains("already exists")) { + return Response.status(Response.Status.CONFLICT).build(); + } else { + throw e; + } + } } /** @@ -694,9 +702,17 @@ public class StratosApiV41 extends AbstractApi { public Response deployApplication( @PathParam("applicationId") String applicationId, @PathParam("applicationPolicyId") String applicationPolicyId) throws RestAPIException { - StratosApiV41Utils.deployApplication(applicationId, applicationPolicyId); - return Response.accepted().entity(new SuccessResponseBean(Response.Status.ACCEPTED.getStatusCode(), - String.format("Application deployed successfully: [application] %s", applicationId))).build(); + try { + StratosApiV41Utils.deployApplication(applicationId, applicationPolicyId); + return Response.accepted().entity(new SuccessResponseBean(Response.Status.ACCEPTED.getStatusCode(), + String.format("Application deployed successfully: [application] %s", applicationId))).build(); + } catch (RestAPIException e) { + if (e.getMessage().contains("already in DEPLOYED")) { + return Response.status(Response.Status.CONFLICT).build(); + } else { + throw e; + } + } } /** @@ -1124,11 +1140,19 @@ public class StratosApiV41 extends AbstractApi { public Response addAutoscalingPolicy( AutoscalePolicyBean autoscalePolicy) throws RestAPIException { - StratosApiV41Utils.addAutoscalingPolicy(autoscalePolicy); - URI url = uriInfo.getAbsolutePathBuilder().path(autoscalePolicy.getId()).build(); - return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), - String.format("Autoscaling policy added successfully: [autoscale-policy] %s", - autoscalePolicy.getId()))).build(); + try { + StratosApiV41Utils.addAutoscalingPolicy(autoscalePolicy); + URI url = uriInfo.getAbsolutePathBuilder().path(autoscalePolicy.getId()).build(); + return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), + String.format("Autoscaling policy added successfully: [autoscale-policy] %s", + autoscalePolicy.getId()))).build(); + } catch (RestAPIException e) { + if (e.getMessage().contains("already exists")) { + return Response.status(Response.Status.CONFLICT).build(); + } else { + throw e; + } + } } /** @@ -1779,7 +1803,7 @@ public class StratosApiV41 extends AbstractApi { public Response notifyRepository( GitNotificationPayloadBean payload) throws RestAPIException { if (log.isInfoEnabled()) { - log.info(String.format("Git update")); + log.info(String.format("Git update notification received.")); } StratosApiV41Utils.notifyArtifactUpdatedEvent(payload); @@ -1942,11 +1966,19 @@ public class StratosApiV41 extends AbstractApi { public Response addKubernetesHostCluster( KubernetesClusterBean kubernetesCluster) throws RestAPIException { - StratosApiV41Utils.addKubernetesCluster(kubernetesCluster); - URI url = uriInfo.getAbsolutePathBuilder().path(kubernetesCluster.getClusterId()).build(); - return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), - String.format("Kubernetes Host Cluster added successfully: [kub-host-cluster] %s", - kubernetesCluster.getClusterId()))).build(); + try { + StratosApiV41Utils.addKubernetesCluster(kubernetesCluster); + URI url = uriInfo.getAbsolutePathBuilder().path(kubernetesCluster.getClusterId()).build(); + return Response.created(url).entity(new SuccessResponseBean(Response.Status.CREATED.getStatusCode(), + String.format("Kubernetes Host Cluster added successfully: [kub-host-cluster] %s", + kubernetesCluster.getClusterId()))).build(); + } catch (RestAPIException e) { + if (e.getMessage().contains("already exists")) { + return Response.status(Response.Status.CONFLICT).build(); + } else { + throw e; + } + } } /** http://git-wip-us.apache.org/repos/asf/stratos/blob/db77fe03/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java index 63f7300..03bb67d 100644 --- a/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java +++ b/components/org.apache.stratos.rest.endpoint/src/main/java/org/apache/stratos/rest/endpoint/api/StratosApiV41Utils.java @@ -541,7 +541,7 @@ public class StratosApiV41Utils { try { autoscalerServiceClient - .deployAutoscalingPolicy(autoscalePolicy); + .addAutoscalingPolicy(autoscalePolicy); } catch (RemoteException e) { log.error(e.getMessage(), e); throw new RestAPIException(e.getMessage(), e);
