def test_update_network_merges_eas(self): original_ea = objects.EA({'User EA': 'user value', 'Subnet ID': 'one'}) new_ea = objects.EA({'Subnet ID': 'two'}) merged_ea = objects.EA({'User EA': 'user value', 'Subnet ID': 'two'}).to_dict() self._update_network_updates_eas(original_ea, new_ea, merged_ea)
def _get_ib_network(self, subnet_id, ip_version=None): db_netviews = dbi.get_network_view_by_mapping( self._context.session, grid_id=self._grid_config.grid_id, subnet_id=subnet_id) if not db_netviews: return None network_view = db_netviews[0].network_view ea = ib_objects.EA({'Subnet ID': subnet_id}) ib_network = None # TODO(hhwang): this should be replaced to use get_network_by_subnet_id # api when the following issue is resolved: # https://github.com/infobloxopen/infoblox-client/issues/58 if ip_version == 4 or ip_version is None: ib_network = ib_objects.NetworkV4.search( self._grid_config.gm_connector, network_view=network_view, search_extattrs=ea) if ip_version == 6 or ib_network is None: ib_network = ib_objects.NetworkV6.search( self._grid_config.gm_connector, network_view=network_view, search_extattrs=ea) return ib_network
def create_a_record(conn, module, a_record): try: if module.params['extattrs']: extattrs = objects.EA(module.params['extattrs']) else: extattrs = None if a_record: module.exit_json(changed=False, ip_addr=a_record.ipv4addr, name=a_record.name) objects.ARecord.create(conn, ip=module.params['ip_address'], name=module.params['name'], view=module.params['dns_view'], extattrs=extattrs) #ARecord.create does not return the ip address etc. so we must seach for it again a_record = objects.ARecord.search(conn, name=module.params['name'], ipv4addr=module.params['ip_address']) module.exit_json(changed=True, name=a_record.name, ip_addr=a_record.ipv4addr, extattrs=ea_to_dict(a_record.extattrs)) except exceptions.InfobloxException as error: module.fail_json(msg=str(error))
def test_ea_set_get(self): ea = objects.EA() ea_name = 'Subnet ID' id = 'subnet-id' generated_eas = {ea_name: {'value': id}} ea.set(ea_name, id) self.assertEqual(id, ea.get(ea_name)) self.assertEqual(generated_eas, ea.to_dict())
def delete_instance_sync(self, payload): """Notifies that an instance has been deleted.""" instance_id = payload.get('instance_id') session = self.context.session dbi.remove_instance(session, instance_id) if self.traceable: LOG.info("Deleted instance: %s", instance_id) vm_id_ea = ib_objects.EA({'VM ID': instance_id}) subnets = dbi.get_external_subnets(self.context.session) for cur_subnet in subnets: subnet = self.plugin.get_subnet(self.context, cur_subnet.id) network = self.plugin.get_network(self.context, cur_subnet.network_id) ib_context = context.InfobloxContext( self.context, self.user_id, network, subnet, self.grid_config, self.plugin, self._cached_grid_members, self._cached_network_views, self._cached_mapping_conditions) dns_controller = dns.DnsController(ib_context) 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, network=subnet['cidr'], search_extattrs=vm_id_ea) if not ib_address: ib_address = ib_objects.HostRecord.search( connector, view=dns_view, zone=dns_controller.dns_zone, search_extattrs=vm_id_ea) if not ib_address: continue if hasattr(ib_address, 'ips'): ips = [ipaddr.ip for ipaddr in ib_address.ips if netaddr.IPAddress(ipaddr.ip) in netaddr.IPNetwork(subnet['cidr'])] else: ips = [ib_address.ip] tenant_id = ib_address.extattrs.get('Tenant ID') db_ports = dbi.get_floatingip_ports( session, ips, cur_subnet.network_id) for port in db_ports: port_id = port[0] device_id = port[1] device_owner = port[2] floating_ip = port[3] port_name = port[4] dns_controller.bind_names( floating_ip, None, port_id, tenant_id, device_id, device_owner, False, port_name) LOG.info("Instance deletion sync: instance id = %s, " "floating ip = %s, port id = %s, device owner = %s", instance_id, floating_ip, port_id, device_owner)
def get_ea_for_forward_zone(user_id, tenant_id, tenant_name, network, subnet, name_template): ea_dict = get_common_ea(network, user_id, tenant_id, tenant_name) if '{subnet_id}' in name_template or '{subnet_name}' in name_template: ea_dict.update(get_subnet_specific_eas(subnet)) ea_dict.update(get_net_specific_eas(network)) elif '{network_id}' in name_template or '{network_name}' in name_template: ea_dict.update(get_net_specific_eas(network)) return ib_objects.EA(ea_dict)
def test_ea_returns_ea_dict(self): ea_dict = {'Subnet ID': 'some-id'} ea = objects.EA(ea_dict) ea_dict_from_EA_object = ea.ea_dict self.assertEqual(ea_dict, ea_dict_from_EA_object) # Make sure a copy of dict is returned, # and updating returned value do not affect EA object ea_dict_from_EA_object['Subnet ID'] = 'another-id' self.assertEqual('some-id', ea.get('Subnet ID'))
def _allocate_pools(self, rollback_list, pools, cidr, ip_version, check_if_exists=False): ea_range = eam.get_ea_for_range(self.ib_cxt.user_id, self.ib_cxt.tenant_id, self.ib_cxt.tenant_name, self.ib_cxt.network) is_shared = self.ib_cxt.network_is_shared_or_external for pool in pools: disable = True # db_base_plugin uses netaddr but neutronclient uses dict for # ip range if isinstance(pool, dict) and pool.get('start'): start_ip = pool.get('start') else: start_ip = netaddr.IPAddress(pool.first, ip_version).format() if isinstance(pool, dict) and pool.get('end'): end_ip = pool.get('end') else: end_ip = netaddr.IPAddress(pool.last, ip_version).format() if check_if_exists: ib_ip_range = ib_objects.IPRange.search( self.ib_cxt.connector, network_view=self.ib_cxt.mapping.network_view, network=cidr, start_addr=start_ip, end_addr=end_ip) if ib_ip_range: managed = "Unmanaged" # Not managed by OpenStack if not is_shared or self._range_is_managed(ib_ip_range): managed = "Managed" # Managed by OpenStack eas = ib_ip_range.extattrs if eas: ea_dict = ib_ip_range.extattrs.ea_dict ea_dict.update(ea_range.ea_dict) ib_ip_range.extattrs = ib_objects.EA(ea_dict) else: ib_ip_range.extattrs = ea_range ib_ip_range.update() LOG.info("%s ip range already existed: %s" % (managed, ib_ip_range)) continue ib_ip_range = self.ib_cxt.ibom.create_ip_range( self.ib_cxt.mapping.network_view, start_ip, end_ip, cidr, disable, ea_range) LOG.info("ip range has been created: %s", ib_ip_range) rollback_list.append(ib_ip_range)
def update_network_options(self, ib_network, extattrs=None): if extattrs: if ib_network.extattrs: # Merge EA values as dicts ea_dict = ib_network.extattrs.ea_dict ea_dict.update(extattrs.ea_dict) merged_ea = obj.EA(ea_dict) ib_network.extattrs = merged_ea else: ib_network.extattrs = extattrs return ib_network.update()
def create_ea_defs(grid_id): print("\nCreating EA definitions...") print(("-" * PRINT_LINE)) print("") credentials = get_credentias() conn = utils.get_connector(credentials) if not (utils.get_features(conn).create_ea_def): LOG.error("WAPI Version '%s' is not supported - Script ABORTED!", conn.wapi_version) exit(1) mgr = object_manager.InfobloxObjectManager(conn) ea_defs_created = mgr.create_required_ea_definitions( const.REQUIRED_EA_DEFS, reraise=True) if ea_defs_created: print(("The following EA Definitions have been created: '%s'" % [ea_def['name'] for ea_def in ea_defs_created])) else: print("All the EAs has been already created.") print("\n") grid_opts = config.get_infoblox_grid_opts(grid_id) gm_name = grid_opts['grid_master_name'] member = objects.Member.search(conn, host_name=gm_name) if member is None: LOG.error("Cannot retrieve member information at GM='%s'" % gm_name) exit(1) print("Adding grid configuration EAs to the grid master...") print(("-" * PRINT_LINE)) print("") ea_set = {} if member.extattrs is None: member.extattrs = objects.EA({}) for ea, val in list(const.GRID_CONFIG_DEFAULTS.items()): if (member.extattrs.get(ea) is None and not (val is None or val == [])): ea_set[ea] = val member.extattrs.set(ea, val) if ea_set: print(("Grid configurations: '%s'" % ea_set)) member.update() else: print("All the grid configurations have been already added.") print("\n")
def get_ea_for_network_view(tenant_id, tenant_name, cloud_adapter_id): """Generates EAs for Network View. :param tenant_id: tenant_id :param tenant_name: tenant_name :return: dict with extensible attributes ready to be sent as part of NIOS WAPI """ # OpenStack should not own entire network view, # since shared or external networks may be created in it attributes = { const.EA_CMP_TYPE: const.CLOUD_PLATFORM_NAME, const.EA_TENANT_ID: tenant_id or const.EA_RESET_VALUE, const.EA_TENANT_NAME: tenant_name, const.EA_CLOUD_API_OWNED: 'False', const.EA_CLOUD_ADAPTER_ID: str(cloud_adapter_id) } return ib_objects.EA(attributes)
def test_ea_to_dict(self): ea = {'Subnet ID': 'some-id', 'Tenant Name': 'tenant-name', 'Cloud API Owned': 'True', 'DNS Record Types': ['record_a', 'record_ptr'], 'False String EA': 'False', 'Empty String EA': '', 'False EA': False, 'Zero EA': 0, 'None EA': None, 'None String EA': 'None', 'Empty List EA': [], 'Zero String EA': '0'} processed_ea = {'Subnet ID': 'some-id', 'Tenant Name': 'tenant-name', 'Cloud API Owned': 'True', 'DNS Record Types': ['record_a', 'record_ptr'], 'False String EA': 'False', 'False EA': 'False', 'Zero EA': '0', 'None String EA': 'None', 'Zero String EA': '0'} ea_exist = ['Subnet ID', 'Tenant Name', 'Cloud API Owned', 'DNS Record Types', 'False String EA', 'False EA', 'Zero EA', 'None String EA', 'Zero String EA'] ea_purged = ['Empty String EA', 'None EA', 'Empty List EA'] ea_dict = objects.EA(ea).to_dict() self.assertIsInstance(ea_dict, dict) for key in ea_exist: self.assertEqual(True, key in ea_dict) for key in ea_purged: self.assertEqual(False, key in ea_dict) for key in processed_ea: self.assertEqual(processed_ea[key], ea_dict.get(key).get('value'))
def get_ea_for_ip(user_id, tenant_id, tenant_name, network, port_id, device_id, device_owner, is_floating_ip=False, instance_name=None): instance_id = None ip_type = const.IP_TYPE_FIXED if is_floating_ip or device_owner == n_const.DEVICE_OWNER_FLOATINGIP: ip_type = const.IP_TYPE_FLOATING if device_owner in const.NEUTRON_DEVICE_OWNER_COMPUTE_LIST: instance_id = device_id common_ea = get_common_ea(network, user_id, tenant_id, tenant_name) ip_dict = get_dict_for_ip(port_id, device_owner, device_id, instance_id, ip_type, instance_name) common_ea.update(ip_dict) return ib_objects.EA(common_ea)
def create_host_record(conn,host_record, module, ip_addr): try: if module.params['extattrs']: extattrs = objects.EA(module.params['extattrs']) else: extattrs = None if host_record: if is_different(module, host_record): #uncomment to verify that this is indeed making it's way to this #module.fail_json(msg='updating...') host_record.create(conn, ip=ip_addr, view=module.params['dns_view'], name=module.params['name'], configure_for_dns=module.params['configure_for_dns'], ttl=module.params['ttl'], comment=module.params['comment'], extattrs=extattrs, update_if_exists=True) module.exit_json(changed=True, ip_addr=ipv4_or_v6(host_record),extattrs=ea_to_dict(host_record.extattrs)) else: module.exit_json(changed=False, ip_addr=ipv4_or_v6(host_record),extattrs=ea_to_dict(host_record.extattrs)) #If host doesn not exist, create host_record = objects.HostRecord.create(conn, ip=ip_addr, view=module.params['dns_view'], name=module.params['name'], configure_for_dns=module.params['configure_for_dns'], ttl=module.params['ttl'], comment=module.params['comment'], extattrs=extattrs) module.exit_json(changed=True, ip_addr=ipv4_or_v6(host_record),extattrs=ea_to_dict(host_record.extattrs)) except exceptions.InfobloxException as error: module.fail_json(msg=str(error))
def get_ea_for_network(user_id, tenant_id, tenant_name, network, subnet): """Generates EAs for Network. :param user_id: user_id :param tenant_id: tenant_id :param subnet: neutron subnet object :param network: neutron network object :return: dict with extensible attributes ready to be sent as part of NIOS WAPI """ subnet = {} if subnet is None else subnet network = {} if network is None else network attributes = get_subnet_specific_eas(subnet) attributes.update(get_net_specific_eas(network)) common_ea = get_common_ea(network, user_id, tenant_id, tenant_name, for_network=True) attributes.update(common_ea) return ib_objects.EA(attributes)
def get_ea_for_range(user_id, tenant_id, tenant_name, network): return ib_objects.EA( get_common_ea(network, user_id, tenant_id, tenant_name))
def get_default_ea_for_ip(user_id, tenant_id, tenant_name): common_ea = get_common_ea(None, user_id, tenant_id, tenant_name) ip_dict = get_dict_for_ip(None, None, None, None, const.IP_TYPE_FIXED) common_ea.update(ip_dict) return ib_objects.EA(common_ea)
def test_update_network_updates_eas(self): original_ea = None new_ea = objects.EA({'Subnet ID': 'two'}) merged_ea = new_ea.to_dict() self._update_network_updates_eas(original_ea, new_ea, merged_ea)
def get_ea_for_reverse_zone(user_id, tenant_id, tenant_name, network, subnet): ea_dict = get_common_ea(network, user_id, tenant_id, tenant_name) ea_dict.update(get_net_specific_eas(network)) ea_dict.update(get_subnet_specific_eas(subnet)) return ib_objects.EA(ea_dict)