def test_add_or_update_instance(self):
        instances = {'instance-id1': 'instance-name1'}
        self._create_instances(instances)
        new_instance_name = 'instance-name-updated'
        infoblox_db.add_or_update_instance(self.ctx.session,
                                           'instance-id1', new_instance_name)
        instance = infoblox_db.get_instance(self.ctx.session, 'instance-id1')
        self.assertEqual(new_instance_name, instance.instance_name)

        infoblox_db.add_or_update_instance(self.ctx.session,
                                           'instance-id2', 'instance-name2')
        instance = infoblox_db.get_instance(self.ctx.session, 'instance-id2')
        self.assertEqual('instance-name2', instance.instance_name)
Example #2
0
    def test_add_or_update_instance(self):
        instances = {'instance-id1': 'instance-name1'}
        self._create_instances(instances)
        new_instance_name = 'instance-name-updated'
        infoblox_db.add_or_update_instance(self.ctx.session, 'instance-id1',
                                           new_instance_name)
        instance = infoblox_db.get_instance(self.ctx.session, 'instance-id1')
        self.assertEqual(new_instance_name, instance.instance_name)

        infoblox_db.add_or_update_instance(self.ctx.session, 'instance-id2',
                                           'instance-name2')
        instance = infoblox_db.get_instance(self.ctx.session, 'instance-id2')
        self.assertEqual('instance-name2', instance.instance_name)
Example #3
0
    def _get_instance_name_from_fip(self, floatingip):
        """Get instance name from fip associated with an instance

        Get instance name using the following info. in floatingip:
        1. port_id - this is the port id for the instance
        2. fixed_ip_address - this is the fixed ip for the instance

        Using the above, construct InfobloxContext and query NIOS
        for FixedAddress/HostRecord for instance. From the result,
        extract instance name from the "VM Name" EA
        """
        port_id = floatingip.get('port_id')
        fixed_ip = floatingip.get('fixed_ip_address')

        port = self.plugin.get_port(self.context, port_id)
        if not port:
            LOG.warning("No port found for port_id: %s" % port_id)
            return None

        if port['device_owner'] in const.NEUTRON_DEVICE_OWNER_COMPUTE_LIST:
            instance = dbi.get_instance(self.context.session,
                                        port['device_id'])
            if instance:
                return instance.instance_name

        subnet_ids = [ip['subnet_id'] for ip in port['fixed_ips']
                      if ip['ip_address'] == fixed_ip]
        if not subnet_ids:
            LOG.warning("No subnet_ids found for port: %s, fixed_ip: " %
                        (port, fixed_ip))
            return None

        subnet = self.plugin.get_subnet(self.context, subnet_ids[0])
        if not subnet:
            LOG.warning("No subnet was found for subnet_id: %s" %
                        subnet_ids[0])
            return

        ib_context = context.InfobloxContext(self.context, self.user_id,
                                             None, subnet, self.grid_config,
                                             self.plugin,
                                             self._cached_grid_members,
                                             self._cached_network_views,
                                             self._cached_mapping_conditions)

        connector = ib_context.connector
        netview = ib_context.mapping.network_view
        dns_view = ib_context.mapping.dns_view
        ib_address = ib_objects.FixedAddress.search(connector,
                                                    network_view=netview,
                                                    ip=fixed_ip)
        if not ib_address:
            ib_address = ib_objects.HostRecord.search(connector,
                                                      view=dns_view,
                                                      ip=fixed_ip)
            if not ib_address:
                return None

        return ib_address.extattrs.get(const.EA_VM_NAME)
Example #4
0
    def update_port_sync(self, payload):
        """Notifies that the port has been updated."""
        port = payload.get('port')

        if 'binding:vif_type' in port:
            instance_name = None
            if port['device_owner'] in const.NEUTRON_DEVICE_OWNER_COMPUTE_LIST:
                instance = dbi.get_instance(self.context.session,
                                            port['device_id'])
                if instance:
                    instance_name = instance.instance_name
            event = 'Port update'
            if port['binding:vif_type'] == 'unbound':
                self._process_port(port, event, None)
            elif instance_name is not None:
                self._process_port(port, event, instance_name)

        if self.traceable:
            LOG.info("Updated port: %s", port)
    def update_port_sync(self, payload):
        """Notifies that the port has been updated."""
        port = payload.get('port')

        if 'binding:vif_type' in port:
            instance_name = None
            if port['device_owner'] in const.NEUTRON_DEVICE_OWNER_COMPUTE_LIST:
                instance = dbi.get_instance(self.context.session,
                                            port['device_id'])
                if instance:
                    instance_name = instance.instance_name
            event = 'Port update'
            if port['binding:vif_type'] == 'unbound':
                self._process_port(port, event, None)
            elif instance_name is not None:
                self._process_port(port, event, instance_name)

        if self.traceable:
            LOG.info("Updated port: %s", port)
Example #6
0
    def bind_names(self, ip_address, instance_name=None, port_id=None,
                   port_tenant_id=None, device_id=None, device_owner=None,
                   is_floating_ip=False, port_name=None):
        if not device_owner:
            return

        if device_owner in constants.NEUTRON_DEVICE_OWNER_COMPUTE_LIST:
            if instance_name is None:
                instance = dbi.get_instance(self.ib_cxt.context.session,
                                            device_id)
                if instance is not None:
                    instance_name = instance.instance_name

        tenant_id = port_tenant_id or self.ib_cxt.context.tenant_id
        tenant_name = self.ib_cxt.get_tenant_name(tenant_id)
        ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id,
                                          tenant_id,
                                          tenant_name,
                                          self.ib_cxt.network,
                                          port_id,
                                          device_id,
                                          device_owner,
                                          is_floating_ip,
                                          instance_name)

        ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc
                    if device_owner == n_const.DEVICE_OWNER_DHCP
                    else self.ib_cxt.ip_alloc)
        try:
            self._bind_names(ip_alloc.bind_names, ip_address,
                             instance_name, port_id,
                             port_tenant_id, device_id,
                             device_owner, ea_ip_address,
                             port_name, tenant_name)
        except ibc_exc.InfobloxCannotCreateObject:
            with excutils.save_and_reraise_exception():
                self.unbind_names(ip_address, instance_name, port_id,
                                  port_tenant_id, device_id, device_owner,
                                  port_name)
Example #7
0
    def bind_names(self, ip_address, instance_name=None, port_id=None,
                   port_tenant_id=None, device_id=None, device_owner=None,
                   is_floating_ip=False, port_name=None):
        if not device_owner:
            return

        if device_owner in constants.NEUTRON_DEVICE_OWNER_COMPUTE_LIST:
            if instance_name is None:
                instance = dbi.get_instance(self.ib_cxt.context.session,
                                            device_id)
                if instance is not None:
                    instance_name = instance.instance_name

        tenant_id = port_tenant_id or self.ib_cxt.context.tenant_id
        tenant_name = self.ib_cxt.get_tenant_name(tenant_id)
        ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id,
                                          tenant_id,
                                          tenant_name,
                                          self.ib_cxt.network,
                                          port_id,
                                          device_id,
                                          device_owner,
                                          is_floating_ip,
                                          instance_name)

        ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc
                    if device_owner == n_const.DEVICE_OWNER_DHCP
                    else self.ib_cxt.ip_alloc)
        try:
            self._bind_names(ip_alloc.bind_names, ip_address,
                             instance_name, port_id, port_tenant_id, device_id,
                             device_owner, ea_ip_address, port_name)
        except ibc_exc.InfobloxCannotCreateObject:
            with excutils.save_and_reraise_exception():
                self.unbind_names(ip_address, instance_name, port_id,
                                  port_tenant_id, device_id, device_owner,
                                  port_name)
 def test_add_and_get_instance(self):
     instances = {'instance-id': 'instance-name'}
     self._create_instances(instances)
     instance = infoblox_db.get_instance(self.ctx.session, 'instance-id')
     self.assertEqual('instance-name', instance.instance_name)
Example #9
0
 def test_add_and_get_instance(self):
     instances = {'instance-id': 'instance-name'}
     self._create_instances(instances)
     instance = infoblox_db.get_instance(self.ctx.session, 'instance-id')
     self.assertEqual('instance-name', instance.instance_name)