Skip to content

Commit

Permalink
update module influxdb2_organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
tbauriedel committed Feb 7, 2024
1 parent b112562 commit f171633
Show file tree
Hide file tree
Showing 17 changed files with 935 additions and 472 deletions.
850 changes: 674 additions & 176 deletions LICENSE

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Lint](https://github.com/tbauriedel/ansible-collection-influxdb2/actions/workflows/yamllint.yml/badge.svg)
![Lint](https://github.com/tbauriedel/ansible-collection-influxdb2/actions/workflows/yamllint.yml/badge.svg) ![Build](https://github.com/tbauriedel/ansible-collection-influxdb2/actions/workflows/molecule.yml/badge.svg)

> **Note:** Collection is work in progress
> **Note:** Collection is not production-ready yet
# ansible-collection-influxdb2

Expand All @@ -16,6 +16,10 @@ It was created with the aim of refreshing my Ansible knowledge and getting in to
* [Role: repos](roles/repos/README.md) - Install the official InfluxDb repositories
* [Role: influxdb2](roles/influxdb2/README.md) - Install and configure InfluxDBv2

## Modules

* [Module: influxdb2_organizsation](plugins/modules/influxdb2_organization.py): Create, update and delete InfluxDBv2 organizations

## Example

```
Expand Down
30 changes: 21 additions & 9 deletions molecule/influxdb2/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@

vars:
influxdb_influxdb2_admin_token: 123456789abc!
influxdb_influxdb2_buckets:
- name: foobar1
state: present
org: default
influxdb_influxdb2_orgs:
- name: org1
desc: this is org-1
state: absent
token: "{{ influxdb_influxdb2_admin_token }}"
host: "{{ influxdb_influxdb2_host }}"
retention:
type: 'expire'
everySeconds: 50000
shardGroupDurationSeconds: 0
host: http://localhost:8086
- name: org2
desc: This is org-2
state: absent
token: "{{ influxdb_influxdb2_admin_token }}"
host: http://localhost:8086

# influxdb_influxdb2_buckets:
# - name: foobar1
# state: present
# org: default
# token: "{{ influxdb_influxdb2_admin_token }}"
# host: "{{ influxdb_influxdb2_host }}"
# retention:
# type: expire
# everySeconds: 50000
# shardGroupDurationSeconds: 0

collections:
- tbauriedel.influxdb2
Expand Down
2 changes: 2 additions & 0 deletions molecule/influxdb2/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ platforms:
pre_build_image: true
provisioner:
name: ansible
env:
ANSIBLE_VERBOSITY: 3
verifier:
name: ansible
5 changes: 4 additions & 1 deletion molecule/influxdb2/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
tasks:
- name: Ensure python3-requests is installed
ansible.builtin.pip:
name: requests
name: "{{ item }}"
loop:
- requests
- influxdb_client
15 changes: 15 additions & 0 deletions plugins/module_utils/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# !/usr/bin/python3

# Copyright (c) 2024, Tobias Bauriedel <tobias.bauriedel@netways.de>
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0


from influxdb_client import InfluxDBClient

class Api():
def new_client(host, token, timeout=10000) -> InfluxDBClient:
return InfluxDBClient(host, token, timeout=timeout)
100 changes: 100 additions & 0 deletions plugins/module_utils/influxdb2_organization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# !/usr/bin/python3

# Copyright (c) 2024, Tobias Bauriedel <tobias.bauriedel@netways.de>
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0

from influxdb_client import Organization
from ansible_collections.tbauriedel.influxdb2.plugins.module_utils.api import (
Api
)

class Org():
def __init__(self, name, state, desc, result, host, token):
self.name = name
self.state = state
self.desc = desc
self.result = result

self.client = Api.new_client(host=host, token=token).organizations_api()

self.handle()

return


def return_result(self) ->dict:
return self.result


def handle(self):
if self.state == 'absent':
self.handle_absent()
elif self.state == 'present':
self.handle_present()

return


def handle_absent(self):
org = Organization(name=self.name, status='inactive')
for row in self.get_all():
if row.name != self.name:
continue
org = row
break

if org.status != 'active':
return

res = self.delete(org.id)
self.result['debug'] = res
self.result['changed'] = True
self.result['msg'] = self.name + " has been deleted"

return


def handle_present(self):
pre_org = Organization(name=self.name, description=self.desc, status='inactive')
for row in self.get_all():
if row.name != self.name:
continue
pre_org = self.get(row.id)
break

if pre_org.status != 'active':
res = self.create()
self.result['changed'] = True
self.result['msg'] = self.name + " has been created"
return

if self.desc != pre_org.description:
self.update(pre_org.id)
self.result['changed'] = True
self.result['msg'] = self.name + " has been updated"

return


def create(self) -> Organization:
return self.client.create_organization(name=self.name, organization=Organization(name=self.name, description=self.desc))


def update(self, id) -> Organization:
return self.client.update_organization(organization=Organization(name=self.name, description=self.desc, id=id))


def delete(self, id):
return self.client.delete_organization(org_id=id)


def get_all(self) -> list[Organization]:
return self.client.find_organizations()


def get(self, id) -> Organization:
return self.client.find_organization(org_id=id)
139 changes: 0 additions & 139 deletions plugins/module_utils/utils.py

This file was deleted.

41 changes: 41 additions & 0 deletions plugins/modules/influxdb2_bucket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/python3
# pylint: disable=missing-module-docstring

from ansible.module_utils.basic import AnsibleModule

def run_module():
'''
Module to manage InfluxDB buckets
'''

# Define new Module with arguments
module = AnsibleModule(
argument_spec=dict(
name=dict(type=str, required=True),
state=dict(type=str, required=True),
token=dict(type=str, Required=True, no_log=True),
host=dict(type=str, Required=True),
org=dict(type=str, required=False),
retention=dict(type=dict, required=False)
)
)

if module.params['state'] != 'absent' and module.params['state'] != 'present':
module.exit_json(
failed=True,
stderr="Invalid state provided. Use 'present' or 'absent'"
)

# Default result
result = dict(
changed=False,
failed=False,
)

# TODO add handling

module.exit_json(**result)


if __name__ == '__main__':
run_module()
Loading

0 comments on commit f171633

Please sign in to comment.