Skip to content

Commit

Permalink
add source to blob_trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
YunchuWang committed Dec 12, 2023
1 parent 0ca53b3 commit b4950ab
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
5 changes: 3 additions & 2 deletions azure/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DecoratorApi, DataType, AuthLevel,
Cardinality, AccessRights, HttpMethod,
AsgiFunctionApp, WsgiFunctionApp,
ExternalHttpFunctionApp)
ExternalHttpFunctionApp, BlobSource)
from ._durable_functions import OrchestrationContext, EntityContext
from .decorators.function_app import (FunctionRegister, TriggerApi,
BindingApi, SettingsApi)
Expand Down Expand Up @@ -94,7 +94,8 @@
'AuthLevel',
'Cardinality',
'AccessRights',
'HttpMethod'
'HttpMethod',
'BlobSource'
)

__version__ = '1.18.0b4'
6 changes: 4 additions & 2 deletions azure/functions/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from .core import Cardinality, AccessRights
from .function_app import FunctionApp, Function, DecoratorApi, DataType, \
AuthLevel, Blueprint, ExternalHttpFunctionApp, AsgiFunctionApp, \
WsgiFunctionApp, FunctionRegister, TriggerApi, BindingApi, SettingsApi
WsgiFunctionApp, FunctionRegister, TriggerApi, BindingApi, \
SettingsApi, BlobSource
from .http import HttpMethod

__all__ = [
Expand All @@ -22,5 +23,6 @@
'AuthLevel',
'Cardinality',
'AccessRights',
'HttpMethod'
'HttpMethod',
'BlobSource'
]
9 changes: 6 additions & 3 deletions azure/functions/decorators/blob.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from typing import Optional
from typing import Optional, Union

from azure.functions.decorators.constants import BLOB_TRIGGER, BLOB
from azure.functions.decorators.core import Trigger, OutputBinding, DataType, \
InputBinding
from azure.functions.decorators.core import BlobSource, Trigger, \
OutputBinding, DataType, InputBinding


class BlobTrigger(Trigger):
def __init__(self,
name: str,
path: str,
connection: str,
source: Union[BlobSource, str],
data_type: Optional[DataType] = None,
**kwargs):
self.path = path
self.connection = connection
self.source = BlobSource[source] \
if isinstance(source, str) else source
super().__init__(name=name, data_type=data_type)

@staticmethod
Expand Down
8 changes: 8 additions & 0 deletions azure/functions/decorators/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ class AccessRights(StringifyEnum):
and all related message handling. """


class BlobSource(StringifyEnum):
"""Source of the blob trigger."""
EVENT_GRID = "EventGrid"
"""Event Grid is the source of the blob trigger."""
LOGS_AND_CONTAINER_SCAN = "LogsAndContainerScan"
"""Standard polling mechanism to detect changes in the container."""


class Binding(ABC):
"""Abstract binding class which captures common attributes and
functions. :meth:`get_dict_repr` can auto generate the function.json for
Expand Down
11 changes: 10 additions & 1 deletion azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from azure.functions.decorators.blob import BlobTrigger, BlobInput, BlobOutput
from azure.functions.decorators.core import Binding, Trigger, DataType, \
AuthLevel, SCRIPT_FILE_NAME, Cardinality, AccessRights, Setting
AuthLevel, SCRIPT_FILE_NAME, Cardinality, AccessRights, Setting, BlobSource
from azure.functions.decorators.cosmosdb import CosmosDBTrigger, \
CosmosDBOutput, CosmosDBInput, CosmosDBTriggerV3, CosmosDBInputV3, \
CosmosDBOutputV3
Expand Down Expand Up @@ -993,6 +993,8 @@ def blob_trigger(self,
arg_name: str,
path: str,
connection: str,
source: Union[BlobSource, str] =
BlobSource.LOGS_AND_CONTAINER_SCAN,
data_type: Optional[DataType] = None,
**kwargs) -> Callable[..., Any]:
"""
Expand All @@ -1010,6 +1012,12 @@ def blob_trigger(self,
:param path: The path to the blob.
:param connection: The name of an app setting or setting collection
that specifies how to connect to Azure Blobs.
:param source: Sets the source of the triggering event.
Use EventGrid for an Event Grid-based blob trigger,
which provides much lower latency.
The default is LogsAndContainerScan,
which uses the standard polling mechanism to detect changes
in the container.
:param data_type: Defines how Functions runtime should treat the
parameter value.
:param kwargs: Keyword arguments for specifying additional binding
Expand All @@ -1026,6 +1034,7 @@ def decorator():
name=arg_name,
path=path,
connection=connection,
source=source,
data_type=parse_singular_param_to_enum(data_type,
DataType),
**kwargs))
Expand Down
9 changes: 8 additions & 1 deletion docs/ProgModelSpec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from abc import ABC
from typing import Callable, Dict, List, Optional, Union, Iterable

from azure.functions import AsgiMiddleware, WsgiMiddleware
from azure.functions.decorators.core import Binding, Trigger, DataType, \
from azure.functions.decorators.core import Binding, BlobSource, Trigger, DataType, \
AuthLevel, Cardinality, AccessRights, Setting
from azure.functions.decorators.function_app import FunctionBuilder, SettingsApi
from azure.functions.decorators.http import HttpMethod
Expand Down Expand Up @@ -495,6 +495,7 @@ class TriggerApi(DecoratorApi, ABC):
arg_name: str,
path: str,
connection: str,
source: Union[BlobSource, str] = BlobSource.LOGS_AND_CONTAINER_SCAN,
data_type: Optional[DataType] = None,
**kwargs) -> Callable:
"""
Expand All @@ -512,6 +513,12 @@ class TriggerApi(DecoratorApi, ABC):
:param path: The path to the blob.
:param connection: The name of an app setting or setting collection
that specifies how to connect to Azure Blobs.
:param source: Sets the source of the triggering event.
Use EventGrid for an Event Grid-based blob trigger,
which provides much lower latency.
The default is LogsAndContainerScan,
which uses the standard polling mechanism to detect changes
in the container.
:param data_type: Defines how Functions runtime should treat the
parameter value.
:param kwargs: Keyword arguments for specifying additional binding
Expand Down
27 changes: 25 additions & 2 deletions tests/decorators/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import unittest

