Giter Site home page Giter Site logo

cf-python-client's Introduction

Cloudfoundry python client

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

Installing

Supported versions

  • Starting version 1.11.0, versions older than 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.
  • Starting version 1.25.0, versions older than python 3.7.0 will not be supported anymore.
  • Starting version 1.36.0, versions older than python 3.8.0 will not be supported anymore.

See official documentation.

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'

You can also instantiate the client by reading the config file generated by cf login, which allows for authenticating via SSO and LDAP:

# init with endpoint & token from the cf cli config file
from cloudfoundry_client.client import CloudFoundryClient

# use the default file, i.e. ~/.cf/config.json
client = CloudFoundryClient.build_from_cf_config()
# or specify an alternative path
# - other kwargs can be passed through to CloudFoundryClient instantiation
client = CloudFoundryClient.build_from_cf_config(config_path="some/path/config.json", proxy=proxy, verify=False)

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
  • roles
  • security_groups
  • 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

cf-python-client's People

Contributors

adililhan avatar alexw19 avatar almoghamdani avatar alogishetty avatar andy-paine avatar antechrestos avatar bonfilsd avatar bpandola avatar cagiti avatar dandersonsw avatar dependabot[bot] avatar dercoop avatar diogomatsubara avatar flothinkspi avatar fouadh avatar hsdp-smulford avatar jochenehret avatar johha avatar kathap avatar krezreb avatar mmisoch avatar mpw96 avatar philippthun avatar romain-dartigues avatar subhash12-smarsh avatar svenxy avatar svkrieger avatar tirkarthi avatar will-gant avatar yusufsheiqh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cf-python-client's Issues

New feature:request

Hi Benjamin, the cf client currently does not support cf bluegreen deployment cf plugin. Can we work on this?

Getting log of applications

Is it possible to get logs of an application using this plugin cloudfoundry client ? I can't see any way of doing this.

Thanks in advance :)

async service instance provisioning

Any plans to support ?accepts_incomplete=true when creating a service instance?

I'm OK with having my application manage the polling if I have to.

But right now I'm blocked from provisioning an instance of an async-only service plan using this library. The Cloud Controller just returns an error:

{
  "description": "This service plan requires client support for asynchronous service operations.", 
  "error_code": "CF-AsyncRequired",
  "code": 10001,
  "http": {
    "uri": "https://my-service-broker.whatever.org/v2/service_instances/6aa18f6f-6eff-4eb1-9576-26882022285b",
    "method": "PUT",
    "status": 422
  }
}

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)

Hello,

Started using the client to connect to my PCF instance but got the below exception:

Error Stack

Traceback (most recent call last):
File "<my_install_location>\Python\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "<my_install_location>\Python\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "<my_install_location>\Python\lib\site-packages\urllib3\connectionpool.py", line 839, in validate_conn
conn.connect()
File "<my_install_location>\Python\lib\site-packages\urllib3\connection.py", line 344, in connect
ssl_context=context)
File "<my_install_location>\Python\lib\site-packages\urllib3\util\ssl
.py", line 344, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "<my_install_location>\Python\lib\ssl.py", line 412, in wrap_socket
session=session
File "<my_install_location>\Python\lib\ssl.py", line 853, in _create
self.do_handshake()
File "<my_install_location>\Python\lib\ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<my_install_location>\Python\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "<my_install_location>\Python\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "<my_install_location>\Python\lib\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='<My_Server_Host>', port=443): Max retries exceeded with url: /v2/info (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<my_install_location>\Python\Scripts\cloudfoundry-client-script.py", line 11, in
load_entry_point('cloudfoundry-client==1.6.0', 'console_scripts', 'cloudfoundry-client')()
File "<my_install_location>\Python\lib\site-packages\cloudfoundry_client-1.6.0-py3.7.egg\cloudfoundry_client\main\main.py", line 224, in main
File "<my_install_location>\Python\lib\site-packages\cloudfoundry_client-1.6.0-py3.7.egg\cloudfoundry_client\main\main.py", line 88, in build_client_from_configuration
File "<my_install_location>\Python\lib\site-packages\cloudfoundry_client-1.6.0-py3.7.egg\cloudfoundry_client\client.py", line 92, in init
File "<my_install_location>\Python\lib\site-packages\cloudfoundry_client-1.6.0-py3.7.egg\cloudfoundry_client\client.py", line 120, in _get_info
File "<my_install_location>\Python\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "<my_install_location>\Python\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "<my_install_location>\Python\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "<my_install_location>\Python\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "<my_install_location>\Python\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='<My_Server_Host>', port=443): Max retries exceeded with url: /v2/info (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056)')))

