Small cloudfoundry client implemented in python

Overview

Cloudfoundry python client

The cf-python-client repo contains a Python client library for Cloud Foundry.

Installing

Supported versions

warning: Starting version 1.11.0, versions older that python 3.6.0 will not be supported anymore. This late version was released by the end 2016.

For those that are still using python 2.7, it won't be supported by the end of 2020 and all library shall stop supporting it.

From pip

$ pip install cloudfoundry-client

From sources

To build the library run :

$ python setup.py install

Run the client

To run the client, enter the following command :

$ cloudfoundry-client

This will explains you how the client works. At first execution, it will ask you information about the platform you want to reach (url, login and so on). Please note that your credentials won't be saved on your disk: only tokens will be kept for further use.

Use the client in your code

You may build the client and use it in your code

Client

To instantiate the client, nothing easier

from cloudfoundry_client.client import CloudFoundryClient
target_endpoint = 'https://somewhere.org'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=False)
# init with user credentials
client.init_with_user_credentials('login', 'password')
# init with refresh token (that will retrieve a fresh access token)
client.init_with_token('refresh-token')
# init with access and refresh token (if the above method is not convenient)
client.refresh_token = 'refresh-token'
client._access_token = 'access-token'

It can also be instantiated with oauth code flow if you possess a dedicated oauth application with its redirection

from flask import request
from cloudfoundry_client.client import CloudFoundryClient
target_endpoint = 'https://somewhere.org'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=False, client_id='my-client-id', client_secret='my-client-secret')

@app.route('/login')
def login():
    global client
    return redirect(client.generate_authorize_url('http://localhost:9999/code', '666'))

@app.route('/code')
def code():
    global client
    client.init_authorize_code_process('http://localhost:9999/code', request.args.get('code'))

And then you can use it as follows:

for organization in client.v2.organizations:
    print(organization['metadata']['guid'])

API V2

Entities

