Discussion:
[openstack-dev] [sdk] issues with using OpenStack SDK Python client
g***@wipro.com
2018-05-04 18:34:36 UTC
Permalink
Hi everybody,

As a bit of a novice, I'm trying to use OpenStack SDK 0.13 in an OPNFV/ONAP project (Auto).

I'm able to use the compute and network proxies, but have problems with the identity proxy,
so I can't create projects and users.
With network, I can create a network, a router, router interfaces, but can't add a gateway to a router. Also, deleting a router fails.
With compute, I can't create flavors, and not sure if there is a "create_image" method ?

Specific issues are listed below with more details.

Any pointers (configuration, installation, usage, ...) and URLs to examples and documentation would be welcome.
For documentation, I've been looking mostly at:
https://docs.openstack.org/openstacksdk/latest/user/proxies/network.html
https://docs.openstack.org/openstacksdk/latest/user/proxies/compute.html
https://docs.openstack.org/openstacksdk/latest/user/proxies/identity_v3.html

Thanks in advance,
Gerard




For all code, import statement and Connection creation is as follows (constants defined before):
import openstack
conn = openstack.connect(cloud=OPENSTACK_CLOUD_NAME, region_name=OPENSTACK_REGION_NAME)


1) problem adding a gateway (external network) to a router:
not sure how to build a dictionary body (couldn't find examples online)

tried this:
network_dict_body = {'network_id': public_network.id}

and this (from looking at a router printout):
network_dict_body = {
'external_fixed_ips': [{'subnet_id' : public_subnet.id}],
'network_id': public_network.id
}

in both cases, tried this command:
conn.network.add_gateway_to_router(onap_router,network_dict_body)

getting the error:
Exception: <class 'TypeError'> add_gateway_to_router() takes 2 positional arguments but 3 were given

printing the router gave this:
openstack.network.v2.router.Router(distributed=False, tenant_id=03aa47d3bcfd48199e0470b1c86a7f5b, created_at=2018-05-01T01:16:08Z, external_gateway_info=None, status=ACTIVE, availability_zone_hints=[], ha=False, tags=[], description=Router created for ONAP, admin_state_up=True, revision=1, flavor_id=None, id=b923fba5-5027-47b6-b679-29c331ac1aba, updated_at=2018-05-01T01:16:08Z, routes=[], name=onap_router, availability_zones=[])


2) problem deleting routers:
onap_router = conn.network.find_router(ONAP_ROUTER_NAME)
conn.network.delete_router(onap_router.id)
(same if conn.network.delete_router(onap_router))

getting the error:
Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'

printing the router that had been created gave this:
openstack.network.v2.router.Router(description=Router created for ONAP, status=ACTIVE, routes=[], updated_at=2018-05-01T01:16:11Z, ha=False, id=b923fba5-5027-47b6-b679-29c331ac1aba, external_gateway_info=None, admin_state_up=True, availability_zone_hints=[], tenant_id=03aa47d3bcfd48199e0470b1c86a7f5b, name=onap_router, availability_zones=['nova'], tags=[], revision=3, distributed=False, flavor_id=None, created_at=2018-05-01T01:16:08Z)



3) problem reaching the identity service:
(although I can reach compute and network services, and although there are users and projects in the Openstack instance: "admin" and "service" projects, "ceilometer", "nova", etc. (and "admin") users)

print("\nList Users:")
i=1
for user in conn.identity.users():
print('User',str(i),'\n',user,'\n')
i+=1

getting the error:
List Users:
Exception: <class 'openstack.exceptions.NotFoundException'> NotFoundException: 404

print("\nList Projects:")
i=1
for project in conn.identity.projects():
print('Project',str(i),'\n',project,'\n')
i+=1

also getting an error, but not the same as users:
List Projects:
Exception: <class 'AttributeError'> 'Proxy' object has no attribute 'projects'


if trying to create a project:

