Exemple #1
0
 async def test_send_custom_schema_event_as_list(
         self, resource_group, eventgrid_topic, eventgrid_topic_primary_key,
         eventgrid_topic_endpoint):
     akc_credential = AzureKeyCredential(eventgrid_topic_primary_key)
     client = EventGridPublisherClient(eventgrid_topic_endpoint,
                                       akc_credential)
     custom_event1 = CustomEvent({
         "customSubject":
         "sample",
         "customEventType":
         "sample.event",
         "customDataVersion":
         "2.0",
         "customId":
         "1234",
         "customEventTime":
         dt.datetime.now(UTC()).isoformat(),
         "customData":
         "sample data"
     })
     custom_event2 = CustomEvent({
         "customSubject":
         "sample2",
         "customEventType":
         "sample.event",
         "customDataVersion":
         "2.0",
         "customId":
         "12345",
         "customEventTime":
         dt.datetime.now(UTC()).isoformat(),
         "customData":
         "sample data 2"
     })
     await client.send([custom_event1, custom_event2])
def publish():
    # [START publish_eg_event_dict]
    credential = AzureKeyCredential(topic_key)
    client = EventGridPublisherClient(endpoint, credential)

    event0 = {
        "eventType": "Contoso.Items.ItemReceived",
        "data": {
            "itemSku": "Contoso Item SKU #1"
        },
        "subject": "Door1",
        "dataVersion": "2.0",
        "id": "randomuuid11",
        "eventTime": datetime.now(UTC())
    }
    event1 = {
        "eventType": "Contoso.Items.ItemReceived",
        "data": {
            "itemSku": "Contoso Item SKU #2"
        },
        "subject": "Door1",
        "dataVersion": "2.0",
        "id": "randomuuid12",
        "eventTime": datetime.now(UTC())
    }
    client.send([event0, event1])
Exemple #3
0
def publish_event():
    # authenticate client
    credential = AzureKeyCredential(key)
    client = EventGridPublisherClient(endpoint, credential)

    custom_schema_event = {
        "customSubject": "sample",
        "customEventType": "sample.event",
        "customDataVersion": "2.0",
        "customId": uuid.uuid4(),
        "customEventTime": dt.datetime.now(UTC()).isoformat(),
        "customData": "sample data"
    }

    # publish events
    for _ in range(3):

        event_list = []  # list of events to publish
        # create events and append to list
        for j in range(randint(1, 3)):
            event_list.append(custom_schema_event)

        # publish list of events
        client.send(event_list)
        print("Batch of size {} published".format(len(event_list)))
        time.sleep(randint(1, 5))
    def __init__(self, subject, event_type, **kwargs):
        # type: (str, str, Any) -> None
        kwargs.setdefault('id', uuid.uuid4())
        kwargs.setdefault('subject', subject)
        kwargs.setdefault("event_type", event_type)
        kwargs.setdefault('event_time', dt.datetime.now(UTC()).isoformat())
        kwargs.setdefault('data', None)

        super(EventGridEvent, self).__init__(**kwargs)
Exemple #5
0
    def __init__(self, source, type, **kwargs):
        # type: (str, str, Any) -> None
        kwargs.setdefault('id', uuid.uuid4())
        kwargs.setdefault("source", source)
        kwargs.setdefault("type", type)
        kwargs.setdefault("time", dt.datetime.now(UTC()).isoformat())
        kwargs.setdefault("specversion", "1.0")

        super(CloudEvent, self).__init__(**kwargs)
Exemple #6
0
 async def test_send_custom_schema_event_as_list(self, variables, eventgrid_custom_event_topic_endpoint):
     client = self.create_eg_publisher_client(eventgrid_custom_event_topic_endpoint)
     custom_event1 = {
                 "customSubject": "sample",
                 "customEventType": "sample.event",
                 "customDataVersion": "2.0",
                 "customId": "1234",
                 "customEventTime": dt.datetime.now(UTC()).isoformat(),
                 "customData": "sample data"
                 }
     custom_event2 = {
                 "customSubject": "sample2",
                 "customEventType": "sample.event",
                 "customDataVersion": "2.0",
                 "customId": "12345",
                 "customEventTime": dt.datetime.now(UTC()).isoformat(),
                 "customData": "sample data 2"
                 }
     await client.send([custom_event1, custom_event2])
