-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from tbauriedel/enhancement/influxdb
Add handling for buckets
- Loading branch information
Showing
10 changed files
with
304 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace: tbauriedel | ||
name: influxdb | ||
version: 0.0.0 | ||
readme: README.md | ||
authors: | ||
- Tobias Bauriedel <[email protected]> | ||
description: > | ||
This collection manages InfluxDB components. | ||
Including the official repositories, installation and configuration. | ||
license: | ||
- Apache-2.0 | ||
tags: | ||
- collection | ||
- influxdb | ||
- metrics | ||
dependencies: {} | ||
repository: https://github.com/tbauriedel/ansible-collection-influxdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#!/usr/bin/python3 | ||
|
||
import json | ||
import requests | ||
|
||
class InfluxApi(): | ||
''' | ||
Common api requests to for InfluxDBv2 | ||
''' | ||
def get_bucket_status(module): | ||
''' | ||
Get state of single bucket of org from InfluxDB. Returns 'present' if found and 'absent' if not present. | ||
''' | ||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
} | ||
|
||
url = module.params['host'] + '/api/v2/buckets?name=' + module.params['name'] + "&orgID=" + InfluxApi.get_orgID_by_name(module) | ||
response = requests.get(url, headers=headers) | ||
json_resp = json.loads(response.content) | ||
|
||
if "code" in json_resp: | ||
if json_resp["code"] == "not found": | ||
return "absent" | ||
|
||
for bucket in json_resp["buckets"]: | ||
if bucket['name'] == module.params['name']: | ||
return 'present' | ||
else: | ||
return 'absent' | ||
|
||
|
||
def get_all_orgs(module): | ||
''' | ||
Get all organizations from InfluxDB. Queries. | ||
Returns JSON. | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
} | ||
response = requests.get(module.params['host'] + '/api/v2/orgs', headers=headers) | ||
|
||
return json.loads(response.content) | ||
|
||
|
||
def get_orgID_by_name(module): | ||
''' | ||
Get organization ID by name. Returns ID | ||
If no organization is found by name, 'not found' will be returned. | ||
''' | ||
|
||
orgs = InfluxApi.get_all_orgs(module) | ||
|
||
for org in orgs['orgs']: | ||
if org['name'] == module.params['org']: | ||
return org['id'] | ||
|
||
return "not found" | ||
|
||
|
||
def create_bucket(module): | ||
''' | ||
Create bucket | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'], | ||
'Content-type': 'application/json' | ||
} | ||
|
||
url = module.params['host'] + '/api/v2/buckets' | ||
payload = { | ||
'orgID': InfluxApi.get_orgID_by_name(module), | ||
'name': module.params['name'], | ||
'retentionRules': [ | ||
{ | ||
'type': module.params['retention']['type'], | ||
'everySeconds': int(module.params['retention']['everySeconds']), | ||
'shardGroupDurationSeconds': int(module.params['retention']['shardGroupDurationSeconds']) | ||
} | ||
] | ||
} | ||
response = requests.post(url, headers=headers, data=json.dumps(payload)) | ||
|
||
return response.status_code, response.content | ||
|
||
|
||
def get_bucketID_by_name(module): | ||
''' | ||
Get bucket ID by name. Returns ID | ||
If no bucket is found by name, 'not found' will be returned | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
} | ||
|
||
url = module.params['host'] + '/api/v2/buckets?name=' + module.params['name'] + "&orgID=" + InfluxApi.get_orgID_by_name(module) | ||
response = requests.get(url, headers=headers) | ||
json_resp = json.loads(response.content) | ||
|
||
for bucket in json_resp['buckets']: | ||
return bucket['id'] | ||
|
||
return "not found" | ||
|
||
|
||
def delete_bucket(module): | ||
''' | ||
Delete bucket | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
} | ||
|
||
url = module.params['host'] + '/api/v2/buckets/' + InfluxApi.get_bucketID_by_name(module) | ||
response = requests.delete(url, headers=headers) | ||
|
||
return response.status_code, response.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/usr/bin/python3 | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
from ansible_collections.tbauriedel.influxdb.plugins.module_utils.utils import ( | ||
InfluxApi | ||
) | ||
|
||
def run_module(): | ||
# 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, | ||
rc="" | ||
) | ||
|
||
# Get state of current bucket | ||
orgID = InfluxApi.get_orgID_by_name(module) | ||
if orgID == "not found": | ||
module.exit_json(dict( | ||
failed=True, | ||
stderr="No orgID found for given org name" | ||
)) | ||
|
||
# Get state of bucket ('present' or 'absent') | ||
bucketState = InfluxApi.get_bucket_status(module) | ||
|
||
# Create bucket if not 'present' but 'present' in configuration | ||
if module.params['state'] == 'present' and bucketState == 'absent': | ||
result['debug'] = "Create bucket" | ||
|
||
rc, content = InfluxApi.create_bucket(module) | ||
result['rc'] = rc | ||
if rc != 201: | ||
module.exit_json( | ||
failed=True, | ||
stderr=content | ||
) | ||
|
||
result['changed'] = True | ||
|
||
# Delete bucket if 'present' but 'absent' in configuration | ||
elif module.params['state'] == 'absent' and bucketState == 'present': | ||
result['debug'] = "Delete bucket" | ||
|
||
rc, content = InfluxApi.delete_bucket(module) | ||
result['rc'] = rc | ||
if rc != 204: | ||
module.exit_json( | ||
failed=True, | ||
stderr=content | ||
) | ||
|
||
result['changed'] = True | ||
|
||
else: | ||
result['debug'] = "Keep state of bucket" | ||
|
||
module.exit_json(**result) | ||
|
||
def main(): | ||
run_module() | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# ansible collections | ||
# - community.general |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters