Skip to content

Commit

Permalink
feat(rest.packages.provider): add deploy/v2 Rest API (#4868)
Browse files Browse the repository at this point in the history
* feat: add sources

WARNING: Build is still broken

* fix: kura.basedir

WARNING: build still broken

* fix: MANIFEST

* fix: packages names

* feat(distrib): add rest.packages bundle

* fix: wrong manifest

* test: add org.eclipse.kura.rest.packages.provider.test

WARNING: Failing

* fix(test): unit tests

* fix(test): correct pid

* fix(test): fix PID once again

* fix(test): request correct component

* style: add copyright headers

* style(test): fix copyright headers

* feat: update URL deploy/v1 -> deploy/v2

* chore: remove files committed by mistake

* refactor(test): remove old integration tests

* test: first working version of integration tests

* test: remove unnecessary code

* refactor: rename ConfigurationRestServiceTest -> PackagesRestServiceTest

* test: fix typo

* test: test with existing installed bundles

WARNING: Passing even if it shouldn't

* test: add debugging "verify"s

* refactor(test): return mock instaed of building a class

* fix(test): correctly inject DeploymentAdmin mock

* test: fix mock reset

* refactor(test): improve readability, remove unnecessary vars

* refactor(test): use Gerkhin style

* style: fix copyright header

* refactor: add then* statement

* test: add uninstall tests

* test: add more complete testing

* fix(build): add forgotten entry in test pom

* test: remove unused imports

* fix(test): remove unnecessary throws statements

* style: applied cleanup profile

* style: applied cleanup profile

* build: remove unnecessary dependencies

* style: apply cleanup profile

* build: removed unnecessary dependencies, fixed export

* build: tag api as internal

* refactor: remove unnecessary resource

* style: fix indentations

* test: use mocks instead of... whatever that was

* style: partial migration to Gerkhin style

* style: complete migration to Gerkhin style

* refactor: move methods to their section

* refactor: rename method

* refactor: more readable test names

* style: fix formatting

* refactor: remove unnecessary unbind methods
  • Loading branch information
mattdibi authored Oct 10, 2023
1 parent d79d486 commit a9e4c78
Show file tree
Hide file tree
Showing 19 changed files with 1,045 additions and 0 deletions.
1 change: 1 addition & 0 deletions kura/distrib/config/kura.build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ org.eclipse.kura.log.filesystem.provider.version=1.3.0-SNAPSHOT
org.eclipse.kura.rest.configuration.provider.version=1.3.0-SNAPSHOT
org.eclipse.kura.rest.inventory.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.rest.command.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.rest.packages.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.rest.position.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.rest.security.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.rest.service.listing.provider.version=1.0.0-SNAPSHOT
Expand Down
7 changes: 7 additions & 0 deletions kura/distrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@
<artifactId>org.eclipse.kura.rest.command.provider</artifactId>
<version>${org.eclipse.kura.rest.command.provider.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.kura</groupId>
<artifactId>org.eclipse.kura.rest.packages.provider</artifactId>
<version>${org.eclipse.kura.rest.packages.provider.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.kura</groupId>
<artifactId>org.eclipse.kura.rest.position.provider</artifactId>
Expand Down Expand Up @@ -820,6 +825,7 @@
<move file="target/plugins/org.eclipse.kura.rest.configuration.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.configuration.provider_${org.eclipse.kura.rest.configuration.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.inventory.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.inventory.provider_${org.eclipse.kura.rest.inventory.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.command.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.command.provider_${org.eclipse.kura.rest.command.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.packages.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.packages.provider_${org.eclipse.kura.rest.packages.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.position.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.position.provider_${org.eclipse.kura.rest.position.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.security.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.security.provider_${org.eclipse.kura.rest.security.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.service.listing.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.service.listing.provider_${org.eclipse.kura.rest.service.listing.provider.version}.jar" />
Expand Down Expand Up @@ -2535,6 +2541,7 @@
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.configuration.provider_${org.eclipse.kura.rest.configuration.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.inventory.provider_${org.eclipse.kura.rest.inventory.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.command.provider_${org.eclipse.kura.rest.command.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.packages.provider_${org.eclipse.kura.rest.packages.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.position.provider_${org.eclipse.kura.rest.position.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.security.provider_${org.eclipse.kura.rest.security.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.service.listing.provider_${org.eclipse.kura.rest.service.listing.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
Expand Down
5 changes: 5 additions & 0 deletions kura/distrib/src/main/ant/build_equinox_distrib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,8 @@ fi]]>
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.inventory.provider_${org.eclipse.kura.rest.inventory.provider.version}.jar@4:start" />
<entry key="osgi.bundles" operation="+"
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.command.provider_${org.eclipse.kura.rest.command.provider.version}.jar@4:start" />
<entry key="osgi.bundles" operation="+"
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.packages.provider_${org.eclipse.kura.rest.packages.provider.version}.jar@4:start" />
<entry key="osgi.bundles" operation="+"
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.position.provider_${org.eclipse.kura.rest.position.provider.version}.jar@4:start" />
<entry key="osgi.bundles" operation="+"
Expand Down Expand Up @@ -1415,6 +1417,9 @@ fi]]>
<zipfileset
file="${project.build.directory}/plugins/org.eclipse.kura.rest.command.provider_${org.eclipse.kura.rest.command.provider.version}.jar"
prefix="${build.output.name}/${plugins.folder}" />
<zipfileset
file="${project.build.directory}/plugins/org.eclipse.kura.rest.packages.provider_${org.eclipse.kura.rest.packages.provider.version}.jar"
prefix="${build.output.name}/${plugins.folder}" />
<zipfileset
file="${project.build.directory}/plugins/org.eclipse.kura.rest.position.provider_${org.eclipse.kura.rest.position.provider.version}.jar"
prefix="${build.output.name}/${plugins.folder}" />
Expand Down
22 changes: 22 additions & 0 deletions kura/org.eclipse.kura.rest.packages.provider/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Kura Deployment Packages Rest Provider
Bundle-SymbolicName: org.eclipse.kura.rest.packages.provider;singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Bundle-ClassPath: .
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/*.xml
Import-Package: com.google.gson;version="2.7.0",
javax.annotation.security;version="1.2.0",
javax.ws.rs;version="2.0.1",
javax.ws.rs.core;version="2.0.1",
javax.ws.rs.ext;version="2.0.1",
org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.deployment.agent;version="[1.0,2.0)",
org.osgi.framework;version="1.8.0",
org.osgi.service.component;version="1.3.0",
org.osgi.service.deploymentadmin;version="1.0.0",
org.osgi.service.useradmin;version="1.1.0";resolution:=optional,
org.slf4j;version="1.7.21"
Export-Package: org.eclipse.kura.rest.deployment.agent.api;version="1.0.0";x-internal:=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Eurotech and/or its affiliates and others
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Eurotech
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.kura.rest.deployment.agent.DeploymentRestService">
<implementation class="org.eclipse.kura.internal.rest.deployment.agent.DeploymentRestService"/>
<property name="kura.service.pid" type="String" value="org.eclipse.kura.rest.deployment.agent.DeploymentRestService"/>
<service>
<provide interface="org.eclipse.kura.internal.rest.deployment.agent.DeploymentRestService"/>
</service>
<reference name="DeploymentAgentService"
cardinality="1..1"
interface="org.eclipse.kura.deployment.agent.DeploymentAgentService"
bind="setDeploymentAgentService"
policy="static"/>
<reference name="DeploymentAdmin"
cardinality="1..1"
interface="org.osgi.service.deploymentadmin.DeploymentAdmin"
bind="setDeploymentAdmin"
policy="static"/>
<reference name="UserAdmin"
cardinality="0..1"
interface="org.osgi.service.useradmin.UserAdmin"
bind="setUserAdmin"
policy="static"/>
</scr:component>
17 changes: 17 additions & 0 deletions kura/org.eclipse.kura.rest.packages.provider/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2023 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Eurotech
#
output.. = target/classes
bin.includes = .,\
META-INF/,\
OSGI-INF/
source.. = src/main/java/
35 changes: 35 additions & 0 deletions kura/org.eclipse.kura.rest.packages.provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Eurotech and/or its affiliates and others
This program and the accompanying materials are made
available under the terms of the Eclipse Public License 2.0
which is available at https://www.eclipse.org/legal/epl-2.0/
SPDX-License-Identifier: EPL-2.0
Contributors:
Eurotech
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.kura</groupId>
<artifactId>kura</artifactId>
<version>5.4.0-SNAPSHOT</version>
</parent>

<properties>
<kura.basedir>${project.basedir}/..</kura.basedir>
<sonar.coverage.jacoco.xmlReportPaths>
${project.basedir}/../test/*/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>

<artifactId>org.eclipse.kura.rest.packages.provider</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*******************************************************************************/
package org.eclipse.kura.internal.rest.deployment.agent;

public class DeploymentPackageInfo {

private final String name;
private final String version;

public DeploymentPackageInfo(String name, String version) {
this.name = name;
this.version = version;
}

public String getName() {
return this.name;
}

public String getVersion() {
return this.version;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*******************************************************************************/
package org.eclipse.kura.internal.rest.deployment.agent;

import static org.eclipse.kura.rest.deployment.agent.api.Validable.validate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.kura.deployment.agent.DeploymentAgentService;
import org.eclipse.kura.rest.deployment.agent.api.DeploymentRequestStatus;
import org.eclipse.kura.rest.deployment.agent.api.InstallRequest;
import org.osgi.service.deploymentadmin.DeploymentAdmin;
import org.osgi.service.deploymentadmin.DeploymentPackage;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdmin;

@Path("/deploy/v2")
public class DeploymentRestService {

private static final String KURA_PERMISSION_REST_DEPLOY_ROLE = "kura.permission.rest.deploy";
private static final String ERROR_INSTALLING_PACKAGE = "Error installing deployment package: ";
private static final String ERROR_UNINSTALLING_PACKAGE = "Error uninstalling deployment package: ";
private static final String BAD_REQUEST_MESSAGE = "Bad request";

private DeploymentAdmin deploymentAdmin;
private DeploymentAgentService deploymentAgentService;
private UserAdmin userAdmin;

public void setUserAdmin(UserAdmin userAdmin) {
this.userAdmin = userAdmin;
this.userAdmin.createRole(KURA_PERMISSION_REST_DEPLOY_ROLE, Role.GROUP);
}

public void setDeploymentAdmin(DeploymentAdmin deploymentAdmin) {
this.deploymentAdmin = deploymentAdmin;
}

public void setDeploymentAgentService(DeploymentAgentService deploymentAgentService) {
this.deploymentAgentService = deploymentAgentService;
}

/**
* GET method.
*
* Provides the list of all the deployment packages installed and tracked by the framework.
*
* @return a list of {@link DeploymentPackageInfo}
*/
@GET
@RolesAllowed("deploy")
@Produces(MediaType.APPLICATION_JSON)
public List<DeploymentPackageInfo> listDeploymentPackages() {

List<DeploymentPackageInfo> deploymentPackageInfos = new ArrayList<>();
List<DeploymentPackage> deploymentPackages = Arrays.asList(this.deploymentAdmin.listDeploymentPackages());

deploymentPackages.forEach(
dp -> deploymentPackageInfos.add(new DeploymentPackageInfo(dp.getName(), dp.getVersion().toString())));

return deploymentPackageInfos;
}

/**
* POST method.
*
* Installs the deployment package specified in the {@link InstallRequest}. If the request was already issued for
* the same {@link InstallRequest}, it returns the status of the installation process.
*
* @param installRequest
* @return a {@link DeploymentRequestStatus} object that represents the status of the installation request
*/
@POST
@RolesAllowed("deploy")
@Path("/_install")
@Produces(MediaType.APPLICATION_JSON)
public DeploymentRequestStatus installDeploymentPackage(InstallRequest installRequest) {
validate(installRequest, BAD_REQUEST_MESSAGE);
String url = installRequest.getUrl();

if (this.deploymentAgentService.isInstallingDeploymentPackage(url)) {
return DeploymentRequestStatus.INSTALLING;
}

try {
this.deploymentAgentService.installDeploymentPackageAsync(url);
} catch (Exception e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.type(MediaType.TEXT_PLAIN).entity(ERROR_INSTALLING_PACKAGE + url).build());
}

return DeploymentRequestStatus.REQUEST_RECEIVED;
}

/**
* DELETE method.
*
* Uninstalls the deployment package identified by the specified name. If the request was already issued, it reports
* the status of the uninstallation operation.
*
* @param name
* @return a {@link DeploymentRequestStatus} object that represents the status of the uninstallation request
*/
@DELETE
@RolesAllowed("deploy")
@Path("/{name}")
@Produces(MediaType.APPLICATION_JSON)
public DeploymentRequestStatus uninstallDeploymentPackage(@PathParam("name") String name) {
if (this.deploymentAgentService.isUninstallingDeploymentPackage(name)) {
return DeploymentRequestStatus.UNINSTALLING;
}
try {
this.deploymentAgentService.uninstallDeploymentPackageAsync(name);
} catch (Exception e) {
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.type(MediaType.TEXT_PLAIN).entity(ERROR_UNINSTALLING_PACKAGE + name).build());
}

return DeploymentRequestStatus.REQUEST_RECEIVED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2023 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*******************************************************************************/
package org.eclipse.kura.rest.deployment.agent.api;

/**
* Enumeration representing the status of the deployment requests received via REST.
*
*/
public enum DeploymentRequestStatus {
REQUEST_RECEIVED,
INSTALLING,
UNINSTALLING;
}
Loading

0 comments on commit a9e4c78

Please sign in to comment.