Commit Graph

257 Commits

Author SHA1 Message Date
Sean Dague ec76e254da Allow global_request_id in Client constructor
This allows us to pass in a global_request_id in the client
constructor so that subsequent calls with this client pass that to the
servers. This enables cross project request_id tracking.

oslo spec I65de8261746b25d45e105394f4eeb95b9cb3bd42

Change-Id: Iea1e754a263a01dae5ed598fdda134394aff54b0
2017-06-01 14:32:03 +00:00
ckonstanski 03900522d4 v2: Content-Type: application/octet-stream header always added
The bug: any existing Content-Type header cannot be found because the
call to headers.get() fails. Therefore we end up with two Content-Type
headers because a new one (applicaion/octet-stream) gets added
unconditionally. The cause: the strings (keys and values) in the headers
dict are converted from unicode sequences of type <str> to utf-8
sequences of type <bytes>. This happens in safe_encode()
(oslo_utils/encodeutils.py:66). <str> != <bytes> even if they appear to
have the same characters.

Hence, for python 3.x, _set_common_request_kwargs() adds content-type
to header even if custom content-type is set in the request.

This results in unsupported media type exception when glance client
is used with keystoneauth and python 3.x

The fix: follow the directions in encode_headers().
It says to do this just before sending the request. Honor this principle;
do not encode headers and then perform more business logic on them.

Change-Id: Idf6079b32f70bc171f5016467048e917d42f296d
Closes-bug: #1641239
Co-Authored-By: Pushkar Umaranikar <pushkar.umaranikar@intel.com>
2017-05-19 19:02:00 +00:00
Eric Fried 7df87fd4a2 Convert IOError from requests
This requests commit [1] changed the behavior when a nonexistent cacert
file is passed in: now it raises IOError.  This is getting through
glanceclient.common.http.HTTPClient._request, which used to raise
CommunicationError in this scenario.

Even though there is arguably a better exception than CommunicationError
to represent this condition (like maybe IOError), for backward
compatibility this change set converts IOError to CommunicationError.

We also improve the unit test to raise the original exception if the
expected conditions aren't met; this improves debugability.

[1] https://github.com/kennethreitz/requests/commit/7d8b87c37f3a5fb993fd83eda6888ac217cd108e

Change-Id: I6a2cf4c6d041b67d3509153b4cef18b459263648
Closes-Bug: #1692085
2017-05-19 14:59:58 -04:00
Jenkins ba46e69eee Merge "Replace functions 'dict.get' and 'del' with 'dict.pop'" 2017-03-06 17:09:00 +00:00
Abhishek Kekane 87e4f7646f x-openstack-request-id logged twice in logs
In the recent release of keystoneauth1 2.18.0 provision is made to log
x-openstack-request-id for session client. Once this new library is synced
in openstack projects, the x-openstack-request-id will be logged twice
on the console if session client is used.

For example,
$ glance --debug image-list

DEBUG:keystoneauth.session:GET call to image for
http://10.232.48.204:9292/v2/schemas/image used request id
req-96a3f203-c605-4c96-a31d-f1199d41705c

DEBUG:glanceclient.common.http:GET call to glance-api for
http://10.232.48.204:9292/v2/schemas/image used request id
req-96a3f203-c605-4c96-a31d-f1199d41705c

Above log will be logged twice on the console.

Removed logging of x-openstack-request-id in case of SessionClient as it
is already logged in keystoneauth1. x-openstack-request-id will only be
logged once on console if HTTPClient is used.

Depends-On: I492b331ff3da8d0b91178bf0d5fe1d3702f15bd7
Closes-Bug: #1657351
Change-Id: I64258f997dc060113f53682aee74bdd40a346e54
2017-02-22 01:49:22 +05:30
bhagyashris 6740f57096 Replace functions 'dict.get' and 'del' with 'dict.pop'
Refactoring code: Making dict to use single instruction: pop()
rather than two instructions: get() and del, giving the codes a
format that carries through.

TrivialRefactoring

Change-Id: Idb21df37c287fdff24c29153676f82544f735297
2017-01-31 10:09:28 +05:30
Ravi Jethani 610177a779 Add request id to returned objects
Adding two classes RequestIdProxy and GeneratorProxy derived from
wrapt.ObjectProxy to wrap objects returned from the API.

GeneratorProxy class is used to wrap generator objects returned
by cases like images.list() etc. whereas RequestIdProxy class is
used to wrap non-generator object cases like images.create() etc.

In all cases the returned object will have the same behavior as
the wrapped(original) object. However now the returned objects
will have an extra property 'request_ids' which is a list of
exactly one request id.

For generator cases the request_ids property will be an empty list
until the underlying generator is invoked at-least once.

Co-Authored-By: Abhishek Kekane <abhishek.kekane@nttdata.com>

