diff --git a/compass/db/api/cluster.py b/compass/db/api/cluster.py index d5b4a845..652cedda 100644 --- a/compass/db/api/cluster.py +++ b/compass/db/api/cluster.py @@ -145,6 +145,13 @@ UPDATED_CLUSTERHOST_LOG_FIELDS = [ ] +def _check_roles(roles): + if not roles: + raise exception.InvalidParameter( + 'roles %s is empty' % roles + ) + + @utils.supported_filters(optional_support_keys=SUPPORTED_FIELDS) @database.run_in_session() @user_api.check_user_permission_in_session( @@ -229,6 +236,7 @@ def is_cluster_editable( ADDED_FIELDS, optional_support_keys=OPTIONAL_ADDED_FIELDS ) +@utils.input_validates(name=utils.check_name) @database.run_in_session() @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_CLUSTER @@ -248,6 +256,7 @@ def add_cluster( @utils.supported_filters(optional_support_keys=UPDATED_FIELDS) +@utils.input_validates(name=utils.check_name) @database.run_in_session() @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_CLUSTER @@ -264,6 +273,17 @@ def update_cluster(session, updater, cluster_id, **kwargs): kwargs.get('reinstall_distributed_system', False) ) ) + if 'name' in kwargs: + clustername = kwargs['name'] + cluster_by_name = utils.get_db_object( + session, models.Cluster, False, name=clustername + ) + if cluster_by_name and cluster_by_name.id != cluster.id: + raise exception.InvalidParameter( + 'cluster name %s is already exists in cluster %s' % ( + clustername, cluster_by_name.id + ) + ) return utils.update_db_object(session, cluster, **kwargs) @@ -454,8 +474,9 @@ def del_cluster_config(session, deleter, cluster_id): @utils.supported_filters( ADDED_HOST_FIELDS, - optional_support_keys=UPDATED_HOST_FIELDS + optional_support_keys=(UPDATED_HOST_FIELDS + UPDATED_CLUSTERHOST_FIELDS) ) +@utils.input_validates(name=utils.check_name, roles=_check_roles) def add_clusterhost_internal( session, cluster, exception_when_existing=False, @@ -486,7 +507,7 @@ def add_clusterhost_internal( if host_by_name and host_by_name.id != host.id: raise exception.InvalidParameter( 'host name %s exists in host %s' % ( - hostname, host_by_name.to_dict() + hostname, host_by_name.id ) ) utils.update_db_object( @@ -504,7 +525,7 @@ def add_clusterhost_internal( if host and host.machine_id != machine_id: raise exception.InvalidParameter( 'host name %s exists in host %s' % ( - hostname, host.to_dict() + hostname, host.id ) ) host = utils.add_db_object( @@ -628,6 +649,7 @@ def add_cluster_host( ) +@utils.input_validates(roles=_check_roles) @user_api.check_user_permission_in_session( permission.PERMISSION_UPDATE_CLUSTER_HOSTS ) @@ -704,7 +726,7 @@ def update_clusterhost( roles='patched_roles' ) @utils.supported_filters( - optional_support_keys=UPDATED_CLUSTERHOST_FIELDS + optional_support_keys=PATCHED_CLUSTERHOST_FIELDS ) @database.run_in_session() def patch_cluster_host( @@ -722,7 +744,7 @@ def patch_cluster_host( roles='patched_roles' ) @utils.supported_filters( - optional_support_keys=UPDATED_CLUSTERHOST_FIELDS + optional_support_keys=PATCHED_CLUSTERHOST_FIELDS ) @database.run_in_session() def patch_clusterhost( diff --git a/compass/db/api/host.py b/compass/db/api/host.py index accb85fb..32033d09 100644 --- a/compass/db/api/host.py +++ b/compass/db/api/host.py @@ -76,7 +76,7 @@ ADDED_NETWORK_FIELDS = [ ] OPTIONAL_ADDED_NETWORK_FIELDS = ['is_mgmt', 'is_promiscuous'] UPDATED_NETWORK_FIELDS = [ - 'ip', 'subnet_id', 'subnet', 'is_mgmt', + 'interface', 'ip', 'subnet_id', 'subnet', 'is_mgmt', 'is_promiscuous' ] IGNORED_NETWORK_FIELDS = [ @@ -260,6 +260,7 @@ def validate_host(session, host): @utils.supported_filters(optional_support_keys=UPDATED_FIELDS) +@utils.input_validates(name=utils.check_name) @utils.wrap_to_dict(RESP_FIELDS) def _update_host(session, updater, host_id, **kwargs): """Update a host internal.""" @@ -278,7 +279,7 @@ def _update_host(session, updater, host_id, **kwargs): if host_by_name and host_by_name.id != host.id: raise exception.InvalidParameter( 'hostname %s is already exists in host %s' % ( - hostname, host_by_name.to_dict() + hostname, host_by_name.id ) ) return utils.update_db_object(session, host, **kwargs) @@ -526,6 +527,21 @@ def _add_host_network( host = utils.get_db_object( session, models.Host, id=host_id ) + host_network = utils.get_db_object( + session, models.HostNetwork, False, + host_id=host_id, interface=interface + ) + if ( + host_network and not ( + host_network.host_id == host_id and + host_network.interface == interface + ) + ): + raise exception.InvalidParameter( + 'interface %s exists in host network %s' % ( + interface, host_network.id + ) + ) ip_int = long(netaddr.IPAddress(ip)) host_network = utils.get_db_object( session, models.HostNetwork, False, @@ -539,7 +555,7 @@ def _add_host_network( ): raise exception.InvalidParameter( 'ip %s exists in host network %s' % ( - ip, host_network.to_dict() + ip, host_network.id ) ) is_host_editable(session, host, creator) @@ -611,6 +627,22 @@ def add_host_networks( def _update_host_network( session, updater, host_network, **kwargs ): + if 'interface' in kwargs: + interface = kwargs['interface'] + host_network_by_interface = utils.get_db_object( + session, models.HostNetwork, False, + host_id=host_network.host_id, + interface=interface + ) + if ( + host_network_by_interface and + host_network_by_interface.id != host_network.id + ): + raise exception.InvalidParameter( + 'interface %s exists in host network %s' % ( + interface, host_network_by_interface.id + ) + ) if 'ip' in kwargs: ip = kwargs['ip'] ip_int = long(netaddr.IPAddress(ip)) @@ -621,7 +653,7 @@ def _update_host_network( if host_network_by_ip and host_network_by_ip.id != host_network.id: raise exception.InvalidParameter( 'ip %s exist in host network %s' % ( - ip, host_network_by_ip.to_dict() + ip, host_network_by_ip.id ) ) is_host_editable(session, host_network.host, updater) diff --git a/compass/db/api/utils.py b/compass/db/api/utils.py index 8b66b856..57d683f7 100644 --- a/compass/db/api/utils.py +++ b/compass/db/api/utils.py @@ -577,6 +577,18 @@ def check_mac(mac): ) +NAME_PATTERN = re.compile(r'[a-zA-Z0-9][a-zA-Z0-9_-]*') + + +def check_name(name): + if not NAME_PATTERN.match(name): + raise exception.InvalidParameter( + 'name %s does not match the pattern %s' % ( + name, NAME_PATTERN.pattern + ) + ) + + def _check_ipmi_credentials_ip(ip): check_ip(ip)