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

Add cloud event to core #16800

Merged
merged 54 commits into from
Mar 3, 2021
Merged

Add cloud event to core #16800

merged 54 commits into from
Mar 3, 2021

Conversation

rakshith91
Copy link
Contributor

@rakshith91 rakshith91 commented Feb 18, 2021

Creating a different PR for specifically core related changes - will rebase the other PR for EG related ones

Fixes #16697
Part 1 of #16585

@ghost ghost added the Azure.Core label Feb 18, 2021
@rakshith91 rakshith91 marked this pull request as ready for review February 18, 2021 07:31
@rakshith91 rakshith91 requested a review from johanste February 18, 2021 07:31
from datetime import timezone
TZ_UTC = timezone.utc # type: ignore
except ImportError:
class UTC(tzinfo):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiangyan99
Copy link
Member

Double check:

https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/azure/core/pipeline/policies/_utils.py imports requests which is heavy.

Do you really want it? That's the reason I said maybe we want to refactor the code. :)

(this is not blocker. just want to make sure we take it into account)

if data and data_base64:
raise ValueError("Invalid input. Only one of data and data_base64 must be present.")
return cls(
id=event.pop("id", None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indentation looks odd?

type=event.pop("type", None),
specversion=event.pop("specversion", None),
data=data or b64decode(data_base64),
time=event.pop("time", None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if time is datetime, need deserializaiton here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be a string

@iscai-msft
Copy link
Contributor

/azp run python - autorest - pr

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@check-enforcer
Copy link

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run python - [service] - ci

:type event: dict
:rtype: CloudEvent
"""
data = event.pop("data", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to mutate the argument passed in. I would be a bit surprised to notice that my dict had lost (all of its) keys when the method returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this

source=event.pop("source", None),
type=event.pop("type", None),
specversion=event.pop("specversion", None),
data=data or b64decode(data_base64),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if data is ''?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed this a lil bit - data '' is accepted

"Extension attributes should be lower cased and alphanumeric."
)
except KeyError:
self.extensions = cast(Dict, None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast does not look correct - it looks like self.extensions should be Optional[Dict], not Dict?

"Invalid input. Only one of data and data_base64 must be present."
)
if 'data' not in event and 'data_base64' not in event:
kwargs.setdefault("data", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using setdefault here? It is generally used when you get a partially filled out dict and you don't want to overwrite existing values. But you are defining kwargs in the method (i.e. it is not a parameter passed in to the method).

Please rename, and use direct assignment (e.g. `eventargs['data'] = None)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...also, do you really need to set "data" to None here? The dict passed in didn't have it, so you should be able to create the CloudEvent instance without passing in the keyword argument, right?

self.id = kwargs.pop("id", str(uuid.uuid4())) # type: str
self.time = kwargs.pop("time", datetime.now(TZ_UTC)) # type: datetime

_optional_attributes = ["datacontenttype", "dataschema", "subject", "data"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By looping through the attribute names like this, I'm pretty sure that you will not get the right type annotations for the attributes (but I may be wrong). Since it is only 4 names, I'd add them explicitly rather than looping through the names:

self.datacontenttype = kwargs.pop('datacontenttype', None)

This is both easier to read and (less important here) more performant.

Copy link
Member

@johanste johanste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type annotations should be fixed before we merge (don't fix things by doing incorrect casts). Also, not looping through attribute names will provide/allow for typing for those attributes as well.


return cls(
id=event.get("id"),
source=cast(str, event.get("source")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no "source" key in event, you will be (incorrectly) casting None to str. It is better to raise a ValueError here if the either of required keys ("type" and "source") is not in event or else the error will surface much later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left it to event["source"]

Copy link
Member

@johanste johanste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address the cast. As a general principle, we need to be careful about casts since doing them incorrectly negates all the value one get from type checking. In which case it is probably better to have them typed as Any rather than making guarantees that one cannot live up to.

@rakshith91 rakshith91 merged commit 303ff1f into Azure:master Mar 3, 2021
@rakshith91 rakshith91 deleted the core_cloud_eventy branch March 3, 2021 18:20
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Mar 3, 2021
…into http_request_json

* 'master' of https://github.com/Azure/azure-sdk-for-python: (147 commits)
  [text analytics] add perf tests (Azure#17060)
  Add cloud event to core (Azure#16800)
  [Perf] Small fixes to storage-blob (Azure#17055)
  [EG] Regenerate Code (Azure#17053)
  Scrub batch shared keys (Azure#17030)
  [Tables] Add SAS to tables (Azure#16717)
  T2 containerservice 2021 03 03 (Azure#17050)
  Addressing issues with CredScan (Azure#16944)
  Communication chat preview4 (Azure#16905) (Azure#17037)
  remove first query section (Azure#17033)
  [formrecognizer] temp disable sample tests until service bug fixed (Azure#17036)
  [device update] allow device update pylint failures (Azure#17034)
  fix build (Azure#17029)
  update artifact names for ALL packages to align with the actual package name
  Create azure-iot-nspkg (Azure#17026)
  [Communication]: SMS 1:N Messages, Custom Tags, and Idempotence (Azure#16836)
  Fixing credentials to use AAD (Azure#16885)
  T2 deviceupdate 2021 03 02 (Azure#17016)
  T2 cosmosdb 2021 02 23 (Azure#16875)
  T2 datadog 2021 03 02 (Azure#17004)
  ...
iscai-msft added a commit that referenced this pull request Mar 3, 2021
…into add_sample_check

* 'master' of https://github.com/Azure/azure-sdk-for-python: (388 commits)
  [text analytics] add normalized_text (#17074)
  Renaming with_token identity function (#17066)
  Adapt to azure core's cloud event (#17063)
  align perf tests with js (#17069)
  [Perfstress][Storage] Added FileShare perf tests (#15834)
  [formrecognizer] Adding custom forms perf test (#16969)
  Fix LanguageShort typo (#17068)
  sas creds updates (#17065)
  [eventgrid] Fix Sample eh (#17064)
  [Perfstress][Storage] Added Datalake perf tests (#15861)
  [text analytics] Healthcare n-ary relations (#16997)
  ServiceBus dict-representation acceptance and kwarg-update functionality  (#14807)
  [text analytics] add perf tests (#17060)
  Add cloud event to core (#16800)
  [Perf] Small fixes to storage-blob (#17055)
  [EG] Regenerate Code (#17053)
  Scrub batch shared keys (#17030)
  [Tables] Add SAS to tables (#16717)
  T2 containerservice 2021 03 03 (#17050)
  Addressing issues with CredScan (#16944)
  ...
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Mar 4, 2021
…into add_query_kwarg

* 'master' of https://github.com/Azure/azure-sdk-for-python: (69 commits)
  march release (Azure#16966)
  Release of Device Update for IoT Hub SDK for Python. (Azure#17005)
  Add Get-AllPackageInfoFromRepo (Azure#16947)
  Track1 package is incorrectly set as track2 (Azure#17075)
  [text analytics] add normalized_text (Azure#17074)
  Renaming with_token identity function (Azure#17066)
  Adapt to azure core's cloud event (Azure#17063)
  align perf tests with js (Azure#17069)
  [Perfstress][Storage] Added FileShare perf tests (Azure#15834)
  [formrecognizer] Adding custom forms perf test (Azure#16969)
  Fix LanguageShort typo (Azure#17068)
  sas creds updates (Azure#17065)
  [eventgrid] Fix Sample eh (Azure#17064)
  [Perfstress][Storage] Added Datalake perf tests (Azure#15861)
  [text analytics] Healthcare n-ary relations (Azure#16997)
  ServiceBus dict-representation acceptance and kwarg-update functionality  (Azure#14807)
  [text analytics] add perf tests (Azure#17060)
  Add cloud event to core (Azure#16800)
  [Perf] Small fixes to storage-blob (Azure#17055)
  [EG] Regenerate Code (Azure#17053)
  ...
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Mar 4, 2021
…into add_reprs

* 'master' of https://github.com/Azure/azure-sdk-for-python: (71 commits)
  EG - more docs imrpovement (Azure#17079)
  [EventHubs] add logging.info to warn the usage of partition key of non-string type (Azure#17057)
  march release (Azure#16966)
  Release of Device Update for IoT Hub SDK for Python. (Azure#17005)
  Add Get-AllPackageInfoFromRepo (Azure#16947)
  Track1 package is incorrectly set as track2 (Azure#17075)
  [text analytics] add normalized_text (Azure#17074)
  Renaming with_token identity function (Azure#17066)
  Adapt to azure core's cloud event (Azure#17063)
  align perf tests with js (Azure#17069)
  [Perfstress][Storage] Added FileShare perf tests (Azure#15834)
  [formrecognizer] Adding custom forms perf test (Azure#16969)
  Fix LanguageShort typo (Azure#17068)
  sas creds updates (Azure#17065)
  [eventgrid] Fix Sample eh (Azure#17064)
  [Perfstress][Storage] Added Datalake perf tests (Azure#15861)
  [text analytics] Healthcare n-ary relations (Azure#16997)
  ServiceBus dict-representation acceptance and kwarg-update functionality  (Azure#14807)
  [text analytics] add perf tests (Azure#17060)
  Add cloud event to core (Azure#16800)
  ...
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Mar 4, 2021
…into http_response_json

* 'master' of https://github.com/Azure/azure-sdk-for-python: (165 commits)
  EG - more docs imrpovement (Azure#17079)
  [EventHubs] add logging.info to warn the usage of partition key of non-string type (Azure#17057)
  march release (Azure#16966)
  Release of Device Update for IoT Hub SDK for Python. (Azure#17005)
  Add Get-AllPackageInfoFromRepo (Azure#16947)
  Track1 package is incorrectly set as track2 (Azure#17075)
  [text analytics] add normalized_text (Azure#17074)
  Renaming with_token identity function (Azure#17066)
  Adapt to azure core's cloud event (Azure#17063)
  align perf tests with js (Azure#17069)
  [Perfstress][Storage] Added FileShare perf tests (Azure#15834)
  [formrecognizer] Adding custom forms perf test (Azure#16969)
  Fix LanguageShort typo (Azure#17068)
  sas creds updates (Azure#17065)
  [eventgrid] Fix Sample eh (Azure#17064)
  [Perfstress][Storage] Added Datalake perf tests (Azure#15861)
  [text analytics] Healthcare n-ary relations (Azure#16997)
  ServiceBus dict-representation acceptance and kwarg-update functionality  (Azure#14807)
  [text analytics] add perf tests (Azure#17060)
  Add cloud event to core (Azure#16800)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cloud Events must check extensions to be lower case only
5 participants