Made a few canges as I learn more about modules

* Set check_mode to False, am not supporting this ATM.
* Cleaned up delete/create() into single update().
* Return the group_id if created or found so tasks can be chained.
This commit is contained in:
John Dewey 2013-12-07 10:19:28 -08:00
parent 8d3aa464b2
commit ed3b65b532

View file

@ -157,14 +157,14 @@ class SecurityGroup(NovaGroup):
self._name = module.params.get('name') self._name = module.params.get('name')
self._description = module.params.get('description') self._description = module.params.get('description')
def exists(self): def get(self):
return self._get_secgroup(self._name) return self._get_secgroup(self._name)
def create(self): def create(self):
self._sg.create(self._name, self._description) return self._sg.create(self._name, self._description)
def delete(self): def delete(self):
self._sg.delete(self._name) return self._sg.delete(self._name)
class SecurityGroupRule(NovaGroup): class SecurityGroupRule(NovaGroup):
@ -219,8 +219,6 @@ class SecurityGroupRule(NovaGroup):
r = self._concat_security_group_rule(rule) r = self._concat_security_group_rule(rule)
if r in self._current_rules: if r in self._current_rules:
return self._current_rules[r] return self._current_rules[r]
else:
return False
def _validate_rules(self): def _validate_rules(self):
for rule in self._rules: for rule in self._rules:
@ -260,6 +258,14 @@ class SecurityGroupRule(NovaGroup):
changed = True changed = True
return changed return changed
def update(self):
changed = False
if self.create():
changed = True
if self.delete():
changed = True
return changed
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
@ -274,7 +280,7 @@ def main():
region_name=dict(default=None), region_name=dict(default=None),
state = dict(default='present', choices=['present', 'absent']), state = dict(default='present', choices=['present', 'absent']),
), ),
supports_check_mode=True, supports_check_mode=False,
) )
login_username = module.params.get('login_username') login_username = module.params.get('login_username')
login_password = module.params.get('login_password') login_password = module.params.get('login_password')
@ -309,24 +315,26 @@ def main():
rules = module.params.get('rules') rules = module.params.get('rules')
state = module.params.get('state') state = module.params.get('state')
security_group = SecurityGroup(nova, module) security_group = SecurityGroup(nova, module)
security_group_rules = SecurityGroupRule(nova, module)
changed = False changed = False
if security_group.exists(): group_id = None
group = security_group.get()
if group:
group_id = group.id
if state == 'absent': if state == 'absent':
security_group.delete() security_group.delete()
changed = True changed = True
elif state == 'present': elif state == 'present':
security_group.create() group = security_group.create()
changed = True changed = True
group_id = group.id
if rules: if rules is not None:
if security_group_rules.create(): security_group_rules = SecurityGroupRule(nova, module)
changed = True if security_group_rules.update():
if security_group_rules.delete():
changed = True changed = True
module.exit_json(changed=changed, group_id=None) module.exit_json(changed=changed, group_id=group_id)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>