use of com.microsoft.azure.functions.annotation.FunctionName in project azure-maven-plugins by microsoft.
the class AnnotationHandlerImpl method generateConfigurations.
@Override
public Map<String, FunctionConfiguration> generateConfigurations(final Set<Method> methods) throws AzureExecutionException {
final Map<String, FunctionConfiguration> configMap = new HashMap<>();
for (final Method method : methods) {
final FunctionName functionAnnotation = method.getAnnotation(FunctionName.class);
final String functionName = functionAnnotation.value();
validateFunctionName(configMap.keySet(), functionName);
log.debug("Starting processing function : " + functionName);
configMap.put(functionName, generateConfiguration(method));
}
return configMap;
}
use of com.microsoft.azure.functions.annotation.FunctionName in project azure-gradle-plugins by lenala.
the class AnnotationHandlerImpl method generateConfigurations.
@Override
public Map<String, FunctionConfiguration> generateConfigurations(final Set<Method> methods) throws Exception {
final Map<String, FunctionConfiguration> configMap = new HashMap<>();
for (final Method method : methods) {
final FunctionName functionAnnotation = method.getAnnotation(FunctionName.class);
final String functionName = functionAnnotation.value();
validateFunctionName(configMap.keySet(), functionName);
logger.quiet("Starting processing function : " + functionName);
configMap.put(functionName, generateConfiguration(method));
}
return configMap;
}
use of com.microsoft.azure.functions.annotation.FunctionName in project java-design-patterns by iluwatar.
the class UsageDetailPublisherFunction method run.
/**
* Azure function which create message, drop it in persistent storage
* and publish the event to Event Grid topic.
* @param request represents HttpRequestMessage
* @param context represents ExecutionContext
* @return HttpResponseMessage
*/
@FunctionName("UsageDetailPublisherFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) {
try {
var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
for (EventGridEvent eventGridEvent : eventGridEvents) {
// Handle system events
if (eventGridEvent.getEventType().equals("Microsoft.EventGrid.SubscriptionValidationEvent")) {
SubscriptionValidationEventData subscriptionValidationEventData = eventGridEvent.getData().toObject(SubscriptionValidationEventData.class);
// Handle the subscription validation event
var responseData = new SubscriptionValidationResponse();
responseData.setValidationResponse(subscriptionValidationEventData.getValidationCode());
return request.createResponseBuilder(HttpStatus.OK).body(responseData).build();
} else if (eventGridEvent.getEventType().equals("UsageDetail")) {
// Create message body
var messageBody = new MessageBody<UsageDetail>();
var usageDetailsList = new ArrayList<UsageDetail>();
var random = new Random();
for (int i = 0; i < 51; i++) {
var usageDetail = new UsageDetail();
usageDetail.setUserId("userId" + i);
usageDetail.setData(random.nextInt(500));
usageDetail.setDuration(random.nextInt(500));
usageDetailsList.add(usageDetail);
}
messageBody.setData(usageDetailsList);
// Create message header
var messageHeader = new MessageHeader();
messageHeader.setId(UUID.randomUUID().toString());
messageHeader.setSubject("UsageDetailPublisher");
messageHeader.setTopic("usagecostprocessorfunction-topic");
messageHeader.setEventType("UsageDetail");
messageHeader.setEventTime(OffsetDateTime.now().toString());
var messageReference = new MessageReference("callusageapp", messageHeader.getId() + "/input.json");
messageHeader.setData(messageReference);
messageHeader.setDataVersion("v1.0");
// Create entire message
var message = new Message<UsageDetail>();
message.setMessageHeader(messageHeader);
message.setMessageBody(messageBody);
// Drop data to persistent storage
this.messageHandlerUtility.dropToPersistantStorage(message, context.getLogger());
// Publish event to event grid topic
eventHandlerUtility.publishEvent(messageHeader, context.getLogger());
context.getLogger().info("Message is dropped and event is published successfully");
return request.createResponseBuilder(HttpStatus.OK).body(message).build();
}
}
} catch (Exception e) {
context.getLogger().warning(e.getMessage());
}
return request.createResponseBuilder(HttpStatus.OK).body(null).build();
}
use of com.microsoft.azure.functions.annotation.FunctionName in project java-design-patterns by iluwatar.
the class UsageCostProcessorFunction method run.
/**
* Azure function which gets triggered when event grid event send event to it.
* After receiving event, it read input file from blob storage, calculate call cost details.
* It creates new message with cost details and drop message to blob storage.
* @param request represents HttpRequestMessage
* @param context represents ExecutionContext
* @return HttpResponseMessage
*/
@FunctionName("UsageCostProcessorFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET, HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, final ExecutionContext context) {
try {
var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
for (var eventGridEvent : eventGridEvents) {
// Handle system events
if (eventGridEvent.getEventType().equals("Microsoft.EventGrid.SubscriptionValidationEvent")) {
SubscriptionValidationEventData subscriptionValidationEventData = eventGridEvent.getData().toObject(SubscriptionValidationEventData.class);
// Handle the subscription validation event
var responseData = new SubscriptionValidationResponse();
responseData.setValidationResponse(subscriptionValidationEventData.getValidationCode());
return request.createResponseBuilder(HttpStatus.OK).body(responseData).build();
} else if (eventGridEvent.getEventType().equals("UsageDetail")) {
// Get message header and reference
var messageReference = eventGridEvent.getData().toObject(MessageReference.class);
// Read message from persistent storage
var message = this.messageHandlerUtilityForUsageDetail.readFromPersistantStorage(messageReference, context.getLogger());
// Get Data and generate cost details
List<UsageDetail> usageDetailsList = BinaryData.fromObject(message.getMessageBody().getData()).toObject(new TypeReference<>() {
});
var usageCostDetailsList = calculateUsageCostDetails(usageDetailsList);
// Create message body
var newMessageBody = new MessageBody<UsageCostDetail>();
newMessageBody.setData(usageCostDetailsList);
// Create message header
var newMessageReference = new MessageReference("callusageapp", eventGridEvent.getId() + "/output.json");
var newMessageHeader = new MessageHeader();
newMessageHeader.setId(eventGridEvent.getId());
newMessageHeader.setSubject("UsageCostProcessor");
newMessageHeader.setTopic("");
newMessageHeader.setEventType("UsageCostDetail");
newMessageHeader.setEventTime(OffsetDateTime.now().toString());
newMessageHeader.setData(newMessageReference);
newMessageHeader.setDataVersion("v1.0");
// Create entire message
var newMessage = new Message<UsageCostDetail>();
newMessage.setMessageHeader(newMessageHeader);
newMessage.setMessageBody(newMessageBody);
// Drop data to persistent storage
this.messageHandlerUtilityForUsageCostDetail.dropToPersistantStorage(newMessage, context.getLogger());
context.getLogger().info("Message is dropped successfully");
return request.createResponseBuilder(HttpStatus.OK).body("Message is dropped successfully").build();
}
}
} catch (Exception e) {
context.getLogger().warning(e.getMessage());
}
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(null).build();
}
use of com.microsoft.azure.functions.annotation.FunctionName in project Insights by CognizantOneDevOps.
the class WebhookEventHandler method run.
@FunctionName("InsightsWebhookHandler")
public HttpResponseMessage run(@HttpTrigger(name = "request", methods = { HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<JSONObject>> request, final ExecutionContext context) throws IOException {
final String endpoint = request.getQueryParameters().get("endpoint");
final String url = System.getenv(endpoint);
final long currentTimeInMillis = System.currentTimeMillis();
boolean isWebhookSent = false;
boolean isUploadSuccess = false;
int responseCode = 0;
JSONObject payload = request.getBody().get();
payload.put("endpoint", endpoint);
String payloadName = endpoint + "_" + currentTimeInMillis;
context.getLogger().info(payload.toString());
isUploadSuccess = uploadPayload(context, payloadName, payload, "Event");
try {
if (url != null) {
responseCode = sendPayloadToWebhookEndpoint(url, payload, context);
if (responseCode == 200) {
isWebhookSent = true;
context.getLogger().info("Payload saved to storage container with name " + payloadName + " and sent to " + endpoint + " webhook endpoint");
} else {
context.getLogger().info("Error ouccured with response code " + responseCode + " while sending " + payloadName + " to " + endpoint + " endpoint");
}
} else {
context.getLogger().info(endpoint + " endpoint url not found for the " + payloadName);
}
} catch (Exception e) {
context.getLogger().info("Exception ouccured while sending payload to webHook endpoint: " + e.toString());
} finally {
if (isWebhookSent == false) {
isUploadSuccess = uploadPayload(context, payloadName, payload, "FailedEvent");
}
}
if (isUploadSuccess) {
return request.createResponseBuilder(HttpStatus.OK).body("payload Successfully sent to " + endpoint + " endpoint").build();
} else {
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("Request failed to sent payload to " + endpoint + " endpoint").build();
}
}
Aggregations