def test_get_network(self):
     """
         check get network
     """
     # good case
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     fake_network = self.generate_fake_client_network('test name')
     fake_client.get_network = mock.MagicMock(return_value=fake_network)
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         self.assertEqual(
             network_plugin.get_network(fake_client, 'test name'),
             fake_network)
     fake_client.get_network.assert_called_with('vdc_name', 'test name')
     # bad case network not exist
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     fake_client.get_network = mock.MagicMock(return_value=None)
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             network_plugin.get_network(fake_client, 'test name')
     # worse case = nework == None
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             network_plugin.get_network(fake_client, None)
 def test_get_network(self):
     """
         check get network
     """
     # good case
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     fake_network = self.generate_fake_client_network(
         'test name'
     )
     fake_client.get_network = mock.MagicMock(
         return_value=fake_network
     )
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         self.assertEqual(
             network_plugin.get_network(
                 fake_client, 'test name'
             ),
             fake_network
         )
     fake_client.get_network.assert_called_with(
         'vdc_name', 'test name'
     )
     # bad case network not exist
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     fake_client.get_network = mock.MagicMock(return_value=None)
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             network_plugin.get_network(
                 fake_client, 'test name'
             )
     # worse case = nework == None
     fake_client = self.generate_client()
     fake_ctx = self.generate_node_context()
     with mock.patch('vcloud_plugin_common.ctx', fake_ctx):
         with self.assertRaises(cfy_exc.NonRecoverableError):
             network_plugin.get_network(
                 fake_client, None
             )
def _create(vca_client, config, server):
    """
        create server by template,
        customize:
         * hardware: memmory/cpu
         * software: root password, computer internal hostname
         connect vm to network
    """
    vapp_name = server['name']
    vapp_template = server['template']
    vapp_catalog = server['catalog']
    connections = _create_connections_list(vca_client)
    ctx.logger.info("Creating VApp with parameters: {0}".format(server))
    task = vca_client.create_vapp(config['vdc'],
                                  vapp_name,
                                  vapp_template,
                                  vapp_catalog,
                                  vm_name=vapp_name)
    if not task:
        raise cfy_exc.NonRecoverableError("Could not create vApp: {0}"
                                          .format(error_response(vca_client)))
    wait_for_task(vca_client, task)

    ctx.instance.runtime_properties[VCLOUD_VAPP_NAME] = vapp_name

    # we allways have connection to management_network_name
    if connections:
        for index, connection in enumerate(connections):
            vdc = vca_client.get_vdc(config['vdc'])
            vapp = vca_client.get_vapp(vdc, vapp_name)
            if vapp is None:
                raise cfy_exc.NonRecoverableError(
                    "vApp '{0}' could not be found".format(vapp_name))

            network_name = connection.get('network')
            network = get_network(vca_client, network_name)

            task = vapp.connect_to_network(network_name, network.get_href())
            if not task:
                raise cfy_exc.NonRecoverableError(
                    "Could not add network {0} to VApp {1}. {2}"
                    .format(network_name, vapp_name, error_response(vapp)))
            wait_for_task(vca_client, task)

            connections_primary_index = None
            if connection.get('primary_interface'):
                connections_primary_index = index
            ip_address = connection.get('ip_address')
            mac_address = connection.get('mac_address')
            ip_allocation_mode = connection.get('ip_allocation_mode',
                                                'POOL').upper()
            connection_args = {
                'network_name': network_name,
                'connection_index': index,
                'connections_primary_index': connections_primary_index,
                'ip_allocation_mode': ip_allocation_mode,
                'mac_address': mac_address,
                'ip_address': ip_address
            }
            ctx.logger.info("Connecting network with parameters {0}"
                            .format(str(connection_args)))
            task = vapp.connect_vms(**connection_args)
            if task is None:
                raise cfy_exc.NonRecoverableError(
                    "Could not connect vApp {0} to network {1}. {2}"
                    .format(vapp_name, network_name, error_response(vapp)))
            wait_for_task(vca_client, task)
