Skip to content

Commit 9e79fc8

Browse files
authored
[EventHub] update tests to remove connection string (#35819)
* update tests for DefaultAzureCredential * add storage blob data owner role for storage checkpoint store test * fix test error * use federated auth * remove extra envvars in tests.yml * add back sub id * update tests.yml * update tests to use devtools_testutils get credential * add test run live env var to tests.yml * set tracing back to none before creating resources * fix issue with empty context pop IndexError in FakeSpan * update client_secret_credential test to use devtools get_credential * add pauls fix to set tracing implementation in fixture to make set tracing independent of test failure * fix failing tests * skipping parts of test that enable tracing * update samples * mypy * udpate readme to remove conn str
1 parent d840732 commit 9e79fc8

File tree

59 files changed

+4028
-2052
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4028
-2052
lines changed

sdk/eventhub/azure-eventhub/README.md

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,25 @@ The following sections provide several code snippets covering some of the most c
118118

119119
Get the partition ids of an Event Hub.
120120

121-
<!-- SNIPPET:connection_string_authentication.connection_string_authentication -->
122-
123121
```python
124122
import os
125123
from azure.eventhub import EventHubConsumerClient
124+
from azure.identity import DefaultAzureCredential
126125

127-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
126+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_HOSTNAME"]
128127
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
129128

130-
consumer_client = EventHubConsumerClient.from_connection_string(
131-
conn_str=CONNECTION_STR,
129+
consumer_client = EventHubConsumerClient(
130+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
132131
consumer_group='$Default',
133132
eventhub_name=EVENTHUB_NAME,
133+
credential=DefaultAzureCredential(),
134134
)
135135

136136
with consumer_client:
137137
pass # consumer_client is now ready to be used.
138138
```
139139

140-
<!-- END SNIPPET -->
141-
142140
### Publish events to an Event Hub
143141

144142
Use the `create_batch` method on `EventHubProducerClient` to create an `EventDataBatch` object which can then be sent using the `send_batch` method.
@@ -165,11 +163,17 @@ the `EventHubConsumerClient.receive` method will be of use as follows:
165163
```python
166164
import logging
167165
from azure.eventhub import EventHubConsumerClient
166+
from azure.identity import DefaultAzureCredential
168167

169-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
168+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
170169
consumer_group = '<< CONSUMER GROUP >>'
171170
eventhub_name = '<< NAME OF THE EVENT HUB >>'
172-
client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)
171+
client = EventHubConsumerClient(
172+
fully_qualified_namespace=fully_qualified_namespace,
173+
eventhub_name=eventhub_name,
174+
consumer_group=consumer_group,
175+
credential=DefaultAzureCredential(),
176+
)
173177

174178
logger = logging.getLogger("azure.eventhub")
175179
logging.basicConfig(level=logging.INFO)
@@ -195,11 +199,17 @@ triggers the callback on a batch of events, attempting to receive a number at a
195199
```python
196200
import logging
197201
from azure.eventhub import EventHubConsumerClient
202+
from azure.identity import DefaultAzureCredential
198203

199-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
204+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
200205
consumer_group = '<< CONSUMER GROUP >>'
201206
eventhub_name = '<< NAME OF THE EVENT HUB >>'
202-
client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)
207+
client = EventHubConsumerClient(
208+
fully_qualified_namespace=fully_qualified_namespace,
209+
eventhub_name=eventhub_name,
210+
consumer_group=consumer_group,
211+
credential=DefaultAzureCredential(),
212+
)
203213

204214
logger = logging.getLogger("azure.eventhub")
205215
logging.basicConfig(level=logging.INFO)
@@ -223,11 +233,11 @@ Use the `create_batch` method on `EventHubProducer` to create an `EventDataBatch
223233
Events may be added to the `EventDataBatch` using the `add` method until the maximum batch size limit in bytes has been reached.
224234
```python
225235
import asyncio
226-
from azure.eventhub.aio import EventHubProducerClient # The package name suffixed with ".aio" for async
236+
from azure.eventhub.aio import EventHubProducerClient # The package name suffixed with ".aio" for async
227237
from azure.eventhub import EventData
238+
from azure.identity.aio import DefaultAzureCredential
228239

