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

mta set credentials for application #446

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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 @@ -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()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't a failed task sets the top-level taskgroup as failed? what is the state of the taskgroup in that case? (weird API if so)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to create application with the same name of existing one. the top level is succeed but task is failed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

.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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make that a Result ? the intention is among other not to throw execptions from the client level. They should be stored in the Result of type Failure.


}

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to update the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will update it

}

}
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not URL? because of the protocol that can be ssh? if that's the case I suggest to replace URL with URI which does support that, or at least behind the hood to use URI as the resolver

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java URI support opaque schemes . URI.create("foo://bar.baz").getScheme() // foo"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok let's replace URL with URI in next pr. it involves UI work too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UI's pattern for url is "^(https?)://"

@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
Loading