def _add_user_permissions(user, session=None, **permission_filters): """add permissions to a user.""" from compass.db.api import permission as permission_api for api_permission in permission_api.list_permissions( session=session, **permission_filters): utils.add_db_object(session, models.UserPermission, False, user.id, api_permission['id'])
def del_switch(switch_id, user=None, session=None, **kwargs): """Delete a switch. If switch is not the default switch, and the machine under this switch is only connected to this switch, the machine will be moved to connect to default switch. Otherwise we can only simply delete the switch machine. The purpose here to make sure there is no machine not connecting to any switch. """ # TODO(xicheng): Simplify the logic if the default switch feature # can be deprecated. switch = _get_switch(switch_id, session=session) default_switch = _get_switch_by_ip(setting.DEFAULT_SWITCH_IP, session=session) if switch.id != default_switch.id: for switch_machine in switch.switch_machines: machine = switch_machine.machine if len(machine.switch_machines) <= 1: utils.add_db_object(session, models.SwitchMachine, False, default_switch.id, machine.id, port=switch_machine.port) return utils.del_db_object(session, switch)
def del_switch(switch_id, user=None, session=None, **kwargs): """Delete a switch. If switch is not the default switch, and the machine under this switch is only connected to this switch, the machine will be moved to connect to default switch. Otherwise we can only simply delete the switch machine. The purpose here to make sure there is no machine not connecting to any switch. """ # TODO(xicheng): Simplify the logic if the default switch feature # can be deprecated. switch = _get_switch(switch_id, session=session) default_switch = _get_switch_by_ip( setting.DEFAULT_SWITCH_IP, session=session ) if switch.id != default_switch.id: for switch_machine in switch.switch_machines: machine = switch_machine.machine if len(machine.switch_machines) <= 1: utils.add_db_object( session, models.SwitchMachine, False, default_switch.id, machine.id, port=switch_machine.port ) return utils.del_db_object(session, switch)
def test_add_with_multiple_args(self): with database.session() as session: db_permission = utils.add_db_object(session, models.Permission, False, "test", alias="test") db_user = utils.add_db_object(session, models.User, False, "*****@*****.**", password="******") db_objs = utils.add_db_object(session, models.UserPermission, True, db_user.id, db_permission.id) self.assertEqual(db_user.id, db_objs.user_id) self.assertEqual(db_permission.id, db_objs.permission_id)
def add_flavors_internal(session, exception_when_existing=True): configs = util.load_configs(setting.ADAPTER_FLAVOR_DIR) for config in configs: logging.info('add config %s to flavor', config) adapter = utils.get_db_object( session, models.Adapter, name=config['ADAPTER_NAME'] ) for flavor_dict in config['FLAVORS']: flavor = utils.add_db_object( session, models.AdapterFlavor, exception_when_existing, flavor_dict['flavor'], adapter.id, display_name=flavor_dict.get('display_name', None), template=flavor_dict.get('template', None) ) role_names = flavor_dict.get('roles', []) for role_name in role_names: role = utils.get_db_object( session, models.AdapterRole, name=role_name, adapter_id=adapter.id ) utils.add_db_object( session, models.AdapterFlavorRole, exception_when_existing, flavor.id, role.id ) utils.update_db_object( session, flavor, patched_ordered_flavor_roles=[role_name] )
def test_add_with_invalid_args(self): with self.assertRaises(exception.InvalidParameter): with database.session() as session: utils.add_db_object(session, models.Permission, True, 'test1', 'test2', name='test1')
def _add_user_permissions(user, session=None, **permission_filters): """add permissions to a user.""" from compass.db.api import permission as permission_api for api_permission in permission_api.list_permissions( session=session, **permission_filters ): utils.add_db_object( session, models.UserPermission, False, user.id, api_permission['id'] )
def _add_clusterhosts(session, cluster, machines): with session.begin(subtransactions=True): for machine_id, machine_attrs in machines.items(): utils.add_db_object( session, models.Host, False, machine_id, cluster.os_id, cluster.creator_id, **machine_attrs ) utils.add_db_object( session, models.ClusterHost, False, cluster.id, machine_id )
def test_add_with_invalid_args(self): with self.assertRaises(exception.InvalidParameter): with database.session() as session: utils.add_db_object( session, models.Permission, True, 'test1', 'test2', name='test1' )
def _set_clusterhosts(session, cluster, machines): with session.begin(subtransactions=True): utils.del_db_objects( session, models.ClusterHost, cluster_id=cluster.id ) for machine_id in machines: utils.add_db_object( session, models.ClusterHost, True, cluster.id, machine_id )
def test_add_duplicate_with_flag(self): with self.assertRaises(exception.DuplicatedRecord): with database.session() as session: utils.add_db_object(session, models.Permission, True, 'test', alias='test') utils.add_db_object(session, models.Permission, True, 'test', alias='test')
def _copy_adapters_from_parent(session, model, parent, system_name): for child in parent.children: if not child.adapters: for adapter in parent.adapters: if adapter.children: continue utils.add_db_object( session, model, True, '%s(%s)' % (child.name, adapter.installer_name), system_name=child, parent=adapter ) _copy_adapters_from_parent(session, model, child, system_name)
def test_add_duplicate_with_no_flag(self): with database.session() as session: db_objs = utils.add_db_object(session, models.Permission, False, 'test', alias='test') duplicate = utils.add_db_object(session, models.Permission, False, 'test', alias='test') self.assertEqual(duplicate, db_objs)
def add_roles_internal(session): configs = util.load_configs(setting.PACKAGE_ROLE_DIR) with session.begin(subtransactions=True): for config in configs: package_adapter = utils.get_db_object( session, models.PackageAdapter, name=config['ADAPTER_NAME'] ) for role_dict in config['ROLES']: utils.add_db_object( session, models.PackageAdapterRole, True, role_dict['role'], package_adapter.id, description=role_dict['description'], optional=role_dict.get('optional', False) )
def _add_metadata( session, field_model, metadata_model, name, config, parent=None, adapter=None ): metadata = config.get('_self', {}) if 'field' in metadata: field = utils.get_db_object( session, field_model, field=metadata['field'] ) else: field = None object = utils.add_db_object( session, metadata_model, True, name, adapter=adapter, parent=parent, field=field, display_name=metadata.get('display_name', name), description=metadata.get('description', None), is_required=metadata.get('is_required', False), required_in_whole_config=metadata.get('required_in_whole_config', False), mapping_to=metadata.get('mapping_to', None), validator=metadata.get('validator', None), js_validator=metadata.get('js_validator', None), default_value=metadata.get('default_value', None), options=metadata.get('options', []), required_in_options=metadata.get('required_in_options', False) ) for key, value in config.items(): if key not in '_self': _add_metadata( session, field_model, metadata_model, key, value, parent=object, adapter=adapter, ) return object
def _add_host_network( session, creator, host_id, exception_when_existing=True, interface=None, ip=None, **kwargs ): host = utils.get_db_object( session, models.Host, id=host_id ) ip_int = long(netaddr.IPAddress(ip)) host_network = utils.get_db_object( session, models.HostNetwork, False, ip_int=ip_int ) if ( host_network and not ( host_network.host_id == host_id and host_network.interface == interface ) ): raise exception.InvalidParameter( 'ip %s exists in host network %s' % ( ip, host_network.id ) ) is_host_editable(session, host, creator) return utils.add_db_object( session, models.HostNetwork, exception_when_existing, host_id, interface, ip=ip, **kwargs )
def test_add_with_multiple_args(self): with database.session() as session: db_permission = utils.add_db_object(session, models.Permission, False, 'test', alias='test') db_user = utils.add_db_object(session, models.User, False, '*****@*****.**', password='******') db_objs = utils.add_db_object(session, models.UserPermission, True, db_user.id, db_permission.id) self.assertEqual(db_user.id, db_objs.user_id) self.assertEqual(db_permission.id, db_objs.permission_id)
def add_os_adapters_internal(session): parents = {} configs = util.load_configs(setting.OS_ADAPTER_DIR) with session.begin(subtransactions=True): for config in configs: if 'OS' in config: os = utils.get_db_object( session, models.OperatingSystem, name=config['OS'] ) else: os = None if 'INSTALLER' in config: installer = utils.get_db_object( session, models.OSInstaller, name=config['INSTALLER'] ) else: installer = None object = utils.add_db_object( session, models.OSAdapter, True, config['NAME'], os=os, installer=installer ) parents[config['NAME']] = (object, config.get('PARENT', None)) for name, (object, parent_name) in parents.items(): if parent_name: parent, _ = parents[parent_name] else: parent = None utils.update_db_object( session, object, parent=parent ) _complement_os_adapters(session)
def record_user_token(session, user, token, expire_timestamp): """record user token in database.""" return utils.add_db_object( session, models.UserToken, True, token, user_id=user.id, expire_timestamp=expire_timestamp )
def test_add_duplicate_with_flag(self): with self.assertRaises(exception.DuplicatedRecord): with database.session() as session: utils.add_db_object( session, models.Permission, True, 'test', alias='test' ) utils.add_db_object( session, models.Permission, True, 'test', alias='test' )
def test_add_duplicate_with_no_flag(self): with database.session() as session: db_objs = utils.add_db_object( session, models.Permission, False, 'test', alias='test' ) duplicate = utils.add_db_object( session, models.Permission, False, 'test', alias='test' ) self.assertEqual(duplicate, db_objs)
def add_subnet(exception_when_existing=True, subnet=None, user=None, session=None, **kwargs): """Create a subnet.""" return utils.add_db_object(session, models.Subnet, exception_when_existing, subnet, **kwargs)
def add_subnet( exception_when_existing=True, subnet=None, user=None, session=None, **kwargs ): """Create a subnet.""" return utils.add_db_object( session, models.Subnet, exception_when_existing, subnet, **kwargs )
def test_add_alias(self): with database.session() as session: db_objs = utils.add_db_object(session, models.Permission, True, 'test', alias='test') expected = 'test' self.assertEqual(expected, db_objs.alias)
def add_permission( user_id, exception_when_missing=True, permission_id=None, user=None, session=None ): """Add an user permission.""" return utils.add_db_object( session, models.UserPermission, exception_when_missing, user_id, permission_id )
def add_switch_internal( session, ip_int, exception_when_existing=True, filters=setting.SWITCHES_DEFAULT_FILTERS, **kwargs ): with session.begin(subtransactions=True): return utils.add_db_object( session, models.Switch, exception_when_existing, ip_int, filters=filters, **kwargs )
def add_subnet(creator, subnet, **kwargs): """Create a subnet.""" with database.session() as session: user_api.check_user_permission_internal( session, creator, permission.PERMISSION_ADD_NETWORK) network = utils.add_db_object( session, models.Network, True, subnet ) return network.to_dict()
def _del_switch_machine(switch_machine, session=None): """Delete switch machine. If this is the last switch machine associated to underlying machine, add a switch machine record to default switch to make the machine searchable. """ default_switch = _get_switch_by_ip(setting.DEFAULT_SWITCH_IP, session=session) machine = switch_machine.machine if len(machine.switch_machines) <= 1: utils.add_db_object(session, models.SwitchMachine, False, default_switch.id, machine.id, port=switch_machine.port) return utils.del_db_object(session, switch_machine)
def add_roles_internal(session, exception_when_existing=True): configs = util.load_configs(setting.ADAPTER_ROLE_DIR) for config in configs: logging.info( 'add config %s to role', config ) adapter = utils.get_db_object( session, models.Adapter, name=config['ADAPTER_NAME'] ) for role_dict in config['ROLES']: utils.add_db_object( session, models.AdapterRole, exception_when_existing, role_dict['role'], adapter.id, display_name=role_dict.get('display_name', None), description=role_dict.get('description', None), optional=role_dict.get('optional', False) )
def add_host_log_history( host_id, exception_when_existing=False, filename=None, user=None, session=None, **kwargs ): """add a host log history.""" return utils.add_db_object( session, models.HostLogHistory, exception_when_existing, host_id, filename, **kwargs )
def del_switch(switch_id, user=None, session=None, **kwargs): """Delete a switch.""" switch = utils.get_db_object(session, models.Switch, id=switch_id) default_switch_ip_int = long(netaddr.IPAddress(setting.DEFAULT_SWITCH_IP)) default_switch = utils.get_db_object( session, models.Switch, ip_int=default_switch_ip_int ) for switch_machine in switch.switch_machines: machine = switch_machine.machine if len(machine.switch_machines) <= 1: utils.add_db_object( session, models.SwitchMachine, False, default_switch.id, machine.id, port=switch_machine.port ) return utils.del_db_object(session, switch)
def _add_installers(session, model, configs, exception_when_existing=True): installers = [] for config in configs: installers.append(utils.add_db_object( session, model, exception_when_existing, config['INSTANCE_NAME'], name=config['NAME'], settings=config.get('SETTINGS', {}) )) return installers
def add_cluster(creator, name, adapter_id, **kwargs): """Create a cluster.""" with database.session() as session: user_api.check_user_permission_internal( session, creator, permission.PERMISSION_ADD_CLUSTER) cluster = utils.add_db_object( session, models.Cluster, True, name, adapter_id, creator.id, **kwargs ) return cluster.to_dict()
def _add_installers(session, model, configs): installers = [] for config in configs: installers.append(utils.add_db_object( session, model, True, config['NAME'], installer_type=config['TYPE'], config=config['CONFIG'] )) return installers
def test_add_nothing(self): with database.session() as session: db_objs = utils.add_db_object( session, models.Permission, True, 'test' ) self.assertEqual('test', db_objs.name) self.assertIsNone(db_objs.alias)
def add_host_log_history( host_id, exception_when_existing=False, filename=None, user=None, session=None, **kwargs ): """add a host log history.""" host = _get_host(host_id, session=session) return utils.add_db_object( session, models.HostLogHistory, exception_when_existing, host.id, filename, **kwargs )
def add_report_record(cluster_id, name=None, report={}, state='verifying', session=None, **kwargs): """Create a health check report record.""" # Replace any white space into '-' words = name.split() name = '-'.join(words) cluster = cluster_api.get_cluster_internal(cluster_id, session=session) return utils.add_db_object( session, models.HealthCheckReport, True, cluster.id, name, report=report, state=state, **kwargs )
def _add_switch( ip, exception_when_existing=True, machine_filters=setting.SWITCHES_DEFAULT_FILTERS, session=None, **kwargs ): """Add switch by switch ip.""" ip_int = long(netaddr.IPAddress(ip)) return utils.add_db_object( session, models.Switch, exception_when_existing, ip_int, machine_filters=machine_filters, **kwargs )
def _add_switch_machine_only( switch, machine, exception_when_existing=True, session=None, port=None, **kwargs ): """add a switch machine.""" return utils.add_db_object( session, models.SwitchMachine, exception_when_existing, switch.id, machine.id, port=port, **kwargs )
def add_report_record(cluster_id, name, report={}, state='verifying', session=None, **kwargs): """Create a health check report record.""" # Replace any white space into '-' words = name.split() name = '-'.join(words) return utils.add_db_object( session, models.HealthCheckReport, True, cluster_id, name, report=report, state=state, **kwargs )
def test_add_alias(self): with database.session() as session: db_objs = utils.add_db_object( session, models.Permission, True, 'test', alias='test' ) expected = 'test' self.assertEqual(expected, db_objs.alias)
def _add_machine(mac, owner_id=None, session=None, **kwargs): """Add a machine.""" if isinstance(owner_id, (int, long)): return utils.add_db_object(session, models.Machine, True, mac, owner_id=owner_id, **kwargs) raise exception.InvalidParameter('owner id %s type is not int compatible' % owner_id)
def add_user(exception_when_existing=True, user=None, session=None, email=None, **kwargs): """Create a user and return created user object.""" add_user = utils.add_db_object(session, models.User, exception_when_existing, email, **kwargs) _add_user_permissions(add_user, session=session, name=setting.COMPASS_DEFAULT_PERMISSIONS) return add_user
def _add_switch_machine_only( switch, machine, exception_when_existing=True, session=None, owner_id=None, port=None, **kwargs ): """add a switch machine.""" return utils.add_db_object( session, models.SwitchMachine, exception_when_existing, switch.id, machine.id, port=port, owner_id=owner_id, **kwargs )
def _add_host_network( host_id, exception_when_existing=True, session=None, user=None, interface=None, ip=None, **kwargs ): """Add hostnetwork to a host.""" host = _get_host(host_id, session=session) check_host_editable(host, user=user) return utils.add_db_object( session, models.HostNetwork, exception_when_existing, host.id, interface, ip=ip, **kwargs )
def add_permission(user_id, permission_id=None, exception_when_existing=True, user=None, session=None): """Add a permission to a user.""" get_user = _get_user(user_id, session=session) from compass.db.api import permission as permission_api get_permission = permission_api.get_permission_internal(permission_id, session=session) return utils.add_db_object(session, models.UserPermission, exception_when_existing, get_user.id, get_permission.id)
def add_permissions_internal(session=None): """internal functions used by other db.api modules only.""" permissions = [] for permission in PERMISSIONS: permissions.append( utils.add_db_object(session, models.Permission, True, permission.name, alias=permission.alias, description=permission.description)) return permissions
def _add_host_network( host_id, exception_when_existing=True, session=None, user=None, interface=None, ip=None, subnet_id=None, **kwargs ): """Add hostnetwork to a host.""" host = _get_host(host_id, session=session) check_host_editable(host, user=user) subnet = network.get_subnet_internal(subnet_id, session=session) check_ip_available(subnet, ip) user_id = user.id return utils.add_db_object( session, models.HostNetwork, exception_when_existing, host.id, interface, user_id, ip=ip, subnet_id=subnet_id, **kwargs )
def record_user_token(token, expire_timestamp, user=None, session=None): """record user token in database.""" user_token = utils.get_db_object(session, models.UserToken, False, user_id=user.id, token=token) if not user_token: return utils.add_db_object(session, models.UserToken, True, token, user_id=user.id, expire_timestamp=expire_timestamp) elif expire_timestamp > user_token.expire_timestamp: return utils.update_db_object(session, user_token, expire_timestamp=expire_timestamp) return user_token
def test_add_nothing(self): with database.session() as session: db_objs = utils.add_db_object(session, models.Permission, True, 'test') self.assertEqual('test', db_objs.name) self.assertIsNone(db_objs.alias)
def log_user_action(user_id, action, session=None): """Log user action.""" utils.add_db_object( session, models.UserLog, True, user_id=user_id, action=action )
def _add_machine_if_not_exist(mac=None, session=None, **kwargs): """Add machine if the mac does not exist in any machine.""" return utils.add_db_object(session, models.Machine, False, mac, **kwargs)