コード例 #1
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def get(self, host_id):
        """
        Lists progress details of a specific cluster host
        :param host_id: cluster host ID in db
        """
        progress_result = {}
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if not host.state:
                progress_result = {
                    'id': host_id,
                    'state': 'UNINITIALIZED',
                    'percentage': 0,
                    'message': "Waiting..............",
                    'severity': "INFO",
                }
            else:
                progress_result['id'] = host_id
                progress_result['state'] = host.state.state
                progress_result['percentage'] = host.state.progress
                progress_result['message'] = host.state.message
                progress_result['severity'] = host.state.severity

        logging.info('progress result for %s: %s', host_id, progress_result)
        return util.make_json_response(
            200, {"status": "OK",
                  "progress": progress_result})
コード例 #2
0
def handle_not_allowed_method(error):
    """Handler of MethodNotAllowed Exception"""
    message = {
        "status": "Method Not Allowed",
        "message": "The method is not allowed to use"
    }
    return util.make_json_response(405, message)
コード例 #3
0
ファイル: api.py プロジェクト: yosshy/compass-core
def list_adapters():
    """Lists details of the adapters, optionally filtered by adapter name.

    :param name: the name of the adapter
    """
    endpoint = '/adapters'
    name = request.args.get('name', type=str)
    adapter_list = []
    adapter_res = {}
    with database.session() as session:
        adapters = []
        if name:
            adapters = session.query(Adapter).filter_by(name=name).all()
        else:
            adapters = session.query(Adapter).all()

        for adapter in adapters:
            adapter_res = {}
            adapter_res['name'] = adapter.name
            adapter_res['os'] = adapter.os
            adapter_res['target_system'] = adapter.target_system
            adapter_res['id'] = adapter.id
            adapter_res['link'] = {
                "href": "/".join((endpoint, str(adapter.id))),
                "rel": "self"}
            adapter_list.append(adapter_res)

    return util.make_json_response(
        200, {"status": "OK",
              "adapters": adapter_list})
コード例 #4
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self):
        """
        Return a list of dashboard links
        """
        cluster_id = request.args.get('cluster_id', None)
        logging.info('get cluster links with cluster_id=%s', cluster_id)
        links = {}
        with database.session() as session:
            hosts = session.query(ModelClusterHost)\
                           .filter_by(cluster_id=cluster_id).all()
            if not hosts:
                error_msg = "Cannot find hosts in cluster id=%s" % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            for host in hosts:
                config = host.config
                if ('has_dashboard_roles' in config
                        and config['has_dashboard_roles']):
                    ip = config.get('networking',
                                    {}).get('interfaces',
                                            {}).get('management',
                                                    {}).get('ip', '')
                    roles = config.get('roles', [])
                    for role in roles:
                        links[role] = 'http://%s' % ip

        return util.make_json_response(200, {
            "status": "OK",
            "dashboardlinks": links
        })
コード例 #5
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def get(self, machine_id):
        """
        Lists details of the machine with specific machine id.
        :param machine_id: the machine ID in db
        """
        machine_res = {}
        with database.session() as session:
            machine = session.query(ModelMachine)\
                             .filter_by(id=machine_id)\
                             .first()
            logging.info('machine for id %s: %s', machine_id, machine)

            if not machine:
                error_msg = "Cannot find the machine with id=%s" % machine_id
                logging.error("[/api/machines/{id}]error_msg: %s", error_msg)

                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            machine_res['id'] = machine.id
            machine_res['mac'] = machine.mac
            machine_res['port'] = machine.port
            machine_res['vlan'] = machine.vlan
            if machine.switch:
                machine_res['switch_ip'] = machine.switch.ip
            else:
                machine_res['switch_ip'] = None
            machine_res['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(machine.id)))}

        logging.info('machine info for %s: %s', machine_id, machine_res)
        return util.make_json_response(
            200, {"status": "OK",
                  "machine": machine_res})
コード例 #6
0
ファイル: api.py プロジェクト: yosshy/compass-core
def list_adapter(adapter_id):
    """
    Lists details of the specified adapter.

    :param adapter_id: the unique identifier of the adapter
    """
    endpoint = '/adapters'
    adapter_res = {}
    with database.session() as session:
        adapter = session.query(Adapter).filter_by(id=adapter_id).first()

        if not adapter:
            error_msg = "Adapter id=%s does not exist!" % adapter_id
            return errors.handle_not_exist(
                errors.ObjectDoesNotExist(error_msg))
        adapter_res['name'] = adapter.name
        adapter_res['os'] = adapter.os
        adapter_res['id'] = adapter.id
        adapter_res['target_system'] = adapter.target_system,
        adapter_res['link'] = {
            "href": "/".join((endpoint, str(adapter.id))),
            "rel": "self"}
    return util.make_json_response(
        200, {"status": "OK",
              "adapter": adapter_res})
コード例 #7
0
ファイル: errors.py プロジェクト: SysCompass/compass-core
def handle_invalid_usage(error):
    """Handler of UserInvalidUsage Exception"""

    message = {'status': 'Invalid parameters',
               'message': error.message}

    return util.make_json_response(400, message)
コード例 #8
0
ファイル: errors.py プロジェクト: SysCompass/compass-core
def handle_mssing_input(error):
    """Handler of InputMissingError Exception"""

    message = {'status': 'Insufficient data',
               'message': error.message}

    return util.make_json_response(400, message)
コード例 #9
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def _deploy(cluster_id):
        """Deploy the cluster"""

        deploy_hosts_urls = []
        with database.session() as session:
            cluster_hosts_ids = session.query(ModelClusterHost.id)\
                                       .filter_by(cluster_id=cluster_id).all()
            if not cluster_hosts_ids:
                # No host belongs to this cluster
                error_msg = ('Cannot find any host in cluster id=%s' %
                             cluster_id)
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            for host_id in cluster_hosts_ids:
                progress_url = '/cluster_hosts/%s/progress' % str(host_id)
                deploy_hosts_urls.append(progress_url)

            # Lock cluster hosts and its cluster
            session.query(ModelClusterHost).filter_by(cluster_id=cluster_id)\
                                           .update({'mutable': False})
            session.query(ModelCluster).filter_by(id=cluster_id)\
                                       .update({'mutable': False})

        celery.send_task("compass.tasks.trigger_install", (cluster_id,))
        return util.make_json_response(
            202, {"status": "OK",
                  "deployment": deploy_hosts_urls})
コード例 #10
0
    def get(self, host_id):
        """
        Lists progress details of a specific cluster host
        :param host_id: cluster host ID in db
        """
        progress_result = {}
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if not host.state:
                progress_result = {
                    'id': host_id,
                    'state': 'UNINITIALIZED',
                    'percentage': 0,
                    'message': "Waiting..............",
                    'severity': "INFO",
                }
            else:
                progress_result['id'] = host_id
                progress_result['state'] = host.state.state
                progress_result['percentage'] = host.state.progress
                progress_result['message'] = host.state.message
                progress_result['severity'] = host.state.severity

        logging.info('progress result for %s: %s', host_id, progress_result)
        return util.make_json_response(200, {
            "status": "OK",
            "progress": progress_result
        })
コード例 #11
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def delete(self, host_id, subkey):
        """
        Delete one attribute in configuration of a specific cluster host
        :param host_id: the cluster host ID
        :param subkey: the attribute name in configuration
        """
        available_delete_keys = ['ip', 'role']
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if subkey not in available_delete_keys:
                error_msg = "subkey %s is not supported!" % subkey
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            if not host.mutable:
                error_msg = "The host 'id=%s' is not mutable!" % host_id
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            config = json.loads(host.config_data)
            # Set the subkey's value to ""
            util.update_dict_value(subkey, "", config)
            host.config = config

        return util.make_json_response(
            200, {"status": "OK"})
コード例 #12
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, cluster_id):
        """Lists progress details of a specific cluster

        :param cluster_id: the unique identifier of the cluster
        """
        progress_result = {}
        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(id=cluster_id)\
                                                 .first()
            if not cluster:
                error_msg = "The cluster id=%s does not exist!" % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if not cluster.state:
                progress_result = {
                    'id': cluster_id,
                    'state': 'UNINITIALIZED',
                    'percentage': 0,
                    'message': "Waiting..............",
                    'severity': "INFO"
                }
            else:
                progress_result['id'] = cluster_id
                progress_result['state'] = cluster.state.state
                progress_result['percentage'] = cluster.state.progress
                progress_result['message'] = cluster.state.message
                progress_result['severity'] = cluster.state.severity

        logging.info('progress result for cluster %s: %s', cluster_id,
                     progress_result)
        return util.make_json_response(200, {
            "status": "OK",
            "progress": progress_result
        })
