-
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.
- Loading branch information
1 parent
dd39616
commit 028b035
Showing
6 changed files
with
138 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Pylint | ||
|
||
on: | ||
push: | ||
branches: | ||
- "*" | ||
pull_request: | ||
branches: | ||
- "*" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.10"] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install -r requirements-test.txt | ||
pip install pylint | ||
pip install requests | ||
- name: Analysing the code with pylint | ||
run: | | ||
pylint $(git ls-files '*.py') | ||
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 |
---|---|---|
@@ -1,121 +1,139 @@ | ||
"""InfluxDB v2 API utilites""" | ||
#!/usr/bin/python3 | ||
|
||
import json | ||
import requests | ||
|
||
class InfluxApi(): | ||
class Influx2Api: | ||
''' | ||
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) | ||
api_token: str | ||
endpoint: str | ||
timeout: int | ||
|
||
if "code" in json_resp: | ||
if json_resp["code"] == "not found": | ||
return "absent" | ||
def __init__(self, token, host) -> None: | ||
''' | ||
Initialize API class | ||
''' | ||
self.api_token, self.endpoint, self.timeout = token, host, 10 | ||
|
||
for bucket in json_resp["buckets"]: | ||
if bucket['name'] == module.params['name']: | ||
return 'present' | ||
else: | ||
return 'absent' | ||
|
||
# Organizations | ||
|
||
def get_all_orgs(module): | ||
def get_all_orgs(self) -> json: | ||
''' | ||
Get all organizations from InfluxDB. Queries. | ||
Returns JSON. | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
'Authorization': 'Token ' + self.api_token | ||
} | ||
response = requests.get(module.params['host'] + '/api/v2/orgs', headers=headers) | ||
response = requests.get(self.endpoint + '/api/v2/orgs', headers=headers, timeout=self.timeout) | ||
|
||
return json.loads(response.content) | ||
|
||
|
||
def get_orgID_by_name(module): | ||
def get_orgid_by_name(self, name) -> str: | ||
''' | ||
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) | ||
orgs = self.get_all_orgs() | ||
|
||
for org in orgs['orgs']: | ||
if org['name'] == module.params['org']: | ||
if org['name'] == name: | ||
return org['id'] | ||
|
||
return "not found" | ||
|
||
|
||
def create_bucket(module): | ||
# Buckets | ||
|
||
def get_bucketid_by_name(self, name, org): # pyling | ||
''' | ||
Create bucket | ||
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'], | ||
'Content-type': 'application/json' | ||
'Authorization': 'Token ' + self.api_token | ||
} | ||
|
||
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)) | ||
url = self.endpoint + '/api/v2/buckets?name=' + name + "&orgID=" + self.get_orgid_by_name(org) | ||
response = requests.get(url, headers=headers, timeout=self.timeout) | ||
json_resp = json.loads(response.content) | ||
|
||
return response.status_code, response.content | ||
for bucket in json_resp['buckets']: | ||
return bucket['id'] | ||
|
||
return "not found" | ||
|
||
|
||
def get_bucketID_by_name(module): | ||
def get_bucket_status(self, name, org_id): | ||
''' | ||
Get bucket ID by name. Returns ID | ||
If no bucket is found by name, 'not found' will be returned | ||
Get state of single bucket of org from InfluxDB. Returns 'present' if found and 'absent' if not present. | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
'Authorization': 'Token ' + self.api_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) | ||
url = self.endpoint + '/api/v2/buckets?name=' + name + "&orgID=" + org_id | ||
response = requests.get(url, headers=headers, timeout=self.timeout) | ||
json_resp = json.loads(response.content) | ||
|
||
for bucket in json_resp['buckets']: | ||
return bucket['id'] | ||
if "code" in json_resp: | ||
if json_resp["code"] == "not found": | ||
return "absent" | ||
|
||
return "not found" | ||
for bucket in json_resp["buckets"]: | ||
if bucket['name'] == name: | ||
return 'present' | ||
|
||
return 'absent' | ||
|
||
|
||
def delete_bucket(module): | ||
def create_bucket(self, name, org_id, retention): | ||
''' | ||
Create bucket | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + self.api_token, | ||
'Content-type': 'application/json' | ||
} | ||
|
||
url = self.endpoint + '/api/v2/buckets' | ||
payload = { | ||
'orgID': org_id, | ||
'name': name, | ||
'retentionRules': [ | ||
{ | ||
'type': retention['type'], | ||
'everySeconds': int(retention['everySeconds']), | ||
'shardGroupDurationSeconds': int(retention['shardGroupDurationSeconds']) | ||
} | ||
] | ||
} | ||
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=self.timeout) | ||
|
||
return response.status_code, response.content | ||
|
||
|
||
def delete_bucket(self, name, org): | ||
''' | ||
Delete bucket | ||
''' | ||
|
||
headers = { | ||
'Authorization': 'Token ' + module.params['token'] | ||
'Authorization': 'Token ' + self.api_token | ||
} | ||
|
||
url = module.params['host'] + '/api/v2/buckets/' + InfluxApi.get_bucketID_by_name(module) | ||
response = requests.delete(url, headers=headers) | ||
url = self.endpoint + '/api/v2/buckets/' + self.get_bucketid_by_name(name, org) | ||
response = requests.delete(url, headers=headers, timeout=self.timeout) | ||
|
||
return response.status_code, response.content | ||
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
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,7 @@ | ||
[MAIN] | ||
|
||
disable= | ||
line-too-long, | ||
no-method-argument, | ||
use-dict-literal, | ||
no-self-argument |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
ansible | ||
ansible-lint | ||
ansible-lint |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
# ansible collections | ||
# - community.general | ||
p |