Entities returned by api V2 calls (organization, space, app..) are navigable ie you can call the method associated with the xxx_url entity attribute (note that if the attribute's name ends with a list, it will be interpreted as a list of object. Other wise you will get a single entity).

for organization in client.v2.organizations:
    for space in organization.spaces(): # perform a GET on spaces_url attribute
        organization_reloaded = space.organization()  # perform a GET on organization_url attribute
Application object provides more methods such as
  • instances
  • stats
  • start
  • stop
  • summary

As instance, you can get all the summaries as follows:

Or else:

for app in client.v2.apps:
    print(app.summary())

Available managers

So far the implemented managers that are available are:

  • service_plans
  • service_plan_visibilities
  • service_instances
  • service_keys
  • service_bindings
  • service_brokers
  • apps
  • events
  • buildpacks
  • organizations
  • spaces
  • services
  • routes
  • shared_domains
  • private_domains
  • security_groups

Note that even if, while navigating, you reach an entity manager that does not exist, the get will be performed and you will get the expected entities. For example, event entity manager is not yet implemented but you can do

for app in client.v2.apps:
    for event in app.events():
        handle_event_object()

All managers provide the following methods:

  • list(**kwargs): return an iterator on entities, according to the given filtered parameters
  • get_first(**kwargs): return the first matching entity according to the given parameters. Returns `None if none returned
  • get: perform a GET on the entity. If the entity cannot be find it will raise an exception due to http NOT FOUND response status
  • __iter__: iteration on the manager itself. Alias for a no-filter list
  • __getitem__: alias for the get operation
  • _create: the create operation. Since it is a generic operation (only takes a dict object), this operation is protected
  • _update: the update operation. Since it is a generic operation (only takes a the resource id and a dict object), this operation is protected
  • _remove: the delete operation. This operation is maintained protected.
# Assume you have an organization named `test-org` with a guid of `test-org-guid`
org_get = client.v2.organizations.get('test-org-guid')
org_get_first = client.v2.organizations.get_first(**{'name': 'test-org'})
org_from_list = list(client.v2.organizations.list(**{'name': 'test-org'}))[0]
assert org_get == org_get_first == org_from_list

# You can also specify multiple values for a query parameter.
for organization in client.v2.organizations.list(**{'name': ['org1', 'org2']}):
    print(organization['metadata']['guid'])

# Order and Paging parameters are also supported.
query = {
    'order-by': 'name',
    'order-direction': 'desc',
    'results-per-page': 100
}
for organization in client.v2.organizations.list(**query):
    print(organization['entity']['name'])

API V3

Entities

Entities returned by API V3 calls transcripts links by providing a call on the object with the name of the link itself. Let's explain it with the next code

for app in client.v3.apps.list(space_guids='space_guid'):
  for task in app.tasks():
      print('Task %s' % task['guid'])
  app.stop()
  space = app.space()

Another example:

app = client.v3.apps['app-guid']
for task in app.tasks():
    task.cancel()
for task in client.v3.tasks.list(app_guids=['app-guid-1', 'app-guid-2']):
    task.cancel()

When supported by the API, parent entities can be included in a single call. The included entities replace the links mentioned above. The following code snippet issues three requests to the API in order to get app, space and organization data:

app = client.v3.apps.get("app-guid")
print("App name: %s" % app["name"])
space = app.space()
print("Space name: %s" % space["name"])
org = space.organization()
print("Org name: %s" % org["name"])

By changing the first line only, a single request fetches all the data. The navigation from app to space and space to organization remains unchanged.

app = client.v3.apps.get("app-guid", include="space.organization")

Available managers on API V3 are:

  • apps
  • buildpacks
  • domains
  • feature_flags
  • isolation_segments
  • jobs
  • organizations
  • organization_quotas
  • processes
  • service_brokers
  • service_credential_bindings
  • service_instances
  • service_offerings
  • service_plans
  • spaces
  • tasks

The managers provide the same methods as the V2 managers with the following differences:

  • get(**kwargs): supports keyword arguments that are passed on to the API, e.g. "include"

Networking

policy server

At the moment we have only the network policies implemented

for policy in client.network.v1.external.policies.list():
  print('destination protocol = {}'.format(policy['destination']['protocol']))
  print('destination from port = {}'.format(policy['destination']['ports']['start']))
  print('destination to port = {}'.format(policy['destination']['ports']['end']))

Available managers on API V3 are:

  • policy

This manager provides:

  • list(**kwargs): return an iterator on entities, according to the given filtered parameters
  • __iter__: iteration on the manager itself. Alias for a no-filter list
  • _create: the create operation. Since it is a generic operation (only takes a dict object), this operation is protected
  • _remove: the delete operation. This operation is maintained protected.

Application logs

Recent logs of an application can be get as follows:

app = client.v2.apps['app-guid']
for log in app.recent_logs():
    print(log)

Logs can also be streamed using a websocket as follows:

app = client.v2.apps['app-guid']
for log in app.stream_logs():
    # read message infinitely (use break to exit... it will close the underlying websocket)
    print(log)
# or
for log in client.doppler.stream_logs('app-guid'):
    # read message infinitely (use break to exit... it will close the underlying websocket)
    print(log)

Logs can also be streamed directly from RLP Gateway:

import asyncio
from cloudfoundry_client.client import CloudFoundryClient

target_endpoint = 'https://somewhere.org'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
rlp_client = CloudFoundryClient(target_endpoint, client_id='client_id', client_secret='client_secret', verify=False)
# init with client credentials
rlp_client.init_with_client_credentials()

async def get_logs_for_app(rlp_client, app_guid):
    async for log in rlp_client.rlpgateway.stream_logs(app_guid,
                                                       params={'counter': '', 'gauge': ''},
                                                       headers={'User-Agent': 'cf-python-client'})):
        print(log)

loop = asyncio.get_event_loop()
loop.create_task(get_logs_for_app(rlp_client, "app_guid"))
loop.run_forever()
loop.close()

Command Line Interface

The client comes with a command line interface. Run cloudfoundry-client command. At first execution, it will ask you information about the target platform and your credential (do not worry they are not saved). After that you may have a help by running cloudfoundry-client -h

Operations (experimental)

For now the only operation that is implemented is the push one.

from cloudfoundry_client.operations.push.push import PushOperation
operation = PushOperation(client)
operation.push(client.v2.spaces.get_first(name='My Space')['metadata']['guid'], path)

Issues and contributions

Please submit issue/pull request.

You can run tests by doing so. In the project directory:

$ export PYTHONPATH=main
$ python -m unittest discover test
# or even
$ python setup.py test
Owner
Cloud Foundry Community
Cloud Foundry Community
QuickStart specific rules for cfn-python-lint

AWS Quick Start cfn-lint rules This repo provides CloudFormation linting rules specific to AWS Quick Start guidelines, for more information see the Co

AWS Quick Start 12 Jul 30, 2022
The aim is to contain multiple models for materials discovery under a common interface

Aviary The aviary contains: - roost, - wren, cgcnn. The aim is to contain multiple models for materials discovery under a common interface Environment

Rhys Goodall 20 Jan 06, 2023
A Python IRC bot with dynamically loadable modules

pybot This is a modular, plugin-based IRC bot written in Python. Plugins can bedynamically loaded and unloaded at runtime. A design goal is the abilli

Jeff Kent 1 Aug 20, 2021
a discord bot that pulls the latest or most relevant research papers from arxiv.org

AI arxiver a discord bot that pulls the latest or most relevant research papers from arxiv.org invite link : Arxiver bot link works in progress Usage

Ensias AI 14 Sep 03, 2022
A Open source Discord Token Grabber with several very useful features coded in python 3.9

Kiwee-Grabber A Open source Discord Token Grabber with several very useful features coded in python 3.9 This only works on any python 3.9 versions. re

Vesper 40 Jan 01, 2023
An Open Source ALL-In-One Telegram RoBot, that can do lot of things.

URL Uploader Bot An Open Source ALL-In-One Telegram RoBot, that can do lot of things. My Features Installation The Easy Way You can also tap the Deplo

NT BOTS 1 Oct 23, 2021
Demonstrating attacks, mitigations, and monitoring on AWS

About Inspectaroo is a web app which allows users to upload images to view metadata. It is designed to show off many AWS services including EC2, Lambd

Alex McCormack 1 Feb 11, 2022
Python SDK for IEX Cloud

iexfinance Python SDK for IEX Cloud. Architecture mirrors that of the IEX Cloud API (and its documentation). An easy-to-use toolkit to obtain data for

Addison Lynch 640 Jan 07, 2023
A tool for extracting plain text from Wikipedia dumps

WikiExtractor WikiExtractor.py is a Python script that extracts and cleans text from a Wikipedia database dump. The tool is written in Python and requ

Giuseppe Attardi 3.2k Dec 31, 2022
A pre-attack hacker tool which aims to find out sensitives comments in HTML comment tag and to help on reconnaissance process

Find Out in Comment Find sensetive comment out in HTML ⚈ About This is a pre-attack hacker tool that searches for sensitives words in HTML comments ta

Pablo Emídio S.S 8 Dec 31, 2022
A Python script for rendering glTF files with V-Ray App SDK

V-Ray glTF viewer Overview The V-Ray glTF viewer is a set of Python scripts for the V-Ray App SDK that allow the parsing and rendering of glTF (.gltf

Chaos 24 Dec 05, 2022
Muzan-Discord-Nuker - A simple discord server nuker in python

Muzan-Discord-Nuker This is Just a simple discord server nuker in python. ✨ Feat

Afnan 3 May 14, 2022
Passive income method via SerpClix. Uses a bot to accept clicks.

SerpClixBotSearcher This bot allows you to get passive income from SerpClix. Each click is usually $0.10 (sometimes $0.05 if offer isnt from the US).

Jason Mei 3 Sep 01, 2021
FAIR Enough Metrics is an API for various FAIR Metrics Tests, written in python

☑️ FAIR Enough metrics for research FAIR Enough Metrics is an API for various FAIR Metrics Tests, written in python, conforming to the specifications

Maastricht University IDS 3 Jul 06, 2022
Migrate BiliBili watched anime to Bangumi

说明 之前为了将B站看过的动画迁移到bangumi写的, 本来只是自己用, 但公开可能对其他人会有帮助. 仓库最近无法维护, 程序有很多缺点, 欢迎 PR 和 Contributors 使用说明 Python版本要求:Python 3.8+ 使用前安装依赖包: pip install -r requ

51 Sep 08, 2022
Anti Spam/NSFW Telegram Bot Written In Python With Pyrogram.

✨ SpamProtectionRobot ✨ Anti Spam/NSFW Telegram Bot Written In Python With Pyrogram. Requirements Python = 3.7 Install Locally Or On A VPS $ git clon

Akshay Rajput 46 Dec 13, 2022
Dodo - A graphical, hackable email client based on notmuch

Dodo Dodo is a graphical email client written in Python/PyQt5, based on the comm

Aleks Kissinger 44 Nov 12, 2022
A program to convert YouTube channel registration information into Json files for ThirdTube.

ThirdTubeImporter A program to convert YouTube channel registration information into Json files for ThirdTube. Usage Japanese https://takeout.google.c

Hidegon 2 Dec 18, 2021
Código python para automatizar a junção de arquivos CSV's e salva-los em uma pasta final de destino.

merge_csv Código python para automatizar a junção de arquivos CSV's e salva-los em uma pasta final de destino. Esse projeto é usado pra unir alguns ar

Welder Fariles 1 Jan 12, 2022