Ejemplo n.º 1
0
    def test_060_attach_disk_to_vm_in_vapp(self):
        logged_in_org = self.client.get_org()
        org = Org(self.client, resource=logged_in_org)
        vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
        vdc = VDC(self.client, resource=vdc_resource)
        assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')

        vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
        assert self.config['vcd']['vapp'] == vapp_resource.get('name')
        vapp = VApp(self.client, resource=vapp_resource)

        disk = vdc.get_disk(self.config['vcd']['idisk_name'])

        result = vapp.attach_disk_to_vm(disk_href=disk.get('href'),
                                        vm_name=self.config['vcd']['vm'])
        task = self.client.get_task_monitor().wait_for_status(
                            task=result,
                            timeout=60,
                            poll_frequency=2,
                            fail_on_statuses=None,
                            expected_target_statuses=[
                                TaskStatus.SUCCESS,
                                TaskStatus.ABORTED,
                                TaskStatus.ERROR,
                                TaskStatus.CANCELED],
                            callback=None)
        assert task.get('status') == TaskStatus.SUCCESS.value
Ejemplo n.º 2
0
def attach_disk(ctx, vm_name, disk_name, block=False):
    try:
        vm = find_vm_in_vapp(ctx, vm_name=vm_name)
        if len(vm) > 0:
            vm = vm[0]
            vapp = ctx.vdc.get_vapp(vm['vapp_name'])
            the_vapp = VApp(ctx.client, vm['vapp_name'], resource=vapp)
            disks = get_disks(ctx)
            for disk in disks:
                if disk['name'] == disk_name:
                    result = the_vapp.attach_disk_to_vm(disk['href'], vm['vm_name'])
                    if block == True:
                        task = ctx.client.get_task_monitor().wait_for_status(
                            task=result,
                            timeout=60,
                            poll_frequency=2,
                            fail_on_statuses=None,
                            expected_target_statuses=[
                                TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
                                TaskStatus.CANCELED
                            ],
                            callback=None)
                        assert task.get('status') == TaskStatus.SUCCESS.value
                        return True
                    else:
                        return result
    except Exception as e:
        if ctx.config['debug'] == True:
            raise
        else:
            pass
    return False
Ejemplo n.º 3
0
    def test_060_attach_disk_to_vm_in_vapp(self):
        logged_in_org = self.client.get_org()
        org = Org(self.client, resource=logged_in_org)
        vdc_resource = org.get_vdc(self.config['vcd']['vdc'])
        vdc = VDC(self.client, resource=vdc_resource)
        assert self.config['vcd']['vdc'] == vdc.get_resource().get('name')

        vapp_resource = vdc.get_vapp(self.config['vcd']['vapp'])
        assert self.config['vcd']['vapp'] == vapp_resource.get('name')
        vapp = VApp(self.client, resource=vapp_resource)

        disk = vdc.get_disk(self.config['vcd']['idisk_name'])

        result = vapp.attach_disk_to_vm(disk_href=disk.get('href'),
                                        vm_name=self.config['vcd']['vm'])
        task = self.client.get_task_monitor().wait_for_status(
            task=result,
            timeout=60,
            poll_frequency=2,
            fail_on_status=None,
            expected_target_statuses=[
                TaskStatus.SUCCESS, TaskStatus.ABORTED, TaskStatus.ERROR,
                TaskStatus.CANCELED
            ],
            callback=None)
        assert task.get('status') == TaskStatus.SUCCESS.value
Ejemplo n.º 4
0
def attach(ctx, vapp_name, vm_name, disk_name, disk_id):
    try:
        client = ctx.obj['client']
        vdc_href = ctx.obj['profiles'].get('vdc_href')
        vdc = VDC(client, href=vdc_href)
        disk = vdc.get_disk(disk_name, disk_id)
        vapp_resource = vdc.get_vapp(vapp_name)
        vapp = VApp(client, resource=vapp_resource)
        task = vapp.attach_disk_to_vm(disk_href=disk.get('href'),
                                      vm_name=vm_name)
        stdout(task, ctx)
    except Exception as e:
        stderr(e, ctx)
Ejemplo n.º 5
0
def attach(ctx, vapp_name, vm_name, disk_name):
    try:
        restore_session(ctx, vdc_required=True)
        client = ctx.obj['client']
        vdc_href = ctx.obj['profiles'].get('vdc_href')
        vdc = VDC(client, href=vdc_href)

        disk_name, disk_id = extract_name_and_id(disk_name)
        disk = vdc.get_disk(name=disk_name, disk_id=disk_id)

        vapp_resource = vdc.get_vapp(vapp_name)
        vapp = VApp(client, resource=vapp_resource)

        task = vapp.attach_disk_to_vm(disk_href=disk.get('href'),
                                      vm_name=vm_name)
        stdout(task, ctx)
    except Exception as e:
        stderr(e, ctx)