229-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
230-
consumer_group = '<< CONSUMER GROUP >>'
240+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
231241
eventhub_name = '<< NAME OF THE EVENT HUB >>'
232242

233243
async def create_batch(client):
@@ -241,7 +251,11 @@ async def create_batch(client):
241251
return event_data_batch
242252

243253
async def send():
244-
client = EventHubProducerClient.from_connection_string(connection_str, eventhub_name=eventhub_name)
254+
client = EventHubProducerClient(
255+
fully_qualified_namespace=fully_qualified_namespace,
256+
eventhub_name=eventhub_name,
257+
credential=DefaultAzureCredential(),
258+
)
245259
batch_data = await create_batch(client)
246260
async with client:
247261
await client.send_batch(batch_data)
@@ -260,8 +274,9 @@ aio, one would need the following:
260274
import logging
261275
import asyncio
262276
from azure.eventhub.aio import EventHubConsumerClient
277+
from azure.identity.aio import DefaultAzureCredential
263278

264-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
279+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
265280
consumer_group = '<< CONSUMER GROUP >>'
266281
eventhub_name = '<< NAME OF THE EVENT HUB >>'
267282

@@ -273,7 +288,12 @@ async def on_event(partition_context, event):
273288
await partition_context.update_checkpoint(event)
274289