Exemple #7
0
    def __init__(self, subject, event_type, data, data_version, **kwargs):
        # type: (str, str, object, str, Any) -> None
        kwargs.setdefault("id", uuid.uuid4())
        kwargs.setdefault("subject", subject)
        kwargs.setdefault("event_type", event_type)
        kwargs.setdefault("event_time", dt.datetime.now(UTC()).isoformat())
        kwargs.setdefault("data", data)
        kwargs.setdefault("data_version", data_version)

        super(EventGridEvent, self).__init__(**kwargs)
Exemple #8
0
 async def test_send_signature_credential(self, resource_group, eventgrid_topic, eventgrid_topic_primary_key, eventgrid_topic_endpoint):
     expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
     signature = generate_shared_access_signature(eventgrid_topic_endpoint, eventgrid_topic_primary_key, expiration_date_utc)
     credential = EventGridSharedAccessSignatureCredential(signature)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, credential)
     eg_event = EventGridEvent(
             subject="sample", 
             data={"sample": "eventgridevent"}, 
             event_type="Sample.EventGrid.Event",
             data_version="2.0"
             )
     await client.send(eg_event)
Exemple #9
0
 def test_send_signature_credential(self, variables, eventgrid_topic_key,
                                    eventgrid_topic_endpoint):
     expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
     signature = generate_sas(eventgrid_topic_endpoint, eventgrid_topic_key,
                              expiration_date_utc)
     credential = AzureSasCredential(signature)
     client = EventGridPublisherClient(eventgrid_topic_endpoint, credential)
     eg_event = EventGridEvent(subject="sample",
                               data={"sample": "eventgridevent"},
                               event_type="Sample.EventGrid.Event",
                               data_version="2.0")
     client.send(eg_event)
 def __init__(self, source, type, **kwargs): # pylint: disable=redefined-builtin
     # type: (str, str, Any) -> None
     self.source = source
     self.type = type
     self.specversion = kwargs.pop("specversion", "1.0")
     self.id = kwargs.pop("id", str(uuid.uuid4()))
     self.time = kwargs.pop("time", dt.datetime.now(UTC()).isoformat())
     self.data = kwargs.pop("data", None)
     self.datacontenttype = kwargs.pop("datacontenttype", None)
     self.dataschema = kwargs.pop("dataschema", None)
     self.subject = kwargs.pop("subject", None)
     self.extensions = {}
     self.extensions.update(dict(kwargs.pop('extensions', {})))
def publish_event():
    # authenticate client
    credential = AzureKeyCredential(key)
    client = EventGridPublisherClient(endpoint, credential)

    # [START publish_custom_schema]
    custom_schema_event = {
        "customSubject": "sample",
        "customEventType": "sample.event",
        "customDataVersion": "2.0",
        "customId": uuid.uuid4(),
        "customEventTime": dt.datetime.now(UTC()).isoformat(),
        "customData": "sample data"
    }

    client.send(custom_schema_event)
def test_query_start_and_end_time():
    credential = _credential()
    client = LogsQueryClient(credential)
    query = "AppRequests | take 5"

    end_time = datetime.now(UTC())
    start_time = end_time - timedelta(days=3)

    def callback(request):
        dic = json.loads(request.http_request.body)
        assert dic.get('timespan') is not None

    client.query_workspace(os.environ['LOG_WORKSPACE_ID'],
                           query,
                           timespan=(start_time, end_time),
                           raw_request_hook=callback)
Exemple #13
0
def test_query_duration_and_start_time():
    credential = _credential()
    client = LogsQueryClient(credential)
    query = "AppRequests | take 5"

    end_time = datetime.now(UTC())
    start_time = end_time - timedelta(days=3)
    duration = timedelta(days=3)

    def callback(request):
        dic = json.loads(request.http_request.body)
        assert '/PT259200.0S' in dic.get('timespan')

    client.query(os.environ['LOG_WORKSPACE_ID'],
                 query,
                 timespan=(start_time, duration),
                 raw_request_hook=callback)
