tesleft
By:
tesleft

how to set quota with python in openstack

September 8, 2016 794 views
Python Ubuntu

got error when set quota in RDO or production environment of openstack

already set insecure = True, but can not set quota

>>> nc.quotas.update(my_tenant.id, floating_ips=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/novaclient/v2/quotas.py", line 57, in update
    return self._update(url, body, 'quota_set')
  File "/usr/lib/python2.7/site-packages/novaclient/base.py", line 182, in _update
    _resp, body = self.api.client.put(url, body=body)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 452, in put
    return self._cs_request(url, 'PUT', **kwargs)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 402, in _cs_request
    self.authenticate()
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 555, in authenticate
    auth_url = self._v2_auth(auth_url)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 646, in _v2_auth
    return self._authenticate(url, body)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 659, in _authenticate
    **kwargs)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 397, in _time_request
    resp, body = self.request(url, method, **kwargs)
  File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 391, in request
    raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.Unauthorized: The request you have made requires authentication. (HTTP 401)


   from keystoneclient.v2_0.client import Client
import os
import csv
import sys
import logging
import time
from nova import db
from nova import config
from nova import context
import argparse
import novaclient.v1_1.client as nvclient


def get_nova_credentials_v2():
    d = {}
    d['version'] = '2'
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    d['insecure'] = "True"
    return d


credentials = get_nova_credentials_v2()
keystone_client = Client(**credentials)
tenants = keystone_client.tenants.list()
rolelist = keystone_client.roles.list()
userlist = keystone_client.users.list()
my_admin = [x for x in rolelist if x.name=="admin"][0]
my_member = [x for x in rolelist if x.name=="_member_"][0]
my_admin_user = [x for x in userlist if x.name=="admin"][0]

my_tenant = [x for x in tenants if x.name=="hello"][0]
nc = nvclient.Client(None, None, None, auth_url=os.environ['OS_AUTH_URL'], tenant_id=my_tenant.id, auth_token=keystone_client.auth_token, insecure='True')
nc.quotas.update(my_tenant.id, floating_ips=1)
1 Answer

While I am no expert on OpenStack (DO uses KVM) a little bit of digging leads me to believe that while "insecure=true" can be useful when running with self-signed certificates, preventing their use from causing errors you will likely still need to specify an authenticated user in order to set quota.

  • how to authenticate?

    have tried two methods still can not be authenticated in ice version

    nc = nvclient.Client("hello@gmail.com", "hello", mytenant.id, os.environ['OSAUTHURL'])
    nc.authenticate()
    /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl
    .py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
    InsecurePlatformWarning
    /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
    InsecureRequestWarning)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.7/dist-packages/novaclient/v11/client.py", line 169, in authenticate
    self.client.authenticate()
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 382, in authenticate
    auth
    url = self.v2auth(authurl)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 469, in _v2
    auth
    return self.authenticate(url, body)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 482, in _authenticate
    **kwargs)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 242, in _time
    request
    resp, body = self.request(url, method, **kwargs)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 236, in request
    raise exceptions.from_response(resp, body, url, method)
    novaclient.exceptions.Unauthorized: The request you have made requires authentication. (HTTP 401)

    nc = nvclient.Client("hello@gmail.com", "hello", mytenant.id, os.environ['OSAUTHURL'])
    nc.authenticate()
    /usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:838: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/security.html
    InsecureRequestWarning)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.7/dist-packages/novaclient/v1
    1/client.py", line 169, in authenticate
    self.client.authenticate()
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 382, in authenticate
    authurl = self.v2auth(authurl)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 469, in v2auth
    return self.authenticate(url, body)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 482, in _authenticate
    **kwargs)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 242, in _time
    request
    resp, body = self.request(url, method, **kwargs)
    File "/usr/lib/python2.7/dist-packages/novaclient/client.py", line 236, in request
    raise exceptions.fromresponse(resp, body, url, method)
    novaclient.exceptions.Unauthorized: The request you have made requires authentication. (HTTP 401)
    nc = nvclient.Client("fongyukhei@gmail.com", "8
    hrRfsa", mytenant.id, os.environ['OSAUTHURL'])
    nc.authenticate(os.environ['OS
    CACERT'])
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: authenticate() takes exactly 1 argument (2 given)

    loader = loading.getpluginloader('password')
    auth = loader.loadfromoptions(authurl=os.environ['OSAUTHURL'],
    ... username="hello@gmail.com",
    ... password="hello",
    ... project
    id=mytenant.id)
    sess = session.Session(auth=auth)
    nc = nvclient.Client("1.1", session=sess)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: _
    init__() got an unexpected keyword argument 'session'

  • if i use oscacert instead of insecure option, it is keystone's turn that can not be authenticated.
    so i use os
    cacert for nova only , and others use insecure option

    from keystoneclient.v20.client import Client
    import os
    import csv
    import sys
    import logging
    import time
    from nova import db
    from nova import config
    from nova import context
    import novaclient.v1
    1.client as nvclient
    from keystoneauth1 import loading
    from keystoneauth1 import session

    from novaclient import nvclient

    def getnovacredentialsv2():
    d = {}
    d['version'] = '2'
    d['username'] = os.environ['OS
    USERNAME']
    d['password'] = os.environ['OSPASSWORD']
    d['auth
    url'] = os.environ['OSAUTHURL']
    d['tenantname'] = os.environ['OSTENANTNAME']
    #d['insecure'] = 'True'
    d['os
    cacert'] = os.environ['OS_CACERT']
    return d

Have another answer? Share your knowledge.