DigitalOcean - Fix TypeError when users have tags named the same as droplets (#41368)

* Fix TypeError when users have tags named the same as droplets
* Updated code based on review comments
This commit is contained in:
ABond 2018-06-15 06:57:50 -04:00 committed by Abhijeet Kasurde
parent a94ddedfd3
commit 5d26477b94

View file

@ -429,6 +429,21 @@ class DigitalOceanInventory(object):
self.data['tags'] = self.manager.all_tags()
self.cache_refreshed = True
def add_inventory_group(self, key):
""" Method to create group dict """
host_dict = {'hosts': [], 'vars': {}}
self.inventory[key] = host_dict
return
def add_host(self, group, host):
""" Helper method to reduce host duplication """
if group not in self.inventory:
self.add_inventory_group(group)
if host not in self.inventory[group]['hosts']:
self.inventory[group]['hosts'].append(host)
return
def build_inventory(self):
""" Build Ansible inventory of droplets """
self.inventory = {
@ -449,8 +464,9 @@ class DigitalOceanInventory(object):
self.inventory['all']['hosts'].append(dest)
self.inventory[droplet['id']] = [dest]
self.inventory[droplet['name']] = [dest]
self.add_host(droplet['id'], dest)
self.add_host(droplet['name'], dest)
# groups that are always present
for group in ('digital_ocean',
@ -459,24 +475,18 @@ class DigitalOceanInventory(object):
'size_' + droplet['size']['slug'],
'distro_' + DigitalOceanInventory.to_safe(droplet['image']['distribution']),
'status_' + droplet['status']):
if group not in self.inventory:
self.inventory[group] = {'hosts': [], 'vars': {}}
self.inventory[group]['hosts'].append(dest)
self.add_host(group, dest)
# groups that are not always present
for group in (droplet['image']['slug'],
droplet['image']['name']):
if group:
image = 'image_' + DigitalOceanInventory.to_safe(group)
if image not in self.inventory:
self.inventory[image] = {'hosts': [], 'vars': {}}
self.inventory[image]['hosts'].append(dest)
self.add_host(image, dest)
if droplet['tags']:
for tag in droplet['tags']:
if tag not in self.inventory:
self.inventory[tag] = {'hosts': [], 'vars': {}}
self.inventory[tag]['hosts'].append(dest)
self.add_host(tag, dest)
# hostvars
info = self.do_namespace(droplet)