44 Commits

Author SHA1 Message Date
Tim Burke
7a1e192803 Use bulk-delete middleware when available
When issuing `delete` commands that would require three or more
individual deletes, check whether the cluster supports bulk deletes
and use that if it's available.

Additionally, a new option is added to the `delete` command:

  * --prefix <prefix>

    Delete all objects that start with <prefix>. This is similar to the
    --prefix option for the `list` command.

Example:

$ swift delete c --prefix obj_prefix/

    ...will delete from container "c" all objects whose name begins with
    "obj_prefix/", such as "obj_prefix/foo" and "obj_prefix/bar".

Change-Id: I6b9504848d6ef562cf4f570bbcd17db4e3da8264
2016-01-12 15:40:57 -08:00
James Nzomo
2c6f367035 Fix upload to pseudo-dir passed by <container> arg
This fix makes it possible to upload objects to pseudo-folders by
passing the upload paths via <container> arg regardless of whether the
container or folder path exist or not.

Change-Id: I575e58aa12adcf71cdaa70d025a0ea5c63f46903
Closes-Bug: #1478210
Partial-Bug: #1432734
Related-Bug: #1432734
2016-01-04 22:48:07 +03:00
Joel Wright
a3a78be87b New API documentation for python-swiftclient
New documentation for python-swiftclient that introduces
the APIs available and gives some opinionated advice about
when to use the shell, the client API and the service API.

Change-Id: I19020f041fab2e72469979f712ffe3951c431d24
2015-11-25 15:15:09 +00:00
Jenkins
50978ddf63 Merge "Centralize header parsing" 2015-11-12 02:53:20 +00:00
Zack M. Davis
6b3638ecec enable autodocumentation for utils module; docstring fixes
This commit adds the utils module to those for which Sphinx
automatically generates documentation from docstrings. (Many of the
functions here may be of little interest to users, but
`generate_temp_url`, at least, definitely deserves to be in the
documentation; in this way, this commit can be seen as a spiritual
companion to ca70dd9e.)

Also, a few markup errors and perceived infelicities in existing
docstrings are amended.

Change-Id: I8c66a23cb359d7dd9302a16459fad9825fedb690
2015-09-10 15:19:26 -07:00
Tim Burke
ce569f4651 Centralize header parsing
All response headers are now exposed as unicode objects. Any
url-encoding is interpretted as UTF-8; if that causes decoding to fail,
the url-encoded form is returned.

As a result, deleting DLOs with unicode characters will no longer raise
UnicodeEncodeErrors under Python 2.

Related-Bug: #1431866
Change-Id: Idb111c5bf3ac1f5ccfa724b3f4ede8f37d5bfac4
2015-09-03 13:46:03 -07:00
Joel Wright
3c0289844f Log and report trace on service operation fails
This patch adds exception logging to the swift service API. Each
operation that results in failure of any operation will now log
the exception as well as report a timestamp and full stack trace
in the results returned by the service API calls.

Change-Id: I7336b28354e7740ea7d048bdf355e3c1a1b4436c
2015-08-31 22:03:26 +01:00
Joel Wright
a8c4df98ee Reduce memory usage for download/delete and add --no-shuffle option to st_download
The current code builds a full object listing before performing either a multiple
download or delete operation (and also shuffles this complete list in the case of
a download). This patch removes the creation of the full object list and adds the
ability to turn off shuffle for files when downloading. Also added is a limit on
the number of list results that can be queued by a single call to service.list
without consuming results (reduces memory overhead for large listings).

Some tests added for service.py download and list.

Change-Id: Ie737cbb7f8b1fa8a79bbb88914730b05aa7f2906
2015-07-20 20:44:51 +01:00
Jenkins
0b592caabf Merge "Add ability to download objects to particular folder." 2015-07-11 06:21:40 +00:00
Charles Hsu
d5d3127744 Add ability to download objects to particular folder.
This patch adds "--output-dir" and "--remove-prefix" options
to the "download" command and unit tests for it.

