Multiple fixes in digital_ocean_tag module (#24361)

* Pep8 fixes
* Removed redundant check for name
* Check validity of api_token
* Don't report changed when tag is already present

Fixes #24265

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/26046/merge
Abhijeet Kasurde 7 years ago committed by René Moser
parent 0a6dcf4169
commit 785ed2cfc0

@ -39,7 +39,7 @@ options:
- The ID of the resource to operate on. - The ID of the resource to operate on.
resource_type: resource_type:
description: description:
- The type of resource to operate on. Currently only tagging of - The type of resource to operate on. Currently, only tagging of
droplets is supported. droplets is supported.
default: droplet default: droplet
choices: ['droplet'] choices: ['droplet']
@ -153,7 +153,7 @@ class Rest(object):
path = path[1:] path = path[1:]
return '%s/%s' % (self.baseurl, path) return '%s/%s' % (self.baseurl, path)
def send(self, method, path, data=None, headers=None): def send(self, method, path, data=None):
url = self._url_builder(path) url = self._url_builder(path)
data = self.module.jsonify(data) data = self.module.jsonify(data)
@ -161,17 +161,17 @@ class Rest(object):
return Response(resp, info) return Response(resp, info)
def get(self, path, data=None, headers=None): def get(self, path, data=None):
return self.send('GET', path, data, headers) return self.send('GET', path, data)
def put(self, path, data=None, headers=None): def put(self, path, data=None):
return self.send('PUT', path, data, headers) return self.send('PUT', path, data)
def post(self, path, data=None, headers=None): def post(self, path, data=None):
return self.send('POST', path, data, headers) return self.send('POST', path, data)
def delete(self, path, data=None, headers=None): def delete(self, path, data=None):
return self.send('DELETE', path, data, headers) return self.send('DELETE', path, data)
def core(module): def core(module):
@ -186,57 +186,77 @@ def core(module):
resource_id = module.params['resource_id'] resource_id = module.params['resource_id']
resource_type = module.params['resource_type'] resource_type = module.params['resource_type']
rest = Rest(module, {'Authorization': 'Bearer {}'.format(api_token), rest = Rest(module, {'Authorization': 'Bearer {0}'.format(api_token),
'Content-type': 'application/json'}) 'Content-type': 'application/json'})
if state in ('present'): # Check if api_token is valid or not
if name is None: response = rest.get('account')
module.fail_json(msg='parameter `name` is missing') if response.status_code == 401:
module.fail_json(msg='Failed to login using api_token, please verify '
# Ensure Tag exists 'validity of api_token')
response = rest.post("tags", data={'name': name}) if state == 'present':
response = rest.get('tags/{0}'.format(name))
status_code = response.status_code status_code = response.status_code
json = response.json resp_json = response.json
if status_code == 201: changed = False
changed = True if status_code == 200 and resp_json['tag']['name'] == name:
elif status_code == 422:
changed = False changed = False
else: else:
module.exit_json(changed=False, data=json) # Ensure Tag exists
response = rest.post("tags", data={'name': name})
status_code = response.status_code
resp_json = response.json
if status_code == 201:
changed = True
elif status_code == 422:
changed = False
else:
module.exit_json(changed=False, data=resp_json)
if resource_id is None: if resource_id is None:
# No resource defined, we're done. # No resource defined, we're done.
if json is None: module.exit_json(changed=changed, data=resp_json)
module.exit_json(changed=changed, data=json)
else:
module.exit_json(changed=changed, data=json)
else: else:
# Tag a resource # Check if resource is already tagged or not
url = "tags/{}/resources".format(name) found = False
payload = { url = "{0}?tag_name={1}".format(resource_type, name)
'resources': [{ response = rest.get(url)
'resource_id': resource_id, status_code = response.status_code
'resource_type': resource_type}]} resp_json = response.json
response = rest.post(url, data=payload) if status_code == 200:
if response.status_code == 204: for resource in resp_json['droplets']:
module.exit_json(changed=True) if not found and resource['id'] == int(resource_id):
found = True
break
if not found:
# If resource is not tagged, tag a resource
url = "tags/{0}/resources".format(name)
payload = {
'resources': [{
'resource_id': resource_id,
'resource_type': resource_type}]}
response = rest.post(url, data=payload)
if response.status_code == 204:
module.exit_json(changed=True)
else:
module.fail_json(msg="error tagging resource '{0}': {1}".format(resource_id, response.json["message"]))
else:
# Already tagged resource
module.exit_json(changed=False)
else: else:
module.fail_json(msg="error tagging resource '{}': {}".format( # Unable to find resource specified by user
resource_id, response.json["message"])) module.fail_json(msg=resp_json['message'])
elif state in ('absent'):
if name is None:
module.fail_json(msg='parameter `name` is missing')
elif state == 'absent':
if resource_id: if resource_id:
url = "tags/{}/resources".format(name) url = "tags/{0}/resources".format(name)
payload = { payload = {
'resources': [{ 'resources': [{
'resource_id': resource_id, 'resource_id': resource_id,
'resource_type': resource_type}]} 'resource_type': resource_type}]}
response = rest.delete(url, data=payload) response = rest.delete(url, data=payload)
else: else:
url = "tags/{}".format(name) url = "tags/{0}".format(name)
response = rest.delete(url) response = rest.delete(url)
if response.status_code == 204: if response.status_code == 204:
module.exit_json(changed=True) module.exit_json(changed=True)

Loading…
Cancel
Save