コード例 #13
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def get(self, cluster_id):
        """Lists progress details of a specific cluster

        :param cluster_id: the unique identifier of the cluster
        """
        progress_result = {}
        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(id=cluster_id)\
                                                 .first()
            if not cluster:
                error_msg = "The cluster id=%s does not exist!" % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if not cluster.state:
                progress_result = {
                    'id': cluster_id,
                    'state': 'UNINITIALIZED',
                    'percentage': 0,
                    'message': "Waiting..............",
                    'severity': "INFO"
                }
            else:
                progress_result['id'] = cluster_id
                progress_result['state'] = cluster.state.state
                progress_result['percentage'] = cluster.state.progress
                progress_result['message'] = cluster.state.message
                progress_result['severity'] = cluster.state.severity

        logging.info('progress result for cluster %s: %s',
                     cluster_id, progress_result)
        return util.make_json_response(
            200, {"status": "OK",
                  "progress": progress_result})
コード例 #14
0
ファイル: api.py プロジェクト: jerryz1982/compass
def list_adapter_roles(adapter_id):
    """
    Lists details of all roles of a specific adapter
    :param adapter_id: the adaper ID in db
    """
    roles_list = []
    with database.session() as session:
        adapter_q = session.query(Adapter)\
                           .filter_by(id=adapter_id).first()
        if not adapter_q:
            error_msg = "Adapter id=%s does not exist!" % adapter_id
            return errors.handle_not_exist(
                errors.ObjectDoesNotExist(error_msg))

        roles = session.query(Role, Adapter)\
                       .filter(Adapter.id == adapter_id,
                               Adapter.target_system == Role.target_system)\
                       .all()

        for role, adapter in roles:
            role_res = {}
            role_res['name'] = role.name
            role_res['description'] = role.description
            roles_list.append(role_res)

    return util.make_json_response(
        200, {"status": "OK",
              "roles": roles_list})
コード例 #15
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, switch_id):
        """Lists details of the specified switch.

        :param switch_id: switch ID in db
        """
        switch_res = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(id=switch_id).first()
            logging.info('switch for id %s: %s', switch_id, switch)

            if not switch:
                error_msg = "Cannot find the switch with id=%s" % switch_id
                logging.error("[/switches/{id}]error_msg: %s", error_msg)

                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            switch_res['id'] = switch.id
            switch_res['ip'] = switch.ip
            switch_res['state'] = switch.state
            switch_res['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(switch.id)))
            }

        logging.info('switch info for %s: %s', switch_id, switch_res)
        return util.make_json_response(200, {
            "status": "OK",
            "switch": switch_res
        })
コード例 #16
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def _deploy(cluster_id):
        """Deploy the cluster"""

        deploy_hosts_urls = []
        with database.session() as session:
            cluster_hosts_ids = session.query(ModelClusterHost.id)\
                                       .filter_by(cluster_id=cluster_id).all()
            if not cluster_hosts_ids:
                # No host belongs to this cluster
                error_msg = ('Cannot find any host in cluster id=%s' %
                             cluster_id)
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            for elm in cluster_hosts_ids:
                host_id = str(elm[0])
                progress_url = '/cluster_hosts/%s/progress' % host_id
                deploy_hosts_urls.append(progress_url)

            # Lock cluster hosts and its cluster
            session.query(ModelClusterHost).filter_by(cluster_id=cluster_id)\
                                           .update({'mutable': False})
            session.query(ModelCluster).filter_by(id=cluster_id)\
                                       .update({'mutable': False})

        celery.send_task("compass.tasks.trigger_install", (cluster_id, ))
        return util.make_json_response(202, {
            "status": "OK",
            "deployment": deploy_hosts_urls
        })
コード例 #17
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def _remove_hosts(cluster_id, hosts):
        """Remove existing cluster host from the cluster"""

        removed_hosts = []
        with database.session() as session:
            failed_hosts = []
            for host_id in hosts:
                host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                      .first()

                if not host:
                    failed_hosts.append(host)
                    continue

                host_res = {"id": host_id, "machine_id": host.machine_id}
                removed_hosts.append(host_res)

            if failed_hosts:
                error_msg = 'Cluster hosts do not exist!'
                value = {"failedHosts": failed_hosts}
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg), value)

            filter_clause = []
            for host_id in hosts:
                filter_clause.append('id=%s' % host_id)

            # Delete the requested hosts from database
            session.query(ModelClusterHost).filter(or_(*filter_clause))\
                   .delete(synchronize_session='fetch')

        return util.make_json_response(200, {
            "status": "OK",
            "cluster_hosts": removed_hosts
        })
コード例 #18
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
def list_adapter(adapter_id):
    """
    Lists details of the specified adapter.

    :param adapter_id: the unique identifier of the adapter
    """
    endpoint = '/adapters'
    adapter_res = {}
    with database.session() as session:
        adapter = session.query(Adapter).filter_by(id=adapter_id).first()

        if not adapter:
            error_msg = "Adapter id=%s does not exist!" % adapter_id
            return errors.handle_not_exist(
                errors.ObjectDoesNotExist(error_msg))
        adapter_res['name'] = adapter.name
        adapter_res['os'] = adapter.os
        adapter_res['id'] = adapter.id
        adapter_res['target_system'] = adapter.target_system,
        adapter_res['link'] = {
            "href": "/".join((endpoint, str(adapter.id))),
            "rel": "self"
        }
    return util.make_json_response(200, {
        "status": "OK",
        "adapter": adapter_res
    })
コード例 #19
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def delete(self, host_id, subkey):
        """
        Delete one attribute in configuration of the specified cluster host

        :param host_id: the unique identifier of the host
        :param subkey: the attribute name in configuration
        """
        available_delete_keys = ['ip', 'roles']
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if subkey not in available_delete_keys:
                error_msg = "subkey %s is not supported!" % subkey
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            if not host.mutable:
                error_msg = "The host 'id=%s' is not mutable!" % host_id
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            config = json.loads(host.config_data)
            # Set the subkey's value to ""
            util.update_dict_value(subkey, "", config)
            host.config = config

        return util.make_json_response(200, {"status": "OK"})