Unable to authenticated to the PCF environment

Hello, I'm running into an issue while authentication with username and password.

The endpoint that I'm using I know it works perfectly when accessing via browser. Any tips that I can try will be really appreciated.

raise InvalidStatusCode(response.status_code, body)

cloudfoundry_client.errors.InvalidStatusCode: 404 :

<title>404 Not Found</title>

404 Not Found


nginx

ERROR: Cannot uninstall 'PyYAML'

ERROR: Cannot uninstall 'PyYAML' is shown during pip install cloudfoundry-client.
Temp fix pip install cloudfoundry-client --ignore-installed PyYAML.

pip 19.1.1 with Python 3.7 on Mac 10.14.5

Thanks for your attention.

invalid URL when following multiple pages results

Hi,

I'm putting this here to not forget; sorry for the short message.

How to reproduce:

>>> cf = cloudfoundry_client.CloudFoundryClient(url, skip_verification=True)
>>> cf.init_with_credentials(username, password)
>>> applications = t(list, cf.api.application.list())
Traceback (most recent call last):
  ...
  File "/usr/lib/python2.7/dist-packages/requests/models.py", line 360, in prepare_url
    "Perhaps you meant http://{0}?".format(url))
requests.exceptions.MissingSchema: Invalid URL u'/v2/apps?order-direction=asc&page=2&results-per-page=50': No schema supplied. Perhaps you meant http:///v2/apps?order-direction=asc&page=2&results-per-page=50?

Versions:

  • CloudFoundry api_version 2.47.0
  • cloudfoundry_client version 0.0.8
  • requests version 2.5.1

For the very little I saw, CF is sending a relative path in it's Location header, and the Python module does not try to prepend the target endpoint.

Manager App v2/v3 shows PCF title services

Hi I couldn't find documentation on how to retrieves services attached to an specific app.

  1. Another thing how do you set a specific org, space in the code?
  2. Is there limitation on the apps in context of pagination - what is the limit?

Scheduler API

Hi Benjamin,

you have helped in past for incorporating task api. On similar line can we have PCF scheduler API available in Python. Please refer to below link for reference it allows create, update, list and delete schedule.

http://docs.pivotal.io/pcf-scheduler/1-0/api/#schedules

if it is already available then can you please share the example how can i do that.

Thanks a lot in advance
Parag

login with different url

when I entered the endpoint for cloudfoundry-client, it saved as a token, but how to disable the token saved but re-ask the endpoint everytime hitting the cloudfoundry-client?

how to list all events?

i want to get the event of
app.crash
audit.app.create
audit.app.delete-request
audit.app.droplet.mapped
audit.app.map-route
audit.app.package.create
audit.app.package.delete
audit.app.package.download
audit.app.package.upload
audit.app.restage
audit.app.ssh-authorized
audit.app.ssh-unauthorized
audit.app.start
audit.app.stop

how can i get using the api?

Restart a specific instance of service

Hello,
I am wondering if there is a way to restart a specific instance of a service, equivalent to cf restart-app-instance command, using this cf python client. I went through the examples but could not find a way to do it.
Using the v2 APIs, I can do it using the following:
DELETE /v2/apps/{app_guid}/instances/0?async=true

Thanks
Siva

Default existing entity attributes to v2

This is sort of moot, because I've already fixed my code, but I think it would have been nice to have the v2/v3 stuff not be a breaking change. In other words, existing code written like client.organizations could be automatically routed to client.v2.organizations behind the scenes. Instead I had to deal with a number of AttributeError: 'CloudFoundryClient' object has no attribute 'organizations' when I tried to upgrade to a newer version of your (very useful!) package.

AttributeError: 'ServicePlanManager' object has no attribute 'get_one'

All managers provide the following methods:

