Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosting #162

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Priority(4000)
public class LodeStarGitLabAPIServiceResponseMapper implements ResponseExceptionMapper<RuntimeException> {
private static final Logger LOGGER = LoggerFactory.getLogger(LodeStarGitLabAPIServiceResponseMapper.class);

@Override
public RuntimeException toThrowable(Response response) {
int status = response.getStatus();

String msg = getBody(response);

return new WebApplicationException(msg, status);
LOGGER.error("Rest client response error {} {}", status, msg);

return new WebApplicationException(msg, status);
}

private String getBody(Response response) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/redhat/labs/lodestar/model/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.redhat.labs.lodestar.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@Builder
@Data
@AllArgsConstructor
public class ErrorMessage {

String message;

public ErrorMessage(String messasge, Object... substitutions) {
this.message = String.format(messasge, substitutions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.redhat.labs.lodestar.model;

public enum HostingEnvOpenShfitRollup {
OCP_VERSION("ocpVersion"), OCP_VERSION_MAJOR("ocpMajorVersion"), OCP_VERSION_MINOR("ocpMinorVersion");

String depth;

HostingEnvOpenShfitRollup(String column) {
this.depth = column;
}

public final String getColumn() {
return depth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ private EventType() {
public static final String GET_PAGE_OF_ENGAGEMENTS_EVENT_ADDRESS = "get.page.of.engagements.event";
public static final String PERSIST_ENGAGEMENT_LIST_EVENT_ADDRESS = "persist.engagement.list.event";
public static final String PERSIST_ENGAGEMENT_EVENT_ADDRESS = "persist.engagement.event";

public static final String UPDATE_ARTIFACTS_EVENT_ADDRESS = "update.artifacts.event";
public static final String UPDATE_COMMITS_EVENT_ADDRESS = "update.commits.event";
public static final String UPDATE_STATUS_EVENT_ADDRESS = "update.status.event";
public static final String UPDATE_HOSTING_EVENT_ADDRESS = "update.hosting.event";
public static final String UPDATE_PARTICIPANTS_EVENT_ADDESS = "update.participants.event";
public static final String UPDATE_ARTIFACTS_EVENT_ADDRESS = "update.artifacts.event";
public static final String UPDATE_STATUS_EVENT_ADDRESS = "update.status.event";

public static final String RELOAD_ACTIVITY_EVENT_ADDRESS = "reload.activity.event";
public static final String RELOAD_ARTIFACTS_EVENT_ADDRESS = "reload.artifacts.event";
public static final String RELOAD_HOSTING_EVENT_ADDRESS = "reload.hosting.event";
public static final String RELOAD_PARTICIPANTS_EVENT_ADDRESS = "reload.participants.event";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.redhat.labs.lodestar.model.filter;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;

import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class PagingOptions {

@Parameter(description = "0 based page number")
@QueryParam("page")
@DefaultValue("0")
private int page;

@QueryParam("pageSize")
@DefaultValue("100")
private int pageSize;

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static com.mongodb.client.model.Filters.regex;
import static com.mongodb.client.model.Projections.exclude;
import static com.mongodb.client.model.Projections.include;
import static com.mongodb.client.model.Updates.set;
import static com.mongodb.client.model.Updates.combine;
import static com.mongodb.client.model.Updates.set;

import java.util.Arrays;
import java.util.List;
Expand All @@ -31,7 +31,6 @@
import com.redhat.labs.lodestar.model.Commit;
import com.redhat.labs.lodestar.model.Engagement;
import com.redhat.labs.lodestar.model.EngagementUserSummary;
import com.redhat.labs.lodestar.model.HostingEnvironment;
import com.redhat.labs.lodestar.model.Score;
import com.redhat.labs.lodestar.model.Status;
import com.redhat.labs.lodestar.model.UseCase;
Expand All @@ -40,7 +39,6 @@
import com.redhat.labs.lodestar.model.pagination.PagedArtifactResults;
import com.redhat.labs.lodestar.model.pagination.PagedCategoryResults;
import com.redhat.labs.lodestar.model.pagination.PagedEngagementResults;
import com.redhat.labs.lodestar.model.pagination.PagedHostingEnvironmentResults;
import com.redhat.labs.lodestar.model.pagination.PagedScoreResults;
import com.redhat.labs.lodestar.model.pagination.PagedStringResults;
import com.redhat.labs.lodestar.model.pagination.PagedUseCaseResults;
Expand Down Expand Up @@ -408,33 +406,6 @@ public PagedScoreResults findScores(ListFilterOptions filterOptions) {

}

/**
* Returns the {@link PagedHostingEnvironmentResults} containing the
* {@link HostingEnvironment}s that match the given {@link ListFilterOptions}.
*
* @param filterOptions
* @return
*/
public PagedHostingEnvironmentResults findHostingEnvironments(ListFilterOptions filterOptions) {

filterOptions.setUnwindFieldName(Optional.of("hostingEnvironments"));
filterOptions.setUnwindProjectFieldNames(Optional.of(ClassFieldUtils
.classFieldNamesAsCommaSeparatedString(HostingEnvironment.class, Optional.of("hostingEnvironments"))));

List<Bson> pipeline = MongoAggregationHelper.generatePagedAggregationPipeline(filterOptions);
Optional<PagedHostingEnvironmentResults> optional = findFirstFromIterable(
mongoCollection().aggregate(pipeline, PagedHostingEnvironmentResults.class));

PagedHostingEnvironmentResults results = optional
.orElse(PagedHostingEnvironmentResults.builder().results(Arrays.asList()).build());

results.setCurrentPage(filterOptions.getPage().orElse(1));
results.setPerPage(filterOptions.getPerPage().orElse(20));

return results;

}

/**
* Returns the {@link PagedUseCaseResults} containing the {@link UseCase}s that
* match the given {@link ListFilterOptions}.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.redhat.labs.lodestar.resource;

import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.eclipse.microprofile.jwt.JsonWebToken;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.annotation.Timed;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeType;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
import org.eclipse.microprofile.openapi.annotations.security.SecurityScheme;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import com.redhat.labs.lodestar.model.Engagement;
import com.redhat.labs.lodestar.model.HostingEnvOpenShfitRollup;
import com.redhat.labs.lodestar.model.HostingEnvironment;
import com.redhat.labs.lodestar.model.filter.PagingOptions;
import com.redhat.labs.lodestar.service.ConfigService;
import com.redhat.labs.lodestar.service.EngagementService;
import com.redhat.labs.lodestar.service.HostingService;
import com.redhat.labs.lodestar.util.JWTUtils;

@RequestScoped
@Path("/hosting/environments")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@SecurityScheme(securitySchemeName = "jwt", type = SecuritySchemeType.HTTP, scheme = "bearer", bearerFormat = "JWT")
@Tag(name = "Hosting", description = "Hosting environment apis")
public class HostingEnvironmentResource {

@Inject
JsonWebToken jwt;

@Inject
JWTUtils jwtUtils;

@Inject
EngagementService engagementService;

@Inject
ConfigService configService;

@Inject
HostingService hostingService;

@GET
@SecurityRequirement(name = "jwt", scopes = {})
@APIResponses(value = { @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"),
@APIResponse(responseCode = "200", description = "hosting environments have been returned.") })
@Operation(summary = "Returns engagement hosting environments")
@Timed(name = "hosting-get-all-timer", unit = MetricUnits.MILLISECONDS)
public Response getHostingEnvironments(@Context UriInfo uriInfo, @BeanParam PagingOptions pagingOptions) {
return hostingService.getHostingEnvironments(pagingOptions.getPage(), pagingOptions.getPageSize());
}

@GET
@Path("/engagements/{engagementUuid}")
@SecurityRequirement(name = "jwt", scopes = {})
@Timed(name = "hosting-env-engagement-timer", unit = MetricUnits.MILLISECONDS)
public Response getHostingForEngagementUuid(@PathParam(value = "engagementUuid") String engagementUuid) {
return hostingService.getHostingEnvironments(engagementUuid);
}

@PUT
@Path("/engagements/{engagementUuid}")
@SecurityRequirement(name = "jwt", scopes = {})
@APIResponses(value = { @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"),
@APIResponse(responseCode = "403", description = "No write access for type"),
@APIResponse(responseCode = "200", description = "hosting environments have been returned.") })
@Timed(name = "hosting-update-timer", unit = MetricUnits.MILLISECONDS)
public Response updateHostingForEnagementUuid(@PathParam(value = "engagementUuid") String engagementUuid,
List<HostingEnvironment> hostingEnvironments) {
Engagement engagement = engagementService.getByUuid(engagementUuid);

boolean writeable = jwtUtils.isAllowedToWriteEngagement(jwt, configService.getPermission(engagement.getType()));

if (!writeable) {
return engagementService.getNotWriteableResponse(engagementUuid, engagement.getType());
}

String email = jwtUtils.getUserEmailFromToken(jwt);
String name = jwtUtils.getUsernameFromToken(jwt);

return hostingService.updateHostingEnvironments(engagementUuid, name, email, hostingEnvironments);
}

@GET
@Path("/openshift/versions")
@SecurityRequirement(name = "jwt", scopes = {})
@Timed(name = "hosting-openshift-versions-timer", unit = MetricUnits.MILLISECONDS)
public Response getOpenShiftVersions(@QueryParam("depth") final HostingEnvOpenShfitRollup rollup, @QueryParam("region") List<String> region) {
return hostingService.getOcpVersionRollup(rollup, region);
}

@HEAD
@Path("/subdomain/valid/{engagementUuid}/{subdomain}")
@APIResponses(value = { @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"),
@APIResponse(responseCode = "409", description = "Subdomain is taken by another engagement"),
@APIResponse(responseCode = "200", description = "Subdomain is able to be used by this engagement.") })
@SecurityRequirement(name = "jwt", scopes = {})
@Timed(name = "hosting-valid-subdomain-timer", unit = MetricUnits.MILLISECONDS)
public Response isSubdomainValid(@PathParam("engagementUuid") String engagementUuid, @PathParam("subdomain") String subdomain) {
return hostingService.isSubdomainValidResponse(engagementUuid, subdomain);
}

}
Loading