コード例 #20
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
def list_adapter_roles(adapter_id):
    """Lists details of all roles of the specified adapter

    :param adapter_id: the unique identifier of the adapter
    """
    roles_list = []
    with database.session() as session:
        adapter_q = session.query(Adapter)\
                           .filter_by(id=adapter_id).first()
        if not adapter_q:
            error_msg = "Adapter id=%s does not exist!" % adapter_id
            return errors.handle_not_exist(
                errors.ObjectDoesNotExist(error_msg))

        roles = session.query(Role, Adapter)\
                       .filter(Adapter.id == adapter_id,
                               Adapter.target_system == Role.target_system)\
                       .all()

        for role, adapter in roles:
            role_res = {}
            role_res['name'] = role.name
            role_res['description'] = role.description
            roles_list.append(role_res)

    return util.make_json_response(200, {"status": "OK", "roles": roles_list})
コード例 #21
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
def list_adapters():
    """Lists details of the adapters, optionally filtered by adapter name.

    :param name: the name of the adapter
    """
    endpoint = '/adapters'
    name = request.args.get('name', type=str)
    adapter_list = []
    adapter_res = {}
    with database.session() as session:
        adapters = []
        if name:
            adapters = session.query(Adapter).filter_by(name=name).all()
        else:
            adapters = session.query(Adapter).all()

        for adapter in adapters:
            adapter_res = {}
            adapter_res['name'] = adapter.name
            adapter_res['os'] = adapter.os
            adapter_res['target_system'] = adapter.target_system
            adapter_res['id'] = adapter.id
            adapter_res['link'] = {
                "href": "/".join((endpoint, str(adapter.id))),
                "rel": "self"
            }
            adapter_list.append(adapter_res)

    return util.make_json_response(200, {
        "status": "OK",
        "adapters": adapter_list
    })
コード例 #22
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def get(self):
        """
        Return a list of dashboard links
        """
        cluster_id = request.args.get('cluster_id', None)
        logging.info('get cluster links with cluster_id=%s', cluster_id)
        links = {}
        with database.session() as session:
            hosts = session.query(ModelClusterHost)\
                           .filter_by(cluster_id=cluster_id).all()
            if not hosts:
                error_msg = "Cannot find hosts in cluster id=%s" % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            for host in hosts:
                config = host.config
                if ('has_dashboard_roles' in config and
                       config['has_dashboard_roles']):
                    ip = config.get(
                        'networking', {}).get(
                        'interfaces', {}).get(
                        'management', {}).get(
                        'ip', '')
                    roles = config.get('roles', [])
                    for role in roles:
                        links[role] = 'http://%s' % ip

        return util.make_json_response(
            200, {"status": "OK",
                  "dashboardlinks": links}
            )
コード例 #23
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def get(self, switch_id):
        """
        Lists details of the switch with the specific switch id.
        :param switch_id: switch ID in db
        """
        switch_res = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(id=switch_id).first()
            logging.info('switch for id %s: %s', switch_id, switch)

            if not switch:
                error_msg = "Cannot find the switch with id=%s" % switch_id
                logging.error("[/switches/{id}]error_msg: %s", error_msg)

                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg)
                    )

            switch_res['id'] = switch.id
            switch_res['state'] = switch.state
            switch_res['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(switch.id)))}

        logging.info('switch info for %s: %s', switch_id, switch_res)
        return util.make_json_response(
            200, {"status": "OK",
                  "switch": switch_res})
コード例 #24
0
def handle_not_allowed_method(error):
    """Handler of MethodNotAllowed Exception."""

    message = {
        "status": "Method Not Allowed",
        "message": "The method is not allowed to use"
    }
    return util.make_json_response(405, message)
コード例 #25
0
ファイル: errors.py プロジェクト: cloudlander/compass-core
def handle_invalid_usage(error):
    """Handler of UserInvalidUsage Exception."""

    message = {
        'status': 'Invalid parameters',
        'message': error.message
    }

    return util.make_json_response(400, message)
コード例 #26
0
ファイル: errors.py プロジェクト: gasopbf1/compasstest
def handle_not_exist(error, failed_objs=None):
    """Handler of ObjectDoesNotExist Exception"""

    message = {'status': 'Not Found', 'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return util.make_json_response(404, message)
コード例 #27
0
ファイル: errors.py プロジェクト: cloudlander/compass-core
def handle_mssing_input(error):
    """Handler of InputMissingError Exception."""

    message = {
        'status': 'Insufficient data',
        'message': error.message
    }

    return util.make_json_response(400, message)
コード例 #28
0
ファイル: errors.py プロジェクト: gasopbf1/compasstest
def handle_duplicate_object(error, failed_objs=None):
    """Handler of ObjectDuplicateError Exception"""

    message = {'status': 'Conflict Error', 'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return util.make_json_response(409, message)
コード例 #29
0
ファイル: errors.py プロジェクト: SysCompass/compass-core
def handle_duplicate_object(error, failed_objs=None):
    """Handler of ObjectDuplicateError Exception"""

    message = {'status': 'Conflict Error',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return util.make_json_response(409, message)
コード例 #30
0
ファイル: errors.py プロジェクト: SysCompass/compass-core
def handle_not_exist(error, failed_objs=None):
    """Handler of ObjectDoesNotExist Exception"""

    message = {'status': 'Not Found',
               'message': error.message}

    if failed_objs and isinstance(failed_objs, dict):
        message.update(failed_objs)

    return util.make_json_response(404, message)
コード例 #31
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def put(self, cluster_id, resource):
        """
        Update the resource information of the specified cluster in database

        :param cluster_id: the unique identifier of the cluster
        :param resource: resource name(security, networking, partition)
        """
        resources = {
            self.SECURITY: {
                'validator': 'is_valid_security_config',
                'column': 'security_config'
            },
            self.NETWORKING: {
                'validator': 'is_valid_networking_config',
                'column': 'networking_config'
            },
            self.PARTITION: {
                'validator': 'is_valid_partition_config',
                'column': 'partition_config'
            },
        }
        request_data = json.loads(request.data)
        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(id=cluster_id)\
                                                 .first()

            if not cluster:
                error_msg = 'You are trying to update a non-existing cluster!'
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))
            if resource not in request_data:
                error_msg = "Invalid resource name '%s'" % resource
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            value = request_data[resource]

            if resource not in resources.keys():
                error_msg = "Invalid resource name '%s'" % resource
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            validate_func = resources[resource]['validator']
            module = globals()['util']
            is_valid, msg = getattr(module, validate_func)(value)

            if is_valid:
                column = resources[resource]['column']
                session.query(ModelCluster).filter_by(id=cluster_id)\
                       .update({column: json.dumps(value)})
            else:
                return errors.handle_mssing_input(
                    errors.InputMissingError(msg))

        return util.make_json_response(200, {"status": "OK"})
コード例 #32
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def post(self):
        """
        Insert switch IP and the credential to db. Invoke a task to poll
        switch at the same time.

        :param ip: switch IP address
        :param credential: a dict for accessing the switch
        """
        ip_addr = None
        credential = None
        logging.debug('post switch request from curl is %s', request.data)
        json_data = json.loads(request.data)
        ip_addr = json_data['switch']['ip']
        credential = json_data['switch']['credential']

        logging.info('post switch ip_addr=%s credential=%s(%s)', ip_addr,
                     credential, type(credential))

        if not util.is_valid_ip(ip_addr):
            error_msg = "Invalid IP address format!"
            return errors.handle_invalid_usage(
                errors.UserInvalidUsage(error_msg))

        new_switch = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(ip=ip_addr).first()
            logging.info('switch for ip %s: %s', ip_addr, switch)

            if switch:
                error_msg = "IP address '%s' already exists" % ip_addr
                value = {'failedSwitch': switch.id}
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value)

            switch = ModelSwitch(ip=ip_addr)
            switch.credential = credential
            session.add(switch)
            session.flush()
            new_switch['id'] = switch.id
            new_switch['ip'] = switch.ip
            new_switch['state'] = switch.state
            link = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(switch.id)))
            }
            new_switch['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr, ))
        logging.info('new switch added: %s', new_switch)
        return util.make_json_response(202, {
            "status": "accepted",
            "switch": new_switch
        })
コード例 #33
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def post(self):
        """
        Insert switch IP and the credential to db. Invoke a task to poll
        switch at the same time.

        :param ip: switch IP address
        :param credential: a dict for accessing the switch
        """
        ip_addr = None
        credential = None
        logging.debug('post switch request from curl is %s', request.data)
        json_data = json.loads(request.data)
        ip_addr = json_data['switch']['ip']
        credential = json_data['switch']['credential']

        logging.info('post switch ip_addr=%s credential=%s(%s)',
                     ip_addr, credential, type(credential))

        if not util.is_valid_ip(ip_addr):
            error_msg = "Invalid IP address format!"
            return errors.handle_invalid_usage(
                errors.UserInvalidUsage(error_msg)
                )

        new_switch = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(ip=ip_addr).first()
            logging.info('switch for ip %s: %s', ip_addr, switch)

            if switch:
                error_msg = "IP address '%s' already exists" % ip_addr
                value = {'failedSwitch': switch.id}
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value
                )

            switch = ModelSwitch(ip=ip_addr)
            switch.credential = credential
            session.add(switch)
            session.flush()
            new_switch['id'] = switch.id
            new_switch['ip'] = switch.ip
            new_switch['state'] = switch.state
            link = {'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(switch.id)))}
            new_switch['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr,))
        logging.info('new switch added: %s', new_switch)
        return util.make_json_response(
            202, {"status": "accepted",
                  "switch":  new_switch}
            )
コード例 #34
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def put(self, host_id):
        """
        Update configuration of the specified cluster host

        :param host_id: the unique identifier of the host
        """
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))
            logging.debug("cluster config put request.data %s", request.data)
            request_data = json.loads(request.data)
            if not request_data:
                error_msg = "request data is %s" % request_data
                return errors.handle_mssing_input(
                    errors.InputMissingError(error_msg))

            if not host.mutable:
                error_msg = "The host 'id=%s' is not mutable!" % host_id
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            #Valid if keywords in request_data are all correct
            if 'hostname' in request_data:
                hostname = request_data['hostname']
                cluster_id = host.cluster_id
                test_host = session.query(ModelClusterHost)\
                                   .filter_by(cluster_id=cluster_id,
                                              hostname=hostname).first()
                if test_host:
                    error_msg = ("Hostname '%s' has been used for other host "
                                 "in the cluster, cluster ID is %s!"
                                 % (hostname, cluster_id))

                    return errors.handle_invalid_usage(
                        errors.UserInvalidUsage(error_msg))

                session.query(ModelClusterHost).filter_by(id=host_id)\
                       .update({"hostname": request_data['hostname']})
                del request_data['hostname']

            try:
                util.valid_host_config(request_data)
            except errors.UserInvalidUsage as exc:
                return errors.handle_invalid_usage(exc)

            host.config = request_data

            return util.make_json_response(
                200, {"status": "OK"})
コード例 #35
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def _add_hosts(cluster_id, hosts):
        """Add cluster host(s) to the cluster by cluster_id"""

        cluseter_hosts = []
        available_machines = []
        failed_machines = []
        with database.session() as session:
            failed_machines = []
            for host in hosts:
                # Check if machine exists
                machine = session.query(ModelMachine).filter_by(id=host)\
                                                     .first()
                if not machine:
                    error_msg = "Machine id=%s does not exist!" % host
                    return errors.handle_not_exist(
                        errors.ObjectDoesNotExist(error_msg)
                        )
                clusterhost = session.query(ModelClusterHost)\
                                     .filter_by(machine_id=host)\
                                     .first()
                if clusterhost:
                    # Machine is already used
                    failed_machines.append(clusterhost.machine_id)
                    continue
                # Add the available machine to available_machines list
                available_machines.append(machine)

            if failed_machines:
                value = {
                    'failedMachines': failed_machines
                }
                error_msg = "Conflict!"
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value
                    )
            for machine, host in zip(available_machines, hosts):
                host = ModelClusterHost(cluster_id=cluster_id,
                                        machine_id=machine.id)
                session.add(host)
                session.flush()
                cluster_res = {}
                cluster_res['id'] = host.id
                cluster_res['machine_id'] = machine.id
                cluseter_hosts.append(cluster_res)

        logging.info('cluster_hosts result is %s', cluseter_hosts)
        return util.make_json_response(
            200, {
                "status": "OK",
                "clusterHosts": cluseter_hosts
                }
            )
コード例 #36
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def put(self, cluster_id, resource):
        """
        Update the resource information of the specified cluster in database

        :param cluster_id: the unique identifier of the cluster
        :param resource: resource name(security, networking, partition)
        """
        resources = {
            self.SECURITY: {'validator': 'is_valid_security_config',
                            'column': 'security_config'},
            self.NETWORKING: {'validator': 'is_valid_networking_config',
                              'column': 'networking_config'},
            self.PARTITION: {'validator': 'is_valid_partition_config',
                             'column': 'partition_config'},
        }
        request_data = json.loads(request.data)
        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(id=cluster_id)\
                                                 .first()

            if not cluster:
                error_msg = 'You are trying to update a non-existing cluster!'
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg)
                    )
            if resource not in request_data:
                error_msg = "Invalid resource name '%s'" % resource
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            value = request_data[resource]

            if resource not in resources.keys():
                error_msg = "Invalid resource name '%s'" % resource
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            validate_func = resources[resource]['validator']
            module = globals()['util']
            is_valid, msg = getattr(module, validate_func)(value)

            if is_valid:
                column = resources[resource]['column']
                session.query(ModelCluster).filter_by(id=cluster_id)\
                       .update({column: json.dumps(value)})
            else:
                return errors.handle_mssing_input(
                    errors.InputMissingError(msg))

        return util.make_json_response(
            200, {"status": "OK"})
コード例 #37
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def _deploy(cluster_id, hosts):
        """Deploy the cluster"""

        deploy_hosts_info = []
        deploy_cluster_info = {}
        with database.session() as session:
            if not hosts:
                # Deploy all hosts in the cluster
                cluster_hosts = session.query(ModelClusterHost)\
                                       .filter_by(cluster_id=cluster_id).all()

                if not cluster_hosts:
                    # No host belongs to this cluster
                    error_msg = ('Cannot find any host in cluster id=%s' %
                                 cluster_id)
                    return errors.handle_not_exist(
                        errors.ObjectDoesNotExist(error_msg))

                for host in cluster_hosts:
                    if not host.mutable:
                        # The host is not allowed to modified
                        error_msg = ("The host id=%s is not allowed to be "
                                     "modified now!") % host.id
                        return errors.UserInvalidUsage(
                            errors.UserInvalidUsage(error_msg))

                    hosts.append(host.id)

            deploy_cluster_info["cluster_id"] = int(cluster_id)
            deploy_cluster_info["url"] = '/clusters/%s/progress' % cluster_id

            for host_id in hosts:
                host_info = {}
                progress_url = '/cluster_hosts/%s/progress' % host_id
                host_info["host_id"] = host_id
                host_info["url"] = progress_url
                deploy_hosts_info.append(host_info)

            # Lock cluster hosts and its cluster
            session.query(ModelClusterHost).filter_by(cluster_id=cluster_id)\
                                           .update({'mutable': False})
            session.query(ModelCluster).filter_by(id=cluster_id)\
                                       .update({'mutable': False})

        celery.send_task("compass.tasks.trigger_install", (cluster_id, hosts))
        return util.make_json_response(
            202, {"status": "accepted",
                  "deployment": {
                      "cluster": deploy_cluster_info,
                      "hosts": deploy_hosts_info
                      }})
コード例 #38
0
ファイル: api.py プロジェクト: yosshy/compass-core
def list_clusters():
    """Lists the details of all clusters"""
    endpoint = '/clusters'
    state = request.args.get('state', None, type=str)
    results = []
    with database.session() as session:
        clusters = []
        if not state:
            # Get all clusters
            clusters = session.query(ModelCluster).all()

        elif state == 'undeployed':
            clusters_state = session.query(ClusterState.id).all()
            cluster_ids = [t[0] for t in clusters_state]
            # The cluster has not been deployed yet
            clusters = session.query(ModelCluster)\
                              .filter(~ModelCluster.id.in_(cluster_ids))\
                              .all()
        elif state == 'installing':
            # The deployment of this cluster is in progress.
            clusters = session.query(ModelCluster)\
                              .filter(ModelCluster.id == ClusterState.id,
                                      or_(ClusterState.state == 'INSTALLING',
                                          ClusterState.state == 'UNINITIALIZED'))\
                              .all()
        elif state == 'failed':
            # The deployment of this cluster is failed.
            clusters = session.query(ModelCluster)\
                              .filter(ModelCluster.id == ClusterState.id,
                                      ClusterState.state == 'ERROR')\
                              .all()
        elif state == 'successful':
            clusters = session.query(ModelCluster)\
                              .filter(ModelCluster.id == ClusterState.id,
                                      ClusterState.state == 'READY')\
                              .all()

        if clusters:
            for cluster in clusters:
                cluster_res = {}
                cluster_res['clusterName'] = cluster.name
                cluster_res['id'] = cluster.id
                cluster_res['link'] = {
                    "href": "/".join((endpoint, str(cluster.id))),
                    "rel": "self"}
                results.append(cluster_res)

    return util.make_json_response(
        200, {"status": "OK",
              "clusters": results})
コード例 #39
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
def list_clusterhosts():
    """
    Lists details of all cluster hosts, optionally filtered by some conditions.

    :param hostname: the name of the host
    :param clstername: the name of the cluster
    """
    endpoint = '/clusterhosts'
    key_hostname = 'hostname'
    key_clustername = 'clustername'

    hosts_list = []
    hostname = request.args.get(key_hostname, None, type=str)
    clustername = request.args.get(key_clustername, None, type=str)
    with database.session() as session:
        hosts = None
        if hostname and clustername:
            hosts = session.query(ModelClusterHost).join(ModelCluster)\
                           .filter(ModelClusterHost.hostname == hostname,
                                   ModelCluster.name == clustername)\
                           .all()

        elif hostname:
            hosts = session.query(ModelClusterHost)\
                           .filter_by(hostname=hostname).all()
        elif clustername:
            cluster = session.query(ModelCluster)\
                             .filter_by(name=clustername).first()
            if cluster:
                hosts = cluster.hosts
        else:
            hosts = session.query(ModelClusterHost).all()

        if hosts:
            for host in hosts:
                host_res = {}
                host_res['hostname'] = host.hostname
                host_res['mutable'] = host.mutable
                host_res['id'] = host.id
                host_res['link'] = {
                    "href": '/'.join((endpoint, str(host.id))),
                    "rel": "self"
                }
                hosts_list.append(host_res)

        return util.make_json_response(200, {
            "status": "OK",
            "cluster_hosts": hosts_list
        })
コード例 #40
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def get(self, cluster_id, resource=None):
        """
        Lists details of the specified cluster if resource is not specified.
        Otherwise, lists details of the resource of this cluster

        :param cluster_id: the unique identifier of the cluster
        :param resource: the resource name(security, networking, partition)
        """
        cluster_resp = {}
        resp = {}
        with database.session() as session:
            cluster = session.query(ModelCluster)\
                             .filter_by(id=cluster_id)\
                             .first()
            logging.debug('cluster is %s', cluster)
            if not cluster:
                error_msg = 'Cannot found the cluster with id=%s' % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg)
                    )

            if resource:
                # List resource details
                if resource == self.SECURITY:
                    cluster_resp = cluster.security
                elif resource == self.NETWORKING:
                    cluster_resp = cluster.networking
                elif resource == self.PARTITION:
                    cluster_resp = cluster.partition
                else:
                    error_msg = "Invalid resource name '%s'!" % resource
                    return errors.handle_invalid_usage(
                        errors.UserInvalidUsage(error_msg)
                    )
                resp = {"status": "OK",
                        resource: cluster_resp}

            else:
                cluster_resp['clusterName'] = cluster.name
                cluster_resp['link'] = {
                    'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(cluster.id)))
                }
                cluster_resp['id'] = cluster.id
                resp = {"status": "OK",
                        "cluster": cluster_resp}

        logging.info('get cluster result is %s', cluster_resp)
        return util.make_json_response(200, resp)
コード例 #41
0
ファイル: api.py プロジェクト: yosshy/compass-core
def list_clusterhosts():
    """
    Lists details of all cluster hosts, optionally filtered by some conditions.

    :param hostname: the name of the host
    :param clstername: the name of the cluster
    """
    endpoint = '/clusterhosts'
    key_hostname = 'hostname'
    key_clustername = 'clustername'

    hosts_list = []
    hostname = request.args.get(key_hostname, None, type=str)
    clustername = request.args.get(key_clustername, None, type=str)
    with database.session() as session:
        hosts = None
        if hostname and clustername:
            hosts = session.query(ModelClusterHost).join(ModelCluster)\
                           .filter(ModelClusterHost.hostname == hostname,
                                   ModelCluster.name == clustername)\
                           .all()

        elif hostname:
            hosts = session.query(ModelClusterHost)\
                           .filter_by(hostname=hostname).all()
        elif clustername:
            cluster = session.query(ModelCluster)\
                             .filter_by(name=clustername).first()
            if cluster:
                hosts = cluster.hosts
        else:
            hosts = session.query(ModelClusterHost).all()

        if hosts:
            for host in hosts:
                host_res = {}
                host_res['hostname'] = host.hostname
                host_res['mutable'] = host.mutable
                host_res['id'] = host.id
                host_res['switch_ip'] = host.machine.switch.ip
                host_res['link'] = {
                    "href": '/'.join((endpoint, str(host.id))),
                    "rel": "self"}
                hosts_list.append(host_res)

        return util.make_json_response(
            200, {"status": "OK",
                  "cluster_hosts": hosts_list})
コード例 #42
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def _add_hosts(cluster_id, hosts):
        """Add cluster host(s) to the cluster by cluster_id"""

        cluseter_hosts = []
        available_machines = []
        failed_machines = []
        with database.session() as session:
            failed_machines = []
            for host in hosts:
                # Check if machine exists
                machine = session.query(ModelMachine).filter_by(id=host)\
                                                     .first()
                if not machine:
                    error_msg = "Machine id=%s does not exist!" % host
                    return errors.handle_not_exist(
                        errors.ObjectDoesNotExist(error_msg))
                clusterhost = session.query(ModelClusterHost)\
                                     .filter_by(machine_id=host)\
                                     .first()
                if clusterhost:
                    # Machine is already used
                    failed_machines.append(clusterhost.machine_id)
                    continue
                # Add the available machine to available_machines list
                available_machines.append(machine)

            if failed_machines:
                value = {'failedMachines': failed_machines}
                error_msg = "Conflict!"
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg), value)
            for machine, host in zip(available_machines, hosts):
                host = ModelClusterHost(cluster_id=cluster_id,
                                        machine_id=machine.id)
                session.add(host)
                session.flush()
                cluster_res = {}
                cluster_res['id'] = host.id
                cluster_res['machine_id'] = machine.id
                cluseter_hosts.append(cluster_res)

        logging.info('cluster_hosts result is %s', cluseter_hosts)
        return util.make_json_response(200, {
            "status": "OK",
            "cluster_hosts": cluseter_hosts
        })
コード例 #43
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def put(self, switch_id):
        """Update an existing switch information.

        :param switch_id: the unqiue identifier of the switch
        """
        switch = None
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(id=switch_id).first()
            logging.info('PUT switch id is %s: %s', switch_id, switch)

            if not switch:
                # No switch is found.
                error_msg = 'Cannot update a non-existing switch!'
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

        credential = None
        logging.debug('PUT a switch request from curl is %s', request.data)
        json_data = json.loads(request.data)
        credential = json_data['switch']['credential']

        logging.info('PUT switch id=%s credential=%s(%s)', switch_id,
                     credential, type(credential))
        ip_addr = None
        switch_res = {}
        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(id=switch_id).first()
            switch.credential = credential
            switch.state = "not_reached"

            ip_addr = switch.ip
            switch_res['id'] = switch.id
            switch_res['ip'] = switch.ip
            switch_res['state'] = switch.state
            link = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(switch.id)))
            }
            switch_res['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr, ))
        return util.make_json_response(202, {
            "status": "accepted",
            "switch": switch_res
        })
コード例 #44
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, cluster_id, resource=None):
        """
        Lists details of the specified cluster if resource is not specified.
        Otherwise, lists details of the resource of this cluster

        :param cluster_id: the unique identifier of the cluster
        :param resource: the resource name(security, networking, partition)
        """
        cluster_resp = {}
        resp = {}
        with database.session() as session:
            cluster = session.query(ModelCluster)\
                             .filter_by(id=cluster_id)\
                             .first()
            logging.debug('cluster is %s', cluster)
            if not cluster:
                error_msg = 'Cannot found the cluster with id=%s' % cluster_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            if resource:
                # List resource details
                if resource == self.SECURITY:
                    cluster_resp = cluster.security
                elif resource == self.NETWORKING:
                    cluster_resp = cluster.networking
                elif resource == self.PARTITION:
                    cluster_resp = cluster.partition
                else:
                    error_msg = "Invalid resource name '%s'!" % resource
                    return errors.handle_invalid_usage(
                        errors.UserInvalidUsage(error_msg))
                resp = {"status": "OK", resource: cluster_resp}

            else:
                cluster_resp['clusterName'] = cluster.name
                cluster_resp['link'] = {
                    'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(cluster.id)))
                }
                cluster_resp['id'] = cluster.id
                resp = {"status": "OK", "cluster": cluster_resp}

        logging.info('get cluster result is %s', cluster_resp)
        return util.make_json_response(200, resp)
コード例 #45
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def put(self, switch_id):
        """Update an existing switch information.

        :param switch_id: the unqiue identifier of the switch
        """
        switch = None
        credential = None
        logging.debug('PUT a switch request from curl is %s', request.data)

        ip_addr = None
        switch_res = {}

        with database.session() as session:
            switch = session.query(ModelSwitch).filter_by(id=switch_id).first()
            logging.info('PUT switch id is %s: %s', switch_id, switch)

            if not switch:
                # No switch is found.
                error_msg = 'Cannot update a non-existing switch!'
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            json_data = json.loads(request.data)
            credential = json_data['switch']['credential']

            logging.info('PUT switch id=%s credential=%s(%s)',
                         switch_id, credential, type(credential))

            switch.credential = credential
            switch.state = "repolling"
            switch.err_msg = ""

            ip_addr = switch.ip
            switch_res['id'] = switch.id
            switch_res['ip'] = switch.ip
            switch_res['state'] = switch.state
            link = {'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(switch.id)))}
            switch_res['link'] = link

        celery.send_task("compass.tasks.pollswitch", (ip_addr,))
        return util.make_json_response(
            202, {"status": "accepted",
                  "switch": switch_res})
コード例 #46
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def _remove_hosts(cluster_id, hosts):
        """Remove existing cluster host from the cluster"""

        removed_hosts = []
        with database.session() as session:
            failed_hosts = []
            for host_id in hosts:
                host = session.query(ModelClusterHost)\
                              .filter_by(id=host_id, cluster_id=cluster_id)\
                              .first()

                if not host:
                    failed_hosts.append(host_id)
                    continue

                host_res = {
                    "id": host_id,
                    "machine_id": host.machine_id
                }
                removed_hosts.append(host_res)

            if failed_hosts:
                error_msg = 'Hosts do not exist! Or not in this cluster'
                value = {
                    "failedHosts": failed_hosts
                }
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg), value
                    )

            filter_clause = []
            for host_id in hosts:
                filter_clause.append('id=%s' % host_id)

            # Delete the requested hosts from database
            session.query(ModelClusterHost).filter(or_(*filter_clause))\
                   .delete(synchronize_session='fetch')

        return util.make_json_response(
            200, {
                "status": "OK",
                "cluster_hosts": removed_hosts
                }
            )
コード例 #47
0
ファイル: api.py プロジェクト: yosshy/compass-core
    def post(self):
        """Create a new cluster.

        :param name: the name of the cluster
        :param adapter_id: the unique identifier of the adapter
        """
        request_data = None
        request_data = json.loads(request.data)
        cluster_name = request_data['cluster']['name']
        adapter_id = request_data['cluster']['adapter_id']
        cluster_resp = {}
        cluster = None

        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(name=cluster_name)\
                                                 .first()
            if cluster:
                error_msg = "Cluster name '%s' already exists!" % cluster.name
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg))

            adapter = session.query(Adapter).filter_by(id=adapter_id).first()
            if not adapter:
                error_msg = "No adapter id=%s can be found!" % adapter_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            # Create a new cluster in database
            cluster = ModelCluster(name=cluster_name, adapter_id=adapter_id)
            session.add(cluster)
            session.flush()
            cluster_resp['id'] = cluster.id
            cluster_resp['name'] = cluster.name
            cluster_resp['adapter_id'] = cluster.adapter_id
            cluster_resp['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(cluster.id)))
            }

        return util.make_json_response(
            200, {"status": "OK",
                  "cluster": cluster_resp}
            )
コード例 #48
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
def list_clusters():
    """Lists the details of all clusters"""
    endpoint = '/clusters'
    results = []
    with database.session() as session:
        clusters = session.query(ModelCluster).all()

        if clusters:
            for cluster in clusters:
                cluster_res = {}
                cluster_res['clusterName'] = cluster.name
                cluster_res['id'] = cluster.id
                cluster_res['link'] = {
                    "href": "/".join((endpoint, str(cluster.id))),
                    "rel": "self"
                }
                results.append(cluster_res)

    return util.make_json_response(200, {"status": "OK", "clusters": results})
コード例 #49
0
ファイル: api.py プロジェクト: jerryz1982/compass
def list_clusters():
    """Lists the details of all clusters"""
    endpoint = '/clusters'
    results = []
    with database.session() as session:
        clusters = session.query(ModelCluster).all()

        if clusters:
            for cluster in clusters:
                cluster_res = {}
                cluster_res['clusterName'] = cluster.name
                cluster_res['id'] = cluster.id
                cluster_res['link'] = {
                    "href": "/".join((endpoint, str(cluster.id))),
                    "rel": "self"}
                results.append(cluster_res)

    return util.make_json_response(
        200, {"status": "OK",
              "clusters": results})
コード例 #50
0
ファイル: api.py プロジェクト: jerryz1982/compass
    def get(self, host_id):
        """
        Lists configuration details of a specific cluster host
        :param host_id: the cluster host ID
        """
        config_res = {}
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                # The host does not exist.
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            config_res = host.config

        logging.debug("The config of host id=%s is %s", host_id, config_res)
        return util.make_json_response(
            200, {"status": "OK",
                  "config": config_res})
コード例 #51
0
ファイル: api.py プロジェクト: jerryz1982/compass
def list_adapters():
    """
    Lists details of the adapter with a specific name
    """
    endpoint = '/adapters'
    name = request.args.get('name')
    adapter_res = {}
    with database.session() as session:
        adapter = session.query(Adapter).filter_by(name=name).first()

        if adapter:
            adapter_res['name'] = adapter.name
            adapter_res['os'] = adapter.os
            adapter_res['target_system'] = adapter.target_system
            adapter_res['id'] = adapter.id
            adapter_res['link'] = {
                "href": "/".join((endpoint, str(adapter.id))),
                "rel": "self"}

    return util.make_json_response(
        200, {"status": "OK",
              "adapter": adapter_res})
コード例 #52
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, host_id):
        """Lists configuration details of the specified cluster host

        :param host_id: the unique identifier of the host
        """
        config_res = {}
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                # The host does not exist.
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            config_res = host.config

        logging.debug("The config of host id=%s is %s", host_id, config_res)
        return util.make_json_response(200, {
            "status": "OK",
            "config": config_res
        })
コード例 #53
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def put(self, host_id):
        """
        Update configuration of the specified cluster host

        :param host_id: the unique identifier of the host
        """
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))
            logging.debug("cluster config put request.data %s", request.data)
            request_data = json.loads(request.data)
            if not request_data:
                error_msg = "request data is %s" % request_data
                return errors.handle_mssing_input(
                    errors.InputMissingError(error_msg))

            if not host.mutable:
                error_msg = "The host 'id=%s' is not mutable!" % host_id
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            #Valid if keywords in request_data are all correct
            if 'hostname' in request_data:
                session.query(ModelClusterHost).filter_by(id=host_id)\
                       .update({"hostname": request_data['hostname']})
                del request_data['hostname']

            try:
                util.valid_host_config(request_data)
            except errors.UserInvalidUsage as exc:
                return errors.handle_invalid_usage(exc)

            host.config = request_data

            return util.make_json_response(200, {"status": "OK"})
コード例 #54
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, machine_id):
        """
        Lists details of the specified machine.

        :param machine_id: the unique identifier of the machine
        """
        machine_res = {}
        with database.session() as session:
            machine = session.query(ModelMachine)\
                             .filter_by(id=machine_id)\
                             .first()
            logging.info('machine for id %s: %s', machine_id, machine)

            if not machine:
                error_msg = "Cannot find the machine with id=%s" % machine_id
                logging.error("[/api/machines/{id}]error_msg: %s", error_msg)

                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))

            machine_res['id'] = machine.id
            machine_res['mac'] = machine.mac
            machine_res['port'] = machine.port
            machine_res['vlan'] = machine.vlan
            if machine.switch:
                machine_res['switch_ip'] = machine.switch.ip
            else:
                machine_res['switch_ip'] = None
            machine_res['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(machine.id)))
            }

        logging.info('machine info for %s: %s', machine_id, machine_res)
        return util.make_json_response(200, {
            "status": "OK",
            "machine": machine_res
        })
コード例 #55
0
ファイル: api.py プロジェクト: jerryz1982/compass
 def get(self, host_id):
     """
     Lists details of a specific cluster host
     :param host_id: cluster host ID in db
     """
     host_res = {}
     with database.session() as session:
         host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                               .first()
         if not host:
             error_msg = "The host id=%s does not exist!" % host_id
             return errors.handle_not_exist(
                 errors.ObjectDoesNotExist(error_msg))
         host_res['hostname'] = host.hostname
         host_res['mutable'] = host.mutable
         host_res['id'] = host.id
         host_res['link'] = {
             "href": '/'.join((self.ENDPOINT, str(host.id))),
             "rel": "self"
         }
     return util.make_json_response(
         200, {"status": "OK",
               "cluster_host": host_res})
コード例 #56
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def post(self):
        """Create a new cluster.

        :param name: the name of the cluster
        :param adapter_id: the unique identifier of the adapter
        """
        request_data = None
        request_data = json.loads(request.data)
        cluster_name = request_data['cluster']['name']
        adapter_id = request_data['cluster']['adapter_id']
        cluster_resp = {}
        cluster = None

        with database.session() as session:
            cluster = session.query(ModelCluster).filter_by(name=cluster_name)\
                                                 .first()
            if cluster:
                error_msg = "Cluster name '%s' already exists!" % cluster.name
                return errors.handle_duplicate_object(
                    errors.ObjectDuplicateError(error_msg))

            # Create a new cluster in database
            cluster = ModelCluster(name=cluster_name, adapter_id=adapter_id)
            session.add(cluster)
            session.flush()
            cluster_resp['id'] = cluster.id
            cluster_resp['name'] = cluster.name
            cluster_resp['adapter_id'] = cluster.adapter_id
            cluster_resp['link'] = {
                'rel': 'self',
                'href': '/'.join((self.ENDPOINT, str(cluster.id)))
            }

        return util.make_json_response(200, {
            "status": "OK",
            "cluster": cluster_resp
        })
コード例 #57
0
def list_adapters():
    """
    Lists details of the adapter with a specific name
    """
    endpoint = '/adapters'
    name = request.args.get('name')
    adapter_res = {}
    with database.session() as session:
        adapter = session.query(Adapter).filter_by(name=name).first()

        if adapter:
            adapter_res['name'] = adapter.name
            adapter_res['os'] = adapter.os
            adapter_res['target_system'] = adapter.target_system
            adapter_res['id'] = adapter.id
            adapter_res['link'] = {
                "href": "/".join((endpoint, str(adapter.id))),
                "rel": "self"
            }

    return util.make_json_response(200, {
        "status": "OK",
        "adapter": adapter_res
    })
コード例 #58
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self, host_id):
        """Lists details of the specified cluster host

        :param host_id: the unique identifier of the host
        """
        host_res = {}
        with database.session() as session:
            host = session.query(ModelClusterHost).filter_by(id=host_id)\
                                                  .first()
            if not host:
                error_msg = "The host id=%s does not exist!" % host_id
                return errors.handle_not_exist(
                    errors.ObjectDoesNotExist(error_msg))
            host_res['hostname'] = host.hostname
            host_res['mutable'] = host.mutable
            host_res['id'] = host.id
            host_res['link'] = {
                "href": '/'.join((self.ENDPOINT, str(host.id))),
                "rel": "self"
            }
        return util.make_json_response(200, {
            "status": "OK",
            "cluster_host": host_res
        })
コード例 #59
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self):
        """
        List details of all switches, optionally filtered by some conditions.
        Note: switchIp and swtichIpNetwork cannot be combined to use.

        :param switchIp: switch IP address
        :param switchIpNetwork: switch IP network
        :param limit: the number of records excepted to return
        """
        qkeys = request.args.keys()
        logging.info('SwitchList query strings : %s', qkeys)
        switch_list = []

        with database.session() as session:
            switches = []
            switch_ips = request.args.getlist(self.SWITCHIP)
            switch_ip_network = request.args.get(self.SWITCHIPNETWORK,
                                                 type=str)
            limit = request.args.get(self.LIMIT, 0, type=int)

            if switch_ips and switch_ip_network:
                error_msg = 'switchIp and switchIpNetwork cannot be combined!'
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            if limit < 0:
                error_msg = "limit cannot be less than 1!"
                return errors.handle_invalid_usage(
                    errors.UserInvalidUsage(error_msg))

            if switch_ips:
                for ip_addr in switch_ips:
                    ip_addr = str(ip_addr)
                    if not util.is_valid_ip(ip_addr):
                        error_msg = 'SwitchIp format is incorrect!'
                        return errors.handle_invalid_usage(
                            errors.UserInvalidUsage(error_msg))
                    switch = session.query(ModelSwitch).filter_by(ip=ip_addr)\
                                                       .first()
                    if switch:
                        switches.append(switch)
                        logging.info('[SwitchList][get] ip %s', ip_addr)

            elif switch_ip_network:
                # query all switches which belong to the same network
                if not util.is_valid_ipnetowrk(switch_ip_network):
                    error_msg = 'SwitchIpNetwork format is incorrect!'
                    return errors.handle_invalid_usage(
                        errors.UserInvalidUsage(error_msg))

                def get_queried_ip_prefix(network, prefix):
                    """ Get Ip prefex as pattern used to query switches.
                        Switches' Ip addresses need to match this pattern.
                    """
                    count = int(prefix / 8)
                    if count == 0:
                        count = 1
                    return network.rsplit('.', count)[0] + '.'

                from netaddr import IPNetwork, IPAddress

                ip_network = IPNetwork(switch_ip_network)
                ip_filter = get_queried_ip_prefix(str(ip_network.network),
                                                  ip_network.prefixlen)

                logging.info('ip_filter is %s', ip_filter)
                result_set = session.query(ModelSwitch).filter(
                    ModelSwitch.ip.startswith(ip_filter)).all()

                for switch in result_set:
                    ip_addr = str(switch.ip)
                    if IPAddress(ip_addr) in ip_network:
                        switches.append(switch)
                        logging.info('[SwitchList][get] ip %s', ip_addr)

                if limit and len(switches) > limit:
                    switches = switches[:limit]

            elif limit and not switches:
                switches = session.query(ModelSwitch).limit(limit).all()
            else:
                switches = session.query(ModelSwitch).all()

            for switch in switches:
                switch_res = {}
                switch_res['id'] = switch.id
                switch_res['ip'] = switch.ip
                switch_res['state'] = switch.state
                switch_res['link'] = {
                    'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(switch.id)))
                }
                switch_list.append(switch_res)
        logging.info('get switch list: %s', switch_list)

        return util.make_json_response(200, {
            "status": 'OK',
            "switches": switch_list
        })
コード例 #60
0
ファイル: api.py プロジェクト: gasopbf1/compasstest
    def get(self):
        """
        Lists details of machines, optionally filtered by some conditions as
        the following.

        :param switchId: the unique identifier of the switch
        :param vladId: the vlan ID
        :param port: the port number
        :param limit: the number of records expected to return
        """
        machines_result = []
        switch_id = request.args.get(self.SWITCHID, type=int)
        vlan = request.args.get(self.VLANID, type=int)
        port = request.args.get(self.PORT, type=int)
        limit = request.args.get(self.LIMIT, 0, type=int)

        with database.session() as session:
            machines = []
            filter_clause = []
            if switch_id:
                filter_clause.append('switch_id=%d' % switch_id)

            if vlan:
                filter_clause.append('vlan=%d' % vlan)

            if port:
                filter_clause.append('port=%d' % port)

            if limit < 0:
                error_msg = 'Limit cannot be less than 0!'
                return errors.UserInvalidUsage(
                    errors.UserInvalidUsage(error_msg))

            if filter_clause:
                if limit:
                    machines = session.query(ModelMachine)\
                                      .filter(and_(*filter_clause))\
                                      .limit(limit).all()
                else:
                    machines = session.query(ModelMachine)\
                                      .filter(and_(*filter_clause)).all()
            else:
                if limit:
                    machines = session.query(ModelMachine).limit(limit).all()
                else:
                    machines = session.query(ModelMachine).all()

            logging.info('all machines: %s', machines)
            for machine in machines:
                machine_res = {}
                machine_res['switch_ip'] = None if not machine.switch else \
                    machine.switch.ip
                machine_res['id'] = machine.id
                machine_res['mac'] = machine.mac
                machine_res['port'] = machine.port
                machine_res['vlan'] = machine.vlan
                machine_res['link'] = {
                    'rel': 'self',
                    'href': '/'.join((self.ENDPOINT, str(machine.id)))
                }
                machines_result.append(machine_res)

        logging.info('machines for %s: %s', switch_id, machines_result)
        return util.make_json_response(200, {
            "status": "OK",
            "machines": machines_result
        })