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_rule(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.secg_name = self.module.params['sec_group'] def run(self): state = self.module.params['state'] try: sec_group = self.cf.search_secgroup(self.secg_name) if not sec_group: self.module.fail_json(msg='Security group not found') mode = state == "present" sec_group_guid = sec_group['metadata']['guid'] result = self.set_sec_group_rule(sec_group_guid, mode) 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 set_sec_group_rule(self, guid, mode): rule = { 'description': self.name, 'destination': self.module.params['destination'], 'protocol': self.module.params['protocol'], 'log': self.module.params['log'] } if self.module.params['ports'] is not None: rule['ports'] = self.module.params['ports'] if self.module.params['code'] is not None: rule['code'] = self.module.params['code'] if self.module.params['type'] is not None: rule['type'] = self.module.params['type'] sec_group = self.cf.manage_secgroup_rule(guid, rule, mode) changed = False msg = "CF security group %s rules not updated" % (self.name) if sec_group is not None: changed = True msg = "CF security group %s rules updated" % (self.name) result = { 'changed': changed, 'msg': msg, 'data': sec_group } 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
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))
class CF_Quota(object): system_quotas = ['default'] 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: quota = self.cf.search_quota(self.name) if state == 'present': result = self.present(quota) elif state == 'absent': if self.name in self.system_quotas and not force: self.module.fail_json( msg="Cannot delete system default quota") result = self.absent(quota) 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, quota, async=False): changed = False if quota is not None: quota_guid = quota['metadata']['guid'] changed = True if not self.module.check_mode: try: self.cf.delete_quota(quota_guid, async) except CFException as e: msg = 'Cannot delete quota %s: %s' % (self.name, str(e)) self.module.fail_json(msg=msg) result = {'changed': changed, 'msg': "CF quota %s deleted" % self.name} return result
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))
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))
def run_module(): # define the available arguments/parameters that a user can pass to # the module module_args = dict( url=dict(type='str', required=True), username=dict(type='str', required=True), password=dict(type='str', required=True, no_log=True), org=dict(type='str', required=True), space=dict(type='str', required=True), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, status=True, apps=[], apps_info=[]) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) # log in to CF cf = CF(module.params['url']) logging.debug("Logging in") cf.login(module.params['username'], module.params['password']) # find org/space information cf_org_guid = get_org_guid(cf, module.params['org']) cf_space_guid = get_space_guid(cf, cf_org_guid, module.params['space']) # request the list of apps in the org/space apps = get_apps_info(cf, cf_org_guid, cf_space_guid) logging.debug("apps = '\n" + json.dumps(apps, indent=4) + "'") result['apps_info'] = apps for app in apps: result['apps'].append(app['name']) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results module.exit_json(**result)
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))
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
#!/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_Config(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'] config_type = self.module.params['type'] mode = True if state == "present" else False try: if config_type == "feature_flag": if self.module.params['value'] is not None: mode = self.module.boolean(self.module.params['value']) result = self.set_flag(mode) elif config_type == "env_var": if self.module.params['value'] is None: self.module.fail_json(msg="No value for env var %s" % self.name) result = self.set_env(mode) elif config_type == "sec_group": result = self.set_sec_group(mode) else: self.module.fail_json(msg='Invalid type: %s' % config_type) 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 set_flag(self, status): flags = self.cf.get_feature_flags() for f in flags: if f['name'] == self.name: break else: msg = 'Feature flag not found: %s' % (self.name) self.module.fail_json(msg=msg) changed = self.cf.manage_feature_flags(self.name, status) result = { 'changed': changed, 'msg': "CF feature flag %s: %s" % (self.name, status) } return result def set_env(self, add): context = self.module.params['context'] value = self.module.params['value'] changed = self.cf.manage_variable_group(self.name, value, context, add) result = { 'changed': changed, 'msg': "CF environment variable group %s: %s" % (self.name, value) } return result def set_sec_group(self, add): sec_group = self.cf.search_secgroup(self.name) if sec_group is None: self.module.fail_json(msg="Security group %s not found" % self.name) sec_group_guid = sec_group['metadata']['guid'] context = self.module.params['context'] changed = self.cf.manage_secgroup_defaults(sec_group_guid, context, add) result = { 'changed': changed, 'msg': "CF sec group %s default %s group: %s" % (self.name, context, changed) } 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_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_Config(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'] config_type = self.module.params['type'] mode = True if state == "present" else False try: if config_type == "feature_flag": if self.module.params['value'] is not None: mode = self.module.boolean(self.module.params['value']) result = self.set_flag(mode) elif config_type == "env_var": if self.module.params['value'] is None: self.module.fail_json(msg="No value for env var %s" % self.name) result = self.set_env(mode) elif config_type == "sec_group": result = self.set_sec_group(mode) else: self.module.fail_json(msg='Invalid type: %s' % config_type) 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 set_flag(self, status): flags = self.cf.get_feature_flags() for f in flags: if f['name'] == self.name: break else: msg = 'Feature flag not found: %s' % (self.name) self.module.fail_json(msg=msg) changed = self.cf.manage_feature_flags(self.name, status) result = { 'changed': changed, 'msg': "CF feature flag %s: %s" % (self.name, status) } return result def set_env(self, add): context = self.module.params['context'] value = self.module.params['value'] changed = self.cf.manage_variable_group(self.name, value, context, add) result = { 'changed': changed, 'msg': "CF environment variable group %s: %s" % (self.name, value) } return result def set_sec_group(self, add): sec_group = self.cf.search_secgroup(self.name) if sec_group is None: self.module.fail_json(msg="Security group %s not found" % self.name) sec_group_guid = sec_group['metadata']['guid'] context = self.module.params['context'] changed = self.cf.manage_secgroup_defaults(sec_group_guid, context, add) result = { 'changed': changed, 'msg': "CF sec group %s default %s group: %s" % (self.name, context, changed) } 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_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)