list(**kwargs): return an iterator on entities, according to the given filtered parameters
get_one(**kwargs): return the first matching entity according to the given parameters. Returns `None if none returned
...

I'm not sure if this is a bug or my misunderstanding of the api:

client.service_plans.get_one(service_guid='12345')

returns

AttributeError: 'ServicePlanManager' object has no attribute 'get_one'

Error 'CloudFoundryClient' object has no attribute 'v3'

Hi,

I have installed latest package. As per instruction i have first created client object by calling

client = CloudFoundryClient(target_endpoint, verify=false)
client.init_with_user_credentials(username, password)

this gives me client object

for V3 as per suggestion when i am trying
app = client.v3.apps['guid']

i am getting error CloudFoundryClient has no attribute v3

am i missing some step?

Thanks a lot in advance

pip install fails

I get the following error when trying to install.

pip install cloudfoundry-client

Collecting cloudfoundry-client
Using cached cloudfoundry-client-0.0.14.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-build-efqba8/cloudfoundry-client/setup.py", line 71, in
tests_require=[line.rstrip() for line in open('test/requirements.txt')],
IOError: [Errno 2] No such file or directory: 'test/requirements.txt'

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-efqba8/cloudfoundry-client/

not able/failing to stop app

successfully instantiating client and retrieving an app with
app = client.v2.apps.get_first(**{'name': <app_name>})

successfully checking app object with
type(app)
app.summary()

but getting this exception when running
app.stop()
would you please help? is there anything I maybe missing?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/v2/apps.py", line 22, in stop
    return self.client.v2.apps.stop(self['metadata']['guid'])
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/v2/apps.py", line 89, in stop
    result = super(AppManager, self)._update(application_guid, dict(state='STOPPED'))
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/v2/entities.py", line 79, in _update
    return self._put(url, data, **kwargs)
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/v2/entities.py", line 91, in _put
    response = self.client.put(url, json=data, **kwargs)
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/client.py", line 175, in put
    return CloudFoundryClient._check_response(response)
  File "/Library/Python/2.7/site-packages/cloudfoundry_client/client.py", line 194, in _check_response
    raise InvalidStatusCode(response.status_code, body)
cloudfoundry_client.errors.InvalidStatusCode: 400 : {"code": 1001, "error_code": "CF-MessageParseError", "description": "Request invalid due to parse error: Decoded JSON cannot be nil"}

Recommendation for sorting (e.g. by application name)

Based on the way the objects are created from the REST API, is there a recommended way to sort the contents of certain objects? For example, what is the best way to print all applications across all spaces alphabetically?

Thanks!

[ERROR] Pushing app manifest

I'm getting this error while i'm trying to push a manifest with PushOperation

Traceback (most recent call last):
  File "/opt/project/launch.py", line 87, in <module>
    cfy.deploy_application_new()
  File "/opt/project/main/cloudfoundryoperation.py", line 253, in deploy_application_new
    operation = operation.push(self.space_guid, './manifest.yml', restart=False)
  File "/usr/local/lib/python3.7/site-packages/cloudfoundry_client/operations/push/push.py", line 29, in push
    self._push_application(organization, space, app_manifest, restart)
  File "/usr/local/lib/python3.7/site-packages/cloudfoundry_client/operations/push/push.py", line 39, in _push_application
    app_manifest.get('routes', []), app_manifest.get('random-route', False))
  File "/usr/local/lib/python3.7/site-packages/cloudfoundry_client/operations/push/push.py", line 96, in _route_application
    self._build_default_route(space, app, random_route)
  File "/usr/local/lib/python3.7/site-packages/cloudfoundry_client/operations/push/push.py", line 107, in _build_default_route
    if not domain['entity']['internal']:
KeyError: 'internal'

In fact when I execute cf curl /v2/domains there is no internal in entity.

Just for more information, i'm using API version 2.84.0.

better documentation on how to use

I am trying to use basic features in a python program like starting/stopping an app and checking the status. It is not apparent how to do this. The two samples in the readme are not complete and do not even run. For example, the example printing all of the organizations in the target does not show how to create client, the sample just starts using client. Can you give a complete sample how to use the library from a python program? Maybe a sample how to check on the status of an app for a particular endpoint, organization, space, and app.

Change error message when application is unreachable?

I sometimes get the following message:

IncompleteRead: IncompleteRead(0 bytes read, 12 more expected)

I think it's when the library tries to ping the application without success. Could you change it to a more explicit message?

oAuth Token

Hi Benjamin,

cf core api allows us to get oAuth Token. on Cli we ran cf oauth-token to retrieve it. We do authenticate user first before calling API.

Do we have mechanism to retrieve oauth-token? can you please share the example to do that.
If it is not there then can we have that as part of module.

Thanks a lot in advance
Parag

Loggregator is going away

I'm told that the loggregator endpoint has been deprecated for a long time, and neither Swisscom nor Pivotal provide a logging_endpoint in their info anymore. This causes the client to fail to initialize:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/licquia/Projects/certification/cf-python-client/venv/lib/python3.5/site-packages/cloudfoundry_client-0.0.20-py3.5.egg/cloudfoundry_client/client.py", line 40, in __init__
KeyError: 'logging_endpoint'

cf task

Hi All,

I want to retrieve task created / attached to my application and want to schedule it to run. Can you please let me know how can i achieve it using cf-python-client.

Thanks a lot in advance
Parag

Iterating through Orgs, Spaces and Apps (V2)

Hello,

Firstly, Thanks for creating the python client, it has made my life so much simpler.

I have been trying to iterate through all orgs, spaces, and apps that I have access to in PCF using v2 as it is straight forward. However, I have noticed that whenever the number of apps goes beyond 50, we get the following error cloudfoundry_client.errors.InvalidStatusCode: 404 : {"description": "Unknown request", "error_code": "CF-NotFound", "code": 10000}.

I am adding the code snippet, error details, runtime and operating system details below for your reference. Please let me know if there is a better way to iterate through the orgs, spaces an apps using v3.

Code Snippet

for organization in client.v2.organizations:
    for space in organization.spaces():
        for app in space.apps():
            print(app)

Error Message

Traceback (most recent call last):
  File "main.py", line 36, in <module>
    main()
  File "main.py", line 31, in main
    for app in space.apps():
  File "\lib\site-packages\cloudfoundry_client\v2\entities.py", line 75, in _list
    response = self.client.get(url_requested)
  File "\lib\site-packages\cloudfoundry_client\client.py", line 170, in get
    return CloudFoundryClient._check_response(response)
  File "\lib\site-packages\cloudfoundry_client\client.py", line 197, in _check_response
    raise InvalidStatusCode(response.status_code, body)
cloudfoundry_client.errors.InvalidStatusCode: 404 : {"description": "Unknown request", "error_code": "CF-NotFound", "code": 10000}

Runtime

Python 3.7.4

Operating System

Windows 10

Thank you.

Services

Hi, any plans on continuing with this project and also making services, instances, bindings (looks you have implemented them already) available through your CLI? Cheers, Vedran

Cloud Foundry API Version

Can you please document the versions of the Cloud Foundry Restful API that this client works with.

We have been trying to use it with 2.54 and have discovered this is not a working against this CF REST API but does work against Pivotal's latest public offering

NoneType' object has no attribute 'items'

Hi There

Stumbled upon this issue when building out a flask application but have also noticed this when just testing the code in a simple consistent loop.

Looking for pointers if there is something obvious I am missing in this.

Using Username/Password to authenticate.

Error usually occurs after 10-15 mins of running without issue.

SCRIPT CODE

foundation_name = 'lab'
target_endpoint = '<URL>'

proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=False)

client.init_with_user_credentials('<USERNAME>','<PAASWORD>')

def app_buildpacks():
	app_buildpack_usage = []
	app_buildpack_table_field_names = ['App Name', 'Buildpack Used', ' Buildpack Stack', 'GUID', 'Filename' , 'Locked']

	
	for app in client.v2.apps:
		app_name = app['entity']['name']
		app_buildpack_name = app['entity']['detected_buildpack'] or app['entity']['buildpack'] 
		app_buildpack_guid = app['entity']['detected_buildpack_guid']


		for buildpack in client.v2.buildpacks:

			buildpack_guid = buildpack['metadata']['guid']

			if app_buildpack_guid == buildpack_guid:

				buildpack_name = buildpack['entity']['name']
				buildpack_stack = buildpack['entity']['stack']
				buildpack_filename = buildpack['entity']['filename']
				buildpack_lock = buildpack['entity']['locked']

				d = { 
				'app_name' : app_name , 
				'buildpack_name' : buildpack_name,
				'buildpack_stack' : buildpack_stack , 
				'buildpack_guid' : buildpack_guid, 
				'buildpack_filename': buildpack_filename,
				'buildpack_lock' : buildpack_lock
				}

				app_buildpack_usage.append(d)

	# Sort the list by name 
	app_buildpack_usage = sorted(app_buildpack_usage, key = lambda i: i['buildpack_name'])
	
	# Return the two values ( Builpack usage list of dicts and table headers )
	return app_buildpack_usage,app_buildpack_table_field_names


def background():
  global app_buildpack_usage, app_buildpack_table_field_names

  while True:
    try:
        app_buildpack_usage,app_buildpack_table_field_names = app_buildpacks()



def foreground():
  @app.route('/app_buildpacks')
  def app_buildpacks_page():
    return render_template('app_buildpack_table.html', colnames=app_buildpack_table_field_names, app_buildpack_usage=app_buildpack_usage)



b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)


b.start()
f.start()

ERROR

  \Python37\lib\site-packages\cloudfoundry_client\v2\entities.py", line 66, in _list
    yield entity_builder(list(resource.items()))

  \Python37\lib\site-packages\cloudfoundry_client\v2\apps.py", line 51, in <lambda>
    lambda pairs: _Application(target_endpoint, client, pairs))

  \Python37\lib\site-packages\cloudfoundry_client\v2\entities.py", line 17, in __init__
    for attribute, value in list(self['entity'].items()):

AttributeError: 'NoneType' object has no attribute 'items'

Example of authenticate with OAuth token

Hi all,

I would like to know how to setup CloudFoundryClient with OAuth token?
Or how can I setup CloudFoundryClient with Mac OS X Keychain?
Thanks for your kind help and attention.

Regards,
Alan

I'm unable to get app.stats()

I'm trying to get the current stats using the following code

code

for organization in client.v2.organizations:
for space in organization.spaces():
for app in space.apps():
print(app.stats())

Error

Traceback (most recent call last):
File "C:\Users\FakeUser\Desktop\cf_python_test\app_stats.py", line 27, in
print(app.stats())
File "C:\Users\FakeUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cloudfoundry_client\v2\apps.py", line 30, in stats
return self.client.v2.apps.get_stats(self['metadata']['guid'])
File "C:\Users\FakeUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cloudfoundry_client\v2\apps.py", line 59, in get_stats
return self._get('%s/%s/stats' % (self.entity_uri, application_guid), JsonObject)
File "C:\Users\FakeUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cloudfoundry_client\v2\entities.py", line 67, in _get
response = self.client.get(url)
File "C:\Users\FakeUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cloudfoundry_client\client.py", line 225, in get
return CloudFoundryClient._check_response(response)
File "C:\Users\FakeUser\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cloudfoundry_client\client.py", line 252, in _check_response
raise InvalidStatusCode(response.status_code, body)
cloudfoundry_client.errors.InvalidStatusCode: <exception str() failed>

I'm on Windows 10 using Python 3.8.2

Create space in org

Thank you for your work!

Is it possible to create space in some org via this lib? I need alternative for this shell command:

cf create-space SPACE -o ORG

RLP gateway support

Current client does not have an implementation for reading apps logs directly from RLP gateway. Are there plans to add it?

ImportError: No module named google.protobuf

I keep hitting the following error. Sometimes it goes away if I run !pip install --user --upgrade --force protobuf first:

----> 1 from cloudfoundry_client.client import CloudFoundryClient
     2 
     3 class CloudFoundryUtil:
     4 
     5     def __init__(self, target_endpoint, username, password):

/gpfs/fs01/user/xxxxx/.local/lib/python2.7/site-packages/cloudfoundry_client/client.py in <module>()
     6 
     7 from cloudfoundry_client.entities import InvalidStatusCode, EntityManager
----> 8 from cloudfoundry_client.loggregator.loggregator import LoggregatorManager
     9 from cloudfoundry_client.v2.apps import AppManager
    10 from cloudfoundry_client.v2.buildpacks import BuildpackManager

/gpfs/fs01/user/xxxxx/.local/lib/python2.7/site-packages/cloudfoundry_client/loggregator/loggregator.py in <module>()
     1 import logging
     2 import re
----> 3 from cloudfoundry_client.loggregator.logmessage_pb2 import LogMessage
     4 from cloudfoundry_client.entities import EntityManager
     5

/gpfs/fs01/user/xxxxx/.local/lib/python2.7/site-packages/cloudfoundry_client/loggregator/logmessage_pb2.py in <module>()
     2 # source: logmessage.proto
     3 
----> 4 from google.protobuf import descriptor as _descriptor
     5 from google.protobuf import message as _message
     6 from google.protobuf import reflection as _reflection

ImportError: No module named google.protobuf

Authentication issue on init (oauth2_client.credentials_manager.OAuthError)

I believe I have run into some sort of dependency problem here, where I cannot figure out what common libraries / binaries may be expected to be available for this library (or probably the oauth2-client) to initialize properly.

I am running on python 3.7.6 with oauth2-client==1.2.1 and cloudfoundry-client==1.12.4.

When trying to call client.init_with_user_credentials(CF_USER, CF_SECRET) or use the commandline tool (cloudfoundry-client) I get the following error:

Traceback (most recent call last):
  File "/home/vcap/deps/0/bin/cloudfoundry-client", line 11, in <module>
    load_entry_point('cloudfoundry-client==1.12.4', 'console_scripts', 'cloudfoundry-client')()
  File "/home/vcap/deps/0/python/lib/python3.7/site-packages/cloudfoundry_client/main/main.py", line 238, in main
    client = build_client_from_configuration()
  File "/home/vcap/deps/0/python/lib/python3.7/site-packages/cloudfoundry_client/main/main.py", line 92, in build_client_from_configuration
    client.init_with_user_credentials(login, password)
  File "/home/vcap/deps/0/python/lib/python3.7/site-packages/oauth2_client/credentials_manager.py", line 140, in init_with_user_credentials
    self._token_request(self._grant_password_request(login, password), True)
  File "/home/vcap/deps/0/python/lib/python3.7/site-packages/oauth2_client/credentials_manager.py", line 190, in _token_request
    CredentialManager._handle_bad_response(response)
  File "/home/vcap/deps/0/python/lib/python3.7/site-packages/oauth2_client/credentials_manager.py", line 78, in _handle_bad_response
    raise OAuthError(HTTPStatus(response.status_code), error.get('error'), error.get('error_description'))
oauth2_client.credentials_manager.OAuthError: 401  - unauthorized : Bad credentials

I have made sure 100 times that the credentials are correct :)

In fact, I have initially been using the exact same code and environment setup successfully, but I was running that code inside a Docker container on CloudFoundry. (This Docker container was based on an official python Docker container: python:3-slim-stretch). I have since changed my CF deployment to no longer use Docker containers, but now use the "native" python_buildpack.

Since then I am seeing the error above and assume that there may be some OS level dependency that is now missing? Is there any advice you could possibly offer to determine any

What have I tried?

  1. curl -X GET "https://api.$CF_DOMAIN" (inside a deployed CF app)
  • works just fine
  1. requests.get("https://api.$CF_DOMAIN") (inside a deployed CF app)
  • also works well
  1. cloudfoundry-client (inside a deployed CF app)
  • manually entering the API endpoint and login credentials
  • results in the above error
  1. client.init_with_user_credentials(CF_USER, CF_SECRET) (inside a deployed CF app)
  • using system level environment variables as well as manually executing this in a python console
  • either way I end up with the above error
  1. I also still have a deployment running inside that "old" Docker container mentioned above, where everything actually continues to work as expected and I don't see any authentication errors. I therefore assume that neither the CF installation nor my account are causing the issue.

ValueError: Domain not found for action None

I am getting "ValueError: Domain not found" after entering the endpoint and password, target endpoint getting from cf api, and login and password are the ones I use for AppsManager credential.

Please enter a target endpoint []: https://api.[...]
Verify ssl (true/false) [true]: false
Please enter your login []: xxx
Please enter your password []: xxxx

raise ValueError("Domain not found for action %s" % arguments.action)
ValueError: Domain not found for action None

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.