From dda7a7f7b829e81af5195ec72abe301a699b3fac Mon Sep 17 00:00:00 2001 From: Yugang Wang Date: Sat, 27 Oct 2018 22:36:02 -0700 Subject: [PATCH] finalize all --- msrestazure/azure_local_creds_prober.py | 6 ++++- tests/test_local_creds_prober.py | 30 ++++++++++++------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/msrestazure/azure_local_creds_prober.py b/msrestazure/azure_local_creds_prober.py index dd82a66..be0b5bb 100644 --- a/msrestazure/azure_local_creds_prober.py +++ b/msrestazure/azure_local_creds_prober.py @@ -169,6 +169,7 @@ def __init__(self, cli_path, subscription_id=None): self.cli_path = cli_path self.subscription_id = subscription_id self.expires_on = None + self.resource = None def set_token(self): from dateutil import parser @@ -187,12 +188,14 @@ def _invoke_cli_token_command(self): args = [self.cli_path, 'account', 'get-access-token'] if self.subscription_id: args.extend(['--subscription', self.subscription_id]) + if self.resource: + args.extend(['--resource', self.resource]) process = Popen(args, stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() process.wait() if stderr: raise ValueError('Retrieving acccess token failed: ' + stderr) - return json.loads(stdout) + return json.loads(stdout.decode()) def signed_session(self, session=None): self.set_token() @@ -209,6 +212,7 @@ def _get_creds_through_local_probing(**kwargs): for prober in probers: creds = prober.probe() if creds: + creds.resource = resource return creds, prober raise ValueError('No credential was detected from the local machine') diff --git a/tests/test_local_creds_prober.py b/tests/test_local_creds_prober.py index 8489590..59effeb 100644 --- a/tests/test_local_creds_prober.py +++ b/tests/test_local_creds_prober.py @@ -25,22 +25,20 @@ #-------------------------------------------------------------------------- -class TestCredsLocalProbing(unittest.TestCase): +from azure.mgmt.storage import StorageManagementClient +from azure.multiapi.storage.v2018_03_28.blob.blockblobservice import BlockBlobService +from msrestazure.azure_local_creds_prober import (get_client_through_local_creds_probing, + get_creds_through_local_probing) - @unittest.skip("demo sample client code") - def test_create_client_from_probed_creds(self): +# test management plane +client = get_client_through_local_creds_probing(StorageManagementClient) +accounts = list(client.storage_accounts.list()) +print('Found {} accounts'.format(len(accounts))) - from azure.mgmt.storage import StorageManagementClient - from azure.multiapi.storage.v2018_03_28.blob.blockblobservice import BlockBlobService - from msrestazure.azure_local_creds_prober import (get_client_through_local_creds_probing, - get_creds_through_local_probing) +# test storage data plane +creds = get_creds_through_local_probing(resource="https://storage.azure.com/") +test_storage_account, container, blob, file_to_upload = 'teststor12345678', 'temp', 'hey.exe', r'c:\temp\hey.exe' +storage_data_client = BlockBlobService(token_credential=creds, account_name=test_storage_account) +storage_data_client.create_blob_from_path(container, blob, file_to_upload) +print('uploaded') - # look for storage account - client = get_client_through_local_creds_probing(StorageManagementClient) - accounts = list(client.storage_accounts.list()) - print('Found {} accounts'.format(len(accounts))) - - # look for containers in the 1st storage account through storage preview service - creds = get_creds_through_local_probing(resource="https://storage.azure.com/") - storage_data_client2 = BlockBlobService(token_credential=creds, account_name=accounts[0].name) - print(len(list(storage_data_client2.list_containers())))