Exemple #1
0
 def setUp(self):
     super(TestSwtichMachineAPI, self).setUp()
     # Create one switch in database
     with database.session() as session:
         test_switch = Switch(ip=self.SWITCH_IP_ADDRESS1)
         test_switch.credential = self.SWITCH_CREDENTIAL
         session.add(test_switch)
Exemple #2
0
 def setUp(self):
     super(TestSwtichMachineAPI, self).setUp()
     # Create one switch in database
     with database.session() as session:
         test_switch = Switch(ip=self.SWITCH_IP_ADDRESS1)
         test_switch.credential = self.SWITCH_CREDENTIAL
         session.add(test_switch)
Exemple #3
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
        })
Exemple #4
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}
            )
Exemple #5
0
    def _prepare_database(self, config_locals):
        """Prepare database."""
        with database.session() as session:
            adapters = {}
            for adapter_config in config_locals['ADAPTERS']:
                adapter = Adapter(**adapter_config)
                session.add(adapter)
                adapters[adapter_config['name']] = adapter

            roles = {}
            for role_config in config_locals['ROLES']:
                role = Role(**role_config)
                session.add(role)
                roles[role_config['name']] = role

            switches = {}
            for switch_config in config_locals['SWITCHES']:
                switch = Switch(**switch_config)
                session.add(switch)
                switches[switch_config['ip']] = switch

            machines = {}
            for switch_ip, machine_configs in (
                config_locals['MACHINES_BY_SWITCH'].items()
            ):
                for machine_config in machine_configs:
                    machine = Machine(**machine_config)
                    machines[machine_config['mac']] = machine
                    machine.switch = switches[switch_ip]
                    session.add(machine)

            clusters = {}
            for cluster_config in config_locals['CLUSTERS']:
                adapter_name = cluster_config['adapter']
                del cluster_config['adapter']
                cluster = Cluster(**cluster_config)
                clusters[cluster_config['name']] = cluster
                cluster.adapter = adapters[adapter_name]
                cluster.state = ClusterState(
                    state="INSTALLING", progress=0.0, message='')
                session.add(cluster)

            hosts = {}
            for cluster_name, host_configs in (
                config_locals['HOSTS_BY_CLUSTER'].items()
            ):
                for host_config in host_configs:
                    mac = host_config['mac']
                    del host_config['mac']
                    host = ClusterHost(**host_config)
                    hosts['%s.%s' % (
                        host_config['hostname'], cluster_name)] = host
                    host.machine = machines[mac]
                    host.cluster = clusters[cluster_name]
                    host.state = HostState(
                        state="INSTALLING", progress=0.0, message='')
                    session.add(host)
Exemple #6
0
def set_fake_switch_machine():
    with database.session() as session:
        credential = { 'version'    :  'v2c',
                       'community'  :  'public',
                     }
        switches = [ {'ip': '192.168.100.250'},
                     {'ip': '192.168.100.251'},
                     {'ip': '192.168.100.252'},
        ]
        session.query(Switch).delete()
        session.query(Machine).delete()
        ip_switch ={}
        for item in switches:
            logging.info('add switch %s', item)     
            switch = Switch(ip=item['ip'], vendor_info='huawei',
                            state='under_monitoring')
            switch.credential = credential
            session.add(switch)
            ip_switch[item['ip']] = switch
        session.flush()

        machines = [
            {'mac': '00:0c:29:32:76:85', 'port':50, 'vlan':1, 'switch_ip':'192.168.100.250'},
            {'mac': '00:0c:29:fa:cb:72', 'port':51, 'vlan':1, 'switch_ip':'192.168.100.250'},
            {'mac': '28:6e:d4:64:c7:4a', 'port':1, 'vlan':1, 'switch_ip':'192.168.100.251'},
            {'mac': '28:6e:d4:64:c7:4c', 'port':2, 'vlan':1, 'switch_ip':'192.168.100.251'},
            {'mac': '28:6e:d4:46:c4:25', 'port': 40, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '26:6e:d4:4d:c6:be', 'port': 41, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '28:6e:d4:62:da:38', 'port': 42, 'vlan': 1, 'switch_ip': '192.168.100.252'},
            {'mac': '28:6e:d4:62:db:76', 'port': 43, 'vlan': 1, 'switch_ip': '192.168.100.252'},
        ]
       
        for item in machines:
            logging.info('add machine %s', item)
            machine = Machine(mac=item['mac'], port=item['port'],
                              vlan=item['vlan'],
                              switch_id=ip_switch[item['switch_ip']].id)
            session.add(machine)
Exemple #7
0
 def update_switch_and_machines(self, switches, switch_machines):
     """update switches and machines."""
     session = database.current_session()
     session.query(Switch).delete(synchronize_session='fetch')
     session.query(Machine).delete(synchronize_session='fetch')
     for switch_data in switches:
         switch = Switch(**switch_data)
         logging.info('add switch %s', switch)
         session.add(switch)
         for machine_data in switch_machines.get(switch.ip, []):
             machine = Machine(**machine_data)
             logging.info('add machine %s under %s', machine, switch)
             machine.switch = switch
             session.add(machine)
Exemple #8
0
    def setUp(self):
        super(TestPollSwitch, self).setUp()
        logsetting.init()
        database.init(self.DATABASE_URL)
        database.create_db()
        self.test_client = app.test_client()

        with database.session() as session:
            # Add one switch to DB
            switch = Switch(ip="127.0.0.1", credential=self.SWITCH_CREDENTIAL)
            session.add(switch)
            # Add filter port to SwitchConfig table
            filter_list = [
                SwitchConfig(ip="127.0.0.1", filter_port='6'),
                SwitchConfig(ip="127.0.0.1", filter_port='7')
            ]
            session.add_all(filter_list)
Exemple #9
0
def set_fake_switch_machine():
    """Set fake switches and machines for test."""
    with database.session() as session:
        credential = {
            'version': 'v2c',
            'community': 'public',
        }
        switches = [
            {
                'ip': '192.168.100.250'
            },
            {
                'ip': '192.168.100.251'
            },
            {
                'ip': '192.168.100.252'
            },
        ]
        session.query(Switch).delete()
        session.query(Machine).delete()
        ip_switch = {}
        for item in switches:
            logging.info('add switch %s', item)
            switch = Switch(ip=item['ip'],
                            vendor_info='huawei',
                            state='under_monitoring')
            switch.credential = credential
            session.add(switch)
            ip_switch[item['ip']] = switch
        session.flush()

        machines = [
            {
                'mac': '00:0c:29:32:76:85',
                'port': 50,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '00:0c:29:fa:cb:72',
                'port': 51,
                'vlan': 1,
                'switch_ip': '192.168.100.250'
            },
            {
                'mac': '28:6e:d4:64:c7:4a',
                'port': 1,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:64:c7:4c',
                'port': 2,
                'vlan': 1,
                'switch_ip': '192.168.100.251'
            },
            {
                'mac': '28:6e:d4:46:c4:25',
                'port': 40,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '26:6e:d4:4d:c6:be',
                'port': 41,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:da:38',
                'port': 42,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
            {
                'mac': '28:6e:d4:62:db:76',
                'port': 43,
                'vlan': 1,
                'switch_ip': '192.168.100.252'
            },
        ]

        for item in machines:
            logging.info('add machine %s', item)
            machine = Machine(mac=item['mac'],
                              port=item['port'],
                              vlan=item['vlan'],
                              switch_id=ip_switch[item['switch_ip']].id)
            session.add(machine)
Exemple #10
0
    def test_get_switchList(self):
        # Prepare testing data
        with database.session() as session:
            switches = [
                Switch(ip='192.168.1.1', credential=self.SWITCH_CREDENTIAL),
                Switch(ip='192.168.1.2', credential=self.SWITCH_CREDENTIAL),
                Switch(ip='192.1.192.1', credential=self.SWITCH_CREDENTIAL),
                Switch(ip='192.1.192.2', credential=self.SWITCH_CREDENTIAL),
                Switch(ip='192.1.195.3', credential=self.SWITCH_CREDENTIAL),
                Switch(ip='192.2.192.4', credential=self.SWITCH_CREDENTIAL)
            ]
            session.add_all(switches)

        # Start to query switches
        # a. query multiple switches with ip
        # b. query switches with only switchIpNetwork
        # c. query only with limit
        # d. query swithes with switchIpNetwork and limit number
        # e. query switches with all conditions
        # f. Invliad switch ip format
        # g. Invalid switch ip network format

        testList = [{
            'url': ('/switches?switchIp=192.168.1.1'
                    '&switchIp=192.168.1.2'),
            'expected_code':
            200,
            'expected_count':
            2
        }, {
            'url': '/switches?switchIpNetwork=192.1.192.0/22',
            'expected_code': 200,
            'expected_count': 3
        }, {
            'url': '/switches?limit=3',
            'expected_code': 200,
            'expected_count': 3
        }, {
            'url': '/switches?limit=-1',
            'expected_code': 400
        }, {
            'url': ('/switches?switchIpNetwork=192.1.192.0/22'
                    '&limit=1'),
            'expected_code':
            200,
            'expected_count':
            1
        }, {
            'url': ('/switches?switchIp=192.168.1.1'
                    '&switchIpNetwork=192.1.192.0/22&limit=3'),
            'expected_code':
            400
        }, {
            'url': '/switches?switchIp=192.168.1.xx',
            'expected_code': 400
        }, {
            'url': '/switches?switchIpNetwork=192.168.1.x',
            'expected_code': 400
        }]

        for test in testList:
            url = test['url']
            rv = self.app.get(url)
            data = json.loads(rv.get_data())
            expected_code = test['expected_code']
            self.assertEqual(rv.status_code, expected_code)

            if 'expected_count' in test:
                expected_count = test['expected_count']
                switch_count = len(data['switches'])
                self.assertEqual(switch_count, expected_count)
def setupDb():
    """setup database."""
    SECURITY_CONFIG = {
        "security": {
            "server_credentials": {
                "username": "******",
                "password": "******"},
            "service_credentials": {
                "username": "******",
                "password": "******"},
            "console_credentials": {
                "username": "******",
                "password": "******"}
        }
    }

    NET_CONFIG = {
        "networking": {
            "interfaces": {
                "management": {
                    "ip_start": "10.120.8.100",
                    "ip_end": "10.120.8.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth0",
                    "promisc": 1
                },
                "tenant": {
                    "ip_start": "192.168.10.100",
                    "ip_end": "192.168.10.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth1",
                    "promisc": 0
                },
                "public": {
                    "ip_start": "12.145.68.100",
                    "ip_end": "12.145.68.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth2",
                    "promisc": 0
                },
                "storage": {
                    "ip_start": "172.29.8.100",
                    "ip_end": "172.29.8.200",
                    "netmask": "255.255.255.0",
                    "gateway": "",
                    "nic": "eth3",
                    "promisc": 0
                }
            },
            "global": {
                "nameservers": "8.8.8.8",
                "search_path": "ods.com",
                "gateway": "192.168.1.1",
                "proxy": "http://127.0.0.1:3128",
                "ntp_server": "127.0.0.1"
            }
        }
    }

    PAR_CONFIG = {
        "partition": "/home 20%;/tmp 10%;/var 30%;"
    }

    HOST_CONFIG = {
        "networking": {
            "interfaces": {
                "management": {
                    "ip": "%s"
                },
                "tenant": {
                    "ip": "%s"
                }
            }
        },
        "roles": ["base"]
    }

    print "Setting up DB ..."
    with database.session() as session:
            # populate switch_config
            switch_config = SwitchConfig(ip='192.168.1.10', filter_port='1')
            session.add(switch_config)

            # populate role table
            role = Role(name='compute', target_system='openstack')
            session.add(role)

            # Populate one adapter to DB
            adapter = Adapter(name='Centos_openstack', os='Centos',
                              target_system='openstack')
            session.add(adapter)

            #Populate switches info to DB
            switches = [Switch(ip="192.168.2.1",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.2",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.3",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring"),
                        Switch(ip="192.168.2.4",
                               credential={"version": "2c",
                                           "community": "public"},
                               vendor="huawei",
                               state="under_monitoring")]
            session.add_all(switches)

            # Populate machines info to DB
            machines = [
                Machine(mac='00:0c:27:88:0c:a1', port='1', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:a2', port='2', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:a3', port='3', vlan='1',
                        switch_id=1),
                Machine(mac='00:0c:27:88:0c:b1', port='1', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:b2', port='2', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:b3', port='3', vlan='1',
                        switch_id=2),
                Machine(mac='00:0c:27:88:0c:c1', port='1', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:c2', port='2', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:c3', port='3', vlan='1',
                        switch_id=3),
                Machine(mac='00:0c:27:88:0c:d1', port='1', vlan='1',
                        switch_id=4),
                Machine(mac='00:0c:27:88:0c:d2', port='2', vlan='1',
                        switch_id=4),
            ]

            session.add_all(machines)
            # Popluate clusters into DB
            """
            a. cluster #1: a new machine will be added to it.
            b. cluster #2: a failed machine needs to be re-deployed.
            c. cluster #3: a new cluster with 3 hosts will be deployed.
            """
            clusters_networking_config = [
                {"networking":
                    {"interfaces": {"management": {"ip_start": "10.120.1.100",
                                                   "ip_end": "10.120.1.200"},
                                    "tenant": {"ip_start": "192.168.1.100",
                                               "ip_end": "192.168.1.200"},
                                    "public": {"ip_start": "12.145.1.100",
                                               "ip_end": "12.145.1.200"},
                                    "storage": {"ip_start": "172.29.1.100",
                                                "ip_end": "172.29.1.200"}}}},
                {"networking":
                    {"interfaces": {"management": {"ip_start": "10.120.2.100",
                                                   "ip_end": "10.120.2.200"},
                                    "tenant": {"ip_start": "192.168.2.100",
                                               "ip_end": "192.168.2.200"},
                                    "public": {"ip_start": "12.145.2.100",
                                               "ip_end": "12.145.2.200"},
                                    "storage": {"ip_start": "172.29.2.100",
                                                "ip_end": "172.29.2.200"}}}}
            ]
            cluster_names = ['cluster_01', 'cluster_02']
            for name, networking_config in zip(cluster_names,
                                               clusters_networking_config):
                nconfig = copy.deepcopy(NET_CONFIG)
                util.merge_dict(nconfig, networking_config)
                c = Cluster(
                    name=name, adapter_id=1,
                    security_config=json.dumps(SECURITY_CONFIG['security']),
                    networking_config=json.dumps(nconfig['networking']),
                    partition_config=json.dumps(PAR_CONFIG['partition']))
                session.add(c)
            # Populate hosts to each cluster
            host_mips = ['10.120.1.100', '10.120.1.101', '10.120.1.102',
                         '10.120.2.100', '10.120.2.101', '10.120.2.102']
            host_tips = ['192.168.1.100', '192.168.1.101', '192.168.1.102',
                         '192.168.2.100', '192.168.2.101', '192.168.2.102']

            hosts_config = []
            for mip, tip in zip(host_mips, host_tips):
                config = copy.deepcopy(HOST_CONFIG)
                config['networking']['interfaces']['management']['ip'] = mip
                config['networking']['interfaces']['tenant']['ip'] = tip
                hosts_config.append(json.dumps(config))

            hosts = [
                ClusterHost(hostname='host_01', machine_id=1, cluster_id=1,
                            config_data=hosts_config[0]),
                ClusterHost(hostname='host_02', machine_id=2, cluster_id=1,
                            config_data=hosts_config[1]),
                ClusterHost(hostname='host_03', machine_id=3, cluster_id=1,
                            config_data=hosts_config[2]),
                ClusterHost(hostname='host_01', machine_id=4, cluster_id=2,
                            config_data=hosts_config[3]),
                ClusterHost(hostname='host_02', machine_id=5, cluster_id=2,
                            config_data=hosts_config[4]),
                ClusterHost(hostname='host_03', machine_id=6, cluster_id=2,
                            config_data=hosts_config[5])
            ]
            session.add_all(hosts)

            # Populate cluster state and host state
            cluster_states = [
                ClusterState(id=1, state="READY", progress=1.0,
                             message="Successfully!"),
                ClusterState(id=2, state="ERROR", progress=0.5,
                             message="Failed!")
            ]
            session.add_all(cluster_states)

            host_states = [
                HostState(id=1, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=2, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=3, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=4, state="ERROR", progress=0.5,
                          message="Failed!"),
                HostState(id=5, state="READY", progress=1.0,
                          message="Successfully!"),
                HostState(id=6, state="ERROR", progress=1.0,
                          message="Failed!")
            ]
            session.add_all(host_states)