Example #4
0
def _create(vca_client, config, server):
    """
        create server by template,
        customize:
         * hardware: memmory/cpu
         * software: root password, computer internal hostname
         connect vm to network
    """
    vapp_name = server['name']
    vapp_template = server['template']
    vapp_catalog = server['catalog']
    ctx.logger.info("Creating VApp with parameters: {0}".format(server))
    task = vca_client.create_vapp(config['vdc'],
                                  vapp_name,
                                  vapp_template,
                                  vapp_catalog,
                                  vm_name=vapp_name)
    if not task:
        raise cfy_exc.NonRecoverableError("Could not create vApp: {0}".format(
            vca_client.response.content))
    wait_for_task(vca_client, task)

    hardware = server.get('hardware')
    if hardware:
        cpu = hardware.get('cpu')
        memory = hardware.get('memory')
        _check_hardware(cpu, memory)
        vapp = vca_client.get_vapp(vca_client.get_vdc(config['vdc']),
                                   vapp_name)
        if memory:
            task = vapp.modify_vm_memory(vapp_name, memory)
            if task:
                wait_for_task(vca_client, task)
                ctx.logger.info("Customize VM memory: {0}.".format(memory))
            else:
                raise cfy_exc.NonRecoverableError(
                    "Customize VM memory failed: {0}.".format(task))
        if cpu:
            task = vapp.modify_vm_cpu(vapp_name, cpu)
            if task:
                wait_for_task(vca_client, task)
                ctx.logger.info("Customize VM cpu: {0}.".format(cpu))
            else:
                raise cfy_exc.NonRecoverableError(
                    "Customize VM cpu failed: {0}.".format(task))

    ctx.instance.runtime_properties[VCLOUD_VAPP_NAME] = vapp_name
    connections = _create_connections_list(vca_client)

    # we allways have connection to management_network_name
    if connections:
        for index, connection in enumerate(connections):
            vdc = vca_client.get_vdc(config['vdc'])
            vapp = vca_client.get_vapp(vdc, vapp_name)
            if vapp is None:
                raise cfy_exc.NonRecoverableError(
                    "vApp {0} could not be found".format(vapp_name))

            network_name = connection.get('network')
            network = get_network(vca_client, network_name)

            task = vapp.connect_to_network(network_name, network.get_href())
            if not task:
                raise cfy_exc.NonRecoverableError(
                    "Could not add network {0} to VApp {1}".format(
                        network_name, vapp_name))
            wait_for_task(vca_client, task)

            connections_primary_index = None
            if connection.get('primary_interface'):
                connections_primary_index = index
            ip_address = connection.get('ip_address')
            mac_address = connection.get('mac_address')
            ip_allocation_mode = connection.get('ip_allocation_mode',
                                                'POOL').upper()
            connection_args = {
                'network_name': network_name,
                'connection_index': index,
                'connections_primary_index': connections_primary_index,
                'ip_allocation_mode': ip_allocation_mode,
                'mac_address': mac_address,
                'ip_address': ip_address
            }
            ctx.logger.info("Connecting network with parameters {0}".format(
                str(connection_args)))
            task = vapp.connect_vms(**connection_args)
            if task is None:
                raise cfy_exc.NonRecoverableError(
                    "Could not connect vApp {0} to network {1}".format(
                        vapp_name, network_name))
            wait_for_task(vca_client, task)

    # customize root password and hostname
    custom = server.get(GUEST_CUSTOMIZATION)
    if custom:
        vdc = vca_client.get_vdc(config['vdc'])
        vapp = vca_client.get_vapp(vdc, vapp_name)
        script = _build_script(custom)
        password = custom.get('admin_password')
        computer_name = custom.get('computer_name')

        task = vapp.customize_guest_os(vapp_name,
                                       customization_script=script,
                                       computer_name=computer_name,
                                       admin_password=password)
        if task is None:
            raise cfy_exc.NonRecoverableError(
                "Could not set guest customization parameters")
        wait_for_task(vca_client, task)
        # This function avialable from API version 5.6
        if vapp.customize_on_next_poweron():
            ctx.logger.info("Customizations successful")
        else:
            raise cfy_exc.NonRecoverableError(
                "Can't run customization in next power on")
