Example #1
0
    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
        })
Example #2
0
    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}
            )
Example #3
0
    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
                }
            )
Example #4
0
    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
        })
Example #5
0
    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}
            )
Example #6
0
    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
        })