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(
             vcloud_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):
             vcloud_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):
             vcloud_network_plugin.get_network(
                 fake_client, None
             )
Example #2
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)
    if not task:
        raise cfy_exc.NonRecoverableError("Could not create vApp: {0}"
                                          .format(error_response(vca_client)))
    wait_for_task(vca_client, task)

    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))

    task = vapp.modify_vm_name(1, vapp_name)
    if not task:
        raise cfy_exc.NonRecoverableError(
            "Can't modyfy VM name".format(vapp_name))
    wait_for_task(vca_client, task)
    ctx.logger.info("VM '{0}' has been renamed.".format(vapp_name))

    # reread vapp
    vapp = vca_client.get_vapp(vdc, vapp_name)
    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):
            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)