Zun

please add documentation and example for python-zunclient API

Bug #1802001 reported by Jacob Burckhardt
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Zun
Fix Committed
Medium
hongbin

Bug Description

https://docs.openstack.org/python-zunclient/latest/readme.html

says:

> It provides a Python API (the zunclient module) and a command-line tool (zun).

I got the command-line tool to work. But I cannot figure out how to use the Python API.

I cannot find documentation of the API or examples of using the API when searching with Google.

Here's an example I tried:

#!/usr/bin/env python

import openstack

conn = openstack.connect(cloud='openstack', region_name='RegionOne')

for network in conn.network.networks():
    print network.name

for container in conn.application_container.containers():
    print container.name

It gave this error:

Traceback (most recent call last):
  File "./test2.py", line 10, in <module>
     for container in conn.application_container.containers():
AttributeError: 'BaseProxy' object has no attribute 'containers'

https://docs.openstack.org/openstacksdk/latest/user/connection.html

says:

> application_container
> application-container Proxy for application-container aka zun

As you can see, I put that proxy name in my code.

The error message says it has no attribute for 'containers'. Should I have used some other name instead? I guessed that name might be correct by following the example of listing networks which I also show above.

What other way can I use the Python API to list containers or do anything at all with the API?

Thanks.

hongbin (hongbin034)
Changed in zun:
status: New → Confirmed
importance: Undecided → Medium
Revision history for this message
hongbin (hongbin034) wrote :

The request for documentation is valid. We will improve the documentation for python-zunclient although I have a question on the example above.

The code you provided is using openstacksdk (not python-zunclient) to consume Zun's API. Do you prefer to use openstacksdk instead? I am not sure if Zun is supported in there but we can work on openstacksdk if this is the preferred way to consume the Zun's service. Looking forward to your remark.

Revision history for this message
Jacob Burckhardt (jburckhardt) wrote :

Hi Hongbin,

I don't have any preference for openstacksdk vs python-zunclient. I will try whichever you recommend.

Below is my attempt to use python-zunclient:

#!/usr/bin/env python
import zunclient.v1.containers
mgr = zunclient.v1.containers.ContainerManager()
mgr.list()

It said:

Traceback (most recent call last):
  File "test2.py", line 3, in <module>
    mgr = zunclient.v1.containers.ContainerManager()
TypeError: __init__() takes exactly 2 arguments (1 given)

Since ContainerManager inherits from base.Manager, I considered this code:

class Manager(object):
    """Provides CRUD operations with a particular API."""
    resource_class = None

    def __init__(self, api):
        self.api = api

So I think the problem is that I need to pass to ContainerManager() the argument list for the above __init__. But I could not figure out what to pass for api. Can you help me with that?

Thanks.

Revision history for this message
hongbin (hongbin034) wrote :

Below is the script I tried and it worked for me (I borrowed it from python-novaclient https://docs.openstack.org/python-novaclient/latest/reference/api/index.html):

from keystoneauth1 import loading
from keystoneauth1 import session
from zunclient import client

AUTH_URL='http://192.168.38.12/identity'
USERNAME='demo'
PASSWORD='XXX'
PROJECT_NAME='demo'
VERSION='1.latest' # master -> 1.latest, stable/rocky -> 1.26, stable/queens -> 1.12

loader = loading.get_plugin_loader('password')
auth = loader.load_from_options(auth_url=AUTH_URL,
                                username=USERNAME,
                                password=PASSWORD,
                                project_name=PROJECT_NAME,
                                user_domain_name='default',
                                project_domain_name='default')
sess = session.Session(auth=auth)
zun = client.Client(VERSION, session=sess)
containers = zun.containers.list()
print(containers)

Revision history for this message
hongbin (hongbin034) wrote :

And I believe 'ContainerManager' is for internal use. 'Client' is the class for external.

Changed in zun:
status: Confirmed → Triaged
assignee: nobody → hongbin (hongbin034)
Revision history for this message
Jacob Burckhardt (jburckhardt) wrote :

The only thing that was difficult was that in AUTH_URL, I changed /identity to :5000/v3

It worked great!

Thank you!

Revision history for this message
hongbin (hongbin034) wrote :

The documentation will be online once this patch is merged: https://review.openstack.org/#/c/616069/

Revision history for this message
hongbin (hongbin034) wrote :
Changed in zun:
status: Triaged → Fix Committed
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/python-zunclient 3.3.0

This issue was fixed in the openstack/python-zunclient 3.3.0 release.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.