onap_project = conn.identity.find_project(ONAP_TENANT_NAME)
if onap_project != None:
print('ONAP project/tenant already exists')
else:
print('Creating ONAP project/tenant...')
onap_project = conn.identity.create_project(
name = ONAP_TENANT_NAME,
description = ONAP_TENANT_DESC,
is_enabled = True)


getting the error:
Exception: <class 'AttributeError'> 'Proxy' object has no attribute 'find_project'


4) problem creating flavors:

tiny_flavor = conn.compute.find_flavor("m1.tiny")
if tiny_flavor != None:
print('m1.tiny Flavor already exists')
else:
print('Creating m1.tiny Flavor...')
tiny_flavor = conn.compute.create_flavor(
name = 'm1.tiny',
vcpus = 1,
disk = 1,
ram = 512,
ephemeral = 0,
#swap = 0,
#rxtx_factor = 1.0,
is_public = True)

(by the way, maybe swap and rxtx are not supposed to be set ?)

getting the error:
Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'


5) how to create images ?
there is a compute proxy method: find_image()
but it looks like there is no create_image() ?

say you wanted to add this image: Ubuntu Server 16.04 LTS (Xenial Xerus)
https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
how would you do it ?



openstacksdk version 0.13.0 is installed:

$ pip3 list
Package Version
------------------- ---------
appdirs 1.4.3
certifi 2018.4.16
chardet 3.0.4
command-not-found 0.3
decorator 4.3.0
deprecation 2.0.2
dogpile.cache 0.6.5
idna 2.6
iso8601 0.1.12
jmespath 0.9.3
jsonpatch 1.23
jsonpointer 2.0
keystoneauth1 3.5.0
language-selector 0.1
munch 2.3.1
netifaces 0.10.6
openstacksdk 0.13.0
os-service-types 1.2.0
packaging 17.1
pbr 4.0.2
pip 10.0.1
pycurl 7.43.0
pygobject 3.20.0
pyparsing 2.2.0
python-apt 1.1.0b1
python-debian 0.1.27
python-systemd 231
PyYAML 3.12
requests 2.18.4
requestsexceptions 1.4.0
setuptools 20.7.0
six 1.10.0
ssh-import-id 5.5
stevedore 1.28.0
ufw 0.35
unattended-upgrades 0.1
urllib3 1.22
wheel 0.29.0

$ pip3 check
No broken requirements found.



