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

Add list of status authorized when the one proposed by the user was not authorized. #75

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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 @@ -17,118 +17,109 @@


/**
* A CLI command to set PDS label archive status in Elasticsearch registry index.
* Status can be updated by LidVid or PackageId.
* A CLI command to set PDS label archive status in Elasticsearch registry index. Status can be
* updated by LidVid or PackageId.
*
* @author karpenko
*/
public class SetArchiveStatusCmd implements CliCommand
{
private Set<String> statusNames;

/**
* Constructor
*/
public SetArchiveStatusCmd()
{
statusNames = new TreeSet<>();
statusNames.add("staged");
statusNames.add("archived");
statusNames.add("certified");
statusNames.add("restricted");
public class SetArchiveStatusCmd implements CliCommand {
private Set<String> statusNames;

/**
* Constructor
*/
public SetArchiveStatusCmd() {
statusNames = new TreeSet<>();
statusNames.add("staged");
statusNames.add("archived");
statusNames.add("certified");
statusNames.add("restricted");
}


@Override
public void run(CommandLine cmdLine) throws Exception {
if (cmdLine.hasOption("help")) {
printHelp();
return;
}

String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200");
String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX);
String authPath = cmdLine.getOptionValue("auth");

String status = getStatus(cmdLine);

String lidvid = cmdLine.getOptionValue("lidvid");
if (lidvid == null)
throw new Exception("Missing required parameter '-lidvid'");

RestClient client = null;

try {
// Call Elasticsearch
client = EsClientFactory.createRestClient(esUrl, authPath);
ProductDao dao = new ProductDao(client, indexName);
ProductService srv = new ProductService(dao);

srv.updateArchveStatus(lidvid, status);
} catch (ResponseException ex) {
throw new Exception(EsUtils.extractErrorMessage(ex));
} finally {
CloseUtils.close(client);
}


@Override
public void run(CommandLine cmdLine) throws Exception
{
if(cmdLine.hasOption("help"))
{
printHelp();
return;
}

String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200");
String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX);
String authPath = cmdLine.getOptionValue("auth");

String status = getStatus(cmdLine);

String lidvid = cmdLine.getOptionValue("lidvid");
if(lidvid == null) throw new Exception("Missing required parameter '-lidvid'");

RestClient client = null;

try
{
// Call Elasticsearch
client = EsClientFactory.createRestClient(esUrl, authPath);
ProductDao dao = new ProductDao(client, indexName);
ProductService srv = new ProductService(dao);

srv.updateArchveStatus(lidvid, status);
}
catch(ResponseException ex)
{
throw new Exception(EsUtils.extractErrorMessage(ex));
}
finally
{
CloseUtils.close(client);
}
}


/**
* Get value of "-status" command-line parameter. Throw exception if invalid status is passed.
*
* @param cmdLine
* @return valid status value
* @throws Exception Throw exception if invalid status is passed.
*/
private String getStatus(CommandLine cmdLine) throws Exception {
String tmp = cmdLine.getOptionValue("status");
if (tmp == null) {
throw new Exception("Missing required parameter '-status'");
}


/**
* Get value of "-status" command-line parameter.
* Throw exception if invalid status is passed.
* @param cmdLine
* @return valid status value
* @throws Exception Throw exception if invalid status is passed.
*/
private String getStatus(CommandLine cmdLine) throws Exception
{
String tmp = cmdLine.getOptionValue("status");
if(tmp == null)
{
throw new Exception("Missing required parameter '-status'");
}

String status = tmp.toLowerCase();
if(!statusNames.contains(status))
{
throw new Exception("Invalid '-status' parameter value: '" + tmp + "'");
}

return status;
String status = tmp.toLowerCase();
if (!statusNames.contains(status)) {
String authorized_status = String.join(", ", this.statusNames);
throw new Exception("Invalid '-status' parameter value: '" + tmp + "'. Authorized values are "
+ authorized_status + ".");
}


/**
* Print help screen
*/
public void printHelp()
{
System.out.println("Usage: registry-manager set-archive-status <options>");

System.out.println();
System.out.println("Set product archive status");
System.out.println();
System.out.println("Required parameters:");
System.out.println(" -status <status> One of the following values:");

for(String name: statusNames)
{
System.out.println(" " + name);
}

System.out.println(" -lidvid <id> Update archive status of a document with given LIDVID.");
System.out.println(" For a collection also update primary references from collection inventory.");
System.out.println("Optional parameters:");
System.out.println(" -auth <file> Authentication config file");
System.out.println(" -es <url> Elasticsearch URL. Default is http://localhost:9200");
System.out.println(" -index <name> Elasticsearch index name. Default is 'registry'");
System.out.println();
// Authorized values are " + String.join(", ", this.statusNames)

return status;
}


/**
* Print help screen
*/
public void printHelp() {
System.out.println("Usage: registry-manager set-archive-status <options>");

System.out.println();
System.out.println("Set product archive status");
System.out.println();
System.out.println("Required parameters:");
System.out.println(" -status <status> One of the following values:");

for (String name : statusNames) {
System.out.println(" " + name);
}

System.out.println(" -lidvid <id> Update archive status of a document with given LIDVID.");
System.out.println(
" For a collection also update primary references from collection inventory.");
System.out.println("Optional parameters:");
System.out.println(" -auth <file> Authentication config file");
System.out.println(" -es <url> Elasticsearch URL. Default is http://localhost:9200");
System.out.println(" -index <name> Elasticsearch index name. Default is 'registry'");
System.out.println();
}

}
Loading