def _build_target(self, iothub, policy, key_type: str = None, include_events=False) -> Dict[str, str]: # This is more or less a compatibility function which produces the # same result as _azure.get_iot_hub_connection_string() # In future iteration we will return a 'Target' object rather than dict # but that will be better served aligning with vNext pattern for Iot Hub target = {} target["cs"] = CONN_STR_TEMPLATE.format( iothub.properties.host_name, policy.key_name, policy.primary_key if key_type == "primary" else policy.secondary_key, ) target["entity"] = iothub.properties.host_name target["policy"] = policy.key_name target["primarykey"] = policy.primary_key target["secondarykey"] = policy.secondary_key target["subscription"] = self.sub_id target["resourcegroup"] = iothub.additional_properties.get( "resourcegroup") target["location"] = iothub.location target["sku_tier"] = iothub.sku.tier.value if isinstance( iothub.sku.tier, (Enum, EnumMeta)) else iothub.sku.tier if include_events: events = {} events["endpoint"] = trim_from_start( iothub.properties.event_hub_endpoints["events"].endpoint, "sb://").strip("/") events["partition_count"] = iothub.properties.event_hub_endpoints[ "events"].partition_count events["path"] = iothub.properties.event_hub_endpoints[ "events"].path events["partition_ids"] = iothub.properties.event_hub_endpoints[ "events"].partition_ids target["events"] = events target["cmd"] = self.cmd return target
def get_iot_hub_connection_string( cmd, hub_name, resource_group_name, policy_name="iothubowner", key_type="primary", include_events=False, login=None, ): """ Function used to build up dictionary of IoT Hub connection string parts Args: cmd (object): Knack cmd hub_name (str): IoT Hub name resource_group_name (str): name of Resource Group contianing IoT Hub policy_name (str): Security policy name for shared key; e.g. 'iothubowner'(default) key_type (str): Shared key; either 'primary'(default) or 'secondary' include_events (bool): Include key event hub properties Returns: (dict): of connection string elements. Raises: CLIError: on input validation failure. """ result = {} target_hub = None policy = None if login: try: decomposed = parse_iot_hub_connection_string(login) decomposed_lower = dict( (k.lower(), v) for k, v in decomposed.items()) except ValueError as e: raise CLIError(e) result = {} result["cs"] = login result["policy"] = decomposed_lower["sharedaccesskeyname"] result["primarykey"] = decomposed_lower["sharedaccesskey"] result["entity"] = decomposed_lower["hostname"] return result client = None if getattr(cmd, "cli_ctx", None): client = iot_hub_service_factory(cmd.cli_ctx) else: client = cmd def _find_iot_hub_from_list(hubs, hub_name): if hubs: return next( (hub for hub in hubs if hub_name.lower() == hub.name.lower()), None) return None if resource_group_name is None: hubs = client.list_by_subscription() if not hubs: raise CLIError("No IoT Hub found in current subscription.") target_hub = _find_iot_hub_from_list(hubs, hub_name) else: try: target_hub = client.get(resource_group_name, hub_name) except Exception: pass if target_hub is None: raise CLIError( "No IoT Hub found with name {} in current subscription.".format( hub_name)) try: addprops = getattr(target_hub, "additional_properties", None) resource_group_name = (addprops.get("resourcegroup") if addprops else getattr(target_hub, "resourcegroup", None)) policy = client.get_keys_for_key_name(resource_group_name, target_hub.name, policy_name) except Exception: pass if policy is None: raise CLIError("No keys found for policy {} of IoT Hub {}.".format( policy_name, hub_name)) result["cs"] = CONN_STR_TEMPLATE.format( target_hub.properties.host_name, policy.key_name, policy.primary_key if key_type == "primary" else policy.secondary_key, ) result["entity"] = target_hub.properties.host_name result["policy"] = policy_name result["primarykey"] = policy.primary_key result["secondarykey"] = policy.secondary_key result["subscription"] = client.config.subscription_id result["resourcegroup"] = resource_group_name result["location"] = target_hub.location result["sku_tier"] = target_hub.sku.tier.value if include_events: events = {} events["endpoint"] = trim_from_start( target_hub.properties.event_hub_endpoints["events"].endpoint, "sb://").strip("/") events["partition_count"] = target_hub.properties.event_hub_endpoints[ "events"].partition_count events["path"] = target_hub.properties.event_hub_endpoints[ "events"].path events["partition_ids"] = target_hub.properties.event_hub_endpoints[ "events"].partition_ids result["events"] = events return result
def get_iot_hub_connection_string( cmd, hub_name, resource_group_name, policy_name='iothubowner', key_type='primary', include_events=False, login=None): """ Function used to build up dictionary of IoT Hub connection string parts Args: cmd (object): Knack cmd hub_name (str): IoT Hub name resource_group_name (str): name of Resource Group contianing IoT Hub policy_name (str): Security policy name for shared key; e.g. 'iothubowner'(default) key_type (str): Shared key; either 'primary'(default) or 'secondary' include_events (bool): Include key event hub properties Returns: (dict): of connection string elements. Raises: CLIError: on input validation failure. """ result = {} target_hub = None policy = None if login: try: decomposed = parse_iot_hub_connection_string(login) except ValueError as e: raise CLIError(e) result = {} result['cs'] = login result['policy'] = decomposed['SharedAccessKeyName'] result['primarykey'] = decomposed['SharedAccessKey'] result['entity'] = decomposed['HostName'] return result client = None if getattr(cmd, 'cli_ctx', None): client = iot_hub_service_factory(cmd.cli_ctx) else: client = cmd def _find_iot_hub_from_list(hubs, hub_name): if hubs: return next((hub for hub in hubs if hub_name.lower() == hub.name.lower()), None) return None if resource_group_name is None: hubs = client.list_by_subscription() if not hubs: raise CLIError('No IoT Hub found in current subscription.') target_hub = _find_iot_hub_from_list(hubs, hub_name) else: try: target_hub = client.get(resource_group_name, hub_name) except Exception: pass if target_hub is None: raise CLIError( 'No IoT Hub found with name {} in current subscription.'.format(hub_name)) try: addprops = getattr(target_hub, 'additional_properties', None) resource_group_name = addprops.get('resourcegroup') if addprops else getattr( target_hub, 'resourcegroup', None) policy = client.get_keys_for_key_name(resource_group_name, target_hub.name, policy_name) except Exception: pass if policy is None: raise CLIError( 'No keys found for policy {} of IoT Hub {}.'.format(policy_name, hub_name) ) result['cs'] = CONN_STR_TEMPLATE.format( target_hub.properties.host_name, policy.key_name, policy.primary_key if key_type == 'primary' else policy.secondary_key) result['entity'] = target_hub.properties.host_name result['policy'] = policy_name result['primarykey'] = policy.primary_key result['secondarykey'] = policy.secondary_key result['subscription'] = client.config.subscription_id result['resourcegroup'] = resource_group_name if include_events: events = {} events['endpoint'] = trim_from_start(target_hub.properties.event_hub_endpoints['events'].endpoint, 'sb://').strip('/') events['partition_count'] = target_hub.properties.event_hub_endpoints['events'].partition_count events['path'] = target_hub.properties.event_hub_endpoints['events'].path events['partition_ids'] = target_hub.properties.event_hub_endpoints['events'].partition_ids result['events'] = events return result