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

Event grid v2 #13177

Merged
merged 7 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 2.0.0b1 (2020-08-07)
## 2.0.0b1 (Unreleased)

- Placeholder - NEEDS TO BE CHANGED

Expand Down
1 change: 1 addition & 0 deletions sdk/eventgrid/azure-eventgrid/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
recursive-include tests *.py *.yaml
recursive-include samples *.py
include *.md
include azure/__init__.py

3 changes: 2 additions & 1 deletion sdk/eventgrid/azure-eventgrid/azure/eventgrid/_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ def deserialize_event(self, event, **kwargs):

:raise: :class:`ValueError`, when events do not follow CloudEvent or EventGridEvent schema.
"""
encode = kwargs.pop('encoding', 'utf-8')
try:
if isinstance(event, six.binary_type):
event = json.loads(event.decode('utf-8'))
event = json.loads(event.decode(encode))
elif isinstance(event, six.string_types):
event = json.loads(event)
return DeserializedEvent(event)
Expand Down
6 changes: 3 additions & 3 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate_shared_access_signature(topic_hostname, shared_access_key, expirati
encoded_expiration_utc = quote(str(expiration_date_utc), safe=constants.SAFE_ENCODE)

unsigned_sas = "r={}&e={}".format(encoded_resource, encoded_expiration_utc)
signature = quote(_hmac(shared_access_key, unsigned_sas), safe=constants.SAFE_ENCODE)
signature = quote(_generate_hmac(shared_access_key, unsigned_sas), safe=constants.SAFE_ENCODE)
signed_sas = "{}&s={}".format(unsigned_sas, signature)
return signed_sas

Expand All @@ -55,9 +55,9 @@ def _get_full_topic_hostname(topic_hostname):

return topic_hostname

def _hmac(key, message):
def _generate_hmac(key, message):
decoded_key = base64.b64decode(key)
bytes_message = bytes(message, 'ascii')
bytes_message = message.encode('ascii')
hmac_new = hmac.new(decoded_key, bytes_message, hashlib.sha256).digest()

return base64.b64encode(hmac_new)
Expand Down
6 changes: 3 additions & 3 deletions sdk/eventgrid/azure-eventgrid/azure/eventgrid/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CloudEvent(InternalCloudEvent): #pylint:disable=too-many-instance-attrib
}

def __init__(self, source, type, **kwargs):
# type: (Any) -> None
# type: (str, str, Any) -> None
kwargs.setdefault('id', uuid.uuid4())
kwargs.setdefault("source", source)
kwargs.setdefault("type", type)
Expand Down Expand Up @@ -119,7 +119,7 @@ class EventGridEvent(InternalEventGridEvent):
}

def __init__(self, subject, event_type, **kwargs):
# type: (Any) -> None
# type: (str, str, Any) -> None
kwargs.setdefault('id', uuid.uuid4())
kwargs.setdefault('subject', subject)
kwargs.setdefault("event_type", event_type)
Expand Down Expand Up @@ -191,7 +191,7 @@ class CustomEvent(DictMixin):
"""

def __init__(self, *args, **kwargs):
# type: (Any) -> None
# type: (Any, Any) -> None
self._update(*args, **kwargs)