The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. www.wipro.com
Monty Taylor
2018-05-04 19:15:43 UTC
Permalink
Post by g***@wipro.com
Hi everybody,
As a bit of a novice, I'm trying to use OpenStack SDK 0.13 in an OPNFV/ONAP project (Auto).
Yay! Welcome.
Post by g***@wipro.com
I'm able to use the compute and network proxies, but have problems with the identity proxy,
so I can't create projects and users.
With network, I can create a network, a router, router interfaces, but
can't add a gateway to a router. Also, deleting a router fails.
With compute, I can't create flavors, and not sure if there is a "create_image" method ?
Specific issues are listed below with more details.
Any pointers (configuration, installation, usage, ...) and URLs to
examples and documentation would be welcome.
https://docs.openstack.org/openstacksdk/latest/user/proxies/network.html
https://docs.openstack.org/openstacksdk/latest/user/proxies/compute.html
https://docs.openstack.org/openstacksdk/latest/user/proxies/identity_v3.html
Yes, that's all good documentation to follow.
Post by g***@wipro.com
Thanks in advance,
Gerard
For all code, import statement and Connection creation is as follows
import openstack
conn = openstack.connect(cloud=OPENSTACK_CLOUD_NAME,
region_name=OPENSTACK_REGION_NAME)
not sure how to build a dictionary body (couldn't find examples online)
network_dict_body = {'network_id': public_network.id}
network_dict_body = {
    'external_fixed_ips': [{'subnet_id' : public_subnet.id}],
    'network_id': public_network.id
}
conn.network.add_gateway_to_router(onap_router,network_dict_body)
Exception: <class 'TypeError'> add_gateway_to_router() takes 2
positional arguments but 3 were given
The signature for add_gateway_to_router looks like this:

def add_gateway_to_router(self, router, **body):

the ** indicate that it's looking for the body as keyword arguments.
Change your code to:

conn.network.add_gateway_to_router(onap_router, **network_dict_body)

and it should work.

You could also, should you feel like it, do:

conn.network.add_gateway_to_router(
onap_router,
external_fixed_ips=[{'subnet_id' : public_subnet.id}],
network_id=public_network.id
)

which is basically what the ** in **network_dict_body is doing.
Post by g***@wipro.com
openstack.network.v2.router.Router(distributed=False,
tenant_id=03aa47d3bcfd48199e0470b1c86a7f5b,
created_at=2018-05-01T01:16:08Z, external_gateway_info=None,
status=ACTIVE, availability_zone_hints=[], ha=False, tags=[],
description=Router created for ONAP, admin_state_up=True, revision=1,
flavor_id=None, id=b923fba5-5027-47b6-b679-29c331ac1aba,
updated_at=2018-05-01T01:16:08Z, routes=[], name=onap_router,
availability_zones=[])
onap_router = conn.network.find_router(ONAP_ROUTER_NAME)
conn.network.delete_router(onap_router.id)
(same if conn.network.delete_router(onap_router))
Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'
I'm not sure yet what's causing this - it's the same issue you're having
below with flavors - I'm looking in to it.

Do you have tracebacks for the exception?
Post by g***@wipro.com
openstack.network.v2.router.Router(description=Router created for ONAP,
status=ACTIVE, routes=[], updated_at=2018-05-01T01:16:11Z, ha=False,
id=b923fba5-5027-47b6-b679-29c331ac1aba, external_gateway_info=None,
admin_state_up=True, availability_zone_hints=[],
tenant_id=03aa47d3bcfd48199e0470b1c86a7f5b, name=onap_router,
availability_zones=['nova'], tags=[], revision=3, distributed=False,
flavor_id=None, created_at=2018-05-01T01:16:08Z)
There is an underlying bug/deficiency in the code that's next on my list
to fix, but I'm waiting to land a patch to keystoneauth first. For now,
add identity_api_version=3 to your openstack.connect line and it should
work.

Also - sorry about that - that's a terrible experience and is definitely
not the way it should/will work.
Post by g***@wipro.com
(although I can reach compute and network services, and although there
are users and projects in the Openstack instance: "admin" and "service"
projects, "ceilometer", "nova", etc. (and "admin") users)
        print("\nList Users:")
        i=1
            print('User',str(i),'\n',user,'\n')
            i+=1
Exception: <class 'openstack.exceptions.NotFoundException'>
NotFoundException: 404
        print("\nList Projects:")
        i=1
            print('Project',str(i),'\n',project,'\n')
            i+=1
Exception: <class 'AttributeError'> 'Proxy' object has no attribute 'projects'
        onap_project = conn.identity.find_project(ONAP_TENANT_NAME)
            print('ONAP project/tenant already exists')
            print('Creating ONAP project/tenant...')
            onap_project = conn.identity.create_project(
                name = ONAP_TENANT_NAME,
                description = ONAP_TENANT_DESC,
                is_enabled = True)
Exception: <class 'AttributeError'> 'Proxy' object has no attribute 'find_project'
        tiny_flavor = conn.compute.find_flavor("m1.tiny")
            print('m1.tiny Flavor already exists')
            print('Creating m1.tiny Flavor...')
            tiny_flavor = conn.compute.create_flavor(
                name = 'm1.tiny',
                vcpus = 1,
                disk = 1,
                ram = 512,
                ephemeral = 0,
                #swap = 0,
                #rxtx_factor = 1.0,
                is_public = True)
(by the way, maybe swap and rxtx are not supposed to be set ?)
Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'
Same thing - not sure what's up yet , do you have a traceback?
Post by g***@wipro.com
5) how to create images ?
there is a compute proxy method: find_image()
but it looks like there is no create_image() ?
That is correct. Image methods are all on the image proxy (and we should
actually get rid of that compute.find_image() method before we release a
1.0) ... HOWEVER ...
Post by g***@wipro.com
say you wanted to add this image: Ubuntu Server 16.04 LTS (Xenial Xerus)
https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img
I *highly* recommend using the shade layer for image creation, because
image creation is exceptionally complicated and all the logic to deal
with it is there. We haven't finished combining them, but it'll work for
you in its current form.

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

