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

Split create_basic_client into two methods #14673

Merged
merged 26 commits into from
Nov 3, 2020
Merged

Split create_basic_client into two methods #14673

merged 26 commits into from
Nov 3, 2020

Conversation

lmazuel
Copy link
Member

@lmazuel lmazuel commented Oct 21, 2020

Should help with

def create_basic_client(self, client_class, **kwargs):
# This is the patch for creating client using aio identity
tenant_id = os.environ.get("AZURE_TENANT_ID", None)
client_id = os.environ.get("AZURE_CLIENT_ID", None)
secret = os.environ.get("AZURE_CLIENT_SECRET", None)
if tenant_id and client_id and secret and self.is_live:
if _is_autorest_v3(client_class):
# Create azure-identity class
from azure.identity.aio import ClientSecretCredential
credentials = ClientSecretCredential(
tenant_id=tenant_id,
client_id=client_id,
client_secret=secret
)
else:
# Create msrestazure class
from msrestazure.azure_active_directory import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
tenant=tenant_id,
client_id=client_id,
secret=secret
)
else:
if _is_autorest_v3(client_class):
credentials = self.AsyncFakeCredential()
#credentials = self.settings.get_azure_core_credentials()
else:
credentials = self.settings.get_credentials()
# Real client creation
# FIXME decide what is the final argument for that
# if self.is_playback():
# kwargs.setdefault("polling_interval", 0)
if _is_autorest_v3(client_class):
kwargs.setdefault("logging_enable", True)
client = client_class(
credential=credentials,
**kwargs
)
else:
client = client_class(
credentials=credentials,
**kwargs
)
if self.is_playback():
try:
client._config.polling_interval = 0 # FIXME in azure-mgmt-core, make this a kwargs
except AttributeError:
pass
if hasattr(client, "config"): # Autorest v2
if self.is_playback():
client.config.long_running_operation_timeout = 0
client.config.enable_http_logger = True
return client

@lmazuel lmazuel requested a review from seankane-msft October 21, 2020 18:41
…ow, added a positional arg for create_client_from_credential for services that might have required positional args
@lmazuel lmazuel requested a review from yunhaoling as a code owner October 21, 2020 21:37
Comment on lines +224 to 228
return ServicePrincipalCredentials(
tenant=tenant_id,
client_id=client_id,
secret=secret
)
Copy link
Contributor

Choose a reason for hiding this comment

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

as now you're approaching the _get_real_credential method for returning real credential, I'm wondering we could do further unification here for this ServicePrincipalCredentials as well.

maybe we could just tweak the _get_real_credential signature a bit, making it

def _get_real_credential(self, is_async=false, crendetial_type=None):
# if crendetial_type is provided, just use the given type
if crendetial_type:
   return crendetial_type(client_id=...)
# otherwise we fall back to some default paths
if is_async:
    from ..

Copy link
Member Author

Choose a reason for hiding this comment

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

@yunhaoling that would have to be a string, since we can't import aio code in 2.7 (right now it works because the aio code is smartly wrapped in the right if).

So I'm not against, but I'm not sure we need in the first place (are we trying to solve an actual problem, or just do something cool for fun? :)) and this string challenge make the API less than ideal.

I'm happy to be pushed back if you strongly believe in it

Copy link
Contributor

@yunhaoling yunhaoling Oct 27, 2020

Choose a reason for hiding this comment

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

@lmazuel emmm, I see the reverse, it's because we're in the py2.7 so that we won't be importing and passing aio credential_type into the method -- it's the caller's duty to check which env/is async/etc. and make the right import.
the callee _get_credential simply takes what has been passed.

as you said, it's mostly "something cool for fun" -- the place where I come from is for the long term maintainability/scalability/readability -- that's what a unified approach could potentially provide.

but I might be bikeshedding and overengineering, let's waste no more time on this and get it work in our repo first😊

(btw: I didn't mean to block the PR, I'm good with these changes)

@seankane-msft
Copy link
Member

Addresses #13594

@seankane-msft
Copy link
Member

/azp run python - tables - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@seankane-msft
Copy link
Member

/azp run python - schemaregistry - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@seankane-msft
Copy link
Member

/azp run python - tables - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lmazuel lmazuel force-pushed the async_cred_test branch 2 times, most recently from 318cdf0 to 9b68e71 Compare November 3, 2020 00:20
iscai-msft
iscai-msft previously approved these changes Nov 3, 2020
@@ -241,6 +256,11 @@ def create_basic_client(self, client_class, **kwargs):
client.config.enable_http_logger = True
return client

def create_basic_client(self, client_class, **kwargs):
""" DO NOT USE ME ANYMORE."""
Copy link
Contributor

Choose a reason for hiding this comment

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

tiniest nit: do we want to log a warning instead of having a comment, more discoverable

