Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
mta set credentials for application
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardW98 authored and openshift-merge-robot committed Jun 19, 2023
1 parent b0ad1f3 commit 78974a7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public CreateApplicationTask(URI serverURL, String bearerToken) {
*/
@Override
public WorkReport execute(WorkContext workContext) {
String appName, repo, branch;
String appName, repo, branch, identityName;
try {
appName = getOptionalParameterValue("applicationName", "");
repo = getRequiredParameterValue("repositoryURL");
identityName = getOptionalParameterValue("identity", null, false);

if (mtaClient == null) {
var serverUrl = getOptionalParameterValue("serverURL", null);
Expand All @@ -55,7 +56,13 @@ public WorkReport execute(WorkContext workContext) {
return new DefaultWorkReport(WorkStatus.FAILED, workContext, e);
}

Result<App> result = mtaClient.create(new App(0, appName, new Repository("git", repo, branch)));
Identity identity = null;
if (identityName != null) {
identity = mtaClient.getIdentity(identityName);
}

Result<App> result = mtaClient
.create(new App(0, appName, new Repository("git", repo, branch), new Identity[] { identity }));

if (result == null) {
taskLogger.logErrorWithSlf4j("MTA client returned empty result with no error");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.redhat.parodos.tasks.migrationtoolkit;

import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import javax.inject.Inject;

Expand Down Expand Up @@ -105,7 +107,9 @@ else if (result instanceof Result.Success<TaskGroup> success) {
"[Migration analysis report](%s) completed.".formatted(reportURL));
return new DefaultWorkReport(WorkStatus.COMPLETED, workContext);
}
else if ("Failed".equals(success.value().state())) {
else if ("Failed".equals(success.value().state())
|| Arrays.stream(Objects.requireNonNull(success.value().tasks()))
.anyMatch(task -> "Failed".equals(task.state()))) {
taskLogger.logErrorWithSlf4j("The underlying task failed, the report will not be ready");
return new DefaultWorkReport(WorkStatus.REJECTED, workContext,
new Throwable("The underlying task failed, the report will not be ready"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
record Repository(String kind, String url, String branch) {
}

record App(int id, String name, Repository repository) {
record App(int id, String name, Repository repository, Identity[] identities) {
}

record Identity(int id, String name) {
}

record Mode(boolean binary, boolean withDeps, boolean diva, String artifact) {
Expand All @@ -30,7 +33,7 @@ static TaskGroup ofCloudReadiness(int appID) {
new Data(new Mode(false, false, false, ""), "/windup/report", new Rules("", null),
new Scope(false, new Packages(new String[] {}, new String[] {})), new String[] {},
new String[] { "cloud-readiness" }),
null, new Task[] { new Task(new App(appID, "parodos", null), null,
null, new Task[] { new Task(new App(appID, "parodos", null, null), null,
String.format("parodos.%s.windup", appID), null, null, null) });
}
}
Expand All @@ -51,6 +54,8 @@ interface MTAApplicationClient {

Result<App> create(App app);

Identity getIdentity(String name);

}

interface MTATaskGroupClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ public Result<App> get(String name) {
}
}

@Override
public Identity getIdentity(String name) {
// identities in MTA have unique constraints on name.
try {
HttpResponse<String> getAll = client.send(
HttpRequest.newBuilder().GET().uri(serverURI.resolve("/hub/identities")).build(),
HttpResponse.BodyHandlers.ofString());
if (getAll.statusCode() != HTTP_OK) {
return null;
}
List<Identity> identities = mapper.readValue(getAll.body(), new TypeReference<>() {
});

Optional<Identity> identity = identities.stream().filter(v -> v.name().equals(name)).findFirst();
if (identity.isPresent()) {
return identity.get();
}
else {
throw new NotFoundException("failed to find identity by name " + name);
}
}
catch (IOException | InterruptedException e) {
return null;
}
}

// TODO unknown if we need to create the app or expect the app to be already present
// in MTA
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void createCompletes() {
ctx.put("branch", REPO_BRANCH);

when(mockClient.create(any())).thenReturn(
new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH))));
new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)));
WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());
underTest.preExecute(ctx);
Expand All @@ -89,11 +89,11 @@ public void createCompletes() {
assertThat(execute.getError()).isNull();
assertThat(execute.getStatus()).isEqualTo(WorkStatus.COMPLETED);
assertThat(execute.getWorkContext().get("application"))
.isEqualTo(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH)));
.isEqualTo(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null));

// 0 is wanted explicitly because it is an empty ID for the server request. (IDs
// are generated by the server)
verify(mockClient, times(1)).create(new App(0, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH)));
verify(mockClient, times(1)).create(new App(0, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static TaskGroup successfulGet() {
new Data(new Mode(false, false, false, ""), "/windup/report", new Rules("", null),
new Scope(false, new Packages(new String[] {}, new String[] {})), new String[] {},
new String[] { "cloud-readiness" }),
null, new Task[] { new Task(new App(APP_ID, "parodos", null), "Succeeded",
null, new Task[] { new Task(new App(APP_ID, "parodos", null, null), "Succeeded",
String.format("parodos.%s.windup", APP_ID), null, null, null) });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void failsGettingAppNotFound() {
@SneakyThrows
public void getByName() {
when(mockClient.get(anyString())).thenReturn(
new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH))));
new Result.Success<>(new App(APP_ID, APP_NAME, new Repository("git", REPO_URL, REPO_BRANCH), null)));
ctx.put("applicationName", APP_NAME);
WorkContextDelegate.write(ctx, WorkContextDelegate.ProcessType.WORKFLOW_EXECUTION,
WorkContextDelegate.Resource.ID, UUID.randomUUID());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void createCompletes() {
@NotNull
private static TaskGroup of(int id, int appID) {
return new TaskGroup(id, "", "", "", null, null,
new Task[] { new Task(new App(appID, "", null), "", "", "", null, "") });
new Task[] { new Task(new App(appID, "", null, null), "", "", "", null, "") });
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static String getPath(String server, String workspaceID, String projectID
throws URISyntaxException {
String path = "/workspaces/%s/projects/%s/outputs/%s".formatted(workspaceID, projectID, outputID);
URI baseUri = new URI(server);
return new URI(baseUri.getScheme(), baseUri.getAuthority(), path, null, null).getPath();
return new URI(baseUri.getScheme(), baseUri.getAuthority(), path, null, null).toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ WorkFlowOption defaultOption() {
@Bean(name = "AnalyzeApplicationAssessment")
@Assessment(parameters = {
@Parameter(key = "repositoryURL", description = "The repository with the code to analyze",
type = WorkParameterType.URL, optional = false),
type = WorkParameterType.TEXT, optional = false),
@Parameter(key = "applicationName", description = "The name of the application to analyze",
type = WorkParameterType.TEXT, optional = false),
@Parameter(key = "branch", description = "The repository branch to analyze", type = WorkParameterType.TEXT,
optional = true),
@Parameter(key = "identity", description = "The identity of ssh key in MTA", type = WorkParameterType.TEXT,
optional = true) })
public WorkFlow AnalyzeApplicationAssessment(CreateApplicationTask createApplicationTask,
GetApplicationTask getAppTask, SubmitAnalysisTask submitAnalysisTask) {
Expand Down

0 comments on commit 78974a7

Please sign in to comment.