Example #5
0
def _create(vca_client, config, server):
    """
        create server by template,
        customize:
         * hardware: memmory/cpu
         * software: root password, computer internal hostname
         connect vm to network
    """
    vapp_name = server['name']
    vapp_template = server['template']
    vapp_catalog = server['catalog']
    ctx.logger.info("Creating VApp with parameters: {0}".format(server))
    task = vca_client.create_vapp(config['vdc'],
                                  vapp_name,
                                  vapp_template,
                                  vapp_catalog,
                                  vm_name=vapp_name)
    if not task:
        raise cfy_exc.NonRecoverableError("Could not create vApp: {0}"
                                          .format(vca_client.response.content))
    wait_for_task(vca_client, task)

    hardware = server.get('hardware')
    if hardware:
        cpu = hardware.get('cpu')
        memory = hardware.get('memory')
        _check_hardware(cpu, memory)
        vapp = vca_client.get_vapp(
            vca_client.get_vdc(config['vdc']), vapp_name
        )
        if memory:
            task = vapp.modify_vm_memory(vapp_name, memory)
            if task:
                wait_for_task(vca_client, task)
                ctx.logger.info("Customize VM memory: {0}.".format(memory))
            else:
                raise cfy_exc.NonRecoverableError(
                    "Customize VM memory failed: {0}.".format(task)
                )
        if cpu:
            task = vapp.modify_vm_cpu(vapp_name, cpu)
            if task:
                wait_for_task(vca_client, task)
                ctx.logger.info("Customize VM cpu: {0}.".format(cpu))
            else:
                raise cfy_exc.NonRecoverableError(
                    "Customize VM cpu failed: {0}.".format(task)
                )

    ctx.instance.runtime_properties[VCLOUD_VAPP_NAME] = vapp_name
    connections = _create_connections_list(vca_client)

    # we allways have connection to management_network_name
    if connections:
        for index, connection in enumerate(connections):
            vdc = vca_client.get_vdc(config['vdc'])
            vapp = vca_client.get_vapp(vdc, vapp_name)
            if vapp is None:
                raise cfy_exc.NonRecoverableError(
                    "vApp '{0}' could not be found".format(vapp_name))

            network_name = connection.get('network')
            network = get_network(vca_client, network_name)

            task = vapp.connect_to_network(network_name, network.get_href())
            if not task:
                raise cfy_exc.NonRecoverableError(
                    "Could not add network {0} to VApp {1}"
                    .format(network_name, vapp_name))
            wait_for_task(vca_client, task)

            connections_primary_index = None
            if connection.get('primary_interface'):
                connections_primary_index = index
            ip_address = connection.get('ip_address')
            mac_address = connection.get('mac_address')
            ip_allocation_mode = connection.get('ip_allocation_mode',
                                                'POOL').upper()
            connection_args = {
                'network_name': network_name,
                'connection_index': index,
                'connections_primary_index': connections_primary_index,
                'ip_allocation_mode': ip_allocation_mode,
                'mac_address': mac_address,
                'ip_address': ip_address
            }
            ctx.logger.info("Connecting network with parameters {0}"
                            .format(str(connection_args)))
            task = vapp.connect_vms(**connection_args)
            if task is None:
                raise cfy_exc.NonRecoverableError(
                    "Could not connect vApp {0} to network {1}"
                    .format(vapp_name, network_name))
            wait_for_task(vca_client, task)

    # customize root password and hostname
    custom = server.get(GUEST_CUSTOMIZATION)
    if custom:
        vdc = vca_client.get_vdc(config['vdc'])
        vapp = vca_client.get_vapp(vdc, vapp_name)
        script = _build_script(custom)
        password = custom.get('admin_password')
        computer_name = custom.get('computer_name')

        task = vapp.customize_guest_os(
            vapp_name,
            customization_script=script,
            computer_name=computer_name,
            admin_password=password
        )
        if task is None:
            raise cfy_exc.NonRecoverableError(
                "Could not set guest customization parameters")
        wait_for_task(vca_client, task)
        # This function avialable from API version 5.6
        if vapp.customize_on_next_poweron():
            ctx.logger.info("Customizations successful")
        else:
            raise cfy_exc.NonRecoverableError(
                "Can't run customization in next power on")
Example #6
0
def _create(vca_client, config, server):
    """
        create server by template,
        customize:
         * hardware: memmory/cpu
         * software: root password, computer internal hostname
         connect vm to network
    """
    vapp_name = server['name']
    vapp_template = server['template']
    vapp_catalog = server['catalog']
    connections = _create_connections_list(vca_client)
    ctx.logger.info("Creating VApp with parameters: {0}".format(server))
    task = vca_client.create_vapp(config['vdc'],
                                  vapp_name,
                                  vapp_template,
                                  vapp_catalog,
                                  vm_name=vapp_name)
    if not task:
        raise cfy_exc.NonRecoverableError("Could not create vApp: {0}".format(
            error_response(vca_client)))
    wait_for_task(vca_client, task)

    ctx.instance.runtime_properties[VCLOUD_VAPP_NAME] = vapp_name

    # we allways have connection to management_network_name
    if connections:
        for index, connection in enumerate(connections):
            vdc = vca_client.get_vdc(config['vdc'])
            vapp = vca_client.get_vapp(vdc, vapp_name)
            if vapp is None:
                raise cfy_exc.NonRecoverableError(
                    "vApp '{0}' could not be found".format(vapp_name))

            network_name = connection.get('network')
            network = get_network(vca_client, network_name)
            ctx.logger.info("Connect network '{0}' to server '{1}'.".format(
                network_name, vapp_name))
            task = vapp.connect_to_network(network_name, network.get_href())
            if not task:
                raise cfy_exc.NonRecoverableError(
                    "Could not add network {0} to VApp {1}. {2}".format(
                        network_name, vapp_name, error_response(vapp)))
            wait_for_task(vca_client, task)

            connections_primary_index = None
            if connection.get('primary_interface'):
                connections_primary_index = index
            ip_address = connection.get('ip_address')
            mac_address = connection.get('mac_address')
            ip_allocation_mode = connection.get('ip_allocation_mode',
                                                'POOL').upper()
            connection_args = {
                'network_name': network_name,
                'connection_index': index,
                'connections_primary_index': connections_primary_index,
                'ip_allocation_mode': ip_allocation_mode,
                'mac_address': mac_address,
                'ip_address': ip_address
            }
            ctx.logger.info("Connecting network with parameters {0}".format(
                str(connection_args)))
            task = vapp.connect_vms(**connection_args)
            if task is None:
                raise cfy_exc.NonRecoverableError(
                    "Could not connect vApp {0} to network {1}. {2}".format(
                        vapp_name, network_name, error_response(vapp)))
            wait_for_task(vca_client, task)