from azure.functions.decorators.blob import BlobTrigger, BlobOutput, BlobInput
from azure.functions.decorators.core import BindingDirection, DataType
from azure.functions.decorators.core import BindingDirection, BlobSource, \
DataType


class TestBlob(unittest.TestCase):
def test_blob_trigger_valid_creation(self):
def test_blob_trigger_creation_with_source_as_string(self):
trigger = BlobTrigger(name="req",
path="dummy_path",
connection="dummy_connection",
source="EventGrid",
data_type=DataType.UNDEFINED,
dummy_field="dummy")

Expand All @@ -22,6 +24,27 @@ def test_blob_trigger_valid_creation(self):
"name": "req",
"dataType": DataType.UNDEFINED,
"path": "dummy_path",
'source': 'EventGrid',
"connection": "dummy_connection"
})

def test_blob_trigger_creation_with_source_as_enum(self):
trigger = BlobTrigger(name="req",
path="dummy_path",
connection="dummy_connection",
source=BlobSource.EventGrid,
data_type=DataType.UNDEFINED,
dummy_field="dummy")

self.assertEqual(trigger.get_binding_name(), "blobTrigger")
self.assertEqual(trigger.get_dict_repr(), {
"type": "blobTrigger",
"direction": BindingDirection.IN,
'dummyField': 'dummy',
"name": "req",
"dataType": DataType.UNDEFINED,
"path": "dummy_path",
'source': 'EventGrid',
"connection": "dummy_connection"
})

Expand Down
7 changes: 6 additions & 1 deletion tests/decorators/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
EVENT_HUB, EVENT_HUB_TRIGGER, COSMOS_DB, COSMOS_DB_TRIGGER, BLOB, \
BLOB_TRIGGER, EVENT_GRID_TRIGGER, EVENT_GRID, TABLE, WARMUP_TRIGGER, \
SQL, SQL_TRIGGER
from azure.functions.decorators.core import DataType, AuthLevel, \
from azure.functions.decorators.core import BlobSource, DataType, AuthLevel, \
BindingDirection, AccessRights, Cardinality
from azure.functions.decorators.function_app import FunctionApp
from azure.functions.decorators.http import HttpTrigger, HttpMethod
Expand Down Expand Up @@ -1490,6 +1490,7 @@ def dummy():
"type": BLOB_TRIGGER,
"name": "req",
"path": "dummy_path",
"source": "LogsAndContainerScan",
"connection": "dummy_conn"
}]})

Expand All @@ -1514,6 +1515,7 @@ def dummy():
"type": BLOB_TRIGGER,
"name": "req",
"path": "dummy_path",
"source": "LogsAndContainerScan",
"connection": "dummy_conn"
})

Expand All @@ -1522,6 +1524,7 @@ def test_blob_input_binding(self):

@app.blob_trigger(arg_name="req", path="dummy_path",
data_type=DataType.STRING,
source=BlobSource.EVENT_GRID,
connection="dummy_conn")
@app.blob_input(arg_name="file", path="dummy_in_path",
connection="dummy_in_conn",
Expand All @@ -1543,6 +1546,7 @@ def dummy():
"type": BLOB_TRIGGER,
"name": "req",
"path": "dummy_path",
"source": "EventGrid",
"connection": "dummy_conn"
})

Expand Down Expand Up @@ -1581,6 +1585,7 @@ def dummy():
"type": BLOB_TRIGGER,
"name": "req",
"path": "dummy_path",
"source": "LogsAndContainerScan",
"connection": "dummy_conn"
})

Expand Down

0 comments on commit b4950ab

Please sign in to comment.