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 8 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,33 +186,51 @@ 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 '
'validity of api_token')
if state == 'present':
response = rest.get('tags/{0}'.format(name))
status_code = response.status_code
resp_json = response.json
changed = False
if status_code == 200 and resp_json['tag']['name'] == name:
changed = False
else:
# Ensure Tag exists # Ensure Tag exists
response = rest.post("tags", data={'name': name}) response = rest.post("tags", data={'name': name})
status_code = response.status_code status_code = response.status_code
json = response.json resp_json = response.json
if status_code == 201: if status_code == 201:
changed = True changed = True
elif status_code == 422: elif status_code == 422:
changed = False changed = False
else: else:
module.exit_json(changed=False, data=json) 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: else:
module.exit_json(changed=changed, data=json) # Check if resource is already tagged or not
else: found = False
# Tag a resource url = "{0}?tag_name={1}".format(resource_type, name)
url = "tags/{}/resources".format(name) response = rest.get(url)
status_code = response.status_code
resp_json = response.json
if status_code == 200:
for resource in resp_json['droplets']:
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 = { payload = {
'resources': [{ 'resources': [{
'resource_id': resource_id, 'resource_id': resource_id,
@ -221,22 +239,24 @@ def core(module):
if response.status_code == 204: if response.status_code == 204:
module.exit_json(changed=True) module.exit_json(changed=True)
else: else:
module.fail_json(msg="error tagging resource '{}': {}".format( module.fail_json(msg="error tagging resource '{0}': {1}".format(resource_id, response.json["message"]))
resource_id, response.json["message"])) else:
# Already tagged resource
elif state in ('absent'): module.exit_json(changed=False)
if name is None: else:
module.fail_json(msg='parameter `name` is missing') # Unable to find resource specified by user
module.fail_json(msg=resp_json['message'])
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