Exemple #14
0
 def __init__(self, source, type, **kwargs): # pylint: disable=redefined-builtin
     # type: (str, str, Any) -> None
     self.source = source
     self.type = type
     self.specversion = kwargs.pop("specversion", "1.0")
     self.id = kwargs.pop("id", str(uuid.uuid4()))
     self.time = kwargs.pop("time", dt.datetime.now(UTC()).isoformat())
     self.data = kwargs.pop("data", None)
     self.datacontenttype = kwargs.pop("datacontenttype", None)
     self.dataschema = kwargs.pop("dataschema", None)
     self.subject = kwargs.pop("subject", None)
     self.data_base64 = kwargs.pop("data_base64", None)
     self.extensions = {}
     self.extensions.update(dict(kwargs.pop('extensions', {})))
     if self.data is not None and self.data_base64 is not None:
         raise ValueError("data and data_base64 cannot be provided at the same time.\
             Use data_base64 only if you are sending bytes, and use data otherwise.")
def test_query_duration_and_end_time():
    credential = _credential()
    client = LogsQueryClient(credential)
    query = "AppRequests | take 5"

    end_time = datetime.now(UTC())
    duration = 'P3D'

    def callback(request):
        dic = json.loads(request.http_request.body)
        assert 'P3D/' in dic.get('timespan')

    client.query(os.environ['LOG_WORKSPACE_ID'],
                 query,
                 duration=duration,
                 end_time=end_time,
                 raw_request_hook=callback)
def test_duration_to_iso8601():
    d1 = timedelta(days=1)
    d2 = timedelta(weeks=1)
    d3 = timedelta(weeks=3, days=4)
    d4 = timedelta(seconds=10)
    d5 = timedelta(microseconds=1000)
    d6 = timedelta(milliseconds=100000)
    d7 = timedelta(hours=24, days=1)

    assert construct_iso8601(timespan=d1) == 'PT86400.0S'
    assert construct_iso8601(timespan=d2) == 'PT604800.0S'
    assert construct_iso8601(timespan=d3) == 'PT2160000.0S'
    assert construct_iso8601(timespan=d4) == 'PT10.0S'
    assert construct_iso8601(timespan=d5) == 'PT0.001S'
    assert construct_iso8601(timespan=d5) == 'PT0.001S'
    assert construct_iso8601(timespan=d7) == 'PT172800.0S'

    with pytest.raises(ValueError,
                       match="timespan must be a timedelta or a tuple."):
        construct_iso8601(timespan=(datetime.now(UTC())))
Exemple #17
0
def test_generate_sas_adds_https_if_not_exists():
    expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
    http_endpoint = "topic.eventgrid.endpoint"
    signature = generate_sas(http_endpoint, "eventgrid_topic_primary_key",
                             expiration_date_utc)
    assert signature.startswith("r=https")
Exemple #18
0
def test_generate_sas_adds_https_appends_api_events():
    expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
    http_endpoint = "https://topic.eventgrid.endpoint"
    signature = generate_sas(http_endpoint, "eventgrid_topic_primary_key",
                             expiration_date_utc)
    assert "%2Fapi%2Fevents" in signature
Exemple #19
0
from azure.monitor.query import LogsQueryClient
from azure.identity import ClientSecretCredential

credential = ClientSecretCredential(
    client_id=os.environ['AZURE_CLIENT_ID'],
    client_secret=os.environ['AZURE_CLIENT_SECRET'],
    tenant_id=os.environ['AZURE_TENANT_ID'])

client = LogsQueryClient(credential)