def _update(self, *args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def send(self, events, **kwargs):
serialized_events = [dict(e) for e in events]
self._client.publish_custom_event_events(self._topic_hostname, serialized_events, **kwargs)
else:
raise Exception("Event schema is not correct. Please send as list of all CloudEvents, list of all EventGridEvents, or list of all CustomEvents.")
raise ValueError("Event schema is not correct.")
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/azure/eventgrid/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# regenerated.
# --------------------------------------------------------------------------

VERSION = "2.0.0"
VERSION = "2.0.0b1"
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ async def send(self, events, **kwargs):
serialized_events = [dict(e) for e in events]
await self._client.publish_custom_event_events(self._topic_hostname, serialized_events, **kwargs)
else:
raise Exception("Event schema is not correct. Please send as list of all CloudEvents, list of all EventGridEvents, or list of all CustomEvents.")
raise ValueError("Event schema is not correct.")

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def main(event: func.EventGridEvent):
'event_type': event.event_type
})
logging.info(result)
#consumer = EventGridConsumer()
#deserialized_event = consumer.deserialize_events(result)
consumer = EventGridConsumer()
deserialized_event = consumer.deserialize_events(result)
## can only be EventGridEvent
#print("model: {}".format(event.model))
print("model: {}".format(event.model))
3 changes: 2 additions & 1 deletion sdk/eventgrid/azure-eventgrid/sdk_packaging.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[packaging]
auto_update = false
package_name = "azure-eventgrid"
package_pprint_name = "Event Grid"
package_doc_id = "event-grid"
is_stable = true
is_stable = false
is_arm = false
6 changes: 3 additions & 3 deletions sdk/eventgrid/azure-eventgrid/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
author_email='azpysdkhelp@microsoft.com',
url='https://github.com/Azure/azure-sdk-for-python',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Development Status :: 4 - Beta',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
Expand All @@ -75,13 +75,13 @@
zip_safe=False,
packages=find_packages(exclude=[
'tests',
'samples',
# Exclude packages that will be covered by PEP420 or nspkg
'azure',
]),
install_requires=[
'msrest>=0.5.0',
'msrestazure>=0.4.32,<2.0.0',
'azure-common~=1.1',
'azure-core<2.0.0,>=1.7.0',
],
extras_require={
":python_version<'3.0'": ['azure-nspkg'],
Expand Down
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/swagger/README.PYTHON_T2.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Azure EventGrid Client Generation for Python
# Azure EventGrid Client for Python

> see https://aka.ms/autorest

Expand Down
74 changes: 74 additions & 0 deletions sdk/eventgrid/azure-eventgrid/tests/_mocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# storage cloud event
cloud_storage_dict = {
"id":"a0517898-9fa4-4e70-b4a3-afda1dd68672",
"source":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
"data":{
"api":"PutBlockList",
"client_request_id":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"request_id":"831e1650-001e-001b-66ab-eeb76e000000",
"e_tag":"0x8D4BCC2E4835CD0",
"content_type":"application/octet-stream",
"content_length":524288,
"blob_type":"BlockBlob",
"url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
"sequencer":"00000000000004420000000000028963",
"storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
},
"type":"Microsoft.Storage.BlobCreated",
"time":"2020-08-07T01:11:49.765846Z",
"specversion":"1.0"
}
cloud_storage_string = json.dumps(cloud_storage_dict)
cloud_storage_bytes = cloud_storage_string.encode("utf-8")

# custom cloud event
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0"
}
cloud_custom_string = json.dumps(cloud_custom_dict)
cloud_custom_bytes = cloud_custom_string.encode("utf-8")

# storage eg event
eg_storage_dict = {
"id":"bbab6625-dc56-4b22-abeb-afcc72e5290c",
"subject":"/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob",
"data":{
"api":"PutBlockList",
"clientRequestId":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"requestId":"831e1650-001e-001b-66ab-eeb76e000000",
"eTag":"0x8D4BCC2E4835CD0",
"contentType":"application/octet-stream",
"contentLength":524288,
"blobType":"BlockBlob",
"url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
"sequencer":"00000000000004420000000000028963",
"storageDiagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
},
"eventType":"Microsoft.Storage.BlobCreated",
"dataVersion":"2.0",
"metadataVersion":"1",
"eventTime":"2020-08-07T02:28:23.867525Z",
"topic":"/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/t-swpill-test/providers/Microsoft.EventGrid/topics/eventgridegsub"
}

eg_storage_string = json.dumps(eg_storage_dict)
eg_storage_bytes = eg_storage_string.encode("utf-8")

# custom eg event
eg_custom_dict = {
"id":"3a30afef-b604-4b67-973e-7dfff7e178a7",
"subject":"Test EG Custom Event",
"data":{"team":"event grid squad"},
"eventType":"Azure.Sdk.Sample",
"dataVersion":"2.0",
"metadataVersion":"1",
"eventTime":"2020-08-07T02:19:05.16916Z",
"topic":"/subscriptions/f8aa80ae-d1c8-60ad-9bce-e1a850ba5b67/resourceGroups/sample-resource-group-test/providers/Microsoft.EventGrid/topics/egtopicsamplesub"
}
eg_custom_string = json.dumps(eg_custom_dict)
eg_custom_bytes = eg_custom_string.encode("utf-8")
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-eventgrid/tests/eventgrid_preparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self,
if random_name_enabled:
self.resource_moniker = self.name_prefix + "egtopic"

self.set_cache(use_cache, name_prefix)
self.set_cache(use_cache)

def create_resource(self, name, **kwargs):
if self.is_live:
Expand Down
86 changes: 11 additions & 75 deletions sdk/eventgrid/azure-eventgrid/tests/test_eg_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,17 @@
from devtools_testutils import AzureMgmtTestCase
from msrest.serialization import UTC
from azure.eventgrid import EventGridConsumer, CloudEvent, EventGridEvent, StorageBlobCreatedEventData

# storage cloud event
cloud_storage_dict = {
"id":"a0517898-9fa4-4e70-b4a3-afda1dd68672",
"source":"/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Storage/storageAccounts/{storage-account}",
"data":{
"api":"PutBlockList",
"client_request_id":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"request_id":"831e1650-001e-001b-66ab-eeb76e000000",
"e_tag":"0x8D4BCC2E4835CD0",
"content_type":"application/octet-stream",
"content_length":524288,
"blob_type":"BlockBlob",
"url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
"sequencer":"00000000000004420000000000028963",
"storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
},
"type":"Microsoft.Storage.BlobCreated",
"time":"2020-08-07T01:11:49.765846Z",
"specversion":"1.0"
}
cloud_storage_string = json.dumps(cloud_storage_dict)
cloud_storage_bytes = cloud_storage_string.encode("utf-8")

# custom cloud event
cloud_custom_dict = {
"id":"de0fd76c-4ef4-4dfb-ab3a-8f24a307e033",
"source":"https://egtest.dev/cloudcustomevent",
"data":{"team": "event grid squad"},
"type":"Azure.Sdk.Sample",
"time":"2020-08-07T02:06:08.11969Z",
"specversion":"1.0"
}
cloud_custom_string = json.dumps(cloud_custom_dict)
cloud_custom_bytes = cloud_custom_string.encode("utf-8")

# storage eg event
eg_storage_dict = {
"id":"bbab6625-dc56-4b22-abeb-afcc72e5290c",
"subject":"/blobServices/default/containers/oc2d2817345i200097container/blobs/oc2d2817345i20002296blob",
"data":{
"api":"PutBlockList",
"clientRequestId":"6d79dbfb-0e37-4fc4-981f-442c9ca65760",
"requestId":"831e1650-001e-001b-66ab-eeb76e000000",
"eTag":"0x8D4BCC2E4835CD0",
"contentType":"application/octet-stream",
"contentLength":524288,
"blobType":"BlockBlob",
"url":"https://oc2d2817345i60006.blob.core.windows.net/oc2d2817345i200097container/oc2d2817345i20002296blob",
"sequencer":"00000000000004420000000000028963",
"storageDiagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
},
"eventType":"Microsoft.Storage.BlobCreated",
"dataVersion":"2.0",
"metadataVersion":"1",
"eventTime":"2020-08-07T02:28:23.867525Z",
"topic":"/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/t-swpill-test/providers/Microsoft.EventGrid/topics/eventgridegsub"
}

eg_storage_string = json.dumps(eg_storage_dict)
eg_storage_bytes = eg_storage_string.encode("utf-8")

# custom eg event
eg_custom_dict = {
"id":"3a30afef-b604-4b67-973e-7dfff7e178a7",
"subject":"Test EG Custom Event",
"data":{"team":"event grid squad"},
"eventType":"Azure.Sdk.Sample",
"dataVersion":"2.0",
"metadataVersion":"1",
"eventTime":"2020-08-07T02:19:05.16916Z",
"topic":"/subscriptions/f8aa80ae-d1c8-60ad-9bce-e1a850ba5b67/resourceGroups/sample-resource-group-test/providers/Microsoft.EventGrid/topics/egtopicsamplesub"
}
eg_custom_string = json.dumps(eg_custom_dict)
eg_custom_bytes = eg_custom_string.encode("utf-8")
from _mocks import (
cloud_storage_dict,
cloud_storage_string,
cloud_storage_bytes,
eg_storage_dict,
eg_storage_string,
eg_storage_bytes,
eg_custom_dict,
eg_custom_string,
eg_custom_bytes
)

class EventGridConsumerTests(AzureMgmtTestCase):

Expand Down