Closes-Bug: #1525259
Blueprint: return-request-id-to-caller
Change-Id: If8c0e0843270ff718a37ca2697afeb8da22aa3b1
2017-01-20 14:50:42 +05:30
Jenkins fa11427af5 Merge "Use import_versioned_module from oslo.utils" 2016-12-16 12:30:19 +00:00
Jenkins 8b96bbc34c Merge "Revert "Add last_request_id member to HTTPClient and SessionClient"" 2016-12-14 17:35:22 +00:00
Li Wei 4773c96672 Use import_versioned_module from oslo.utils
oslo.utils 3.17 provides this funtion, so just use it directly.

Change-Id: I85cb78a6fd33a5b1f7e09648efed1b0737678eee
Closes-Bug: #1627313
2016-12-06 09:35:55 +00:00
Jenkins 993d45293c Merge "Add Apache 2.0 license to source file" 2016-11-18 15:48:45 +00:00
Abhishek Kekane c2898998a7 Move old oslo-incubator code out of openstack/common
As part of the first community-wide goal, teams were asked to
remove the openstack/common package of their projects if one
existed. This was a byproduct of the old oslo-incubator form
of syncing common functionality.

Package openstack/common/apiclient is moved to glanceclient/v1
package as it is used by v1 api only.

NOTE:
Removed glanceclient/common/base.py as it is deprecated and not
used anywhere.

Closes-Bug: #1639487
Change-Id: Ib3ac09743ce761ab0186e99e1c9de02517f89510
2016-11-08 11:33:09 +05:30
Cao Xuan Hoang 7a6cd5ebe2 Add Apache 2.0 license to source file
As per OpenStack licensing guide lines [1]:
[H102 H103] Newly contributed Source Code should be licensed under
the Apache 2.0 license.
[H104] Files with no code shouldn't contain any license header nor
comments, and must be left completely empty.

[1] http://docs.openstack.org/developer/hacking/#openstack-licensing

Change-Id: I15cbb71d028e9297cb49b5aab7d0427f7be36c49
2016-10-04 09:38:39 +07:00
Itisha Dewan a9115b4cd8 switch from keystoneclient to keystoneauth
move glanceclient to keystoneauth as keystoneclient's auth session,
plugins and adapter code has been deprecated.

refer to [1] for more information.

1: https://github.com/openstack/python-keystoneclient/commit/1a84e24fa4ce6d3169b59e385f35b2a63f2257f0

implements bp: use-keystoneauth

Co-Authored-By: Itisha <ishadewan07@gmail.com>
Change-Id: I88fb327628e1bec48dc391f50d66b3deab4a8ab9
2016-09-03 03:22:04 +00:00
Jenkins c3524b6d8c Merge "Fix string interpolation to delayed to be handled by the logging code" 2016-07-30 22:02:00 +00:00
Jenkins c039237007 Merge "Log request-id before exceptions raised" 2016-07-30 22:01:48 +00:00
Sirushti Murugesan 376037d371 py3: Fix encoding and use sys.stdin.buffer
* exc.py: Encode body in response before calling replace over it.
* http.py: prepend the bytes literal to the empty string or else
  we hit bug 1342080 again in python 3.
* utils.py: Use sys.stdin.buffer in python 3.

Change-Id: Ieefb8c633658e507486438e5518c5d53e819027d
2016-07-25 20:13:08 +05:30
Jenkins 2047653e06 Merge "Add comment about workaround for py3" 2016-07-20 10:53:57 +00:00
Abhishek Kekane 155183a21e Log request-id before exceptions raised
As of now request-id is not logged if an excpetion
is raised.

Rearranged code so that request-id is logged even in case of
an exception.

Change-Id: Iee0398404ee752c0d880edf3054207c35862e71a
Closes-Bug: #1603863
2016-07-18 12:19:55 +05:30
haobing1 2d7b864b17 Fix string interpolation to delayed to be handled by the logging code
String interpolation should be delayed to be handled by the logging
code, rather than being done at the point of the logging call.
See the oslo i18n guideline.
* http://docs.openstack.org/developer/oslo.i18n/guidelines.html

Change-Id: If06663076e4081c6268ba88c157513723b734b31
Closes-Bug: #1596829
2016-07-18 05:34:46 +00:00
Jenkins 3a65a59220 Merge "Log request-id for each api call" 2016-07-15 10:56:26 +00:00
Louis Taylor 3047afb121 Add comment about workaround for py3
Change-Id: Ibe8720c14e8ec401bc0d595915cbb962f4021bcb
2016-07-13 17:04:19 +00:00
Stuart McLaren 7ed22a91d3 image-download: tests to catch stray output
Add unit tests to ensure that any stray output (eg print
statements) during image-download cause a test failure.