Example:

    $ swift list example --prefix swift2.2
    swift2.2/bin/swift-object-auditor
    swift2.2/bin/swift-object-expirer
    swift2.2/bin/swift-object-info
    swift2.2/bin/swift-object-replicator
    swift2.2/bin/swift-object-server
    swift2.2/bin/swift-object-updater

When given "--output-dir <directory>", client downloads objects to <directory>.

    $ swift download example --prefix swift2.2 \
                    --output-dir new/swift/dir

    The folder structure:
    .
    └── new
        └── swift
            └── dir
                └── swift2.2
                    └── bin
                        ├── swift-object-auditor
                        ├── swift-object-expirer
                        ├── swift-object-info
                        ├── swift-object-replicator
                        ├── swift-object-server
                        └── swift-object-updater

When given "--remove-prefix", client downloads objects without <prefix>.

    $ swift download example --prefix swift2.2 \
                    --remove-prefix \
                    --output-dir swift

    The folder structure:
    .
    └── swift
        └── bin
            ├── swift-object-auditor
            ├── swift-object-expirer
            ├── swift-object-info
            ├── swift-object-replicator
            ├── swift-object-server
            └── swift-object-updater

Co-Authored-By: Clay Gerrard <clay.gerrard@gmail.com>
Change-Id: I7463fe2941cc94f9a50a4756a97c2ccdf946294d
Implements: blueprint swiftclient-download-pseudo-folder-to-specific-target
2015-07-08 14:23:05 +08:00
Jenkins
92b8277752 Merge "SwiftClient object upload beginning with / or "./"" 2015-07-07 22:21:38 +00:00
Jenkins
0df888ea55 Merge "Remove all DLO segments on upload of replacement" 2015-07-02 00:04:50 +00:00
Clint Byrum
7c716997a8 Fix docstring typo for SwiftService.upload
The option from argparse in shell is 'header' and that is what is used
in the code.

Change-Id: I5c889192ef7c46c299dc0ec7cbc7c4d027dae6d5
2015-06-18 09:04:33 -07:00
Pradeep Kumar Singh
794b125e76 SwiftClient object upload beginning with / or "./"
Currently SwiftClient populate response dictionary before
removing "./" or "/" at begining of object name. This patch
fixes that by changing that order.

Closes-bug: #1412425
Change-Id: I80222754caba5d42a468f4677ac539e46682dd31
2015-06-17 12:27:43 +05:30
Tim Burke
ff0b3b02f0 Compare each chunk of large objects when downloading
This allows the use of --skip-identical when downloading SLOs and DLOs,
while still only requiring one request for normal objects.

Change-Id: I237ec5a8cc4d1119b6d3f166db81a977a50a4e4e
Related-Bug: #1379252
2015-05-27 17:40:24 -04:00
Tim Burke
317df7e527 Remove simplejson dependency
In addition to removing an unnecessary dependency, this closes another
hole that was allowing raw bytes to appear in user-facing messages.

Change-Id: Ia0b76426a38e5a5c368c4c7e7ba2aef286758aca
2015-05-20 18:14:53 -07:00
Jenkins
766cf73247 Merge "Compare each chunk of large objects when uploading" 2015-04-30 18:48:59 +00:00
Tim Burke
a4fb70ece1 Compare each chunk of large objects when uploading
Previously, we compared the ETag from Swift against the MD5 of the
entire large object. However, the ETag for large objects is generally
the MD5 of the concatenation of the ETags for each segment, unless the
object is a DLO whose segments span more than one page of a container
listing. Rather than worry about ETags, just compare each chunk of the
segmented file. This allows the use of --skip-identical when uploading
SLOs and DLOs.

Additionally, there are several test-related improvements:
 * The default arguments for OutputManager are now evaluated on
   construction, rather than on definition, so that
   TestOutputManager.test_instantiation will succeed when using nosetest
   as a test runner. (See also: bug 1251507)
 * An account_username option is now available in the functional tests
   config file for auth systems that do not follow the account:username
   format.
 * CaptureOutput no longer writes to the captured stream, and
   MockHttpTest now captures output. These were polluting test output
   unnecessarily. (See also: bug 1201376)

