Skip to content

Commit

Permalink
add module graylog_stream (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwinkel authored Apr 28, 2022
1 parent e57f678 commit 90c62f5
Show file tree
Hide file tree
Showing 13 changed files with 756 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*]
indent_size = 2
indent_style = space
insert_final_newline = true
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.analysis.extraPaths": [
"/mnt/c/Projects"
]
}
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
# ansible-collection-graylog
# ansible-collection-graylog

## Usage

### Undocumented fields

#### Stream-Rules object

| Field | Description | Allowed values | Sample value |
|---|---|---|---|
| `description` | Description for the rule | any `string` | `foo` |
| `field` | Field name | any `string` | `bar` |
| `value` | Field value | any `string` | `baz` |
| `type` | Matching type | `integer`: <br/>`1` match exactly <br/>`2` match regular expression <br/>`3` greater than <br/>`4` smaller than <br/>`5` field presence <br/>`6` contain <br/>`7` always match <br/>`8` match input | 1 |
| `inverted` | Rule inversion | `boolean` | false |



## Local development


### Clone project

- clone project into a path which is in your extraPaths (vscode) or PYTHONPATH variable
- directory structure should be `./ansible_collections/fio/graylog`


### Prepare development environment

```sh
$ pip install virtualenv
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
```


### Test your module


Docs:
- ...


Testing single module with a stub: `python plugins/modules/graylog_stream.py tests/stubs/graylog_streams.json`

Unit-Testing single module: `python -m pytest -r a --fulltrace --color yes tests/units/plugins/modules/test_graylog_stream.py`
9 changes: 9 additions & 0 deletions galaxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace: fio
name: graylog
version: 1.0.0
readme: README.md
authors:
- FIO SYSTEMS AG
description: modules for usage of Graylog API
license_file: LICENSE
tags: [graylog]
178 changes: 178 additions & 0 deletions plugins/module_utils/streams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
from typing import Tuple


class StreamBase():

def __init__(self):
self._title = ""
self._description = ""
self._index_set_id = ""
self._started = False
self._rules = []


@property
def title(self) -> str:
return self._title


@title.setter
def title(self, value) -> None:
self._title = value


@property
def description(self) -> str:
return self._description


@description.setter
def description(self, value) -> None:
self._description = value


@property
def index_set_id(self) -> str:
return self._index_set_id


@index_set_id.setter
def index_set_id(self, value) -> None:
self._index_set_id = value


@property
def started(self) -> list:
return self._started


@started.setter
def started(self, value) -> None:
self._started = value


@property
def rules(self) -> list:
return self._rules


@rules.setter
def rules(self, value) -> None:
self._rules = value


def properties_are_equal(self, stream: "StreamBase") -> bool:
return (
self.title == stream.title
and self.description == stream.description
and self.index_set_id == stream.index_set_id
)


def started_is_equal(self, stream: "StreamBase") -> bool:
return self.started == stream.started


def rules_are_equal(self, stream: "StreamBase") -> bool:
if len(self.rules) != len(stream.rules):
return False

for item in self.rules:
if (any(x for x in stream.rules if self._rule_equals(x, item) is False)):
return False

return True


# returns tuple(add, delete) lists
def get_rules_changes(self, stream: "StreamBase") -> Tuple[list, list]:
rules = stream.rules
add = []
delete = []

for item in self.rules:
if len(rules) == 0 or any(x for x in rules if self._rule_equals(x, item)) is False:
delete.append(item)

for item in rules:
if len(self.rules) == 0 or any(x for x in self.rules if self._rule_equals(x, item) is False):
add.append(item)

return add, delete


def _rule_equals(self, a: dict, b: dict) -> bool:
return (a.get('field') == b.get('field')
and a.get('value') == b.get('value')
and a.get('type') == b.get('type')
and a.get('inverted') == b.get('inverted'))


def __str__(self) -> str:
return ("Title: %s | Description: %s | Index-Set ID: %s | Started: %s | Rules: [%s]" % (self.title, self.description, self.index_set_id, self.started, self._str_rules()))


def _str_rules(self) -> str:
ret = ""
for rule in self.rules:
ret += " { Field: %s | Value: %s | Type: %s | Inverted: %s }" % (rule.get('field'), rule.get('value'), rule.get('type'), rule.get('inverted'))

ret += " "
return ret



class StreamParams(StreamBase):

def __init__(self, params: dict):
super().__init__()
self.title = params.get('name', '')
self.description = params.get('name', '')
self.index_set_id = params.get('index_set_id', '')
self.rules = params.get('rules', [])


def map_to_dto(self, destination: dict = None) -> dict:
if destination is None:
destination = {}

destination['title'] = self.title
destination['description'] = self.description
destination['remove_matches_from_default_stream'] = True
destination['index_set_id'] = self.index_set_id
destination['rules'] = self.rules

return destination



class Stream(StreamBase):

def __init__(self, dto: dict):
super().__init__()
self._dto = dto
self._id = dto.get('id', '')
self.title = dto.get('title', '')
self.description = dto.get('description', '')
self.index_set_id = dto.get('index_set_id', '')
self.rules = dto.get('rules', [])


@property
def dto(self) -> dict:
return self._dto


@dto.setter
def dto(self, value) -> None:
self._dto = value


@property
def id(self) -> str:
return self._id


@id.setter
def id(self, value) -> None:
self._id = value
Loading

0 comments on commit 90c62f5

Please sign in to comment.