Regression test for bug 1488914.

Change-Id: Ic19ba5693d059bf7c283702e7c333672a878a1a1
Partial-bug: 1488914
2016-06-29 16:08:30 +00:00
Abhishek Kekane 96151efd51 Log request-id for each api call
Added support to log 'X-Openstack-Request-Id' for each api call.
If glanceclient is used from command line then following log will
be logged on console if --debug flag is used.

DEBUG:glanceclient.common.http:GET call to glance-api for
http://172.26.88.20:9292/v2/schemas/image used request id
req-e0c7c97a-8fc0-4ce3-a669-d0b1eb5d7aae

If python-glanceclient is used in applications (e.g. Nova) then
following log message will be logged in service logs.

DEBUG glanceclient.common.http
[req-be074f1e-1c17-4786-b703-2a221751c8f4 demo demo] GET call to
glance-api for
http://172.26.88.20:9292/v1/images/detail?is_public=none&limit=20
used request id req-9b1dd929-df30-46b2-a8f2-dfd6ffbad3fc

DocImpact:
To use this feature user need to set 'default_log_levels' in third
party application. For example nova uses glance then in nova.conf
'default_log_levels' should be set as below:

default_log_levels = glanceclient=DEBUG

Implements: blueprint log-request-id
Change-Id: Ib04a07bac41ad2a5e997348f3b0bccc640169dc9
2016-06-27 11:31:07 +05:30
kairat_kushaev f58a73495f Remove unused skip_authentication decorator
skip_authentication is not used as decorator for glanceclient methods.
So this method can be safely removed from glance codebase because
it is artifact from old implementation.

Change-Id: I235b4c6b835c75266d8fae1bb603685aa17ad497
2016-06-02 13:25:30 +00:00
Jenkins 6f2fae7a19 Merge "Fix "Codec can't encode characters"" 2016-05-23 14:31:04 +00:00
Abhishek Kekane 7d106c677a Revert "Add last_request_id member to HTTPClient and SessionClient"
This reverts commit 9e532db8b0.

If glanceclient is used in multi-threaded environment, then there is a
possibility of getting invalid/wrong last request-id. To avoid this,
need to use thread local storage to store last-request-id and add
public method to return this request-id to caller.

http://specs.openstack.org/openstack/openstack-specs/specs/return-request-id.html#alternatives

Change-Id: I08d8d87fc0cc291f1b930b2c0cfc110ec8394131
2016-05-23 14:14:55 +00:00
Darja Shakhray 9329ef0bc4 Fix "Codec can't encode characters"
Headers were encoded in HTTPClient, but when glance client started
to use SessionClient this functionality was lost.

This commit replaces static method "encode_headers" from HTTPClient
and makes it a common function, that SessionClient can use when
converting image meta to headers.


Change-Id: If9f8020220d2a0431b4241b38b9c83c09c0d75cb
Closes-bug: #1574587
2016-05-19 16:48:03 +00:00
Jenkins 7150ceee1a Merge "Add last_request_id member to HTTPClient and SessionClient" 2016-05-18 21:44:53 +00:00
Jenkins 9ffebbb25e Merge "Enable hacking checks" 2016-04-20 17:13:16 +00:00
Jenkins 12e92558e5 Merge "Re-enable stacktracing when --debug is used" 2016-04-18 17:04:57 +00:00
Tin Lam cd5925bc60 Enable hacking checks
Enabled following hacking checks from tox.ini:
- H233 Python 3.x incompatible use of print operator
- H303 no wildcard import
- H404 multi line docstring should start with a summary

Change-Id: I2553bcd3e80c00acc08d135a1d2dadfb6cda49fe
Partial-Bugs: #1475054
2016-04-18 16:23:59 +00:00
Dao Cong Tien 9faa9d47aa Fix typos in docstrings and comments
Update a comment to be more meaningful

Change-Id: Ie1aa46917c1a253db92a0dc819803a1d3e795b07
2016-04-11 09:08:02 +07:00
Stuart McLaren d0ec3a7ebb Re-enable stacktracing when --debug is used
Commit 1f89beb609 introduced the behaviour
that a stacktrace is printed if an exception is encountered.

This helped make the client more supportable:

 $ glance --debug image-list
  .
  .
  .
  File "glanceclient/common/http.py", line 337, in get_http_client
    xxx
 NameError: global name 'xxx' is not defined
 global name 'xxx' is not defined

The behaviour was lost at some point. This patch re-enables it.

Change-Id: I25fc8624797909d606590747f54b9cf649ade079
Closes-bug: 1563830
2016-04-07 15:42:51 +00:00
Cao ShuFeng 9e532db8b0 Add last_request_id member to HTTPClient and SessionClient
apiclient.base.Resource.get method requires manager.client to have
last_request_id member. Otherwise get operation fails with
AttributeError exception.