Change-Id: Ic484e9a0c186c9283c4012c6a2fa77b96b8edf8a
Closes-Bug: #1201376
Closes-Bug: #1379252
Related-Bug: #1251507
2015-03-23 18:35:45 -07:00
Tim Burke
d931ec1381 Remove all DLO segments on upload of replacement
Previously, only the first container-listing's worth of segments was
deleted, which would leave behind orphaned segments when the object was
very large with small segments or the server's container_listing_limit
was small.

In addition, process DLO and SLO deletions on the segment thread pool,
rather than the object thread pool.

Change-Id: I1587375261a6237fa55a9cb96bda8dae918cc795
Related-Bug: #1418007
2015-03-23 12:11:47 -07:00
Daniel Wakefield
13780f37c3 Add improvements to MD5 validation.
With MD5Sum checking being added, a concern was brought up that It was
a change with no possibility of reverting to the old behaviour.
This change adds the flag '--ignore-checksum' to the upload subcommand
allowing the checks to be turned off.

Changed occurrences of the magic string for a null md5 to use a descriptive
constant instead.

Updated Error messages generated when validation fails. They should now be more descriptive
and not output a literal newline sequence.

Change-Id: Id1756cbb6700bb7e38f0ee0e75bc535e37f777ed
2015-03-04 14:01:55 +00:00
Daniel Wakefield
f0300e3714 Verify MD5 of uploaded objects.
Changed existing code to calculate the MD5 of the object
during the upload stream. Checks this MD5 against the etag
returned in the response.
An exception is raised if they do not match.

Closes-Bug: 1379263

Change-Id: I6c8bc1366dfb591a26d934a30cd21c9e6b9a04ce
2015-02-19 11:21:32 +00:00
Joel Wright
06c73c6020 Fix deleting SLO segments on overwrite
This patch fixes a bug that results in SLO segments being left
behind regardless of the setting of --leave-segments.

Change-Id: I2b3c8ab4817df6facb4b5961dbe570645441a552
Closes-Bug: 1418007
2015-02-04 14:17:54 +00:00
Jenkins
eb28d8aff6 Merge "This patch fixes downloading files to stdout." 2015-02-03 18:55:47 +00:00
Jenkins
21473f1bc4 Merge "Fix cross account upload using --os-storage-url" 2015-02-02 08:30:40 +00:00
Joel Wright
bd42c2b00d This patch fixes downloading files to stdout.
This patch fixes downloading files to stdout and modifies
_SwiftReader to operate as an iterator that performs file
checks at the end of iteration as well as a context manager.
File verification checks have been removed from __exit__
and added to __iter__.

Change-Id: I3250bdeeef8484a9122c4b5b854756a7c8f8731e
Closes-Bug: 1395922
Closes-Bug: 1387376
2015-01-23 11:46:13 +00:00
Clay Gerrard
eef91b3513 Fix environment sanitization for TestServiceUtils
I'm not sure how test_process_options_defaults ever passed for anyone that had
all the ST_(AUTH|USER|KEY) set in their environment when they ran it.  Despite
our efforts to patch os.environ swiftclient.servce was acctually importing
environ from os and then sticking the results into some global state.  The
tests seemed to work fine on infra, but for me locally until I `unset ST_AUTH`
the test would always fail.  Seems to work now and looks fairly reasonable on
the surface.

Change-Id: Id68590d6af16040a2877e719bc38d1e126603e42
2015-01-21 17:20:53 -08:00
Jenkins
7709fea51e Merge "Check that content_type header exists before using" 2015-01-10 03:42:45 +00:00
Alistair Coles
9593d4b58a Fix cross account upload using --os-storage-url
Removes an account stat from the object upload path.
This stat fails when user is not account admin even
though the user may have container ACL permission to
write objects.

Reduces the severity of the CLI output message when
upload fails to create the given container (this is
not an error since the container may exist - the user
just does not have permission to PUT or POST the
container).

