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(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)
#!/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)