Ejemplo n.º 6
0
def attach(ctx, vapp_name, vm_name, disk_name):
    try:
        restore_session(ctx, vdc_required=True)
        client = ctx.obj['client']
        vdc_href = ctx.obj['profiles'].get('vdc_href')
        vdc = VDC(client, href=vdc_href)

        disk_name, disk_id = extract_name_and_id(disk_name)
        disk = vdc.get_disk(name=disk_name, disk_id=disk_id)

        vapp_resource = vdc.get_vapp(vapp_name)
        vapp = VApp(client, resource=vapp_resource)

        task = vapp.attach_disk_to_vm(
            disk_href=disk.get('href'), vm_name=vm_name)
        stdout(task, ctx)
    except Exception as e:
        stderr(e, ctx)
Ejemplo n.º 7
0
    def test_0050_attach_disk_to_vm_in_vapp(self):
        """Test the  method vapp.attach_disk_to_vm().

        Invoke the method for the second independent disk, and attach it to the
        first vm in the vApp created during setup. The vApp must be in deployed
        state before we try to attach the disk to it.

        This test passes if the disk attachment task succeeds.
        """
        vdc = Environment.get_test_vdc(TestDisk._client)
        vapp = VApp(TestDisk._client, href=TestDisk._test_vapp_href)
        vm_name = TestDisk._test_vapp_first_vm_name
        disk = vdc.get_disk(disk_id=TestDisk._idisk2_id)

        # vApp needs to be deployed for attach to succeed.
        if vapp.is_suspended():
            task = vapp.deploy()
            TestDisk._client.get_task_monitor().wait_for_success(task=task)

        task = vapp.attach_disk_to_vm(disk_href=disk.get('href'),
                                      vm_name=vm_name)
        TestDisk._client.get_task_monitor().wait_for_success(task=task)
