Add update_server method

Currently, we have no method exposing the ability from novaclient
to update a name or a description.

Change-Id: Ic372f730c0781cd12f792bd49e073bf1681dd1e2
This commit is contained in:
Ricardo Carrillo Cruz 2016-07-21 17:55:43 +00:00
parent 51a5eb575e
commit cdea9eb827
5 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,3 @@
---
features:
- Add update_server method to update name or description of a server.

View File

@ -167,6 +167,11 @@ class ServerDelete(task_manager.Task):
return client.nova_client.servers.delete(**self.args)
class ServerUpdate(task_manager.Task):
def main(self, client):
return client.nova_client.servers.update(**self.args)
class ServerRebuild(task_manager.Task):
def main(self, client):
return client.nova_client.servers.rebuild(**self.args)

View File

@ -4471,6 +4471,30 @@ class OpenStackCloud(object):
self._servers_time = self._servers_time - self._SERVER_AGE
return True
@_utils.valid_kwargs(
'name', 'description')
def update_server(self, name_or_id, **kwargs):
"""Update a server.
:param name_or_id: Name of the server to be updated.
:name: New name for the server
:description: New description for the server
:returns: a dictionary representing the updated server.
:raises: OpenStackCloudException on operation error.
"""
server = self.get_server(name_or_id=name_or_id)
if server is None:
raise OpenStackCloudException(
"failed to find server '{server}'".format(server=name_or_id))
with _utils.shade_exceptions(
"Error updating server {0}".format(name_or_id)):
return self.manager.submitTask(
_tasks.ServerUpdate(
server=server['id'], **kwargs))
def create_server_group(self, name, policies):
"""Create a new server group.

View File

@ -250,3 +250,16 @@ class TestCompute(base.BaseFunctionalTestCase):
exc.OpenStackCloudException,
self.demo_cloud.delete_server_metadata,
self.server_name, ['key1'])
def test_update_server(self):
self.addCleanup(self._cleanup_servers_and_volumes, self.server_name)
self.demo_cloud.create_server(
name=self.server_name,
image=self.image,
flavor=self.flavor,
wait=True)
server_updated = self.demo_cloud.update_server(
self.server_name,
name='new_name'
)
self.assertEqual('new_name', server_updated['name'])

View File

@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
test_update_server
----------------------------------
Tests for the `update_server` command.
"""
from mock import patch, Mock
import os_client_config
from shade import OpenStackCloud
from shade.exc import OpenStackCloudException
from shade.tests import base, fakes
class TestUpdateServer(base.TestCase):
def setUp(self):
super(TestUpdateServer, self).setUp()
config = os_client_config.OpenStackConfig()
self.client = OpenStackCloud(
cloud_config=config.get_one_cloud(validate=False))
self.client._SERVER_AGE = 0
def test_update_server_with_update_exception(self):
"""
Test that an exception in the novaclient update raises an exception in
update_server.
"""
with patch("shade.OpenStackCloud"):
config = {
"servers.update.side_effect": Exception("exception"),
}
OpenStackCloud.nova_client = Mock(**config)
self.assertRaises(
OpenStackCloudException, self.client.update_server,
'server-name')
def test_update_server_name(self):
"""
Test that update_server updates the name without raising any exception
"""
with patch("shade.OpenStackCloud"):
fake_server = fakes.FakeServer('1234', 'server-name', 'ACTIVE')
fake_update_server = fakes.FakeServer('1234', 'server-name2',
'ACTIVE')
fake_floating_ip = fakes.FakeFloatingIP('1234', 'ippool',
'1.1.1.1', '2.2.2.2',
'5678')
config = {
"servers.list.return_value": [fake_server],
"servers.update.return_value": fake_update_server,
"floating_ips.list.return_value": [fake_floating_ip]
}
OpenStackCloud.nova_client = Mock(**config)
self.assertEqual(
'server-name2',
self.client.update_server(
'server-name', name='server-name2')['name'])