Skip to content

Commit 8e0a414

Browse files
authored
Add ability to refresh hosting env (#176)
* fix path to category suggestions * ability to refresh hosting envs * add hosting event type * add config option requests
1 parent e948720 commit 8e0a414

File tree

11 files changed

+140
-3
lines changed

11 files changed

+140
-3
lines changed

src/main/java/com/redhat/labs/lodestar/model/event/EventType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ private EventType() {
3131
public static final String RELOAD_ARTIFACTS_EVENT_ADDRESS = "reload.artifacts.event";
3232
public static final String RELOAD_PARTICIPANTS_EVENT_ADDRESS = "reload.participants.event";
3333
public static final String RELOAD_ENGAGEMENT_STATUS_EVENT_ADDRESS = "reload.engagement.status.event";
34+
public static final String RELOAD_HOSTING_EVENT_ADDRESS = "reload.hosting.event";
3435

3536
}

src/main/java/com/redhat/labs/lodestar/resource/ConfigResource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redhat.labs.lodestar.resource;
22

3+
import java.util.Map;
34
import java.util.Optional;
45

56
import javax.enterprise.context.RequestScoped;
@@ -55,4 +56,22 @@ public Response invalidateRbacCache() {
5556
configService.invalidateCache();
5657
return Response.ok().build();
5758
}
59+
60+
@GET
61+
@Path("artifact/options")
62+
public Map<String, String> getArtifactOptions() {
63+
return configService.getArtifactOptions();
64+
}
65+
66+
@GET
67+
@Path("engagement/options")
68+
public Map<String, String> getEngagementOptions() {
69+
return configService.getEngagementOptions();
70+
}
71+
72+
@GET
73+
@Path("region/options")
74+
public Map<String, String> getRegionOptions() {
75+
return configService.getRegionOptions();
76+
}
5877
}

src/main/java/com/redhat/labs/lodestar/resource/HostingResource.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.redhat.labs.lodestar.model.filter.ListFilterOptions;
2525
import com.redhat.labs.lodestar.model.pagination.PagedHostingEnvironmentResults;
2626
import com.redhat.labs.lodestar.service.EngagementService;
27+
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
2728

2829
import java.util.*;
2930