275290
async def receive():
276-
client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)
291+
client = EventHubConsumerClient(
292+
fully_qualified_namespace=fully_qualified_namespace,
293+
eventhub_name=eventhub_name,
294+
consumer_group=consumer_group,
295+
credential=DefaultAzureCredential(),
296+
)
277297
async with client:
278298
await client.receive(
279299
on_event=on_event,
@@ -296,8 +316,9 @@ the same within asyncio as follows:
296316
import logging
297317
import asyncio
298318
from azure.eventhub.aio import EventHubConsumerClient
319+
from azure.identity.aio import DefaultAzureCredential
299320

300-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
321+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
301322
consumer_group = '<< CONSUMER GROUP >>'
302323
eventhub_name = '<< NAME OF THE EVENT HUB >>'
303324

@@ -309,7 +330,12 @@ async def on_event_batch(partition_context, events):
309330
await partition_context.update_checkpoint()
310331

311332
async def receive_batch():
312-
client = EventHubConsumerClient.from_connection_string(connection_str, consumer_group, eventhub_name=eventhub_name)
333+
client = EventHubConsumerClient(
334+
fully_qualified_namespace=fully_qualified_namespace,
335+
eventhub_name=eventhub_name,
336+
consumer_group=consumer_group,
337+
credential=DefaultAzureCredential(),
338+
)
313339
async with client:
314340
await client.receive_batch(
315341
on_event_batch=on_event_batch,
@@ -351,11 +377,12 @@ import asyncio
351377

352378
from azure.eventhub.aio import EventHubConsumerClient
353379
from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore
380+
from azure.identity.aio import DefaultAzureCredential
354381

355-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
382+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
356383
consumer_group = '<< CONSUMER GROUP >>'
357384
eventhub_name = '<< NAME OF THE EVENT HUB >>'
358-
storage_connection_str = '<< CONNECTION STRING FOR THE STORAGE >>'
385+
blob_account_url = '<< STORAGE ACCOUNT URL >>'
359386
container_name = '<<NAME OF THE BLOB CONTAINER>>'
360387

361388
async def on_event(partition_context, event):
@@ -369,11 +396,16 @@ async def receive(client):
369396
)
370397

371398
async def main():
372-
checkpoint_store = BlobCheckpointStore.from_connection_string(storage_connection_str, container_name)
373-
client = EventHubConsumerClient.from_connection_string(
374-
connection_str,
375-
consumer_group,
399+
checkpoint_store = BlobCheckpointStore(
400+
blob_account_url=blob_account_url,
401+
container_name=container_name,
402+
credential=DefaultAzureCredential()
403+
)
404+
client = EventHubConsumerClient(
405+
fully_qualified_namespace=fully_qualified_namespace,
376406
eventhub_name=eventhub_name,
407+
credential=DefaultAzureCredential(),
408+
consumer_group=consumer_group,
377409
checkpoint_store=checkpoint_store, # For load balancing and checkpoint. Leave None for no load balancing
378410
)
379411
async with client:
@@ -464,16 +496,24 @@ $ pip install uamqp
464496

465497
```python
466498
from azure.eventhub import EventHubProducerClient, EventHubConsumerClient
499+
from azure.identity import DefaultAzureCredential
467500

468-
connection_str = '<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>'
501+
fully_qualified_namespace = '<< EVENT HUBS FULLY QUALIFIED NAMESPACE >>'
469502
consumer_group = '<< CONSUMER GROUP >>'
470503
eventhub_name = '<< NAME OF THE EVENT HUB >>'
471504

472-
client = EventHubProducerClient.from_connection_string(
473-
connection_str, eventhub_name=eventhub_name, uamqp_transport=True
505+
client = EventHubProducerClient(
506+
fully_qualified_namespace=fully_qualified_namespace,
507+
eventhub_name=eventhub_name,
508+
credential=DefaultAzureCredential(),
509+
uamqp_transport=True
474510
)
475-
client = EventHubConsumerClient.from_connection_string(
476-
connection_str, consumer_group, eventhub_name=eventhub_name, uamqp_transport=True
511+
client = EventHubConsumerClient(
512+
fully_qualified_namespace=fully_qualified_namespace,
513+
eventhub_name=eventhub_name,
514+
credential=DefaultAzureCredential(),
515+
consumer_group=consumer_group,
516+
uamqp_transport=True
477517
)
478518
```
479519

sdk/eventhub/azure-eventhub/samples/async_samples/connection_to_custom_endpoint_address_async.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import asyncio
1414
from azure.eventhub import EventData
1515
from azure.eventhub.aio import EventHubProducerClient, EventHubConsumerClient
16+
from azure.identity.aio import DefaultAzureCredential
1617

17-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
18+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_CONN_STR"]
1819
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
1920
# The custom endpoint address to use for establishing a connection to the Event Hubs service,
2021
# allowing network requests to be routed through any application gateways
@@ -27,8 +28,9 @@
2728

2829

2930
async def producer_connecting_to_custom_endpoint():
30-
producer_client = EventHubProducerClient.from_connection_string(
31-
conn_str=CONNECTION_STR,
31+
producer_client = EventHubProducerClient(
32+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
33+
credential=DefaultAzureCredential(),
3234
eventhub_name=EVENTHUB_NAME,
3335
custom_endpoint_address=CUSTOM_ENDPOINT_ADDRESS,
3436
connection_verify=CUSTOM_CA_BUNDLE_PATH,
@@ -50,10 +52,11 @@ async def on_event(partition_context, event):
5052

5153

5254
async def consumer_connecting_to_custom_endpoint():
53-
consumer_client = EventHubConsumerClient.from_connection_string(
54-
conn_str=CONNECTION_STR,
55-
consumer_group='$Default',
55+
consumer_client = EventHubConsumerClient(
56+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
57+
credential=DefaultAzureCredential(),
5658
eventhub_name=EVENTHUB_NAME,
59+
consumer_group='$Default',
5760
custom_endpoint_address=CUSTOM_ENDPOINT_ADDRESS,
5861
connection_verify=CUSTOM_CA_BUNDLE_PATH,
5962
)

sdk/eventhub/azure-eventhub/samples/async_samples/proxy_async.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import asyncio
1414
from azure.eventhub.aio import EventHubConsumerClient, EventHubProducerClient
1515
from azure.eventhub import EventData
16+
from azure.identity.aio import DefaultAzureCredential
1617

17-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
18+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_HOSTNAME"]
1819
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
1920

2021
HTTP_PROXY: Dict[str, Union[str, int]] = {
@@ -32,16 +33,18 @@ async def on_event(partition_context, event):
3233

3334

3435
async def main():
35-
consumer_client = EventHubConsumerClient.from_connection_string(
36-
conn_str=CONNECTION_STR,
37-
consumer_group='$Default',
36+
consumer_client = EventHubConsumerClient(
37+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
3838
eventhub_name=EVENTHUB_NAME,
39+
credential=DefaultAzureCredential(),
40+
consumer_group='$Default',
3941
http_proxy=HTTP_PROXY
4042
)
4143

42-
producer_client = EventHubProducerClient.from_connection_string(
43-
conn_str=CONNECTION_STR,
44+
producer_client = EventHubProducerClient(
45+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
4446
eventhub_name=EVENTHUB_NAME,
47+
credential=DefaultAzureCredential(),
4548
http_proxy=HTTP_PROXY
4649
)
4750

sdk/eventhub/azure-eventhub/samples/async_samples/receive_batch_with_checkpoint_async.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
import logging
1818
from azure.eventhub.aio import EventHubConsumerClient
1919
from azure.eventhub.extensions.checkpointstoreblobaio import BlobCheckpointStore
20+
from azure.identity.aio import DefaultAzureCredential
2021

21-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
22+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_HOSTNAME"]
2223
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
23-
STORAGE_CONNECTION_STR = os.environ["AZURE_STORAGE_CONN_STR"]
24+
25+
storage_account_name = os.environ["AZURE_STORAGE_ACCOUNT"]
26+
protocol = os.environ.get("PROTOCOL", "https")
27+
suffix = os.environ.get("ACCOUNT_URL_SUFFIX", "core.windows.net")
28+
BLOB_ACCOUNT_URL = f"{protocol}://{storage_account_name}.blob.{suffix}"
2429
BLOB_CONTAINER_NAME = "your-blob-container-name" # Please make sure the blob container resource exists.
2530

2631
logging.basicConfig(level=logging.INFO)
@@ -39,11 +44,16 @@ async def on_event_batch(partition_context, event_batch):
3944

4045

4146
async def receive_batch():
42-
checkpoint_store = BlobCheckpointStore.from_connection_string(STORAGE_CONNECTION_STR, BLOB_CONTAINER_NAME)
43-
client = EventHubConsumerClient.from_connection_string(
44-
CONNECTION_STR,
45-
consumer_group="$Default",
47+
checkpoint_store = BlobCheckpointStore(
48+
blob_account_url=BLOB_ACCOUNT_URL,
49+
container_name=BLOB_CONTAINER_NAME,
50+
credential=DefaultAzureCredential()
51+
)
52+
client = EventHubConsumerClient(
53+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
4654
eventhub_name=EVENTHUB_NAME,
55+
credential=DefaultAzureCredential(),
56+
consumer_group="$Default",
4757
checkpoint_store=checkpoint_store,
4858
)
4959
async with client:

sdk/eventhub/azure-eventhub/samples/async_samples/recv_async.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
import asyncio
1313
import os
1414
from azure.eventhub.aio import EventHubConsumerClient
15+
from azure.identity.aio import DefaultAzureCredential
1516

16-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
17+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_HOSTNAME"]
1718
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
1819

1920

@@ -49,10 +50,11 @@ async def on_error(partition_context, error):
4950

5051

5152
async def main():
52-
client = EventHubConsumerClient.from_connection_string(
53-
conn_str=CONNECTION_STR,
53+
client = EventHubConsumerClient(
54+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
55+
eventhub_name=EVENTHUB_NAME,
56+
credential=DefaultAzureCredential(),
5457
consumer_group="$default",
55-
eventhub_name=EVENTHUB_NAME
5658
)
5759
async with client:
5860
await client.receive(

sdk/eventhub/azure-eventhub/samples/async_samples/recv_for_period_async.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import os
1414
import time
1515
from azure.eventhub.aio import EventHubConsumerClient
16+
from azure.identity.aio import DefaultAzureCredential
1617

17-
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
18+
FULLY_QUALIFIED_NAMESPACE = os.environ["EVENT_HUB_HOSTNAME"]
1819
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
1920
RECEIVE_DURATION = 15
2021

@@ -50,10 +51,11 @@ async def on_error(partition_context, error):
5051

5152

5253
async def main():
53-
client = EventHubConsumerClient.from_connection_string(
54-
conn_str=CONNECTION_STR,
54+
client = EventHubConsumerClient(
55+
fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
56+
eventhub_name=EVENTHUB_NAME,
57+
credential=DefaultAzureCredential(),
5558
consumer_group="$default",
56-
eventhub_name=EVENTHUB_NAME
5759
)
5860

5961
print('Consumer will keep receiving for {} seconds, start time is {}.'.format(RECEIVE_DURATION, time.time()))

0 commit comments

Comments
 (0)