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 the selection for HttpTrigger's authLevel property #70

Merged
merged 3 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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,38 @@ 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 = OptionTypedProperty.get(property) == null ?
Copy link
Member

Choose a reason for hiding this comment

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

how about just one method call like getOptionsforProperty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point

null : getOptionsForProperty(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 +387,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 +464,45 @@ 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> getOptionsForProperty(final String property) {
final OptionTypedProperty optionTypedProperty = OptionTypedProperty.get(property);
if (optionTypedProperty != null) {
switch (optionTypedProperty) {
case AUTH_LEVEL:
return Arrays.asList("ANONYMOUS", "FUNCTION", "ADMIN");
default:
return null;
}
}
return null;
}

private enum OptionTypedProperty {
AUTH_LEVEL("authLevel");

private final String property;

OptionTypedProperty(String property) {
this.property = property;
}

@Nullable
public static OptionTypedProperty get(final String property) {
return Arrays.stream(values())
.filter(prop -> prop.property.equals(property))
.findFirst()
.orElse(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