Skip to content

Commit

Permalink
Merge pull request #9832 from mcalmer/issv3-refresh-after-register
Browse files Browse the repository at this point in the history
perform product refresh on peripheral after registration
  • Loading branch information
mcalmer authored Feb 23, 2025
2 parents 5d04c9c + f503915 commit 3dbe085
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions java/buildconf/ivy/ivy-suse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<!-- Compilation and testing -->
<dependency org="suse" name="velocity-engine-core" rev="2.3"/>
<dependency org="suse" name="jetty-util" rev="9.4.56" />
<dependency org="suse" name="jetty-server" rev="9.4.56" />
<dependency org="suse" name="jetty-http" rev="9.4.56" />
<dependency org="suse" name="jetty-io" rev="9.4.56" />
<dependency org="checkstyle" name="checkstyle" rev="10.12.7" transitive="false">
<artifact name="all" type="jar" url="https://github.com/checkstyle/checkstyle/releases/download/checkstyle-10.12.7/checkstyle-10.12.7-all.jar"/>
</dependency>
Expand Down
6 changes: 6 additions & 0 deletions java/buildconf/ivy/obs-maven-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ artifacts:
repository: Uyuni_Other
- artifact: jetty-util
repository: Leap_sle
- artifact: jetty-server
repository: Leap_sle
- artifact: jetty-http
repository: Leap_sle
- artifact: jetty-io
repository: Leap_sle
- artifact: glassfish-jaxb-api
repository: Leap
- artifact: jaxb-runtime
Expand Down
11 changes: 11 additions & 0 deletions java/code/src/com/redhat/rhn/taskomatic/TaskomaticApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,15 @@ public void scheduleSingleGpgKeyImport(String gpgKey) throws TaskomaticApiExcept
}
invoke("tasko.scheduleSingleSatBunchRun", "custom-gpg-key-import-bunch", Map.of("gpg-key", gpgKey));
}

