def eventhub_namespace(resource_group): try: SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"] except KeyError: pytest.skip('AZURE_SUBSCRIPTION_ID defined') return resource_client = EventHubManagementClient(EnvironmentCredential(), SUBSCRIPTION_ID) namespace_name = NAMESPACE_PREFIX + str(uuid.uuid4()) try: namespace = resource_client.namespaces.begin_create_or_update( resource_group.name, namespace_name, { "location": LOCATION }).result() key = resource_client.namespaces.list_keys( resource_group.name, namespace_name, EVENTHUB_DEFAULT_AUTH_RULE_NAME) connection_string = key.primary_connection_string key_name = key.key_name primary_key = key.primary_key yield namespace.name, connection_string, key_name, primary_key finally: try: resource_client.namespaces.begin_delete(resource_group.name, namespace_name).wait() except: warnings.warn(UserWarning("eventhub namespace teardown failed"))
def live_eventhub(resource_group, eventhub_namespace): # pylint: disable=redefined-outer-name try: SUBSCRIPTION_ID = os.environ["AZURE_SUBSCRIPTION_ID"] except KeyError: pytest.skip('AZURE_SUBSCRIPTION_ID defined') return resource_client = EventHubManagementClient(EnvironmentCredential(), SUBSCRIPTION_ID) eventhub_name = EVENTHUB_PREFIX + str(uuid.uuid4()) eventhub_ns_name, connection_string, key_name, primary_key = eventhub_namespace try: eventhub = resource_client.event_hubs.create_or_update( resource_group.name, eventhub_ns_name, eventhub_name, {"partition_count": PARTITION_COUNT}) live_eventhub_config = { 'resource_group': resource_group.name, 'hostname': "{}.servicebus.windows.net".format(eventhub_ns_name), 'key_name': key_name, 'access_key': primary_key, 'namespace': eventhub_ns_name, 'event_hub': eventhub.name, 'consumer_group': '$Default', 'partition': '0', 'connection_str': connection_string + ";EntityPath=" + eventhub.name } yield live_eventhub_config finally: try: resource_client.event_hubs.delete(resource_group.name, eventhub_ns_name, eventhub_name) except: warnings.warn(UserWarning("eventhub teardown failed"))
def get_event_hub_resource_id(self): namespace_name = self.get_resource_name( "SUMOBREventHubNamespace", "Microsoft.EventHub/namespaces") eventhub_client = EventHubManagementClient(self.credentials, self.subscription_id) ehitr = eventhub_client.event_hubs.list_by_namespace( self.RESOURCE_GROUP_NAME, namespace_name) return next(ehitr).id # assming single eventhub
def main(): SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None) GROUP_NAME = "testgroupx" NAMESPACE_NAME = "namespacex" # Create client # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ resource_client = ResourceManagementClient( credential=DefaultAzureCredentials(), subscription_id=SUBSCRIPTION_ID) eventhub_client = EventHubManagementClient( credential=DefaultAzureCredentials(), subscription_id=SUBSCRIPTION_ID) # Create resource group resource_client.resource_groups.create_or_update(GROUP_NAME, {"location": "eastus"}) # Create Namespace namesapce = eventhub_client.namespaces.begin_create_or_update( GROUP_NAME, NAMESPACE_NAME, { "sku": { "name": "Standard", "tier": "Standard" }, "location": "eastus", "tags": { "tag1": "value1", "tag2": "value2" } }).result() print("Create Namespace: {}".format(namesapce)) # Get Namesapce namespace = eventhub_client.namespaces.get(GROUP_NAME, NAMESPACE_NAME) print("Get Namespace: {}".format(namespace)) # Update Namespace namespace = eventhub_client.namespaces.update(GROUP_NAME, NAMESPACE_NAME, { "location": "eastus", "tags": { "tag3": "value3", "tag4": "value4" } }) print("Update Namespace: {}".format(namesapce)) # Delete Namespace eventhub_client.namespaces.begin_delete(GROUP_NAME, NAMESPACE_NAME).result() print("Delete Namespace.") # Delete resource group resource_client.resource_groups.begin_delete(GROUP_NAME).result()
def _get_eventhub_client(self) -> EventHubManagementClient: """Constructs an EventHub Management client Returns: An EventHub Management client """ credentials = get_azure_credentials_object(self.config, self.vault_name, self.vault_client) return EventHubManagementClient( credentials, SubscriptionId(self.vault_name, self.vault_client).subscription_id(self.config))
def _get_eventhub_client(self) -> EventHubManagementClient: """Constructs an EventHub Management client Returns: An EventHub Management client """ credentials = ActiveDirectoryUserCredentials( vault_name=self.vault_name, vault_client=self.vault_client).credentials(self.config) return EventHubManagementClient( credentials, SubscriptionId(self.vault_name, self.vault_client).subscription_id(self.config))
def create_mgmt_client(credentials, subscription, location='westus'): from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.eventhub import EventHubManagementClient resource_client = ResourceManagementClient(credentials, subscription) rg_name = 'pytest-{}'.format(uuid.uuid4()) resource_group = resource_client.resource_groups.create_or_update( rg_name, {'location': location}) eh_client = EventHubManagementClient(credentials, subscription) namespace = 'pytest-{}'.format(uuid.uuid4()) creator = eh_client.namespaces.create_or_update(resource_group.name, namespace) create.wait() return resource_group, eh_client
def insert_mock_logs_in_EventHub(self): print("Inserting fake logs in EventHub") namespace_name = self.get_eventhub_namespace() eventhub_name = 'insights-metrics-pt1m' defaultauthorule_name = "RootManageSharedAccessKey" eventhub_client = EventHubManagementClient(self.credentials, self.subscription_id) ehkeys = eventhub_client.namespaces.list_keys(self.RESOURCE_GROUP_NAME, namespace_name, defaultauthorule_name) sbs = ServiceBusService(namespace_name, shared_access_key_name=defaultauthorule_name, shared_access_key_value=ehkeys.primary_key, request_session=Session()) mock_logs = json.load(open('metrics_fixtures.json')) print("inserting %s" % (mock_logs)) sbs.send_event(eventhub_name, json.dumps(mock_logs)) print("Event inserted")
def insert_mock_logs_in_EventHub(self, filename): print("Inserting fake logs in EventHub") namespace_name = self.get_resource_name( self.event_hub_namespace_prefix, "Microsoft.EventHub/namespaces") defaultauthorule_name = "RootManageSharedAccessKey" eventhub_client = EventHubManagementClient(self.credentials, self.subscription_id) ehkeys = eventhub_client.namespaces.list_keys(self.RESOURCE_GROUP_NAME, namespace_name, defaultauthorule_name) sbs = ServiceBusService(namespace_name, shared_access_key_name=defaultauthorule_name, shared_access_key_value=ehkeys.primary_key, request_session=Session()) mock_logs = json.load(open(filename)) print("inserting %s" % (mock_logs)) sbs.send_event(self.eventhub_name, json.dumps(mock_logs)) print("Event inserted")
def main(): SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None) GROUP_NAME = "testgroupx" STORAGE_ACCOUNT_NAME = "storageaccountxyzxyzxx" NAMESPACE = "namespacex" EVENTHUB = "eventhub" AUTHORIZATION_RULE = "authorizationx" INSIGHT = "insightx" WORKSPACE_NAME = "workspacex" WORKFLOW_NAME = "workflow" # Create client # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ resource_client = ResourceManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) storage_client = StorageManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) eventhub_client = EventHubManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) monitor_client = MonitorClient(credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) loganalytics_client = LogAnalyticsManagementClient( credentials=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) logic_client = LogicManagementClient(credentials=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) # Create resource group resource_client.resource_groups.create_or_update(GROUP_NAME, {"location": "eastus"}) # Create Storage storage_account = storage_client.storage_accounts.begin_create( GROUP_NAME, STORAGE_ACCOUNT_NAME, { "sku": { "name": "Standard_LRS" }, "kind": "Storage", "location": "eastus", "enable_https_traffic_only": True }).result() # Create eventhub authorization rule eventhub_client.namespaces.begin_create_or_update( GROUP_NAME, NAMESPACE, { "sku": { "name": "Standard", "tier": "Standard" }, "location": "eastus", "tags": { "tag1": "value1", "tag2": "value2" } }).result() eventhub_client.namespaces.create_or_update_authorization_rule( GROUP_NAME, NAMESPACE, AUTHORIZATION_RULE, {"rights": ["Listen", "Send", "Manage"]}) eventhub_client.event_hubs.create_or_update( GROUP_NAME, NAMESPACE, EVENTHUB, { "message_retention_in_days": "4", "partition_count": "4", "status": "Active", "capture_description": { "enabled": True, "encoding": "Avro", "interval_in_seconds": "120", "size_limit_in_bytes": "10485763", "destination": { "name": "EventHubArchive.AzureBlockBlob", "storage_account_resource_id": storage_account.id, "blob_container": "container", "archive_name_format": "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}" } } }) eventhub_client.event_hubs.create_or_update_authorization_rule( GROUP_NAME, NAMESPACE, EVENTHUB, AUTHORIZATION_RULE, {"rights": ["Listen", "Send", "Manage"]}) # Create workspace workspace = loganalytics_client.workspaces.create_or_update( GROUP_NAME, WORKSPACE_NAME, { "sku": { "name": "PerNode" }, "retention_in_days": 30, "location": "eastus", "tags": { "tag1": "val1" } }).result() # Create workflow workflow = logic_client.workflows.create_or_update( GROUP_NAME, WORKFLOW_NAME, { "location": "eastus", "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "contentVersion": "1.0.0.0", "parameters": {}, "triggers": {}, "actions": {}, "outputs": {} } }) RESOURCE_URI = workflow.id # Create diagnostic setting diagnostic_setting = monitor_client.diagnostic_settings.create_or_update( RESOURCE_URI, INSIGHT, { "storage_account_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + GROUP_NAME + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME + "", "workspace_id": workspace.id, "event_hub_authorization_rule_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + GROUP_NAME + "/providers/microsoft.eventhub/namespaces/" + NAMESPACE + "/authorizationrules/" + AUTHORIZATION_RULE, "event_hub_name": EVENTHUB, "metrics": [], "logs": [{ "category": "WorkflowRuntime", "enabled": True, "retention_policy": { "enabled": False, "days": "0" } }], }) print("Create diagnostic setting:\n{}".format(diagnostic_setting)) # Get diagnostic setting diagnostic_setting = monitor_client.diagnostic_settings.get( RESOURCE_URI, INSIGHT) print("Get diagnostic setting:\n{}".format(diagnostic_setting)) # Delete diagnostic setting monitor_client.diagnostic_settings.delete(RESOURCE_URI, INSIGHT) print("Delete diagnostic setting.") # Delete Group resource_client.resource_groups.begin_delete(GROUP_NAME).result()
def main(): SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None) GROUP_NAME = "testgroupx" STORAGE_ACCOUNT_NAME = "storageaccountxyztest" NAMESPACE_NAME = "namespacex" EVENTHUB_NAME = "eventhubx" CONSUMERGROUP_NAME = "consumergroup" # Create client # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ resource_client = ResourceManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) eventhub_client = EventHubManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) storage_client = StorageManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) # Create resource group resource_client.resource_groups.create_or_update(GROUP_NAME, {"location": "eastus"}) # Create StorageAccount storage_client.storage_accounts.begin_create(GROUP_NAME, STORAGE_ACCOUNT_NAME, { "sku": { "name": "Standard_LRS" }, "kind": "StorageV2", "location": "eastus" }).result() # Create Namespace eventhub_client.namespaces.begin_create_or_update( GROUP_NAME, NAMESPACE_NAME, { "sku": { "name": "Standard", "tier": "Standard" }, "location": "South Central US", "tags": { "tag1": "value1", "tag2": "value2" } }).result() # Create EventHub eventhub_client.event_hubs.create_or_update( GROUP_NAME, NAMESPACE_NAME, EVENTHUB_NAME, { "message_retention_in_days": "4", "partition_count": "4", "status": "Active", "capture_description": { "enabled": True, "encoding": "Avro", "interval_in_seconds": "120", "size_limit_in_bytes": "10485763", "destination": { "name": "EventHubArchive.AzureBlockBlob", "storage_account_resource_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + GROUP_NAME + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME + "", "blob_container": "container", "archive_name_format": "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}" } } }) # Create Consumer Group consumer_group = eventhub_client.consumer_groups.create_or_update( GROUP_NAME, NAMESPACE_NAME, EVENTHUB_NAME, CONSUMERGROUP_NAME, {"user_metadata": "New consumergroup"}) print("Create consumer group:\n{}".format(consumer_group)) # Get Consumer Group consumer_group = eventhub_client.consumer_groups.get( GROUP_NAME, NAMESPACE_NAME, EVENTHUB_NAME, CONSUMERGROUP_NAME) print("Get consumer group:\n{}".format(consumer_group)) # Delete Consumer Group eventhub_client.consumer_groups.delete(GROUP_NAME, NAMESPACE_NAME, EVENTHUB_NAME, CONSUMERGROUP_NAME) print("Delete consumer group.") # Delete EventHub eventhub_client.event_hubs.delete(GROUP_NAME, NAMESPACE_NAME, EVENTHUB_NAME) # Delete Namespace eventhub_client.namespaces.begin_delete(GROUP_NAME, NAMESPACE_NAME).result() # Delete StorageAccount storage_client.storage_accounts.delete(GROUP_NAME, STORAGE_ACCOUNT_NAME) # Delete resource group resource_client.resource_groups.begin_delete(GROUP_NAME).result()
def main(): SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None) GROUP_NAME = "testgroupx" NAMESPACE_NAME = "namespacex" NAMESPACE_NAME_2 = "namespacextwo" DISASTER_RECOVERY_CONFIG_NAME = "disasterrecoveryconfig" # Create client # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ resource_client = ResourceManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) eventhub_client = EventHubManagementClient( credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID) # Create resource group resource_client.resource_groups.create_or_update(GROUP_NAME, {"location": "eastus"}) # Create Namespace eventhub_client.namespaces.begin_create_or_update( GROUP_NAME, NAMESPACE_NAME, { "sku": { "name": "Standard", "tier": "Standard" }, "location": "eastus", "tags": { "tag1": "value1", "tag2": "value2" } }).result() # Create Second Namespace second_namespace = eventhub_client.namespaces.begin_create_or_update( GROUP_NAME, NAMESPACE_NAME_2, { "sku": { "name": "Standard", "tier": "Standard" }, "location": "westus", "tags": { "tag1": "value1", "tag2": "value2" } }).result() # Check name availability result = eventhub_client.disaster_recovery_configs.check_name_availability( GROUP_NAME, NAMESPACE_NAME, {"name": NAMESPACE_NAME_2}) print("Check name availability: {}".format(result)) # Create disaster recovery config config = eventhub_client.disaster_recovery_configs.create_or_update( GROUP_NAME, NAMESPACE_NAME, DISASTER_RECOVERY_CONFIG_NAME, {"partner_namespace": second_namespace.id}) print("Create disaster recovery config: {}".format(config)) # Get disaster recovery config for _ in range(5): config = eventhub_client.disaster_recovery_configs.get( GROUP_NAME, NAMESPACE_NAME, DISASTER_RECOVERY_CONFIG_NAME) if config.provisioning_state == "Successed": break time.sleep(30) print("Get disaster recovery config: {}".format(config)) # Break pairing disaster recovery config eventhub_client.disaster_recovery_configs.break_pairing( GROUP_NAME, NAMESPACE_NAME, DISASTER_RECOVERY_CONFIG_NAME) print("Break pairing disaster recovery config.") # Fail over disaster recovery config eventhub_client.disaster_recovery_configs.fail_over( GROUP_NAME, NAMESPACE_NAME_2, DISASTER_RECOVERY_CONFIG_NAME) print("Fail over disaster recovery config.") # Delete disaster recovery config for _ in range(5): try: eventhub_client.disaster_recovery_configs.delete( GROUP_NAME, NAMESPACE_NAME, DISASTER_RECOVERY_CONFIG_NAME) except HttpResponseError: time.sleep(30) else: break print("Delete disaster recovery config.") # Delete Namespace eventhub_client.namespaces.begin_delete(GROUP_NAME, NAMESPACE_NAME).result() # Delete Second Namespace eventhub_client.namespaces.begin_delete(GROUP_NAME, NAMESPACE_NAME_2).result() # Delete resource group resource_client.resource_groups.begin_delete(GROUP_NAME).result()