Skip to content

Commit

Permalink
add the selection for HttpTrigger's authLevel property (#70)
Browse files Browse the repository at this point in the history
* add the selection for HttpTrigger's authLevel property

* remove the enum which is redundant

* change switch to equalignorecase, which is more safer
  • Loading branch information
jdneo authored Dec 15, 2017
1 parent 323ee2f commit 5d1e1f8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static org.codehaus.plexus.util.IOUtil.copy;
import static org.codehaus.plexus.util.StringUtils.isNotEmpty;

import javax.annotation.Nullable;

/**
* Create new Azure Function (as Java class) and add to current project.
*/
Expand Down Expand Up @@ -274,18 +276,37 @@ protected Map<String, String> prepareTemplateParameters(final FunctionTemplate t
for (final String property : template.getMetadata().getUserPrompt()) {
info(format("Trigger specific parameter [%s]", property));

final List<String> options = getOptionsForUserPrompt(property);
if (settings != null && !settings.isInteractiveMode()) {
assureInputInBatchMode(System.getProperty(property),
str -> isNotEmpty(str),
String initValue = System.getProperty(property);
if (options != null && options.size() > 0) {
final String foundElement = findElementInOptions(options, initValue);
initValue = foundElement == null ? options.get(0) : foundElement;
}

assureInputInBatchMode(
initValue,
StringUtils::isNotEmpty,
str -> params.put(property, str),
false);
false
);
} else {
assureInputFromUser(format("Enter value for %s: ", property),
System.getProperty(property),
str -> isNotEmpty(str),
"Input should be a non-empty string.",
str -> params.put(property, str));

if (options == null) {
assureInputFromUser(
format("Enter value for %s: ", property),
System.getProperty(property),
StringUtils::isNotEmpty,
"Input should be a non-empty string.",
str -> params.put(property, str)
);
} else {
assureInputFromUser(
format("Enter value for %s: ", property),
System.getProperty(property),
options,
str -> params.put(property, str)
);
}
}
}

Expand Down Expand Up @@ -365,8 +386,10 @@ protected void saveToTargetFile(final File targetFile, final String newFunctionC

protected void assureInputFromUser(final String prompt, final String initValue, final List<String> options,
final Consumer<String> setter) {
if (options.stream().filter(Objects::nonNull).anyMatch(o -> o.equalsIgnoreCase(initValue))) {
final String option = findElementInOptions(options, initValue);
if (option != null) {
info(FOUND_VALID_VALUE);
setter.accept(option);
return;
}

Expand Down Expand Up @@ -440,5 +463,21 @@ protected Scanner getScanner() {
return new Scanner(System.in, "UTF-8");
}

@Nullable
private String findElementInOptions(List<String> options, String item) {
return options.stream()
.filter(o -> o != null && o.equalsIgnoreCase(item))
.findFirst()
.orElse(null);
}

@Nullable
private List<String> getOptionsForUserPrompt(final String promptName) {
if ("authlevel".equalsIgnoreCase(promptName.trim())) {
return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN");
}
return null;
}

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"description": "$HttpTriggerJava_description",
"defaultFunctionName": "httpTriggerJava",
"language": "Java",
"userPrompt": []
"userPrompt": [
"authLevel"
]
},
"files": {
"function.java": "package $packageName$;\n\nimport java.util.*;\nimport com.microsoft.azure.serverless.functions.annotation.*;\nimport com.microsoft.azure.serverless.functions.*;\n\n/**\n * Azure Functions with HTTP trigger.\n */\npublic class $className$ {\n /**\n * This function will listen at HTTP endpoint \"/api/$functionName$\". Two approaches to invoke it using \"curl\" command in bash:\n * 1. curl -d \"Http Body\" {your host}/api/$functionName$\n * 2. curl {your host}/api/$functionName$?name=HTTP%20Query\n */\n @FunctionName(\"$functionName$\")\n public HttpResponseMessage<String> httpHandler(\n @HttpTrigger(name = \"req\", methods = { \"get\", \"post\" }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,\n final ExecutionContext context\n ) {\n context.getLogger().info(\"Java HTTP trigger processed a request.\");\n\n // Parse query parameters\n String query = request.getQueryParameters().get(\"name\");\n String name = request.getBody().orElse(query);\n\n if (name == null) {\n return request.createResponse(400, \"Please pass a name on the query string or in the request body\");\n } else {\n return request.createResponse(200, \"Hello, \" + name);\n }\n }\n}\n"
"function.java": "package $packageName$;\n\nimport java.util.*;\nimport com.microsoft.azure.serverless.functions.annotation.*;\nimport com.microsoft.azure.serverless.functions.*;\n\n/**\n * Azure Functions with HTTP trigger.\n */\npublic class $className$ {\n /**\n * This function will listen at HTTP endpoint \"/api/$functionName$\". Two approaches to invoke it using \"curl\" command in bash:\n * 1. curl -d \"Http Body\" {your host}/api/$functionName$\n * 2. curl {your host}/api/$functionName$?name=HTTP%20Query\n */\n @FunctionName(\"$functionName$\")\n public HttpResponseMessage<String> httpHandler(\n @HttpTrigger(name = \"req\", methods = { \"get\", \"post\" }, authLevel = AuthorizationLevel.$authLevel$) HttpRequestMessage<Optional<String>> request,\n final ExecutionContext context\n ) {\n context.getLogger().info(\"Java HTTP trigger processed a request.\");\n\n // Parse query parameters\n String query = request.getQueryParameters().get(\"name\");\n String name = request.getBody().orElse(query);\n\n if (name == null) {\n return request.createResponse(400, \"Please pass a name on the query string or in the request body\");\n } else {\n return request.createResponse(200, \"Hello, \" + name);\n }\n }\n}\n"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ public void getConfiguration() throws Exception {
@Test
public void doExecute() throws Exception {
final AddMojo mojo = getMojoFromPom();
final Settings settings = new Settings();
settings.setInteractiveMode(false);
ReflectionUtils.setVariableValueInObject(mojo, "basedir", new File("target/test"));
ReflectionUtils.setVariableValueInObject(mojo, "settings", settings);
mojo.setFunctionTemplate("HttpTrigger");
mojo.setFunctionName("NewFunction");
mojo.setFunctionName("New-Function");
mojo.setFunctionPackageName("com.microsoft.azure");

final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/NewFunction.java");
final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/New_Function.java");
newFunctionFile.delete();

mojo.doExecute();
Expand All @@ -67,22 +70,6 @@ public void doExecuteWithInvalidFunctionName() throws Exception {
mojo.doExecute();
}

@Test
public void doExecuteWithSpecialFunctionName() throws Exception {
final AddMojo mojo = getMojoFromPom();
ReflectionUtils.setVariableValueInObject(mojo, "basedir", new File("target/test"));
mojo.setFunctionTemplate("HttpTrigger");
mojo.setFunctionName("New-Function");
mojo.setFunctionPackageName("com.microsoft.azure");

final File newFunctionFile = new File("target/test/src/main/java/com/microsoft/azure/New_Function.java");
newFunctionFile.delete();

mojo.doExecute();

assertTrue(newFunctionFile.exists());
}

@Test
public void assureInputFromUser() throws Exception {
final AddMojo mojo = getMojoFromPom();
Expand Down

0 comments on commit 5d1e1f8

Please sign in to comment.