Bypass the database if limit=0 for server-list requests

Not all database engines handle fetching 0 rows the same and there isn't
really a point in going to the database to return nothing, so bypass the
DB query if limit=0.

The Nova API is explicitly checking if limit >= 0 so we can't make any
changes there since they would be backwards incompatible, i.e. to start
enforcing limit > 0.

Closes-Bug: #1296929

Change-Id: Ib3d0318bec972af5f1f25202101e76066f930f34
xenapi: add a test for _get_partitions

Currently vm_utils._get_partitions is not covered by any unit tests.
This patch adds a unit test for that code.

Change-Id: I3ff612ca88250ad3d4d3e19aa12d299e12e442ed
libvirt: remove_logical_volumes should remove each separately

Currently, remove_logical_volumes() is trying to remove
all of the provided volumes path in a single lvremove command,
while retrying it several times.
This is causing the command to fail, when the deleted volumes
are reappearing in the next iteration of the retry loop.

Handling the removal of each volume separately
and raising an exception for volumes which failed to be removed.
This will increase the chance for individual volume to be removed.

Closes-Bug: #1282423
Change-Id: Ib25f5603929c3038716661a589a41116b4c48909
Make compute manager use network api method return values

Calls to network api methods add_fixed_ip_to_instance() and
remove_fixed_ip_from_instance() return updated nw_info models
so there is no need to make separate calls to get this info.

This change refactors the affected code to avoid such extra
calls. Related methods in the neutron api are updated to return
the same information as their nova-network api counterparts.

Change-Id: I39dcf257ed5c101b13c2243266eed265c64e3c98
Closes-Bug: #1284345
ec2: Convert instance_get_by_uuid calls to objects

Convert a couple uses of db.instance_get_by_uuid() to use the Instance
object instead.

Change-Id: I6c317c72661643d46ea0577d6efe87a24bf98b74
Refactor unit tests for image service CRUD

Refactors the image service unit tests of the create() method to be unit
tests and not functional tests. There is no need to test whether an
image ID that is specified in the payload to the glance image service is
preserved; the unit and functional tests in the glanceclient package do
that, and besides, all this was testing was the behavior of the fake
glance client, which is pointless. There was also a test that created an
image and then tested the show method would raise InstanceNotFound if
not passing in the same instance name that was just created. This test
was removed, as ImageNotFound is already thoroughly tested in the show()
unit tests.

Missing in the existing unit tests was a test of failure conditions
resulting in error codes returned from the glanceclient.create() call,
and this patch adds a unit test that stresses the failure paths.

These new unit tests use mocks and assert that all code paths in the
create(), update() and delete() methods are fully tested.

Change-Id: I6d903979290b7d027c8dc66d2da712e1e1961a00
Related-bug: #1293938
Make quota rollback checks more robust in conductor tests

The existing cold migrate tests in conductor were mocking out the quota
object's rollback method but not actually validating that the quota
object is constructed properly or that the quota.QUOTAS.rollback static
method was called with the correct project_id and user_id given the
context and instance being used.  This patches makes the tests more
robust by not just making sure a method is called but that it's called
correctly.

Related-Bug: #1296502

Change-Id: Ib367d728b6bd50998db32d235f41212d0f94a8f7
Make _get_compute_info() use objects

This removes a direct call to the conductor api for getting service
info and replaces it with an object call. We still need to convert
the compute nodes to primitives for the live migration compute info
blob, but once that is converted to objects, we should be good.

Related to blueprint compute-manager-objects-juno

Change-Id: Ic8213bac5ce4754bfc95d7b47e8b9d4a65dffcc6
Add with_compute_node to service_get()

Right now, a service_get() call cannot joinedload the compute_node
attribute. This means anything that needs that data has to trigger
a lazy-load. A subsequent change will depend on this.

Related to blueprint compute-manager-objects-juno

Change-Id: Ie537f30374280203ea153230cfa1a7aecfa5b891
Add get_by_service() method to ComputeNodeList object

This allows us to get the ComputeNode objects related to a single
Service, in the case of the many-to-one situations of baremetal
and vmwareapi. In order to avoid passing the whole Service over
RPC, this adds a non-remotable API in front of the RPC one that
takes a full service, but ends up only passing the ID.

Related to blueprint compute-manager-objects-juno

Change-Id: I995fa6948bda64d2f5d60f0c81270321133bbfa8
Make update_available_resource() use objects

This makes the update_available_resource() periodic task use
objects instead of direct calls to conductor.

Related to blueprint compute-manager-objects-juno

Change-Id: I39fe7cae852c60e0490a5b2f1a98a011ac8b8425
Convert poll_rescued_instances() periodic task to objects

This converts poll_rescued_instances() to use instance objects for
fetching the list of unrescued instances, and makes it call directly
to compute api instead of going through conductor. Now that
compute_api.unrescue() uses objects natively, we can avoid the
indirection through conductor, which makes it faster and reduces
messaging load.

Related to blueprint compute-manager-objects-juno

Change-Id: I68b2b57831048932277e1f4e388f8a1aa8309aff
Make aggregate host operations use Aggregate object

This removes use of the conductor aggregate host methods, which
can now be done on the aggregate object itself. This is only used
in the xenapi driver, and the tests validate the operation in the
database afterwards, so no test changes are required here. This
will allow us to deprecate those aggregate methods in the conductor
api.

Related to blueprint compute-manager-objects-juno

Change-Id: I78f8d0d9ad0dd73c5c52582940dd70db73522600
Move _get_locations to module level plus tests

Moves the nova.image.glance.GlanceImageService._get_locations object
method into module level scope (the self argument was unused) and
adds full unit tests for the nova.image.glance._get_location() function.

Change-Id: Ib386330b70abe1e0353f1fafa3e68bc3e7a2d1b1
Partial-bug: #1293938
Allow -1 as the length of "get console output" API

Current "get console output" API does not allow -1 as the length and
returns a BadRequest response if receiving it. The other APIs(quota)
considers -1 as an unlimited value.
This patch allows -1 as the length for API consistency.

DocImpact

Change-Id: Idf88a238d1b0e545ebab5be872269b1b1030cc56
Related-Bug: #1295426
Merge "libvirt: remove_logical_volumes should remove each separately"
Merge "ec2: Convert instance_get_by_uuid calls to objects"
Revert "Fix network-api direct database hits in metadata server"

This reverts commit 79ab96e34ba5b8dd3e4e542dd3a7f65624b13367.

This commit broke the Nova metadata API when using neutron.

Change-Id: I6855b03f00492fdf51e18c0dcb0a73590a71b374
Closes-bug: #1307582
Don't explode if we fail to unplug VIFs after a failed boot

Libvirt currently attempts to unplug VIFs after destroying an
instance, which can fail if the instance was never built
properly in the first place. This patch makes it ignore errors
when trying to tear down VIFs during that cleanup. Failure
in this case would block the deletion of the instance (which
should never happen) and any real failures aren't something
we could do anything about.

Closes-bug: #1306718

Change-Id: I84a4433c3959b8f14e0259be0aa51da1654006c8
Merge "Make _get_compute_info() use objects"