Пример #1
0
    def show_attached(
        self, cloud_service_name, instance_name=None, at_lun=None
    ):
        """
            Show details of the data disks attached to the virtual
            machine. If a lun is specified show only details for the disk
            at the specified lun
        """
        if not instance_name:
            instance_name = cloud_service_name

        disks = []
        luns = [at_lun] if at_lun is not None else list(range(Defaults.max_vm_luns()))
        for lun in luns:
            try:
                disks.append(self.service.get_data_disk(
                    cloud_service_name, cloud_service_name, instance_name, lun
                ))
            except Exception as e:
                if at_lun is not None:
                    # only if a disk information is requested for a specific
                    # lun but does not exist, an exception is raised
                    raise AzureDataDiskShowError(
                        '%s: %s' % (type(e).__name__, format(e))
                    )
        return [self.__decorate_attached_disk(disk) for disk in disks]
Пример #2
0
 def __get_first_available_lun(self, cloud_service_name, instance_name):
     lun = 0
     while lun < Defaults.max_vm_luns():
         try:
             self.service.get_data_disk(
                 cloud_service_name, cloud_service_name, instance_name, lun
             )
         except AzureMissingResourceHttpError:
             return lun
         else:
             lun += 1
     raise AzureDataDiskNoAvailableLun(
         "All LUNs on this VM are occupied."
     )
Пример #3
0
    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)