Ejemplo n.º 8
0
class VappVMDisk(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(VappVMDisk, self).__init__(**kwargs)
        vapp_resource = self.get_resource()
        self.vapp = VApp(self.client, resource=vapp_resource)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.add_disk()

        if state == "update":
            return self.update_disk()

        if state == "absent":
            return self.delete_disk()

        if state == "attached":
            return self.attach_disk()

        if state == "detached":
            return self.detach_disk()

    def manage_operations(self):
        operation = self.params.get('operation')

        if operation == "read":
            return self.read_disks()

    def get_resource(self):
        vapp = self.params.get('vapp')
        vdc = self.params.get('vdc')
        org_resource = Org(self.client, resource=self.client.get_org())
        vdc_resource = VDC(self.client, resource=org_resource.get_vdc(vdc))
        vapp_resource_href = vdc_resource.get_resource_href(
            name=vapp, entity_type=EntityType.VAPP)
        vapp_resource = self.client.get_resource(vapp_resource_href)

        return vapp_resource

    def get_vm(self):
        vapp_vm_resource = self.vapp.get_vm(self.params.get('vm_name'))

        return VM(self.client, resource=vapp_vm_resource)

    def add_disk(self):
        size = self.params.get('size')
        vm_name = self.params.get('vm_name')
        response = dict()
        response['changed'] = False

        disks = self.read_disks().get('disks')
        last_disk = list(disks.keys())[-1]
        last_disk_instance_id = int(disks[last_disk]['InstanceID'])
        new_disk_name = str(int(last_disk.split(' ')[2]) + 1)
        new_disk_instance_id = last_disk_instance_id + 1
        add_disk_task = self.vapp.add_disk_to_vm(vm_name, size)
        self.execute_task(add_disk_task)
        msg = 'Vapp VM disk of size {0} has been added.'
        response['msg'] = msg.format(str(size))
        response['metadata'] = {
            'new_disk_name': 'Hard disk {0}'.format(new_disk_name),
            'new_disk_size': size,
            'new_disk_instance_id': new_disk_instance_id
        }
        response['changed'] = True

        return response

    def attach_disk(self):
        vm = self.get_vm()
        vm_name = vm.resource.get('name')
        disk_name = self.params.get('disk_name')
        response = dict()
        response['changed'] = False

        vdc = self.params.get('vdc')
        org_resource = Org(self.client, resource=self.client.get_org())
        vdc_resource = VDC(self.client, resource=org_resource.get_vdc(vdc))
        disk = vdc_resource.get_disk(name=disk_name)

        attach_disk_task = self.vapp.attach_disk_to_vm(disk_href=disk.get('href'), vm_name=vm_name)
        self.execute_task(attach_disk_task)

        msg = 'Independent disk {0} has been attached.'
        response['msg'] = msg.format(disk_name)

        response['changed'] = True

        return response

    def detach_disk(self):
        vm = self.get_vm()
        vm_name = vm.resource.get('name')
        disk_name = self.params.get('disk_name')
        response = dict()
        response['changed'] = False

        vdc = self.params.get('vdc')
        org_resource = Org(self.client, resource=self.client.get_org())
        vdc_resource = VDC(self.client, resource=org_resource.get_vdc(vdc))
        disk = vdc_resource.get_disk(name=disk_name)

        detach_disk_task = self.vapp.detach_disk_from_vm(disk_href=disk.get('href'), vm_name=vm_name)
        self.execute_task(detach_disk_task)

        msg = 'Independent disk {0} has been detached.'
        response['msg'] = msg.format(disk_name)

        response['changed'] = True

        return response

    def read_disks(self):
        vm = self.get_vm()
        response = dict()
        response['changed'] = False
        response['disks'] = dict()
        disks = self.client.get_resource(vm.resource.get('href') + '/virtualHardwareSection/disks')
        for disk in disks.Item:
            if disk['{' + NSMAP['rasd'] + '}Description'] == "Hard disk":
                disk_name = str(disk['{' + NSMAP['rasd'] + '}ElementName'])
                disk_instance = int(disk['{' + NSMAP['rasd'] + '}InstanceID'])
                disk_size = int(disk['{' + NSMAP['rasd'] + '}VirtualQuantity'])
                disk_hostresource = disk['{' + NSMAP['rasd'] + '}HostResource']
                disk_capacity = int(disk_hostresource.get('{' + NSMAP['vcloud'] + '}capacity'))
                response['disks'][disk_name] = {
                    'InstanceID': disk_instance,
                    'VirtualQuantity': self.convert_bytes_to_gb(disk_size),
                    'HostResource': str(round(disk_capacity / 1024, 1)) + ' GB'
                }

        return response

    def convert_bytes_to_gb(self, disk_size):
        log_value = int(math.floor(math.log(disk_size, 1024)))
        pow_value = math.pow(1024, log_value)

        return str(round(disk_size / pow_value, 1)) + ' GB'

    def update_disk(self):
        vm = self.get_vm()
        disk_name = self.params.get('disk_name')
        size = self.params.get('size')
        response = dict()
        response['changed'] = False
        index = -1

        if not size:
            err = '''Hard disk size argument is missing. Disk \'update\' operation only allows disk size to be updated
            for the exisiting VM\'s.'''
            raise Exception(err)

        disks = self.client.get_resource(vm.resource.get('href') + '/virtualHardwareSection/disks')
        for i, disk in enumerate(disks.Item):
            if disk['{' + NSMAP['rasd'] + '}ElementName'] == disk_name:
                index = i

        if index < 0:
            err = 'Can\'t find the specified VM disk with name {0}'
            err = err.format(disk_name)
            raise EntityNotFoundException(err)

        disks.Item[index]['{' + NSMAP['rasd'] + '}VirtualQuantity'] = size * 1024 * 1024
        disks.Item[index]['{' + NSMAP['rasd'] + '}HostResource'].set(
            '{' + NSMAP['vcloud'] + '}capacity', str(size))
        update_disk_task = self.client.put_resource(
            vm.resource.get('href') +
            '/virtualHardwareSection/disks', disks, EntityType.RASD_ITEMS_LIST.value)
        self.execute_task(update_disk_task)

        msg = 'Vapp VM disk with name {0} has been updated.'
        response['msg'] = msg.format(disk_name)
        response['changed'] = True

        return response

    def delete_disk(self):
        vm = self.get_vm()
        disks_to_remove = self.params.get('disks')
        response = dict()
        response['changed'] = False

        disks = self.client.get_resource(vm.resource.get('href') + '/virtualHardwareSection/disks')

        for disk in disks.Item:
            if disk['{' + NSMAP['rasd'] + '}ElementName'] in disks_to_remove:
                disks.remove(disk)
                disks_to_remove.remove(disk['{' + NSMAP['rasd'] + '}ElementName'])

        if len(disks_to_remove) > 0:
            err = 'VM disk(s) with name {0} was not found.'
            err = err.format(','.join(disks_to_remove))

            raise EntityNotFoundException(err)

        remove_disk_task = self.client.put_resource(vm.resource.get(
            'href') + '/virtualHardwareSection/disks',
            disks, EntityType.RASD_ITEMS_LIST.value)
        self.execute_task(remove_disk_task)
        response['msg'] = 'VM disk(s) has been deleted.'
        response['changed'] = True

        return response