@@ -32,6 +33,7 @@
3233
@Produces(MediaType.APPLICATION_JSON)
3334
@Consumes(MediaType.APPLICATION_JSON)
3435
@SecurityScheme(securitySchemeName = "jwt", type = SecuritySchemeType.HTTP, scheme = "bearer", bearerFormat = "JWT")
36+
@Tag(name = "Hosting Environments", description = "Hosting Environment Info")
3537
public class HostingResource {
3638

3739
@Inject

src/main/java/com/redhat/labs/lodestar/resource/RefreshResource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Response refresh(
4848
@Parameter(description = "Refresh participants") @QueryParam("participants") boolean refreshParticipants,
4949
@Parameter(description = "Refresh activity") @QueryParam("activity") boolean refreshActivity,
5050
@Parameter(description = "Refresh engagement status") @QueryParam("status") boolean refreshStatus,
51+
@Parameter(description = "Refresh hosting environments") @QueryParam("hosting") boolean refreshHosting,
5152
@Parameter(description = "Refresh engagements") @QueryParam("engagements") boolean refreshEngagements) {
5253

5354
boolean didPickSomething = false;
@@ -58,6 +59,11 @@ public Response refresh(
5859
didPickSomething = true;
5960
}
6061

62+
if(refreshHosting) {
63+
eventBus.publish(EventType.RELOAD_HOSTING_EVENT_ADDRESS, EventType.RELOAD_HOSTING_EVENT_ADDRESS);
64+
didPickSomething = true;
65+
}
66+
6167
if (refreshActivity) {
6268
eventBus.publish(EventType.RELOAD_ACTIVITY_EVENT_ADDRESS, EventType.RELOAD_ACTIVITY_EVENT_ADDRESS);
6369
didPickSomething = true;

src/main/java/com/redhat/labs/lodestar/rest/client/ConfigApiClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@
1919
@RegisterRestClient(configKey = "lodestar.config.api")
2020
@RegisterProvider(value = ServiceResponseMapper.class, priority = 50)
2121
@Path("/api/v1/configs/runtime")
22+
@Produces("application/json")
2223
public interface ConfigApiClient {
2324

2425
@GET
25-
@Produces("application/json")
2626
Response getRuntimeConfig(@QueryParam("type") String type);
2727

2828
@GET
2929
@Path("/rbac")
30-
@Produces("application/json")
3130
Map<String, List<String>> getPermission();
3231

32+
@GET
33+
@Path("artifact/options")
34+
Map<String, String> getArtifactOptions();
35+
36+
@GET
37+
@Path("engagement/options")
38+
Map<String, String> getEngagementOptions();
39+
40+
@GET
41+
@Path("region/options")
42+
Map<String, String> getRegionOptions();
43+
3344
}

src/main/java/com/redhat/labs/lodestar/service/ConfigService.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import javax.enterprise.context.ApplicationScoped;
99
import javax.inject.Inject;
10+
import javax.ws.rs.GET;
11+
import javax.ws.rs.Path;
1012
import javax.ws.rs.core.Response;
1113

1214
import org.eclipse.microprofile.rest.client.inject.RestClient;
@@ -40,12 +42,33 @@ public List<String> getPermission(String engagementType) {
4042
}
4143

4244
@CacheInvalidateAll(cacheName = "rbac-cache")
45+
@CacheInvalidateAll(cacheName = "artifact-options")
46+
@CacheInvalidateAll(cacheName = "engagement-options")
47+
@CacheInvalidateAll(cacheName = "region-options")
4348
public void invalidateCache() {
44-
LOGGER.debug("Invalidating rbac cache");
49+
LOGGER.debug("Invalidating config cache");
4550
}
4651

4752
public Response getRuntimeConfig(Optional<String> type) {
4853

4954
return configApiClient.getRuntimeConfig(type.isPresent() ? type.get() : null);
5055
}
56+
57+
@CacheResult(cacheName = "artifact-options")
58+
public Map<String, String> getArtifactOptions() {
59+
LOGGER.debug("cache miss for artifact options");
60+
return configApiClient.getArtifactOptions();
61+
}
62+
63+
@CacheResult(cacheName = "engagement-options")
64+
public Map<String, String> getEngagementOptions() {
65+
LOGGER.debug("cache miss for engagement options");
66+
return configApiClient.getEngagementOptions();
67+
}
68+
69+
@CacheResult(cacheName = "region-options")
70+
public Map<String, String> getRegionOptions() {
71+
LOGGER.debug("cache miss for region options");
72+
return configApiClient.getRegionOptions();
73+
}
5174
}

src/main/java/com/redhat/labs/lodestar/service/HostingService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,23 @@
22

33
import com.redhat.labs.lodestar.model.Author;
44
import com.redhat.labs.lodestar.model.HostingEnvironment;
5+
import com.redhat.labs.lodestar.model.event.EventType;
56
import com.redhat.labs.lodestar.rest.client.HostingEnvironmentApiClient;
7+
import io.quarkus.vertx.ConsumeEvent;
68
import org.eclipse.microprofile.rest.client.inject.RestClient;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
711

812
import javax.enterprise.context.ApplicationScoped;
913
import javax.inject.Inject;
14+
import javax.ws.rs.WebApplicationException;
1015
import javax.ws.rs.core.*;
1116
import java.util.List;
1217
import java.util.Set;
1318

1419
@ApplicationScoped
1520
public class HostingService {
21+
private static final Logger LOGGER = LoggerFactory.getLogger(HostingService.class);
1622

1723
@Inject
1824
@RestClient
@@ -33,4 +39,15 @@ public List<HostingEnvironment> updateAndReload(String engagementUuid, List<Host
3339
public Response isSubdomainValid(String engagementUuid, String subdomain) {
3440
return hostingEnvironmentApiClient.isSubdomainValid(engagementUuid, subdomain);
3541
}
42+
43+
@ConsumeEvent(value = EventType.RELOAD_HOSTING_EVENT_ADDRESS, blocking = true)
44+
public void refresh(String message) {
45+
try {
46+
LOGGER.debug("refresh {}", message);
47+
hostingEnvironmentApiClient.refresh();
48+
LOGGER.debug("refresh {} completed", message);
49+
} catch (WebApplicationException wae) { //without catching this it will fail silently
50+
LOGGER.error("Failed to refresh hosting {}", wae.getResponse(), wae);
51+
}
52+
}
3653
}

src/test/java/com/redhat/labs/lodestar/resource/ConfigResourceTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.redhat.labs.lodestar.resource;
22

33
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.CoreMatchers.equalTo;
45
import static org.hamcrest.CoreMatchers.is;
56

67
import javax.ws.rs.core.Response;
78

9+
import com.fasterxml.jackson.databind.ObjectMapper;
810
import com.redhat.labs.lodestar.rest.client.ConfigApiClient;
11+
import com.redhat.labs.lodestar.utils.ResourceLoader;
912
import io.quarkus.test.common.http.TestHTTPEndpoint;
1013
import io.quarkus.test.junit.mockito.InjectMock;
1114
import org.eclipse.microprofile.rest.client.inject.RestClient;
15+
import org.hamcrest.Matchers;
16+
import org.junit.jupiter.api.BeforeAll;
1217
import org.junit.jupiter.api.Tag;
1318
import org.junit.jupiter.api.Test;
1419
import org.mockito.Mockito;
@@ -17,15 +22,26 @@
1722

1823
import io.quarkus.test.junit.QuarkusTest;
1924

25+
import java.util.Map;
26+
2027
@QuarkusTest
2128
@TestHTTPEndpoint(ConfigResource.class)
2229
@Tag("nested")
2330
class ConfigResourceTest {
2431

32+
static String VALID_TOKEN;
33+
2534
@InjectMock
2635
@RestClient
2736
public ConfigApiClient configApiClient;
2837

38+
ObjectMapper om = new ObjectMapper();
39+
40+
@BeforeAll
41+
static void generateToken() {
42+
VALID_TOKEN = TokenUtils.generateTokenString("/JwtClaimsReader.json");
43+
}
44+
2945
@Test
3046
void testGetConfigTokenHasWrongRole() {
3147

@@ -91,4 +107,34 @@ void testVoidCache() {
91107
given().when().auth().oauth2(token).put("/rbac/cache").then().statusCode(200);
92108
}
93109

110+
@Test
111+
void testRegionOptions() throws Exception {
112+
String regions = ResourceLoader.load("config-region-options.json");
113+
Map<String, String> regionMap = om.readValue(regions, Map.class);
114+
Mockito.when(configApiClient.getRegionOptions()).thenReturn(regionMap);
115+
given().when().auth().oauth2(VALID_TOKEN).get("/region/options").then().statusCode(200)
116+
.body("$", Matchers.hasKey("region1"))
117+
.body("$", Matchers.hasKey("region2"));
118+
}
119+
120+
@Test
121+
void testArtifactOptions() throws Exception {
122+
String artifacts = ResourceLoader.load("config-artifact-options.json");
123+
Map<String, String> artifactsMap = om.readValue(artifacts, Map.class);
124+
Mockito.when(configApiClient.getArtifactOptions()).thenReturn(artifactsMap);
125+
given().when().auth().oauth2(VALID_TOKEN).get("/artifact/options").then().statusCode(200)
126+
.body("$", Matchers.hasKey("doc"))
127+
.body("$", Matchers.hasKey("paper"));
128+
}
129+
130+
@Test
131+
void testEngagementOptions() throws Exception {
132+
String engagements = ResourceLoader.load("config-engagement-options.json");
133+
Map<String, String> engagementsMap = om.readValue(engagements, Map.class);
134+
Mockito.when(configApiClient.getEngagementOptions()).thenReturn(engagementsMap);
135+
given().when().auth().oauth2(VALID_TOKEN).get("/engagement/options").then().statusCode(200)
136+
.body("$", Matchers.hasKey("res"))
137+
.body("$", Matchers.hasKey("training"));
138+
}
139+
94140
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"doc": "Document",
3+
"paper": "White Paper"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"res": "Resy",
3+
"training": "Training"
4+
}

0 commit comments

Comments
 (0)