# Response time trend
# request duration over the last 12 hours.
query = """AppRequests |
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""

end_time = datetime.now(UTC())

# returns LogsQueryResults
response = client.query(os.environ['LOG_WORKSPACE_ID'],
                        query,
                        duration='PT1H',
                        end_time=end_time)

if not response.tables:
    print("No results for the query")

#response.tables is a LogsQueryResultTable
for table in response.tables:
    for col in table.columns:  #LogsQueryResultColumn
        print(col.name + "/" + col.type + " | ", end="")
    print("\n")
Exemple #20
0
def test_generate_sas_fails_with_http():
    expiration_date_utc = dt.datetime.now(UTC()) + timedelta(hours=1)
    http_endpoint = "http://topic.eventgrid.endpoint"
    with pytest.raises(ValueError):
        signature = generate_sas(http_endpoint, "eventgrid_topic_primary_key",
                                 expiration_date_utc)
    def _to_outgoing_amqp_message(self):
        message_header = None
        ttl_set = False
        if self.header:
            message_header = uamqp.message.MessageHeader()
            message_header.delivery_count = self.header.delivery_count
            message_header.time_to_live = self.header.time_to_live
            message_header.first_acquirer = self.header.first_acquirer
            message_header.durable = self.header.durable
            message_header.priority = self.header.priority
            if self.header.time_to_live and self.header.time_to_live != MAX_DURATION_VALUE:
                ttl_set = True
                creation_time_from_ttl = int(
                    time.mktime(datetime.now(UTC()).timetuple()))
                absolute_expiry_time_from_ttl = int(
                    min(MAX_ABSOLUTE_EXPIRY_TIME,
                        creation_time_from_ttl + self.header.time_to_live))

        message_properties = None
        if self.properties:
            creation_time = None
            absolute_expiry_time = None
            if ttl_set:
                creation_time = creation_time_from_ttl
                absolute_expiry_time = absolute_expiry_time_from_ttl
            else:
                if self.properties.creation_time:
                    creation_time = int(self.properties.creation_time)
                if self.properties.absolute_expiry_time:
                    absolute_expiry_time = int(
                        self.properties.absolute_expiry_time)

            message_properties = uamqp.message.MessageProperties(
                message_id=self.properties.message_id,
                user_id=self.properties.user_id,
                to=self.properties.to,
                subject=self.properties.subject,
                reply_to=self.properties.reply_to,
                correlation_id=self.properties.correlation_id,
                content_type=self.properties.content_type,
                content_encoding=self.properties.content_encoding,
                creation_time=creation_time,
                absolute_expiry_time=absolute_expiry_time,
                group_id=self.properties.group_id,
                group_sequence=self.properties.group_sequence,
                reply_to_group_id=self.properties.reply_to_group_id,
                encoding=self._encoding)
        elif ttl_set:
            message_properties = uamqp.message.MessageProperties(
                creation_time=creation_time_from_ttl if ttl_set else None,
                absolute_expiry_time=absolute_expiry_time_from_ttl
                if ttl_set else None,
            )

        amqp_body = self._message._body  # pylint: disable=protected-access
        if isinstance(amqp_body, uamqp.message.DataBody):
            amqp_body_type = uamqp.MessageBodyType.Data
            amqp_body = list(amqp_body.data)
        elif isinstance(amqp_body, uamqp.message.SequenceBody):
            amqp_body_type = uamqp.MessageBodyType.Sequence
            amqp_body = list(amqp_body.data)
        else:
            # amqp_body is type of uamqp.message.ValueBody
            amqp_body_type = uamqp.MessageBodyType.Value
            amqp_body = amqp_body.data

        return uamqp.message.Message(
            body=amqp_body,
            body_type=amqp_body_type,
            header=message_header,
            properties=message_properties,
            application_properties=self.application_properties,
            annotations=self.annotations,
            delivery_annotations=self.delivery_annotations,
            footer=self.footer)
Exemple #22
0
def test_from_json():
    json_str = '{"id": "de0fd76c-4ef4-4dfb-ab3a-8f24a307e033", "subject": "https://egtest.dev/cloudcustomevent", "data": {"team": "event grid squad"}, "event_type": "Azure.Sdk.Sample", "event_time": "2020-08-07T02:06:08.11969Z", "data_version": "1.0"}'
    event = EventGridEvent.from_json(json_str)
    assert event.data == {"team": "event grid squad"}
    assert event.event_time == datetime.datetime(2020, 8, 7, 2, 6, 8, 119690,
                                                 UTC())
Exemple #23
0
from azure.core.credentials import AzureKeyCredential
from azure.eventgrid import EventGridPublisherClient, CustomEvent

key = os.environ["CUSTOM_SCHEMA_ACCESS_KEY"]
topic_hostname = os.environ["CUSTOM_SCHEMA_TOPIC_HOSTNAME"]

# authenticate client
credential = AzureKeyCredential(key)
client = EventGridPublisherClient(topic_hostname, credential)

custom_schema_event = {
    "customSubject": "sample",
    "customEventType": "sample.event",
    "customDataVersion": "2.0",
    "customId": uuid.uuid4(),
    "customEventTime": dt.datetime.now(UTC()).isoformat(),
    "customData": "sample data"
}

# publish events
while True:

    event_list = []  # list of events to publish
    # create events and append to list
    for j in range(randint(1, 3)):
        event = CustomEvent(custom_schema_event)
        event_list.append(event)

    # publish list of events
    client.send(event_list)
    print("Batch of size {} published".format(len(event_list)))
def utc_from_timestamp(timestamp):
    return datetime.datetime.fromtimestamp(timestamp, tz=UTC())
def utc_now():
    return datetime.datetime.now(UTC())