cloudfoundry的客户端库
cloudfoundry-client的Python项目详细描述
cf python client repo包含一个用于cloud foundry的python客户端库。
安装
来自PIP
$ pip install cloudfoundry-client
来源
构建库运行:
$ python setup.py install
运行客户端
要运行客户端,请输入以下命令:
$ cloudfoundry-client
这将向您解释客户是如何工作的。在第一次执行时,它会询问您想要访问的平台的信息(url、登录等等)。 请注意,您的凭据不会保存在磁盘上:将只保留令牌以供进一步使用。
在代码中使用客户端
您可以构建客户端并在代码中使用它
客户
要实例化客户机,再简单不过了
fromcloudfoundry_client.clientimportCloudFoundryClienttarget_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 credentialsclient.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'
然后您可以按如下方式使用它:
fororganizationinclient.v2.organizations:print(organization['metadata']['guid'])
API v2
实体
api v2调用返回的实体(organization,space,app)是可导航的,即您可以调用与xxx\u url实体属性关联的方法。 (注意,如果属性的名称以列表结尾,则它将被解释为对象列表。否则你会得到一个实体)。
fororganizationinclient.v2.organizations:forspaceinorganization.spaces():# perform a GET on spaces_url attributeorganization_reloaded=space.organization()# perform a GET on organization_url attribute
- 应用程序对象提供了更多方法,例如
- 实例
- 统计
- 开始
- 停止
- 摘要
例如,您可以获得以下所有摘要:
否则:
forappinclient.v2.apps:print(app.summary())
可用经理
到目前为止,已实施的可用管理器有:
- service_plans
- service_instances
- service_keys
- service_bindings
- service_brokers
- apps
- buildpacks
- organizations
- spaces
- services
- routes
- shared_domains
- private_domains
- security_groups
注意,即使在导航时,你到达了一个不存在的实体管理器,GET将被执行,并且你将得到预期的实体。 例如,事件实体管理器尚未实现,但您可以执行
forappinclient.v2.apps:foreventinapp.events():handle_event_object()
所有经理都提供以下方法:
- list(**kwargs):根据给定的筛选参数,返回实体上的iterator
- get_first(**kwargs):根据给定的参数返回第一个匹配的实体。如果未返回`None,则返回`None。
- get:对实体执行get。如果找不到实体,它将引发异常,原因是httpnot found响应状态
- __iter__:管理器本身的迭代。无筛选器列表的别名
- __getitem__:用于get操作的别名
- _create:创建操作。由于它是一个泛型操作(只接受一个dict对象),因此该操作受到保护
- _update:更新操作。因为它是一个泛型操作(只接受一个资源id和一个dict对象),所以该操作受到保护
- _remove:删除操作。此操作保持受保护。
# 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]assertorg_get==org_get_first==org_from_list# You can also specify multiple values for a query parameter.fororganizationinclient.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}fororganizationinclient.v2.organizations.list(**query):print(organization['entity']['name'])
API V3
实体
api v3返回的实体通过提供对具有链接本身名称的对象的调用来调用脚本链接。 让我们用下一个代码来解释它
forappinclient.v3.apps.list(space_guids='space_guid'):fortaskinapp.tasks():print('Task %s'%task['guid'])app.stop()space=app.space()
另一个例子:
app=client.v3.apps['app-guid']fortaskinapp.tasks():task.cancel()fortaskinclient.v3.tasks.list(app_guids=['app-guid-1','app-guid-2']):task.cancel()
API V3上可用的管理器是:
- apps
- organizations
- spaces
- tasks
管理器提供的方法与v2管理器相同。
应用程序日志
应用程序的最新日志如下:
app=client.v2.apps['app-guid']forloginapp.recent_logs():print(log)
日志也可以使用websocket进行流式传输,如下所示:
app=client.v2.apps['app-guid']forloginapp.stream_logs():# read message infinitely (use break to exit... it will close the underlying websocket)print(log)# orforloginclient.doppler.stream_logs('app-guid'):# read message infinitely (use break to exit... it will close the underlying websocket)print(log)
命令行界面
客户带有一个逗号D线接口。运行cloudfoundry-client命令。在第一次执行时,它将询问有关目标平台和凭据的信息(不要担心它们不会被保存)。之后,您可以通过运行cloudfoundry-client-h
操作(实验)
目前唯一实现的操作是push操作。
fromcloudfoundry_client.operations.push.pushimportPushOperationoperation=PushOperation(client)operation.push(client.v2.spaces.get_first(name='My Space')['metadata']['guid'],path)
问题和贡献
请提交问题/请求。
您可以通过这样做来运行测试。在项目目录中:
$ exportPYTHONPATH=main $ python -m unittest discover test# or even $ python setup.py test