Change-Id: I0ece85e3f61f2a7f176520ddf3ebee7792e51993
Closes-bug: 1552533
2016-03-30 13:56:27 +00:00
kairat_kushaev 2561e4061c Update auth_token before sending request
Previously auth_token was initialized once in __init__ method.
After that we stored token in session headers. So to refresh token
users need to instantiate a new session inside http client or
re-create client itself.
In order to provide possibility to refresh token we need
to add token header before sending the request. So users can
just update auth_token attribute in the HTTPClient to refresh
user token.

Change-Id: Ifebe9011870bbddc46fc6d6a26563641d5559e97
Closes-Bug: #1563495
2016-03-30 13:33:19 +03:00
Jenkins 6aaa6f2f8c Merge "Handle 403 forbidden on download" 2016-02-29 13:04:08 +00:00
Stuart McLaren 5b9f21b38b Handle 403 forbidden on download
A download of a deactivated image may result in a 403.
The cli should catch this error rather than stack trace.

We also catch other unexpected http responses.

Change-Id: If33fbc3a56cdb02b3ab32a6479a67fff20b4b1a9
Closes-bug: 1523612
2016-02-24 18:31:38 +00:00
kairat_kushaev b4a2e28295 Remove code needed for python2.5
glance has a code specific for python2.5. We need to delete this
code cause glanceclient doesn't support neither python2.5 or
python2.6.

Change-Id: I17e4905b6e02fcfff033a6cde03324e2a47bfce2
2016-02-01 13:23:18 +03:00
kairat_kushaev 799febf73e Remove monkey-patching for getsockopt
Not getsocketopts is presented in GreenSocket for Linux.
See the bug for the info. So we don't need to patch it anymore.

Closes-Bug: #1348269

Change-Id: Ie2211238656eddfb0af5f3ef84ab638f6248a10a
2016-01-15 16:29:27 +03:00
Andy Botting 3caeb4504e Fix image-download to stdout on Python 3.x
Glance image-download to stdout fails on Python3 due to sys.stdout.write
not allowing bytes to be written directly.

A good description of the issue is listed at http://bugs.python.org/issue18512

Closes-Bug: #1528083

Change-Id: I2963914e2e0744410267b5735ff77939413916d4
2015-12-22 14:42:15 +11:00
Monty Taylor 9bc0018eda Remove broken try/except workaround for old requests
Not only is this code broken on the requests we require on
distro-provided requests, it's not needed anymore. Remove it.

Closes-bug: 1526254

Change-Id: I47a07bf9910f118392785fc20e015f036a2e8a7c
2015-12-15 10:30:04 +00:00
Vincent Untz f65ba82268 Ensure that identity token in header is not an unicode string
We need all the headers to be safe strings so they can be joined
together and not become an unicode string in doing so.

This fixes a bug when creating an image with non-ascii characters in the
name.

This is required for python 2.6 compatibility.

Change-Id: I66ebc27edf4ccd8f903399da58705711c372536d
Closes-Bug: 1448080
2015-11-17 18:14:20 +01:00
wangxiyuan dfcb468c1d Fix the missing help descripiton of "image-create"
Now, when use "glance help" to show the help message, the description
of 'image-create' is missing.

Change-Id: I748209222c540e0024580dccac850ea465d176b4
Closes-bug: #1510340
2015-10-27 09:20:56 +08:00
kairat_kushaev 6b9133c9b6 Import i18n functions directly
As stated in i18n guide it is normal to import i18n functions
(_, _LW..)  directly and we can include i18n functions in
hacking exceptions.
Also there is no need to make exceptions for six moves
because pep8 passes correctly without it.

Change-Id: I9c9aa490f1447bb7ae221809df7bc110c27d1336
2015-10-16 11:26:11 +03:00
Jenkins 2fcff11b90 Merge "Add support for setting Accept-Language header" 2015-10-14 15:25:03 +00:00
Frode Nordahl ca050ed4c1 Add support for setting Accept-Language header
DocImpact

Closes-Bug: 1480529
Change-Id: I35a37d55edb700a5993bd5cc352335a87a15e47a
2015-10-14 12:57:07 +00:00
wangxiyuan bf02b048bf Support image deletion in batches in v2
Client doesn't support image deletion in batches in v2 now.
It's useful. So it's need to add it.

Change-Id: Idf5a6890b3fd01a65fecab2033b21367c30bc6b1
Closes-bug:#1485407
2015-10-10 10:18:02 +08:00
Jenkins 542941cef0 Merge "Fix Typos in comments" 2015-09-30 18:34:02 +00:00
Jenkins 8c0c3c9ed4 Merge "Replace exception_to_str with oslo.utils function" 2015-09-30 18:10:40 +00:00