def run(user, password, api, entity_filter, entity_fields=['name']): cf = CF(api) cf.login(user, password) result = cf.request('GET', "/v2/apps", {"results-per-page": 100}) apps = result[0] print("* Total results: %s" % apps['total_results']) data = apps['resources'] fun = "filter(lambda x: %s, data)" % entity_filter filtered = list(eval(fun)) for entity in filtered: app = entity['entity'] fields = [str(app[x]) if x in app else x for x in entity_fields] #print(app) print(" ".join(fields)) print("* Apps: %d" % len(filtered))
#!/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_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_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)