def create(self, cloud_service_name, location, description='CloudService', label=None): """ create cloud service with specified name and return the request id """ service_record = { 'service_name': cloud_service_name, 'label': cloud_service_name, 'description': description, 'location': location } if label: service_record['label'] = label if self.__cloud_service_exists(cloud_service_name): # indicate an existing cloud service by returning request id: 0 return 0 if self.__cloud_service_url_in_use(cloud_service_name): message = ('The cloud service name "%s" ' 'is already in use. ' 'Please choose a different name.') raise AzureCloudServiceAddressError(message % cloud_service_name) try: result = self.service.create_hosted_service(**service_record) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureCloudServiceCreateError('%s: %s' % (type(e).__name__, format(e)))
def update( self, name, description, label, account_type, regenerate_primary_key=False, regenerate_secondary_key=False ): try: current = self.service.get_storage_account_properties(name) result = self.service.update_storage_account( name, (description or current.storage_service_properties.description), (label or current.storage_service_properties.label), account_type=( account_type or current.storage_service_properties.account_type ) ) if regenerate_primary_key: self.service.regenerate_storage_account_keys(name, 'Primary') if regenerate_secondary_key: self.service.regenerate_storage_account_keys(name, 'Secondary') except Exception as e: raise AzureStorageAccountUpdateError( '%s: %s' % (type(e).__name__, format(e)) ) return Defaults.unify_id(result.request_id)
def delete(self, name): try: role = self.__get_role() config = self.__get_network_config_for_role(role) if not config.input_endpoints: raise AzureEndpointDeleteError("No endpoints found.") for index, endpoint in \ enumerate(config.input_endpoints.input_endpoints): if endpoint.name.upper() == name.upper(): del config.input_endpoints.input_endpoints[index] break else: # If for loop finishes normally no endpoint # exists that matches the name. raise AzureEndpointDeleteError( "No endpoint named %s was found." % name) result = self.service.update_role( self.cloud_service_name, self.cloud_service_name, role.role_name, os_virtual_hard_disk=role.os_virtual_hard_disk, network_config=config, availability_set_name=role.availability_set_name, data_virtual_hard_disks=role.data_virtual_hard_disks) except AzureEndpointDeleteError: raise # re-raise except Exception as e: raise AzureEndpointDeleteError('%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def create(self, name, external_port, internal_port, protocol, idle_timeout): try: role = self.__get_role() config = self.__get_network_config_for_role(role) new_endpoint = ConfigurationSetInputEndpoint( name=name, protocol=protocol, port=external_port, local_port=internal_port, idle_timeout_in_minutes=idle_timeout, enable_direct_server_return=False, ) # If there are no endpoints the input_endpoints # attribute is None. Thus create and set new instance. if not config.input_endpoints: config.input_endpoints = ConfigurationSetInputEndpoints() config.input_endpoints.input_endpoints.append(new_endpoint) result = self.service.update_role( self.cloud_service_name, self.cloud_service_name, role.role_name, os_virtual_hard_disk=role.os_virtual_hard_disk, network_config=config, availability_set_name=role.availability_set_name, data_virtual_hard_disks=role.data_virtual_hard_disks) except Exception as e: raise AzureEndpointCreateError('%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def setup(self): account = AzureAccount( Config(region_name='East US 2', filename='../data/config')) self.service = mock.Mock() account.get_management_service = mock.Mock(return_value=self.service) account.get_blob_service_host_base = mock.Mock(return_value='test.url') account.storage_key = mock.Mock() # now that that's done, instantiate a DataDisk with the account self.data_disk = DataDisk(account) # asynchronous API operations return a request object self.my_request = mock.Mock(request_id=Defaults.unify_id(42)) # variables used in multiple tests self.cloud_service_name = 'mockcloudservice' self.instance_name = 'mockcloudserviceinstance1' self.lun = 0 self.host_caching = 'ReadWrite' self.disk_filename = 'mockcloudserviceinstance1-data-disk-0.vhd' self.disk_name = 'mockcloudserviceinstance1-data-disk-0' self.disk_url = ('https://' + account.storage_name() + '.blob.' + account.get_blob_service_host_base() + '/' + account.storage_container() + '/' + self.disk_filename) self.disk_label = 'Mock data disk' self.disk_size = 42 self.timestamp = datetime.utcnow() self.time_string = datetime.isoformat(self.timestamp).replace(':', '_') self.account = account
def delete(self, name): try: result = self.service.delete_reserved_ip_address(name) except Exception as e: raise AzureReservedIpDeleteError('%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def create(self, name, blob_name, label=None, container_name=None): if not container_name: container_name = self.account.storage_container() if not label: label = name try: storage = BaseBlobService( self.account.storage_name(), self.account.storage_key(), endpoint_suffix=self.account.get_blob_service_host_base() ) storage.get_blob_properties( container_name, blob_name ) except Exception as e: raise AzureBlobServicePropertyError( '%s not found in container %s' % (blob_name, container_name) ) try: media_link = storage.make_blob_url(container_name, blob_name) result = self.service.add_os_image( label, media_link, name, 'Linux' ) return Defaults.unify_id(result.request_id) except Exception as e: raise AzureOsImageCreateError( '%s: %s' % (type(e).__name__, format(e)) )
def add_certificate(self, cloud_service_name, ssh_private_key_file): """ create Azure conform certificate from given ssh private key and add the CER formatted public pem file to the cloud service. The method returns the certificate fingerprint """ pem_cert = self.get_pem_certificate(ssh_private_key_file) pem_cert_file = NamedTemporaryFile() pem_cert_file.write(pem_cert) pem_cert_file.flush() openssl = subprocess.Popen([ 'openssl', 'pkcs12', '-export', '-inkey', ssh_private_key_file, '-in', pem_cert_file.name, '-passout', 'pass:'******'%s' % openssl_error) try: add_cert_request = self.service.add_service_certificate( cloud_service_name, base64.b64encode(pfx_cert).decode(), 'pfx', '') except Exception as e: raise AzureCloudServiceAddCertificateError( '%s: %s' % (type(e).__name__, format(e))) # Wait for the certficate to be created request_result = RequestResult( Defaults.unify_id(add_cert_request.request_id)) request_result.wait_for_request_completion(self.service) return self.get_fingerprint(pem_cert)
def setup(self): # construct an account account = AzureAccount( Config(region_name='East US 2', filename='../data/config')) self.service = mock.Mock() account.get_management_service = mock.Mock(return_value=self.service) account.get_blob_service_host_base = mock.Mock(return_value='test.url') account.storage_key = mock.Mock() # now that that's done, instantiate an Endpoint with the account self.endpoint = Endpoint(account) # asynchronous API operations return a request object self.my_request = mock.Mock(request_id=Defaults.unify_id(42)) # variables used in multiple tests self.cloud_service_name = 'mockcloudservice' self.instance_name = 'mockcloudserviceinstance1' self.endpoint_name = 'HTTPS' self.port = '443' self.idle_timeout = 10 self.protocol = 'tcp' self.udp_endpoint_name = 'SNMP' self.udp_port = '131' self.instance_port = '10000' self.udp_protocol = 'udp' # identify the instance for the Endpoint to work on self.endpoint.set_instance(self.cloud_service_name, self.instance_name) # mock out the get_role function of service self.service.get_role = mock.Mock(return_value=self.mock_role())
def delete(self, name): try: result = self.service.delete_storage_account(name) except Exception as e: raise AzureStorageAccountDeleteError( '%s: %s' % (type(e).__name__, format(e)) ) return Defaults.unify_id(result.request_id)
def unreplicate(self, name): try: result = self.service.unreplicate_vm_image(name) return Defaults.unify_id(result.request_id) except Exception as e: raise AzureOsImageUnReplicateError( '%s: %s' % (type(e).__name__, format(e)) )
def publish(self, name, permission): try: result = self.service.share_vm_image(name, permission) return Defaults.unify_id(result.request_id) except Exception as e: raise AzureOsImagePublishError( '%s: %s' % (type(e).__name__, format(e)) )
def create(self, name, region): try: result = self.service.create_reserved_ip_address(name, location=region) except Exception as e: raise AzureReservedIpCreateError('%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def delete(self, name, delete_disk=False): try: result = self.service.delete_os_image( name, delete_disk ) return Defaults.unify_id(result.request_id) except Exception as e: raise AzureOsImageDeleteError( '%s: %s' % (type(e).__name__, format(e)) )
def start_instance(self, cloud_service_name, instance_name): """ Start the specified virtual disk image instance. """ try: result = self.service.start_role(cloud_service_name, cloud_service_name, instance_name) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureVmStartError('%s: %s' % (type(e).__name__, format(e)))
def disassociate(self, name, cloud_service_name): try: result = self.service.disassociate_reserved_ip_address( name=name, service_name=cloud_service_name, deployment_name=cloud_service_name) except Exception as e: raise AzureReservedIpDisAssociateError( '%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def delete_instance(self, cloud_service_name, instance_name): """ delete a virtual disk image instance """ try: result = self.service.delete_role(cloud_service_name, cloud_service_name, instance_name, True) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureVmDeleteError('%s: %s' % (type(e).__name__, format(e)))
def reboot_instance(self, cloud_service_name, instance_name): """ Requests reboot of a virtual disk image instance """ try: result = self.service.reboot_role_instance(cloud_service_name, cloud_service_name, instance_name) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureVmRebootError('%s: %s' % (type(e).__name__, format(e)))
def delete(self, cloud_service_name, complete=False): """ delete specified cloud service, if complete is set to true all OS/data disks and the source blobs for the disks will be deleted too """ try: result = self.service.delete_hosted_service( cloud_service_name, complete) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureCloudServiceDeleteError('%s: %s' % (type(e).__name__, format(e)))
def create(self, name, description, label, account_type): try: result = self.service.create_storage_account( name, (description or ''), (label or name), location=self.account.config.get_region_name(), account_type=account_type ) except Exception as e: raise AzureStorageAccountCreateError( '%s: %s' % (type(e).__name__, format(e)) ) return Defaults.unify_id(result.request_id)
def update(self, name, external_port=None, internal_port=None, protocol=None, idle_timeout=None): try: role = self.__get_role() config = self.__get_network_config_for_role(role) if not config.input_endpoints: raise AzureEndpointUpdateError("No endpoints found.") for index, endpoint in \ enumerate(config.input_endpoints.input_endpoints): if endpoint.name.upper() == name.upper(): endpoint_index = index break else: # If for loop finishes normally no endpoint # exists that matches the name. raise AzureEndpointUpdateError( "No endpoint named %s was found." % name) endpoint = config.input_endpoints.input_endpoints[endpoint_index] if external_port: endpoint.port = external_port if internal_port: endpoint.local_port = internal_port if protocol: endpoint.protocol = protocol if idle_timeout: endpoint.idle_timeout_in_minutes = idle_timeout result = self.service.update_role( self.cloud_service_name, self.cloud_service_name, role.role_name, os_virtual_hard_disk=role.os_virtual_hard_disk, network_config=config, availability_set_name=role.availability_set_name, data_virtual_hard_disks=role.data_virtual_hard_disks) except Exception as e: raise AzureEndpointUpdateError('%s: %s' % (type(e).__name__, format(e))) return Defaults.unify_id(result.request_id)
def detach(self, lun, cloud_service_name, instance_name=None): """ Delete data disk from the instance, retaining underlying vhd blob """ if not instance_name: instance_name = cloud_service_name try: result = self.service.delete_data_disk( cloud_service_name, cloud_service_name, instance_name, lun, delete_vhd=False ) except Exception as e: raise AzureDataDiskDeleteError( '%s: %s' % (type(e).__name__, format(e)) ) return Defaults.unify_id(result.request_id)
def replicate(self, name, regions, offer, sku, version): ''' Note: The regions are not additive. If a VM Image has already been replicated to Regions A, B, and C, and a request is made to replicate to Regions A and D, the VM Image will remain in Region A, will be replicated in Region D, and will be unreplicated from Regions B and C ''' if 'all' in regions: regions = [] for location in self.service.list_locations(): regions.append(location.name) try: result = self.service.replicate_vm_image( name, regions, offer, sku, version ) return Defaults.unify_id(result.request_id) except Exception as e: raise AzureOsImageReplicateError( '%s: %s' % (type(e).__name__, format(e)) )
def setup(self): account = AzureAccount( Config(region_name='East US 2', filename='../data/config')) self.service = mock.Mock() account.get_management_service = mock.Mock(return_value=self.service) account.get_blob_service_host_base = mock.Mock( return_value='.blob.test.url') account.storage_key = mock.Mock() MyStruct = namedtuple( 'MyStruct', 'name address label state in_use service_name deployment_name \ location') self.list_ips = [ MyStruct(name='some-name', address='1.2.3.4', label='', state='Created', in_use=True, service_name='foo', deployment_name='bar', location='Region') ] self.result_list = [{ 'name': 'some-name', 'address': '1.2.3.4', 'state': 'Created', 'in_use': True, 'cloud_service_name': 'foo', 'instance_name': 'bar', 'region': 'Region' }] MyResult = namedtuple('MyResult', 'request_id') self.myrequest = MyResult(request_id=Defaults.unify_id(42)) self.reserved_ip = ReservedIp(account)
def shutdown_instance(self, cloud_service_name, instance_name, deallocate_resources=False): """ Shuts down the specified virtual disk image instance If deallocate_resources is set to true the machine shuts down and releases the compute resources. You are not billed for the compute resources that this Virtual Machine uses in this case. If a static Virtual Network IP address is assigned to the Virtual Machine, it is reserved. """ post_shutdown_action = 'Stopped' if deallocate_resources: post_shutdown_action = 'StoppedDeallocated' try: result = self.service.shutdown_role(cloud_service_name, cloud_service_name, instance_name, post_shutdown_action) return (Defaults.unify_id(result.request_id)) except Exception as e: raise AzureVmShutdownError('%s: %s' % (type(e).__name__, format(e)))
def setup(self): account = AzureAccount( Config( region_name='East US 2', filename='../data/config' ) ) self.service = mock.Mock() account.get_management_service = mock.Mock(return_value=self.service) account.get_blob_service_host_base = mock.Mock( return_value='.blob.test.url' ) account.storage_key = mock.Mock() self.containers_list = ['container_a', 'container_b'] self.mock_storage_service = mock.Mock( storage_service_keys=None, storage_service_properties=mock.Mock( status='Created', account_type='Standard_GRS', description='mock storage service', geo_secondary_region='West US', creation_time='2015-12-09T07:53:54Z', geo_primary_region='East US 2', label='mockstorageservice', status_of_primary='Available', location='East US 2', affinity_group='', last_geo_failover_time='', status_of_secondary='Available', endpoints=[ 'https://mockstorageservice.blob.core.windows.net/', 'https://mockstorageservice.queue.core.windows.net/', 'https://mockstorageservice.table.core.windows.net/', 'https://mockstorageservice.file.core.windows.net/' ], geo_replication_enabled='True' ), url=( 'https://management.core.windows.net/' + '00000000-0000-0000-0000-000000000000/services/' + 'storageservices/mockstorageservice' ), service_name='mockstorageservice', capabilities='None', extended_properties={ 'ResourceGroup': 'Default-Storage-EastUS2', 'ResourceLocation': 'East US 2' }, containers=None ) self.keyed_service = mock.Mock( storage_service_keys=mock.Mock( primary='1234567890abcdef==', secondary='fedcba0987654321==' ) ) self.expected_list_result = [{ "backup": { "backup-region": "West US", "backup-region-status": "Available", "last-failover": "", "status": "Available" }, "backup-strategy": "--geo-redundant", "description": "mock storage service", "endpoints": [ "https://mockstorageservice.blob.core.windows.net/", "https://mockstorageservice.queue.core.windows.net/", "https://mockstorageservice.table.core.windows.net/", "https://mockstorageservice.file.core.windows.net/" ], "label": "mockstorageservice", "name": "mockstorageservice", "region": "East US 2", "status": "Created" }] self.expected_show_result = { "backup": { "backup-region": "West US", "backup-region-status": "Available", "last-failover": "", "status": "Available" }, "backup-strategy": "--geo-redundant", "containers": self.containers_list, "description": "mock storage service", "endpoints": [ "https://mockstorageservice.blob.core.windows.net/", "https://mockstorageservice.queue.core.windows.net/", "https://mockstorageservice.table.core.windows.net/", "https://mockstorageservice.file.core.windows.net/" ], "keys": { "primary": self.keyed_service.storage_service_keys.primary, "secondary": self.keyed_service.storage_service_keys.secondary }, "label": "mockstorageservice", "name": "mockstorageservice", "region": "East US 2", "status": "Created" } self.my_request = mock.Mock(request_id=Defaults.unify_id(42)) self.storage_account = StorageAccount(account)
def attach( self, disk_name, cloud_service_name, instance_name=None, label=None, lun=None, host_caching=None, blob_name=None ): """ Attach existing data disk to the instance """ if not instance_name: instance_name = cloud_service_name if lun not in list(range(Defaults.max_vm_luns())): lun = self.__get_first_available_lun( cloud_service_name, instance_name ) if disk_name and not blob_name: # assume existing data-disk args = { 'disk_name': disk_name } elif disk_name and blob_name: # create new data-disk based using disk_name args = { 'disk_name': disk_name, 'source_media_link': self.__data_disk_url(blob_name) } elif not disk_name and blob_name: # find data-disk name for blob_name, # or create a new data-disk for blob_name disk_name = self.__find_existing_disk_name_for_blob_name( blob_name, self.service.list_disks() ) if disk_name: args = { 'disk_name': disk_name } else: args = { 'disk_name': self.__strip_platform_extension(blob_name), 'source_media_link': self.__data_disk_url(blob_name) } else: raise AzureDataDiskAttachError( "Neither disk_name nor blob_name was supplied" ) if host_caching: args['host_caching'] = host_caching if label: args['disk_label'] = label try: result = self.service.add_data_disk( cloud_service_name, cloud_service_name, instance_name, lun, **args ) self.attached_lun = lun except Exception as e: raise AzureDataDiskAttachError( '%s: %s' % (type(e).__name__, format(e)) ) return Defaults.unify_id(result.request_id)
def setup(self): MyResult = namedtuple('MyResult', 'request_id') self.myrequest = MyResult(request_id=Defaults.unify_id(42)) self.fake_image_name = 'some-name' MyStruct = namedtuple( 'MyStruct', 'affinity_group category description eula icon_uri image_family \ is_premium label language location logical_size_in_gb media_link \ name os os_state pricing_detail_link privacy_uri \ published_date publisher_name recommended_vm_size show_in_gui \ small_icon_uri') self.list_os_images = [ MyStruct(affinity_group='group', category='cloud', description='nice', eula='eula', icon_uri='url', image_family='disks', is_premium=False, label='bob', language='English', location='West US', logical_size_in_gb=30, media_link='url', name=self.fake_image_name, os='linux', os_state='brilliant', pricing_detail_link='url', privacy_uri='url', published_date='date', publisher_name='suse', recommended_vm_size=10, show_in_gui=True, small_icon_uri='url') ] self.os_image = mock.Mock() self.os_image.eula = 'eula' self.os_image.description = 'description' self.os_image.language = 'en_US' self.os_image.image_family = 'family' self.os_image.icon_uri = 'OpenSuse12_100.png' self.os_image.label = 'label' self.os_image.small_icon_uri = 'OpenSuse12_45.png' self.os_image.published_date = '2016-01-20' self.os_image.privacy_uri = 'http://privacy.uri' self.os_image.show_in_gui = 'true' self.os_image.recommended_vm_size = 'large' self.os_image_updated = mock.Mock() self.os_image_updated.eula = 'eula' self.os_image_updated.description = 'description' self.os_image_updated.language = 'en_US' self.os_image_updated.image_family = 'family' self.os_image_updated.icon_uri = 'OpenSuse12_100.png' self.os_image_updated.label = 'label' self.os_image_updated.small_icon_uri = 'OpenSuse12_45.png' self.os_image_updated.published_date = '2016-01-20T00:00:00Z' self.os_image_updated.privacy_uri = 'http://privacy.uri/' self.os_image_updated.show_in_gui = True self.os_image_updated.recommended_vm_size = 'large' account = AzureAccount( Config(region_name='East US 2', filename='../data/config')) self.service = mock.Mock() account.get_management_service = mock.Mock(return_value=self.service) account.get_blob_service_host_base = mock.Mock(return_value='test.url') account.storage_key = mock.Mock() self.image = Image(account)
def setup(self): MyResult = namedtuple('MyResult', 'request_id') self.myrequest = MyResult(request_id=Defaults.unify_id(42)) MyStruct = namedtuple( 'MyStruct', 'name label os category description location \ affinity_group media_link') self.list_os_images = [ MyStruct(name='some-name', label='bob', os='linux', category='cloud', description='nice', location='here', affinity_group='ok', media_link='url') ] MyPublicIP = namedtuple('MyPublicIP', 'name address') self.public_ips = [MyPublicIP(name='name', address='address')] MyInstanceEndPoint = namedtuple( 'MyInstanceEndPoint', 'name vip public_port local_port protocol') self.instance_endpoints = [ MyInstanceEndPoint(name='name', vip='vip', public_port='public_port', local_port='local_port', protocol='protocol') ] MyVirtualIP = namedtuple('MyVirtualIP', 'address reserved_ip_name type') self.virtual_ips = [ MyVirtualIP(address='address', reserved_ip_name='reserved_ip_name', type='type') ] MyInputEndpoint = namedtuple('MyInputEndpoint', 'role_name vip port') self.input_endpoints = [ MyInputEndpoint(role_name='role_name', vip='vip', port='port') ] MyInstance = namedtuple( 'MyInstance', 'role_name instance_name instance_status instance_size \ instance_state_details ip_address power_state fqdn host_name \ public_ips instance_endpoints') self.instances = [ MyInstance(role_name='role_name', instance_name='instance_name', instance_status='instance_status', instance_size='instance_size', instance_state_details='instance_state_details', ip_address='ip_address', power_state='power_state', fqdn='fqdn', host_name='host_name', public_ips=self.public_ips, instance_endpoints=self.instance_endpoints) ] MyServiceProperties = namedtuple( 'MyServiceProperties', 'description location affinity_group label status \ date_created date_last_modified') self.hosted_service_properties = MyServiceProperties( description='description', location='location', affinity_group='affinity_group', label='label', status='status', date_created='date_created', date_last_modified='date_last_modified') MyDeployment = namedtuple( 'MyDeployment', 'name deployment_slot private_id status label url created_time \ last_modified_time virtual_network_name virtual_ips \ input_endpoint_list role_instance_list') self.deployments = [ MyDeployment(name='name', deployment_slot='deployment_slot', private_id='private_id', status='status', label='label', url='url', created_time='created_time', last_modified_time='last_modified_time', virtual_network_name='virtual_network_name', virtual_ips=self.virtual_ips, input_endpoint_list=self.input_endpoints, role_instance_list=self.instances) ] MyProperties = namedtuple( 'MyProperties', 'service_name hosted_service_properties deployments') self.properties = MyProperties( service_name='service_name', hosted_service_properties=self.hosted_service_properties, deployments=self.deployments) account = AzureAccount( Config(region_name='East US 2', filename='../data/config')) self.mgmt_service = mock.Mock() account.get_management_service = mock.Mock( return_value=self.mgmt_service) account.storage_key = mock.Mock() self.cloud_service = CloudService(account)