class CF_User(object): def __init__(self, module): self.module = module admin_user = self.module.params['admin_user'] admin_password = self.module.params['admin_password'] api_url = self.module.params['api_url'] self.name = self.module.params['name'] try: self.cf = CF(api_url) self.cf.login(admin_user, admin_password) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) def run(self): force = self.module.params['force'] state = self.module.params['state'] try: user = self.cf.search_user(self.name) if state == 'present': space_guid = None if self.module.params['default_organization'] is not None: org = self.cf.search_org( self.module.params['default_organization']) if not org: self.module.fail_json(msg='Organization not found') org_guid = org['metadata']['guid'] space_name = self.module.params['default_space'] space = self.cf.search_space(org_guid, space_name) if not space: self.module.fail_json(msg='Space not found') space_guid = space['metadata']['guid'] result = self.present(user, space_guid, force) elif state == 'absent': result = self.absent(user, True) else: self.module.fail_json(msg='Invalid state: %s' % state) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: raise self.module.fail_json(msg="Exception: %s" % str(e)) self.module.exit_json(**result) def absent(self, user, force, async=False): changed = False if user is not None: user_id = user['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_user(user_id, async, force) except CFException as e: msg = 'Cannot delete user %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = {'changed': changed, 'msg': "CF user %s deleted" % self.name} return result
class CF_Space(object): def __init__(self, module): self.module = module admin_user = self.module.params['admin_user'] admin_password = self.module.params['admin_password'] api_url = self.module.params['api_url'] self.name = self.module.params['name'] try: self.cf = CF(api_url) self.cf.login(admin_user, admin_password) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) self.oname = self.module.params['organization'] def run(self): state = self.module.params['state'] try: org = self.cf.search_org(self.oname) if not org: msg = 'Organization %s not found' % self.oname self.module.fail_json(msg=msg) org_guid = org['metadata']['guid'] space = self.cf.search_space(org_guid, self.name) if state == 'present': result = self.present(space, org_guid) elif state == 'absent': recursive = self.module.params['force'] result = self.absent(space, org_guid, recursive) else: self.module.fail_json(msg='Invalid state: %s' % state) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) self.module.exit_json(**result) def absent(self, space, org_guid, recursive, async=False): changed = False if space is not None: space_guid = space['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_space(space_guid, async, recursive) except CFException as e: msg = 'Cannot delete space %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = { 'changed': changed, 'msg': "CF space %s deleted from %s org" % (self.name, self.oname) } return result
class CF_User(object): def __init__(self, module): self.module = module admin_user = self.module.params['admin_user'] admin_password = self.module.params['admin_password'] api_url = self.module.params['api_url'] self.name = self.module.params['name'] try: self.cf = CF(api_url) self.cf.login(admin_user, admin_password) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) def run(self): force = self.module.params['force'] state = self.module.params['state'] try: user = self.cf.search_user(self.name) if state == 'present': space_guid = None if self.module.params['default_organization'] is not None: org = self.cf.search_org(self.module.params['default_organization']) if not org: self.module.fail_json(msg='Organization not found') org_guid = org['metadata']['guid'] space_name = self.module.params['default_space'] space = self.cf.search_space(org_guid, space_name) if not space: self.module.fail_json(msg='Space not found') space_guid = space['metadata']['guid'] result = self.present(user, space_guid, force) elif state == 'absent': result = self.absent(user, True) else: self.module.fail_json(msg='Invalid state: %s' % state) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: raise self.module.fail_json(msg="Exception: %s" % str(e)) self.module.exit_json(**result) def absent(self, user, force, async=False): changed = False if user is not None: user_id = user['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_user(user_id, async, force) except CFException as e: msg = 'Cannot delete user %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = { 'changed': changed, 'msg': "CF user %s deleted" % self.name } return result
class CF_Secgroup(object): def __init__(self, module): self.module = module admin_user = self.module.params['admin_user'] admin_password = self.module.params['admin_password'] api_url = self.module.params['api_url'] self.name = self.module.params['name'] try: self.cf = CF(api_url) self.cf.login(admin_user, admin_password) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) def run(self): state = self.module.params['state'] try: sec_group = self.cf.search_secgroup(self.name) if state == 'present': space_state = self.module.params['space_state'] mode = True if space_state == "present" else False space_guid = None if self.module.params['organization'] is not None: org = self.cf.search_org(self.module.params['organization']) if not org: self.module.fail_json(msg='Organization not found') org_guid = org['metadata']['guid'] space_name = self.module.params['space'] space = self.cf.search_space(org_guid, space_name) if not space: self.module.fail_json(msg='Space not found') space_guid = space['metadata']['guid'] result = self.present(sec_group, space_guid, mode) elif state == 'absent': result = self.absent(sec_group) else: self.module.fail_json(msg='Invalid state: %s' % state) except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) self.module.exit_json(**result) def absent(self, sec_group, async=False): changed = False if sec_group is not None: sec_group_guid = sec_group['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_secgroup(sec_group_guid, async) except CFException as e: msg = 'Cannot delete security group %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = { 'changed': changed, 'msg': "CF security group %s deleted" % (self.name) } return result