Skip to content
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
7 changes: 2 additions & 5 deletions src/main/java/org/justserve/cli/JustServeCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.justserve.cli;

import io.micronaut.configuration.picocli.PicocliRunner;
import org.justserve.cli.command.BaseCommand;
import org.justserve.cli.command.GetTempPassword;
import org.justserve.cli.command.MakeOrgAdmin;
import org.justserve.cli.command.UnReassignProjects;
import org.justserve.cli.command.*;
import org.justserve.cli.util.JustServeVersionProvider;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParameterException;
import picocli.jansi.graalvm.AnsiConsole;

@Command(subcommands = {GetTempPassword.class, MakeOrgAdmin.class, UnReassignProjects.class},
@Command(subcommands = {GetTempPassword.class, MakeOrgAdmin.class, UnReassignProjects.class, AssignOrgToProject.class},
mixinStandardHelpOptions = true,
name = "justserve", versionProvider = JustServeVersionProvider.class,
description = "justserve-cli is a terminal tool to help specialists and admin using JustServe")
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/justserve/cli/command/AssignOrgToProject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.justserve.cli.command;

import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import lombok.extern.slf4j.Slf4j;
import org.justserve.client.ProjectClient;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.util.UUID;

import static org.justserve.cli.util.JustServePrinter.printError;
import static org.justserve.cli.util.JustServePrinter.printNormal;

@Slf4j
@Command(name = "assignOrgToProject", description = "Assigns an organization to a project", mixinStandardHelpOptions = true)
public class AssignOrgToProject extends BaseCommand implements Runnable {

@Option(names = {"--project", "-p"}, description = "the project ID", required = true)
private UUID projectId;

@Option(names = {"--org", "-o"}, description = "the organization ID", required = true)
private UUID orgId;

@Inject
Provider<ProjectClient> projectClientProvider;

@Override
public void run() {
if (isTokenInvalid()) {
return;
}

ProjectClient client = projectClientProvider.get();

try {
log.atTrace().log("Assigning organization {} to project {}", orgId, projectId);
HttpResponse<Object> response = client.assignOrganizationToProject(projectId, orgId);
if (response.status() == HttpStatus.OK) {
printNormal("Successfully assigned organization %s to project %s", orgId, projectId);
log.atTrace().log("received api response status: {}", response.status());
} else {
printError("Failed to assign organization " + orgId + " to project " + projectId +
". Expected HTTP Status 'OK', but got " + response.status());
log.atError().log("Failed to assign organization {} to project {}. Expected HTTP Status 'OK', but got {}",
orgId, projectId, response.status());
}
} catch (HttpClientResponseException e) {
printError("Failed to assign organization %s to project %s. (%s: %s)",
orgId, projectId, e.getStatus().getCode(), e.getMessage());
log.atError().setCause(e).log("Error response from API: {}", e.getResponse().body());
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</encoder>
</appender>

<root level="WARN">
<root level="OFF">
<appender-ref ref="STDOUT" />
</root>
</configuration>
25 changes: 24 additions & 1 deletion src/main/resources/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ paths:
application/json:
schema:
type: null
/api/v1/projects/{id}/organization/{organizationId}/assign:
put:
tags: [ Project ]
description: Assigns an organization to a project.
operationId: assignOrganizationToProject
parameters:
- name: id
in: path
description: ID of the project
required: true
schema: { type: string, format: uuid }
- name: organizationId
in: path
description: ID of the organization to assign
required: true
schema: { type: string, format: uuid }
responses:
'200':
description: OK
content:
application/json:
schema:
type: null
/api/v1/users:
post:
description: Register a new user on JustServe
Expand Down Expand Up @@ -1231,7 +1254,7 @@ components:
description: {}
url: {}
internalUrl: {}
organizationId: {}
organizationId: { type: string, format: uuid }
reviewedBy: {}
reviewedOn: {}
linked:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.justserve.cli.command

import io.micronaut.test.extensions.spock.annotation.MicronautTest
import org.justserve.model.ProjectCard
import spock.lang.Execution

import static org.spockframework.runtime.model.parallel.ExecutionMode.SAME_THREAD

@Execution(SAME_THREAD)
@MicronautTest
class AssignOrgToProjectSpec extends BaseCommandSpec {

def "can assign an organization to a project"() {
given:
def orgSearchResponse = authOrgClient.searchByLocation(createSearchRequestForElkGrove())
UUID orgId = orgSearchResponse.body().organizations.first().id
ProjectCard project = searchResults.first()
def args = ["assignOrgToProject", "-p", project.getId().toString(), "-o", orgId.toString()]

when:
def (outputStream, errorStream) = executeCommand(ctx, args as String[])

then:
errorStream.matches(blankRegex)
outputStream.contains("Successfully assigned organization ${orgId} to project ${project.getId()}")
}

def "fails gracefully when project or org does not exist"() {
given:
UUID fakeId = UUID.randomUUID()
def args = ["assignOrgToProject", "-p", fakeId.toString(), "-o", fakeId.toString()]

when:
def (outputStream, errorStream) = executeCommand(ctx, args as String[])

then:
outputStream.matches(blankRegex)
errorStream.contains("Failed to assign organization ${fakeId} to project ${fakeId}. (400: Client 'justserve': Bad Request)")
}
}
21 changes: 21 additions & 0 deletions src/test/groovy/org/justserve/client/ProjectClientSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ class ProjectClientSpec extends JustServeSpec {
}
}

void "can assign an organization to a project"() {
given:
def orgSearchResponse = authOrgClient.searchByLocation(createSearchRequestForElkGrove())
UUID orgId = orgSearchResponse.body().organizations.first().id
ProjectCard project = searchResults.first()

when:
def response = projectClient.assignOrganizationToProject(project.getId(), orgId)

then:
verifyAll {
response.status == OK
}

and: "validate that the reassignment worked - this is testing the underlying tech, not our codebase"
def updatedProject = projectClient.getProject(project.getId(), "en-US", new GetProjectRequest()).body()
verifyAll {
updatedProject.organization.organizationId == orgId
}
}

}
Loading