def ListDisks( self, resource_group_name: Optional[str] = None ) -> Dict[str, 'AZComputeDisk']: """List disks in an Azure subscription / resource group. Args: resource_group_name (str): Optional. The resource group name to list disks from. If none specified, then all disks in the AZ subscription will be listed. Returns: Dict[str, AZComputeDisk]: Dictionary mapping disk names (str) to their respective AZComputeDisk object. """ disks = {} # type: Dict[str, AZComputeDisk] az_disk_client = self.compute_client.disks if not resource_group_name: responses = common.ExecuteRequest(az_disk_client, 'list') else: responses = common.ExecuteRequest( az_disk_client, 'list_by_resource_group', {'resource_group_name': resource_group_name}) for response in responses: for disk in response: disks[disk.name] = AZComputeDisk(self.az_account, disk.id, disk.name, disk.location, zones=disk.zones) return disks
def ListInstances( self, resource_group_name: Optional[str] = None ) -> Dict[str, 'AZComputeVirtualMachine']: """List instances in an Azure subscription / resource group. Args: resource_group_name (str): Optional. The resource group name to list instances from. If none specified, then all instances in the Azure subscription will be listed. Returns: Dict[str, AZComputeVirtualMachine]: Dictionary mapping instance names (str) to their respective AZComputeVirtualMachine object. """ instances = {} # type: Dict[str, AZComputeVirtualMachine] az_vm_client = self.compute_client.virtual_machines if not resource_group_name: responses = common.ExecuteRequest(az_vm_client, 'list_all') else: responses = common.ExecuteRequest( az_vm_client, 'list', {'resource_group_name': resource_group_name}) for response in responses: for instance in response: instances[instance.name] = AZComputeVirtualMachine( self.az_account, instance.id, instance.name, instance.location, zones=instance.zones) return instances
def _CreateNetworkInterfaceElements( self, name_prefix: str, region: Optional[str] = None) -> Tuple[Any, ...]: """Creates required elements for creating a network interface. Args: name_prefix (str): A name prefix to use for the network interface elements to create. region (str): Optional. The region in which to create the elements. Default uses default_region of the AZAccount object. Returns: Tuple[Any, Any, Any, Any]: A tuple containing a public IP address object, a virtual network object, a subnet object and a network security group object. Raises: ResourceCreationError: If the elements could not be created. """ if not region: region = self.az_account.default_region # IP address public_ip_name = '{0:s}-public-ip'.format(name_prefix) # Virtual Network vnet_name = '{0:s}-vnet'.format(name_prefix) # Subnet subnet_name = '{0:s}-subnet'.format(name_prefix) # Network security group nsg_name = '{0:s}-nsg'.format(name_prefix) client_to_creation_data = { self.network_client.public_ip_addresses: { 'resource_group_name': self.az_account.default_resource_group_name, 'public_ip_address_name': public_ip_name, 'parameters': { 'location': region, 'public_ip_allocation_method': 'Dynamic' } }, self.network_client.virtual_networks: { 'resource_group_name': self.az_account.default_resource_group_name, 'virtual_network_name': vnet_name, 'parameters': { 'location': region, 'address_space': { 'address_prefixes': ['10.0.0.0/16'] } } }, self.network_client.subnets: { 'resource_group_name': self.az_account.default_resource_group_name, 'virtual_network_name': vnet_name, 'subnet_name': subnet_name, 'subnet_parameters': { 'address_prefix': '10.0.0.0/24' } }, self.network_client.network_security_groups: { 'resource_group_name': self.az_account.default_resource_group_name, 'network_security_group_name': nsg_name, 'parameters': { 'location': region, # Allow SSH traffic 'security_rules': [{ 'name': 'Allow-SSH', 'direction': 'Inbound', 'protocol': 'TCP', 'source_address_prefix': '*', 'destination_address_prefix': '*', 'source_port_range': '*', 'destination_port_range': 22, 'access': 'Allow', 'priority': 300 }] } } } # type: Dict[str, Any] result = [] try: for client in client_to_creation_data: request = common.ExecuteRequest( client, 'begin_create_or_update', client_to_creation_data[client])[0] request.wait() result.append(request.result()) except azure_exceptions.AzureError as exception: raise errors.ResourceCreationError( 'Could not create network interface elements: {0!s}'.format( exception), __name__) from exception return tuple(result)
def _CreateNetworkInterfaceElements( self, vm_name: str, region: Optional[str] = None) -> Tuple[Any, ...]: """Creates required elements for creating a network interface. Args: vm_name (str): The name of the VM to create the network interface elements for. region (str): Optional. The region in which to create the elements. Default uses default_region of the AZAccount object. Returns: Tuple[Any, Any, Any]: A tuple containing a public IP address object, a virtual network object and a subnet object. Raises: RuntimeError: If the elements could not be created. """ if not region: region = self.default_region public_ip_name = '{0:s}-public-ip'.format(vm_name) vnet_name = '{0:s}-vnet'.format(vm_name) subnet_name = '{0:s}-subnet'.format(vm_name) client_to_creation_data = { self.network_client.public_ip_addresses: { 'resource_group_name': self.default_resource_group_name, 'public_ip_address_name': public_ip_name, 'parameters': { 'location': region, 'public_ip_allocation_method': 'Dynamic' } }, self.network_client.virtual_networks: { 'resource_group_name': self.default_resource_group_name, 'virtual_network_name': vnet_name, 'parameters': { 'location': region, 'address_space': {'address_prefixes': ['10.0.0.0/16']} } }, self.network_client.subnets: { 'resource_group_name': self.default_resource_group_name, 'virtual_network_name': vnet_name, 'subnet_name': subnet_name, 'subnet_parameters': { 'address_prefix': '10.0.0.0/24' } } } # type: Dict[str, Any] result = [] try: for client in client_to_creation_data: request = common.ExecuteRequest( client, 'create_or_update', client_to_creation_data[client])[0] request.wait() result.append(request.result()) except azure_exceptions.CloudError as exception: raise RuntimeError('Could not create network interface elements: ' '{0:s}'.format(str(exception))) return tuple(result)