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_Org(object): system_orgs = ['pivotal'] 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: org = self.cf.search_org(self.name) if state == 'present': quota_name = self.module.params['quota'] result = self.present(org, quota_name) elif state == 'absent': if self.name in self.system_orgs and not force: self.module.fail_json(msg="Cannot delete a system org") result = self.absent(org, force) 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, org, recursive, async=False): changed = False if org is not None: org_guid = org['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_org(org_guid, async, recursive) except CFException as e: msg = 'Cannot delete org %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = { 'changed': changed, 'msg': "CF org %s deleted" % self.name } return result
#!/usr/bin/env python # -*- coding: utf-8 -*- from cfconfigurator.cf import CF api_url = "https://api.test.cf.springer-sbm.com" admin_user = "******" admin_password = "******" cf = CF(api_url) cf.login(admin_user, admin_password) org = cf.search_org("pivotal") print(org) services = cf.request('GET', "/v2/services", {"results-per-page": 1}) print(services) services = cf.request('GET', "https://api.test.cf.springer-sbm.com/v2/services", {"results-per-page": 1}) print(services)
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_Org_Facts(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 get_all_orgs(self): all_orgs = [] # raise Exception(self.cf.api_url + '/v2/organizations') response, rcode = self.cf.request( 'GET', self.cf.api_url + '/v2/organizations') # raise Exception(response) if rcode == 200: return [org['entity']['name'] for org in response['resources']] def get_quota(self, url): facts = {} quota, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: facts['guid'] = quota['metadata']['guid'] facts['created_at'] = quota['metadata']['created_at'] facts['updated_at'] = quota['metadata']['updated_at'] for key in quota['entity']: facts[key] = quota['entity'][key] return facts def get_private_domains(self, url, org_owner=None): owner_domains = [] shared_domains = [] domains, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for domain in domains['resources']: fact = {} fact['guid'] = domain['metadata']['guid'] fact['created_at'] = domain['metadata']['created_at'] fact['updated_at'] = domain['metadata']['updated_at'] fact['name'] = domain['entity']['name'] if org_id_owner is not None and domain['entity'][ 'owning_organization_guid'] == org_owner: owner_domains.append(fact) else: shared_domains.append(fact) facts = { 'owner_domains': owner_domains, 'shared_domains': shared_domains } return facts def get_users(self, url): facts = [] users, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for user in users['resources']: fact = {} fact['guid'] = user['metadata']['guid'] fact['created_at'] = user['metadata']['created_at'] fact['updated_at'] = user['metadata']['updated_at'] fact['name'] = user['entity']['username'] fact['admin'] = user['entity']['admin'] fact['active'] = user['entity']['active'] facts.append(fact) return facts def get_secgroups(self, url): facts = [] secgroups, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for sg in secgroups['resources']: fact = {} fact['guid'] = sg['metadata']['guid'] fact['created_at'] = sg['metadata']['created_at'] fact['updated_at'] = sg['metadata']['updated_at'] fact['name'] = sg['entity']['name'] fact['running_default'] = sg['entity']['running_default'] fact['staging_default'] = sg['entity']['staging_default'] fact['rules'] = sg['entity']['rules'] facts.append(fact) return facts def get_spaces(self, url, space_name=None, users_type=['developers', 'managers', 'auditors']): facts = [] spaces, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for space in spaces['resources']: if space_name is not None and space['entity'][ 'name'] != space_name: break fact = {} fact['guid'] = space['metadata']['guid'] fact['created_at'] = space['metadata']['created_at'] fact['updated_at'] = space['metadata']['updated_at'] fact['name'] = space['entity']['name'] fact['allow_ssh'] = space['entity']['allow_ssh'] if 'quota_definition_url' in space['entity']: fact['quota'] = self.get_quota( space['entity']['quota_definition_url']) else: fact['quota'] = {} fact['sec_groups'] = self.get_secgroups( space['entity']['security_groups_url']) fact['users'] = {} for user_type in users_type: if user_type not in fact['users']: fact['users'][user_type] = [] user_url = user_type + '_url' if user_url in space['entity']: fact['users'][user_type] = self.get_users( space['entity'][user_url]) facts.append(fact) return facts def run(self): facts = {} try: space_name = self.module.params['space'] if self.name is not None: org = self.cf.search_org(self.name) if org is not None: if space_name is not None: facts = self.get_spaces(org['entity']['spaces_url'], space_name)[0] else: facts['name'] = org['entity']['name'] facts['guid'] = org['metadata']['guid'] facts['status'] = org['entity']['status'] facts['created_at'] = org['metadata']['created_at'] facts['updated_at'] = org['metadata']['updated_at'] facts['spaces'] = self.get_spaces( org['entity']['spaces_url']) if 'quota_definition_url' in org['entity']: facts['quota'] = self.get_quota( org['entity']['quota_definition_url']) else: facts['quota'] = {} facts['users'] = {} for user_type in [ 'users', 'managers', 'billing_managers', 'auditors' ]: if user_type not in facts['users']: facts['users'][user_type] = [] user_url = user_type + '_url' if user_url in org['entity']: facts['users'][user_type] = self.get_users( org['entity'][user_url]) domains = self.get_private_domains( org['entity']['private_domains_url'], facts['guid']) facts.update(domains) else: facts['orgs'] = self.get_all_orgs() except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) result = {'ansible_facts': facts} self.module.exit_json(**result)
class CF_Domain(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.kind = self.module.params['type'] def run(self): state = self.module.params['state'] try: domain = self.cf.search_domain(self.name, self.kind) if state == 'present': shared_state = self.module.params['shared_state'] mode = True if shared_state == "present" else False owner_org_guid = None if self.kind == "private": if self.module.params['owner_organization'] is not None: owner_org_name = self.module.params['owner_organization'] owner_org = self.cf.search_org(owner_org_name) if not owner_org: msg = 'Organization %s not found' % owner_org_name self.module.fail_json(msg=msg) owner_org_guid = owner_org['metadata']['guid'] else: if domain is None: # It cannot a not existing private domain if owner org # is not provided self.module.fail_json(msg='No domain found and organization is unknown!') shared_org_guid = None if self.module.params['shared_organization'] is not None: shared_org_name = self.module.params['shared_organization'] shared_org = self.cf.search_org(shared_org_name) if not shared_org: msg = 'Organization to share domain to %s not found' % shared_org_name self.module.fail_json(msg=msg) shared_org_guid = shared_org['metadata']['guid'] result = self.present(domain, owner_org_guid, shared_org_guid, mode) elif state == 'absent': result = self.absent(domain) 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, domain, async=False): changed = False if domain is not None: domain_guid = domain['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_domain(domain_guid, self.kind, async) except CFException as e: msg = 'Cannot delete domain %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = { 'changed': changed, 'msg': "CF %s domain %s deleted" % (self.kind, 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
class CF_Org_Facts(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 get_all_orgs(self): all_orgs = [] # raise Exception(self.cf.api_url + '/v2/organizations') response, rcode = self.cf.request('GET', self.cf.api_url + '/v2/organizations') # raise Exception(response) if rcode == 200: return [ org['entity']['name'] for org in response['resources'] ] def get_quota(self, url): facts = {} quota, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: facts['guid'] = quota['metadata']['guid'] facts['created_at'] = quota['metadata']['created_at'] facts['updated_at'] = quota['metadata']['updated_at'] for key in quota['entity']: facts[key] = quota['entity'][key] return facts def get_private_domains(self, url, org_owner=None): owner_domains = [] shared_domains = [] domains, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for domain in domains['resources']: fact = {} fact['guid'] = domain['metadata']['guid'] fact['created_at'] = domain['metadata']['created_at'] fact['updated_at'] = domain['metadata']['updated_at'] fact['name'] = domain['entity']['name'] if org_id_owner is not None and domain['entity']['owning_organization_guid'] == org_owner: owner_domains.append(fact) else: shared_domains.append(fact) facts = { 'owner_domains': owner_domains, 'shared_domains': shared_domains } return facts def get_users(self, url): facts = [] users, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for user in users['resources']: fact = {} fact['guid'] = user['metadata']['guid'] fact['created_at'] = user['metadata']['created_at'] fact['updated_at'] = user['metadata']['updated_at'] fact['name'] = user['entity']['username'] fact['admin'] = user['entity']['admin'] fact['active'] = user['entity']['active'] facts.append(fact) return facts def get_secgroups(self, url): facts = [] secgroups, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for sg in secgroups['resources']: fact = {} fact['guid'] = sg['metadata']['guid'] fact['created_at'] = sg['metadata']['created_at'] fact['updated_at'] = sg['metadata']['updated_at'] fact['name'] = sg['entity']['name'] fact['running_default'] = sg['entity']['running_default'] fact['staging_default'] = sg['entity']['staging_default'] fact['rules'] = sg['entity']['rules'] facts.append(fact) return facts def get_spaces(self, url, space_name=None, users_type=['developers', 'managers', 'auditors']): facts = [] spaces, rcode = self.cf.request('GET', self.cf.api_url + url) if rcode == 200: for space in spaces['resources']: if space_name is not None and space['entity']['name'] != space_name: break fact = {} fact['guid'] = space['metadata']['guid'] fact['created_at'] = space['metadata']['created_at'] fact['updated_at'] = space['metadata']['updated_at'] fact['name'] = space['entity']['name'] fact['allow_ssh'] = space['entity']['allow_ssh'] if 'quota_definition_url' in space['entity']: fact['quota'] = self.get_quota(space['entity']['quota_definition_url']) else: fact['quota'] = {} fact['sec_groups'] = self.get_secgroups(space['entity']['security_groups_url']) fact['users'] = {} for user_type in users_type: if user_type not in fact['users']: fact['users'][user_type] = [] user_url = user_type + '_url' if user_url in space['entity']: fact['users'][user_type] = self.get_users(space['entity'][user_url]) facts.append(fact) return facts def run(self): facts = {} try: space_name = self.module.params['space'] if self.name is not None: org = self.cf.search_org(self.name) if org is not None: if space_name is not None: facts = self.get_spaces(org['entity']['spaces_url'], space_name)[0] else: facts['name'] = org['entity']['name'] facts['guid'] = org['metadata']['guid'] facts['status'] = org['entity']['status'] facts['created_at'] = org['metadata']['created_at'] facts['updated_at'] = org['metadata']['updated_at'] facts['spaces'] = self.get_spaces(org['entity']['spaces_url']) if 'quota_definition_url' in org['entity']: facts['quota'] = self.get_quota(org['entity']['quota_definition_url']) else: facts['quota'] = {} facts['users'] = {} for user_type in ['users', 'managers', 'billing_managers', 'auditors']: if user_type not in facts['users']: facts['users'][user_type] = [] user_url = user_type + '_url' if user_url in org['entity']: facts['users'][user_type] = self.get_users(org['entity'][user_url]) domains = self.get_private_domains(org['entity']['private_domains_url'], facts['guid']) facts.update(domains) else: facts['orgs'] = self.get_all_orgs() except CFException as e: self.module.fail_json(msg=str(e)) except Exception as e: self.module.fail_json(msg="Exception: %s" % str(e)) result = {'ansible_facts': facts} self.module.exit_json(**result)