Changes the 'swift upload' exit return code from 1 to
0 if container PUT fails but object PUT succeeds.

For segment uploads, makes the attempt to create the
segment container conditional on it not being the same
as the manifest container. This avoids an unnecessary
container PUT.

Fixes another bug that became apparent: with segmented
upload a container HEAD may be attempted to determine
the policy to be used for the segment container. When
this failed the result dict has headers=None which was
causing an exception in the shell result handler.

Add unit tests for object upload/download and container
list with --os-storage-url option.

Closes-Bug: #1371650
Change-Id: If1f8a02ee7459ea2158ffa6e958f67d299ec529e
2015-01-06 16:13:39 +00:00
Daniel Wakefield
8fa6cc9b1b Fix misplaced check for None in SwiftUploadObject.
Check for None was being done after a method call which
caused an attribute error if the value was None.
The Method call was also a mistake and has been corrected to the
hasattr function like intended.

Added Tests.

Closes-Bug: 1392651
Change-Id: Ifb1c84e26533bccbaddcce5738f434f36feca74e
2014-12-16 14:16:34 +00:00
Daniel Wakefield
5756fcc4f2 Fix misnamed dictionary key.
The response dictionary in _delete_segment attach's any error
it encounters to the dict key 'exception', all other response
dict's use 'error' to store the exception.

Changed to make it consistent and added tests

Also added a third branch in st_delete as messages
created in _delete_container where being silently dropped.

Change-Id: Ifbc3b1fae78910fbc6acf4a86cfb0f60bb1aa336
2014-12-08 15:45:40 +00:00
Jenkins
cfbabe7266 Merge "Fix misnamed variable in SwiftReader." 2014-11-17 11:12:13 +00:00
Jenkins
202bbab2d0 Merge "Make swift post output an error message when failing" 2014-11-13 03:30:01 +00:00
Daniel Wakefield
93cc42b21d Fix misnamed variable in SwiftReader.
actual_md5 was being used instead of _actual_md5 only when assigning
the md5 object. This resulted in all checks and updates of the md5
to be skipped.

When fixed it revealed another bug that the md5 of the read content
is always checked even if there is no _expected_etag.

Added tests

Change-Id: Iaf1e21324ce592049d02cd5be123604b99833e86
2014-11-07 12:55:51 +00:00
Chris Buccella
68f271aae2 Check that content_type header exists before using
When downloading an object, the content_type header is inspected
to determine if a directory needs to be created. This header is
assumed to always be in the response; if it isn't an Exception is
raised. swiftclient should not assume content_type will always be
set.

Change-Id: I156195c02b6e0bc398fa962eb1f78c4dbddd1596
2014-11-07 01:01:53 -05:00
Jenkins
19b427eacb Merge "Adds user friendly message when --segment-size is a non-integer" 2014-10-28 20:52:03 +00:00
Mahati Chamarthy
51ed14d9ae Adds user friendly message when --segment-size is a non-integer
Change-Id: Ic2c062110b2fe2e725b0f4a4517a300cfbf663a8
2014-10-28 21:33:58 +05:30
Alistair Coles
95056d1ef4 Make swift post output an error message when failing
swiftclient.shell.st_post was ignoring the result dict
returned from SwiftService.post and therefore giving no
indication when posts failed e.g. due to invalid auth
credentials.

This patch ensures that SwiftService always returns a
result_dict from post() and then checks the result dict.
On failure the shell now outputs the ClientException
message and exits with error code 1.

Also adds unit tests and cleans up some unnecessary mocked
return values in existing tests.

Closes-Bug: 1383243

Change-Id: I111150eb3d026c8d01c2cac13d3613ca7304e5b9
2014-10-24 18:13:37 +01:00
Daniel Wakefield
025da14326 Replaces Stacktraces with useful error messages.
Changed the message shown when a user doesn't enter project/tenant
id/name to be more informative.

When attempting to stat a container without supplying project/tenant
name or id, an empty response was being returned instead of an error
being raised.

Changed the error raised in swiftclient.client when no tenant or project
is specified to be more specific.

Add tests for basic regression checking.

Closes-Bug: #1372589

Change-Id: I4eb6964d9f1702db119cc0294edc02841b1ecd5f
2014-10-21 08:23:26 +00:00
Alistair Coles
589d34ecda Fix race between container create jobs during upload
During a segmented object upload, attempts are made
to create the container for the manifest and the
segment container. These jobs are currently placed
on separate threads and can occur out of order which
causes the unit test failure seen in the bug report.

The container jobs should be ordered because the
segment container job attempts to HEAD the manifest
container to determine correct storage policy.

This patch enforces ordering of the two jobs and
modifies the unit test to assert that both container
PUTs occur in the expected order.

Change-Id: If90bec126867a4be2df34067ccefee660202f59f
Closes-Bug: #1376878
2014-10-20 08:57:37 +01:00
Jenkins
bb4d2ab59c Merge "Remove a debugging print statement" 2014-09-26 22:35:39 +00:00
Joel Wright
3496e07ea4 Remove a debugging print statement
This patch removes a debugging print statement that slipped into
service.py in patch https://review.openstack.org/#/c/85453/

Change-Id: I80eee49eaa74b52e9c36f00669206aa7b2cdec9b
2014-09-25 11:03:43 +00:00
Alistair Coles
f90d7f28e5 Fix bug with some OS options not being passed to client
Fixes a regression that is preventing swiftclient from
authenticating using keystone v3 options.

swiftclient/shell.py processes the dict of command
line options to set up a child dict ('os_options')
of keystone auth specific options (line 960). The
processing includes stripping --os- prefixes from
the command line options before adding keys to
os_options.

A recent patch https://review.openstack.org/#/c/85453/
introduced a duplication of this option processing
in service.py (line 79) which replaces the os_options
created in shell.py, but omits keystone v3 related
options. Consequently the keystone v3 options are
not being passed to the client get_auth() method.

This patch adds the keystone v3 related options to
the option processing in service.py.

For pragmatic reasons (i.e. fixing the bug quickly)
the option processing code has not been removed from
parse_args in shell.py. It is likely that the code in
parse_args is now redundant, but all code paths between
parse_args and process_options should be inspected and
test coverage added before removing that code.

Unit tests have been added in test_shell.py to verify
that command line options are correctly passed to the
client get_auth method.

The MockHttpTest class is re-used in test_shell.py,
so it is moved from test_swiftclient.py
to tests/unit/utils.py

Closes-bug: #1372465

Change-Id: I4fed013cdb8936509609d06093337cc147ade0d6
2014-09-24 18:19:12 +01:00
Pete Zaitcev
8f1b394325 Stop creating extraneous directories
Cannot help streamlining the code a bit, sorry. But the meat is that
we should not try to make directories if output path is explicit.

Previously we created directories using the URL path, which is
obviously wrong if explicit output file is supplied... unless
a crafty user supplied the same path with -o that is contained
in the URL path. If anyone was doing such tricks, it's not going
to work anymore (we are forcing a regression for the sake of
theoretical correctness here).

Fixes bug: 1369546

Change-Id: Ifce31f2ba233eb55550f3810348bf16bf2447d62
2014-09-17 20:32:03 -06:00
Joel Wright
24673f8d19 Add importable SwiftService incorporating shell.py logic
This patch adds a SwiftService class that incorporates the high
level logic from swiftclient/shell.py. It also ports shell.py to
use the new class, and updates the code in swiftclient/multithreading.py
to allow the SwiftService to be used for multiple operations whilst
using only one thread pool.

Currently, code that imports swiftclient has to have its own logic for
things like creating large objects, parallel uploads, and parallel
downloads. This patch adds a SwiftService class that makes that
functionality available in Python code as well as through the shell.

Change-Id: I08c5796b4c01001d79fd571651c3017c16462ffd
Implements: blueprint bin-swift-logic-as-importable-library
2014-08-26 14:14:21 +02:00