You'll need to download the image file locally - as of right now there
is not support for directly importing from a url on the glance side.
Once you do:

conn.create_image(
'Ubuntu Server 16.04 LTS (Xenial Xerus',
filename='xenial-server-cloudimg-amd64-disk1.img')

should do the trick - although you might have to add a parameter or two.


I'll keep looking in to the other two errors and see what I can find,
but hopefully this will help you for now.
Post by g***@wipro.com
how would you do it ?
$ pip3 list
Package             Version
------------------- ---------
appdirs             1.4.3
certifi             2018.4.16
chardet             3.0.4
command-not-found   0.3
decorator           4.3.0
deprecation         2.0.2
dogpile.cache       0.6.5
idna                2.6
iso8601             0.1.12
jmespath            0.9.3
jsonpatch           1.23
jsonpointer         2.0
keystoneauth1       3.5.0
language-selector   0.1
munch               2.3.1
netifaces           0.10.6
openstacksdk        0.13.0
os-service-types    1.2.0
packaging           17.1
pbr                 4.0.2
pip                 10.0.1
pycurl              7.43.0
pygobject           3.20.0
pyparsing           2.2.0
python-apt          1.1.0b1
python-debian       0.1.27
python-systemd      231
PyYAML              3.12
requests            2.18.4
requestsexceptions  1.4.0
setuptools          20.7.0
six                 1.10.0
ssh-import-id       5.5
stevedore           1.28.0
ufw                 0.35
unattended-upgrades 0.1
urllib3             1.22
wheel               0.29.0
$ pip3 check
No broken requirements found.
The information contained in this electronic message and any attachments
to this message are intended for the exclusive use of the addressee(s)
and may contain proprietary, confidential or privileged information. If
you are not the intended recipient, you should not disseminate,
distribute or copy this e-mail. Please notify the sender immediately and
Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The
company accepts no liability for any damage caused by any virus
transmitted by this email. www.wipro.com
__________________________________________________________________________
OpenStack Development Mailing List (not for usage questions)
http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev
__________________________________________________________________________
OpenStack Development Mailing List (not for usage questions)
Unsubscribe: OpenStack-dev-***@lists.openstack.org?subject:unsubscribe
http://lists.openstack.org/cgi-bin/mailman/list
g***@wipro.com
2018-05-04 22:25:59 UTC
Permalink
Many thanks for the welcome ;)

And many thanks for the speedy and very useful response !

Details below.

Best regards,
Gerard


--------------------------------------------------------------------
For add_gateway_to_router():

So I tried this:
external_network = conn.network.find_network(EXTERNAL_NETWORK_NAME)
network_dict_body = {'network_id' : external_network.id}
conn.network.add_gateway_to_router(onap_router, **network_dict_body)

==> no errors, but the router is not updated (no gateway is set)
(external_gateway_info is still None)

(same with conn.network.add_gateway_to_router(onap_router, network_id=external_network.id) )

Is the body parameter for add_gateway_to_router() expected to correspond to a Network ?
(from a router point of view, a "gateway" is an external network)