@lmazuel lmazuel merged commit 4762e58 into master Nov 3, 2020
@lmazuel lmazuel deleted the async_cred_test branch November 3, 2020 21:15
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Nov 4, 2020
…into add_invoice_multipage

* 'master' of https://github.com/Azure/azure-sdk-for-python: (31 commits)
  add more samples (Azure#15007)
  Address feedbacks: Rename type, improve readme (Azure#14905)
  Correct Visual Studio Code default environment name (Azure#14648)
  [ServiceBus] Adjust AutoLockRenewer to only allow registration of intended types (ReceivedMessage and ServiceBusSession) (Azure#14600)
  Validate tenant IDs used in URLs (Azure#14955)
  [ServiceBus] Make sub-client initializers internal (add '_' to from_conn_str for receiver/sender) (Azure#14502)
  Raise minimum msal version to 1.6.0 (Azure#14992)
  update monitor to target '*' instead of azure-* (Azure#14985)
  Split create_basic_client into two methods (Azure#14673)
  [ServiceBus] receive related methods/constructor raise error if given max_wait_time <= 0 (Azure#14925)
  String unicode deserial (Azure#14958)
  Clarified exceptions docs (Azure#14947)
  search-documents: correct method calls in sample (Azure#14980)
  Catch exceptions thrown by async download (Azure#14946)
  Increment package version after release of azure_eventgrid (Azure#14266)
  [ServiceBus] Add additional stress test coverage to ensure parity with cross-language priorities (Azure#14437)
  Service Bus Connection String Parser (Azure#14772)
  Remove AmendCommit parameter (Azure#14965)
  revert creation takes full object change (Azure#14957)
  [ServiceBus] Settlement move from Message to Receiver (Azure#14681)
  ...
iscai-msft pushed a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Nov 4, 2020
* Update azure_testcase.py

* updated test_table(_async) to verify changes to azure_testcase work now, added a positional arg for create_client_from_credential for services that might have required positional args

* removed the positional *args in favor of pushing towards kwargs

* added private _get_real_credential with kwarg for is_async

* added the async fake credential that was copied from schema registry, and removed extra code from schema registry to verify works

* breaking the fake async credential into its own file and importing it when in Python 3

* fixing up comments based on laurent/adams review

* removing the second boolean for is_real, using self.is_live instead which does the same thing

* updating credential to be self.xxx_cred, caught in the tables live tests

* fixed a create_credential in test table async that did not use kwargs correctly

* Update azure_testcase.py

* updated test_table(_async) to verify changes to azure_testcase work now, added a positional arg for create_client_from_credential for services that might have required positional args

* removed the positional *args in favor of pushing towards kwargs

* added private _get_real_credential with kwarg for is_async

* added the async fake credential that was copied from schema registry, and removed extra code from schema registry to verify works

* breaking the fake async credential into its own file and importing it when in Python 3

* fixing up comments based on laurent/adams review

* removing the second boolean for is_real, using self.is_live instead which does the same thing

* updating credential to be self.xxx_cred, caught in the tables live tests

* fixed a create_credential in test table async that did not use kwargs correctly

* changing a word to force check-enforcer to re-evaulate CI

* Clarify async protocol

* adding a logging message to warn about create_basic_client and use get_cred/create from cred

* fixing up the syntax of the logger

Co-authored-by: seankane-msft <[email protected]>
iscai-msft added a commit to iscai-msft/azure-sdk-for-python that referenced this pull request Nov 4, 2020
…into add_invoice_multipage_tests

* 'master' of https://github.com/Azure/azure-sdk-for-python: (31 commits)
  add more samples (Azure#15007)
  Address feedbacks: Rename type, improve readme (Azure#14905)
  Correct Visual Studio Code default environment name (Azure#14648)
  [ServiceBus] Adjust AutoLockRenewer to only allow registration of intended types (ReceivedMessage and ServiceBusSession) (Azure#14600)
  Validate tenant IDs used in URLs (Azure#14955)
  [ServiceBus] Make sub-client initializers internal (add '_' to from_conn_str for receiver/sender) (Azure#14502)
  Raise minimum msal version to 1.6.0 (Azure#14992)
  update monitor to target '*' instead of azure-* (Azure#14985)
  Split create_basic_client into two methods (Azure#14673)
  [ServiceBus] receive related methods/constructor raise error if given max_wait_time <= 0 (Azure#14925)
  String unicode deserial (Azure#14958)
  Clarified exceptions docs (Azure#14947)
  search-documents: correct method calls in sample (Azure#14980)
  Catch exceptions thrown by async download (Azure#14946)
  Increment package version after release of azure_eventgrid (Azure#14266)
  [ServiceBus] Add additional stress test coverage to ensure parity with cross-language priorities (Azure#14437)
  Service Bus Connection String Parser (Azure#14772)
  Remove AmendCommit parameter (Azure#14965)
  revert creation takes full object change (Azure#14957)
  [ServiceBus] Settlement move from Message to Receiver (Azure#14681)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants