Exemple #1
0
 def check_query(self):
     where_index = self.query.find(' WHERE ')
     if where_index < 0:
         self.query = self.split_params(self.query)
     else:
         where = self.query[where_index + len(' WHERE '):]
         self.query = self.split_params(self.query[:where_index])
         # where must be of the format '<attribute> <operator> <value>'
         m = re.match(r'^\s*([^ ]+)\s+([^ ]+)\s+(.*)$', where)
         if not m:
             self.errors("invalid syntax for 'WHERE %s'" % where)
         try:
             self.where = [
                 m.group(1),  # attribute
                 m.group(2),  # operator
                 parse_argument_value(m.group(3).rstrip())[0],  # value
             ]
         except ParseError as exc:
             self.errors("invalid syntax for 'WHERE %s': %s" % (where, exc))
     try:
         idx = self.query.index('WHERE')
         self.where = self.query[idx + 1:]
         self.query = self.query[:idx]
     except ValueError:
         # Raised when WHERE has not been found
         pass
Exemple #2
0
def test_parse_argument_value_errors(command, kwargs, message):
    with pytest.raises(ParseError) as exc:
        parse_argument_value(command, **kwargs)
    print(exc.value.args[0], message)
    assert exc.value.args[0] == message
Exemple #3
0
def test_parse_argument_value(command, kwargs, result):
    result_ = parse_argument_value(command, **kwargs)
    print(result_, result)
    assert result_ == result
Exemple #4
0
    def __init__(self):
        module_args = dict(
            username=dict(type='str', required=True),
            password=dict(type='str', required=True, no_log=True),
            hostname=dict(type='str', required=True),
            port=dict(type='int'),
            tls=dict(type='bool', default=False, aliases=['ssl']),
            path=dict(type='str', required=True),
            add=dict(type='str'),
            remove=dict(type='str'),
            update=dict(type='str'),
            cmd=dict(type='str'),
            query=dict(type='str'),
            validate_certs=dict(type='bool', default=True),
            validate_cert_hostname=dict(type='bool', default=False),
            ca_path=dict(type='path'),
        )

        self.module = AnsibleModule(argument_spec=module_args,
                                    supports_check_mode=False,
                                    mutually_exclusive=(('add', 'remove', 'update',
                                                         'cmd', 'query'),),)

        if not HAS_LIB:
            self.module.fail_json(msg=missing_required_lib("librouteros"),
                                  exception=LIB_IMP_ERR)

        self.api = self.ros_api_connect(self.module.params['username'],
                                        self.module.params['password'],
                                        self.module.params['hostname'],
                                        self.module.params['port'],
                                        self.module.params['tls'],
                                        self.module.params['validate_certs'],
                                        self.module.params['validate_cert_hostname'],
                                        self.module.params['ca_path'],
                                        )

        self.path = self.module.params['path'].split()
        self.add = self.module.params['add']
        self.remove = self.module.params['remove']
        self.update = self.module.params['update']
        self.arbitrary = self.module.params['cmd']

        self.where = None
        self.query = self.module.params['query']
        if self.query:
            where_index = self.query.find(' WHERE ')
            if where_index < 0:
                self.query = self.split_params(self.query)
            else:
                where = self.query[where_index + len(' WHERE '):]
                self.query = self.split_params(self.query[:where_index])
                # where must be of the format '<attribute> <operator> <value>'
                m = re.match(r'^\s*([^ ]+)\s+([^ ]+)\s+(.*)$', where)
                if not m:
                    self.errors("invalid syntax for 'WHERE %s'" % where)
                try:
                    self.where = [
                        m.group(1),  # attribute
                        m.group(2),  # operator
                        parse_argument_value(m.group(3).rstrip())[0],  # value
                    ]
                except ParseError as exc:
                    self.errors("invalid syntax for 'WHERE %s': %s" % (where, exc))
            try:
                idx = self.query.index('WHERE')
                self.where = self.query[idx + 1:]
                self.query = self.query[:idx]
            except ValueError:
                # Raised when WHERE has not been found
                pass

        self.result = dict(
            message=[])

        # create api base path
        self.api_path = self.api_add_path(self.api, self.path)

        # api call's
        if self.add:
            self.api_add()
        elif self.remove:
            self.api_remove()
        elif self.update:
            self.api_update()
        elif self.query:
            self.api_query()
        elif self.arbitrary:
            self.api_arbitrary()
        else:
            self.api_get_all()