Browse Source

add delete methode and change error handling for missing host, user or token

test
Markus Bergholz 2 years ago
parent
commit
36738ef884
  1. 22
      plugins/module_utils/auth.py

22
plugins/module_utils/auth.py

@ -5,13 +5,17 @@ from ansible.errors import AnsibleError
class NextcloudHandler:
def __init__(self, kwargs):
self.HOST = kwargs.get('host') or os.environ.get('NEXTCLOUD_HOST')
if self.HOST is None:
raise AnsibleError('Unable to continue. No Nextcloud Host is given.')
self.USER = kwargs.get('user') or os.environ.get('NEXTCLOUD_USER')
if self.USER is None:
raise AnsibleError('Unable to continue. No Nextcloud User is given.')
self.TOKEN = kwargs.get('api_token') or os.environ.get('NEXTCLOUD_TOKEN')
if self.TOKEN is None:
raise AnsibleError('Unable to continue. No Nextcloud Token is given.')
if None in [self.HOST, self.USER, self.TOKEN]:
raise AnsibleError('Unable to perform nextcloud passwords lookup. '
'host, user, api_token and label are required.')
def get(self, path):
r = requests.get(
'https://{HOST}/{PATH}'.format(HOST=self.HOST, PATH=path),
@ -22,5 +26,15 @@ class NextcloudHandler:
else:
raise AnsibleError('Nextcloud retured with status code {SC}'.format(r.status_code))
def delete(self, path):
r = requests.delete(
'https://{HOST}/{PATH}'.format(HOST=self.HOST, PATH=path),
auth=(self.USER, self.TOKEN)
)
if r.status_code == 200:
return r
else:
raise AnsibleError('Nextcloud retured with status code {SC}'.format(r.status_code))
def user(self):
return self.USER

Loading…
Cancel
Save