Skip to content

Commit

Permalink
Event grid v2 (#13177)
Browse files Browse the repository at this point in the history
* other fixes

* auto update

* Update sdk/eventgrid/azure-eventgrid/CHANGELOG.md

* Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_helpers.py

* Update sdk/eventgrid/azure-eventgrid/CHANGELOG.md

* Update sdk/eventgrid/azure-eventgrid/azure/eventgrid/_version.py

* comments
  • Loading branch information
Rakshith Bhyravabhotla authored Aug 18, 2020
1 parent 27f743c commit e05ce13
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 88 deletions.
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
4 changes: 2 additions & 2 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,7 +55,7 @@ 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 = message.encode('ascii')
hmac_new = hmac.new(decoded_key, bytes_message, hashlib.sha256).digest()
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.")
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))
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

0 comments on commit e05ce13

Please sign in to comment.