Should the network's subnet(s) be also specified in the dictionary ? Maybe only
if certain specific subnets are desired for the gateway role. Otherwise,
the default would apply: there is usually only 1 subnet, and that's the one
to be used. So network_id would be enough to specify a gateway used in a standard way.

Maybe more details about what is expected in this body dictionary should be documented
in the add_gateway_to_router() section?

In Horizon, when selecting a router, and selecting "Set Gateway", the user is only
asked to pick an external network from a dropdown list. Then, a router interface is
implicitly created, with an IP@ picked from the subnet of that network.


--------------------------------------------------------------------
For router deletion: it looks like it's the "!= None" test on the returned object that has an issue

onap_router = conn.network.find_router(ONAP_ROUTER_NAME)
if onap_router != None:
print('Deleting ONAP router...')
conn.network.delete_router(onap_router.id)
else:
print('No ONAP router found...')

I added traceback printouts in the code.

printing the router before trying to delete it:
onap_router:
openstack.network.v2.router.Router(updated_at=2018-05-04T21:07:23Z, description=Router created for ONAP, status=ACTIVE, ha=False, name=ONAP_router, created_at=2018-05-04T21:07:20Z, tenant_id=03aa47d3bcfd48199e0470b1c86a7f5b, availability_zone_hints=[], admin_state_up=True, availability_zones=['nova'], tags=[], revision=3, routes=[], id=675abd14-096a-4b28-b764-31ca7098913b, external_gateway_info=None, distributed=False, flavor_id=None)


*** Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'
*** traceback.print_tb():
File "auto_script_config_openstack_for_onap.py", line 141, in delete_all_ONAP
if onap_router != None:
File "/usr/local/lib/python3.5/dist-packages/openstack/resource.py", line 358, in __eq__
return all([self._body.attributes == comparand._body.attributes,
*** traceback.print_exception():
Traceback (most recent call last):
File "auto_script_config_openstack_for_onap.py", line 141, in delete_all_ONAP
if onap_router != None:
File "/usr/local/lib/python3.5/dist-packages/openstack/resource.py", line 358, in __eq__
return all([self._body.attributes == comparand._body.attributes,
AttributeError: 'NoneType' object has no attribute '_body'



--------------------------------------------------------------------
For identity_api_version=3 :

yes, that worked !

Could that identity_api_version parameter also/instead be specified in the clouds.yaml file ?


--------------------------------------------------------------------
Here's the traceback info for the flavor error, also on the "!= None" test :

*** Exception: <class 'AttributeError'> 'NoneType' object has no attribute '_body'
*** traceback.print_tb():
File "auto_script_config_openstack_for_onap.py", line 537, in configure_all_ONAP
if tiny_flavor != None:
File "/usr/local/lib/python3.5/dist-packages/openstack/resource.py", line 358, in __eq__
return all([self._body.attributes == comparand._body.attributes,
*** traceback.print_exception():
Traceback (most recent call last):
File "auto_script_config_openstack_for_onap.py", line 537, in configure_all_ONAP
if tiny_flavor != None:
File "/usr/local/lib/python3.5/dist-packages/openstack/resource.py", line 358, in __eq__
return all([self._body.attributes == comparand._body.attributes,
AttributeError: 'NoneType' object has no attribute '_body'



--------------------------------------------------------------------
For the image creation:

ah, OK, indeed, there is an image proxy (even 2: v1, v2),
and maybe the compute / image operations are redundant (or maybe not, for convenience) ?

and yes, it worked ! There was no need for additional parameters.



The information contained in this electronic message and any attachments to this message are intended for the exclusive use of the addressee(s) and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately and destroy all copies of this message and any attachments. WARNING: Computer viruses can be transmitted via email. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. www.wipro.com
__________________________________________________________________________
OpenStack Development Mailing List (not for usage questions)
Unsubscribe: OpenStack-dev-***@lists.openstack.org?subject:unsubscribe
http://lists.openstack.org/cgi-bin/

Loading...