/**
* Schedule a product refresh via taskomatic
* @param earliest earliest execution
* @param withReposync perform also a repo-sync
* @throws TaskomaticApiException if there is an error
*/
public void scheduleProductRefresh(Date earliest, boolean withReposync) throws TaskomaticApiException {
invoke("tasko.scheduleSingleSatBunchRun", "mgr-sync-refresh-bunch",
Map.of("noRepoSync", !withReposync), earliest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public String replaceTokens(String newHubToken) throws IOException {
return invokePost("hub/sync", "replaceTokens", newHubToken, String.class);
}

@Override
public void scheduleProductRefresh() throws IOException {
invokePost("hub", "scheduleProductRefresh", Map.of());
}

@Override
public void deregister() throws IOException {
invokePost("hub/sync", "deregister", null);
Expand Down
25 changes: 23 additions & 2 deletions java/code/src/com/suse/manager/hub/HubController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.redhat.rhn.common.hibernate.ConnectionManager;
import com.redhat.rhn.common.hibernate.ConnectionManagerFactory;
import com.redhat.rhn.common.hibernate.ReportDbHibernateFactory;
import com.redhat.rhn.taskomatic.TaskomaticApi;
import com.redhat.rhn.taskomatic.TaskomaticApiException;
import com.redhat.rhn.taskomatic.task.ReportDBHelper;

Expand All @@ -52,6 +53,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand All @@ -66,6 +69,8 @@ public class HubController {

private final HubManager hubManager;

private final TaskomaticApi taskomaticApi;

private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Date.class, new ECMAScriptDateAdapter())
.serializeNulls()
Expand All @@ -75,15 +80,17 @@ public class HubController {
* Default constructor
*/
public HubController() {
this(new HubManager());
this(new HubManager(), new TaskomaticApi());
}

/**
* Builds an instance with the specified hub manager
* @param hubManagerIn the hub manager
* @param taskomaticApiIn the taskomatic api
*/
public HubController(HubManager hubManagerIn) {
public HubController(HubManager hubManagerIn, TaskomaticApi taskomaticApiIn) {
this.hubManager = hubManagerIn;
this.taskomaticApi = taskomaticApiIn;
}

/**
Expand All @@ -97,6 +104,8 @@ public void initRoutes() {
post("/hub/sync/storeCredentials", asJson(usingTokenAuthentication(onlyFromHub(this::storeCredentials))));
post("/hub/sync/setHubDetails", asJson(usingTokenAuthentication(onlyFromHub(this::setHubDetails))));
get("/hub/managerinfo", asJson(usingTokenAuthentication(onlyFromHub(this::getManagerInfo))));
post("/hub/scheduleProductRefresh",
asJson(usingTokenAuthentication(onlyFromHub(this::scheduleProductRefresh))));
post("/hub/storeReportDbCredentials",
asJson(usingTokenAuthentication(onlyFromHub(this::setReportDbCredentials))));
post("/hub/removeReportDbCredentials",
Expand All @@ -121,6 +130,18 @@ public void initRoutes() {
asJson(usingTokenAuthentication(onlyFromHub(this::synchronizeSubscriptions))));
}

private String scheduleProductRefresh(Request request, Response response, IssAccessToken issAccessToken) {
try {
Date earliest = Date.from(Instant.now().plus(10, ChronoUnit.SECONDS));
taskomaticApi.scheduleProductRefresh(earliest, false);
}
catch (TaskomaticApiException ex) {
LOGGER.error("Scheduling a product refresh failed. ", ex);
return internalServerError(response, "Scheduling product refresh failed");
}
return success(response);
}

private String setHubDetails(Request request, Response response, IssAccessToken accessToken) {
Map<String, String> data = GSON.fromJson(request.body(), Map.class);

Expand Down
5 changes: 5 additions & 0 deletions java/code/src/com/suse/manager/hub/HubInternalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public interface HubInternalClient {
*/
String replaceTokens(String newHubToken) throws IOException;

/**
* Schedule a product refresh on the remote peripheral server
* @throws IOException when the communication fails
*/
void scheduleProductRefresh() throws IOException;
}
1 change: 1 addition & 0 deletions java/code/src/com/suse/manager/hub/HubManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ private void registerToRemote(User user, IssServer remoteServer, String remoteTo
if (changed) {
setReportDbUser(user, peripheralServer, false);
}
internalApi.scheduleProductRefresh();
}
catch (Exception ex) {
// cleanup the remote side
Expand Down
28 changes: 27 additions & 1 deletion java/code/src/com/suse/manager/hub/test/HubControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.frontend.xmlrpc.channel.software.ChannelSoftwareHandler;
import com.redhat.rhn.manager.channel.ChannelManager;
import com.redhat.rhn.taskomatic.TaskomaticApi;
import com.redhat.rhn.testing.ChannelTestUtils;
import com.redhat.rhn.testing.ErrataTestUtils;
import com.redhat.rhn.testing.JMockBaseTestCaseWithUser;
import com.redhat.rhn.testing.TestUtils;
import com.redhat.rhn.testing.UserTestUtils;

import com.suse.manager.hub.HubController;
import com.suse.manager.hub.HubManager;
import com.suse.manager.model.hub.ChannelInfoJson;
import com.suse.manager.model.hub.CustomChannelInfoJson;
import com.suse.manager.model.hub.IssAccessToken;
Expand All @@ -59,6 +61,8 @@

import com.google.gson.JsonObject;

import org.jmock.Expectations;
import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -93,7 +97,13 @@ public class HubControllerTest extends JMockBaseTestCaseWithUser {
@BeforeEach
public void setUp() throws Exception {
super.setUp();
HubController dummyHubController = new HubController();
setImposteriser(ByteBuddyClassImposteriser.INSTANCE);
TaskomaticApi taskomaticMock = mock(TaskomaticApi.class);
context().checking(new Expectations() {{
allowing(taskomaticMock).scheduleProductRefresh(with(any(Date.class)), with(false));
}});

HubController dummyHubController = new HubController(new HubManager(), taskomaticMock);
dummyHubController.initRoutes();
//add dummy route that throws
post("/hub/testThrowsRuntimeException", asJson(usingTokenAuthentication(this::testThrowsRuntimeException)));
Expand All @@ -110,6 +120,7 @@ private static Stream<Arguments> allApiEndpoints() {
Arguments.of(HttpMethod.post, "/hub/sync/replaceTokens", IssRole.HUB),
Arguments.of(HttpMethod.post, "/hub/sync/storeCredentials", IssRole.HUB),
Arguments.of(HttpMethod.post, "/hub/sync/setHubDetails", IssRole.HUB),
Arguments.of(HttpMethod.post, "/hub/scheduleProductRefresh", IssRole.HUB),
Arguments.of(HttpMethod.get, "/hub/managerinfo", IssRole.HUB),
Arguments.of(HttpMethod.post, "/hub/storeReportDbCredentials", IssRole.HUB),
Arguments.of(HttpMethod.post, "/hub/removeReportDbCredentials", IssRole.HUB),
Expand Down Expand Up @@ -329,6 +340,21 @@ public void checkStoreReportDbCredentialsApiEndpoint() throws Exception {
"cleanup of user not working for user " + testReportDbUserName);
}

@Test
public void checkScheduleProductRefreshEndpoint() throws Exception {
String apiUnderTest = "/hub/scheduleProductRefresh";

String answer = (String) testUtils.withServerFqdn(DUMMY_SERVER_FQDN)
.withApiEndpoint(apiUnderTest)
.withHttpMethod(HttpMethod.post)
.withRole(IssRole.HUB)
.withBearerTokenInHeaders()
.simulateControllerApiCall();
JsonObject jsonObj = Json.GSON.fromJson(answer, JsonObject.class);

assertTrue(jsonObj.get("success").getAsBoolean(), apiUnderTest + " API call is failing");
}

@Test
public void checkRemoveReportDbCredentialsApiEndpoint() throws Exception {
String apiUnderTest = "/hub/removeReportDbCredentials";
Expand Down
2 changes: 2 additions & 0 deletions java/code/src/com/suse/manager/hub/test/HubManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ public void canRegisterPeripheralWithUserNameAndPassword()
will(returnValue(mgrInfo));

allowing(internalClient).storeReportDbCredentials(with(any(String.class)), with(any(String.class)));

allowing(internalClient).scheduleProductRefresh();
}});

// Register the remote server as PERIPHERAL for this local server
Expand Down
2 changes: 1 addition & 1 deletion java/code/src/com/suse/manager/webui/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private static void initISSv3Routes() {
HubManager hubManager = new HubManager();

// ISS v3 Internal APIs
HubController hubController = new HubController(hubManager);
HubController hubController = new HubController(hubManager, new TaskomaticApi());
hubController.initRoutes();

// API for the web interface
Expand Down

0 comments on commit 3dbe085

Please sign in to comment.