From d7ca548883971b8042660af5523ba74e3eb78bf0 Mon Sep 17 00:00:00 2001
From: Gary Kotton <gkotton@vmware.com>
Date: Thu, 13 Jun 2013 15:25:39 +0000
Subject: [PATCH] Nova CLI for server groups
CLI support for blueprint instance-group-api-extension
REST API support:- https://review.openstack.org/#/c/62557/
DocImpact
- supports create, list, get and delete
- only V2 is supported
Change-Id: Iaa5a2922b9a0eed9f682b7584c2acf582379b422
---
novaclient/tests/v1_1/fakes.py | 45 ++++++++++++++++++
novaclient/tests/v1_1/test_server_groups.py | 52 +++++++++++++++++++++
novaclient/v1_1/client.py | 2 +
novaclient/v1_1/server_groups.py | 71 +++++++++++++++++++++++++++++
novaclient/v1_1/shell.py | 39 ++++++++++++++++
5 files changed, 209 insertions(+)
create mode 100644 novaclient/tests/v1_1/test_server_groups.py
create mode 100644 novaclient/v1_1/server_groups.py
diff --git a/novaclient/tests/v1_1/fakes.py b/novaclient/tests/v1_1/fakes.py
index dd8fdcc..50658c1 100644
--- a/novaclient/tests/v1_1/fakes.py
+++ b/novaclient/tests/v1_1/fakes.py
@@ -1996,3 +1996,48 @@ class FakeHTTPClient(base_client.HTTPClient):
return (200, {}, {'events': [
{'name': 'network-changed',
'server_uuid': '1234'}]})
+
+ #
+ # Server Groups
+ #
+
+ def get_os_server_groups(self, *kw):
+ return (200, {},
+ {"server_groups": [
+ {"members": [], "metadata": {},
+ "id": "2cbd51f4-fafe-4cdb-801b-cf913a6f288b",
+ "policies": [], "name": "ig1"},
+ {"members": [], "metadata": {},
+ "id": "4473bb03-4370-4bfb-80d3-dc8cffc47d94",
+ "policies": ["anti-affinity"], "name": "ig2"},
+ {"members": [], "metadata": {"key": "value"},
+ "id": "31ab9bdb-55e1-4ac3-b094-97eeb1b65cc4",
+ "policies": [], "name": "ig3"},
+ {"members": ["2dccb4a1-02b9-482a-aa23-5799490d6f5d"],
+ "metadata": {},
+ "id": "4890bb03-7070-45fb-8453-d34556c87d94",
+ "policies": ["anti-affinity"], "name": "ig2"}]})
+
+ def _return_server_group(self):
+ r = {'server_group':
+ self.get_os_server_groups()[2]['server_groups'][0]}
+ return (200, {}, r)
+
+ def post_os_server_groups(self, body, **kw):
+ return self._return_server_group()
+
+ def get_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b(self,
+ **kw):
+ return self._return_server_group()
+
+ def put_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b(self,
+ **kw):
+ return self._return_server_group()
+
+ def post_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b_action(
+ self, body, **kw):
+ return self._return_server_group()
+
+ def delete_os_server_groups_2cbd51f4_fafe_4cdb_801b_cf913a6f288b(
+ self, **kw):
+ return (202, {}, None)
diff --git a/novaclient/tests/v1_1/test_server_groups.py b/novaclient/tests/v1_1/test_server_groups.py
new file mode 100644
index 0000000..fde5def
--- /dev/null
+++ b/novaclient/tests/v1_1/test_server_groups.py
@@ -0,0 +1,52 @@
+# Copyright (c) 2014 VMware, Inc.
+# All Rights Reserved.
+#
+# 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.
+
+from novaclient.tests import utils
+from novaclient.tests.v1_1 import fakes
+from novaclient.v1_1 import server_groups
+
+
+cs = fakes.FakeClient()
+
+
+class ServerGroupsTest(utils.TestCase):
+
+ def test_list_server_groups(self):
+ result = cs.server_groups.list()
+ cs.assert_called('GET', '/os-server-groups')
+ for server_group in result:
+ self.assertTrue(isinstance(server_group,
+ server_groups.ServerGroup))
+
+ def test_create_server_group(self):
+ kwargs = {'name': 'ig1',
+ 'policies': ['anti-affinity']}
+ server_group = cs.server_groups.create(**kwargs)
+ body = {'server_group': kwargs}
+ cs.assert_called('POST', '/os-server-groups', body)
+ self.assertTrue(isinstance(server_group,
+ server_groups.ServerGroup))
+
+ def test_get_server_group(self):
+ id = '2cbd51f4-fafe-4cdb-801b-cf913a6f288b'
+ server_group = cs.server_groups.get(id)
+ cs.assert_called('GET', '/os-server-groups/%s' % id)
+ self.assertTrue(isinstance(server_group,
+ server_groups.ServerGroup))
+
+ def test_delete_server_group(self):
+ id = '2cbd51f4-fafe-4cdb-801b-cf913a6f288b'
+ cs.server_groups.delete(id)
+ cs.assert_called('DELETE', '/os-server-groups/%s' % id)
diff --git a/novaclient/v1_1/client.py b/novaclient/v1_1/client.py
index ce4aea1..9291270 100644
--- a/novaclient/v1_1/client.py
+++ b/novaclient/v1_1/client.py
@@ -37,6 +37,7 @@ from novaclient.v1_1 import quota_classes
from novaclient.v1_1 import quotas
from novaclient.v1_1 import security_group_rules
from novaclient.v1_1 import security_groups
+from novaclient.v1_1 import server_groups
from novaclient.v1_1 import servers
from novaclient.v1_1 import services
from novaclient.v1_1 import usage
@@ -131,6 +132,7 @@ class Client(object):
self.os_cache = os_cache or not no_cache
self.availability_zones = \
availability_zones.AvailabilityZoneManager(self)
+ self.server_groups = server_groups.ServerGroupsManager(self)
# Add in any extensions...
if extensions:
diff --git a/novaclient/v1_1/server_groups.py b/novaclient/v1_1/server_groups.py
new file mode 100644
index 0000000..be6ff8e
--- /dev/null
+++ b/novaclient/v1_1/server_groups.py
@@ -0,0 +1,71 @@
+# Copyright (c) 2014 VMware, Inc.
+# All Rights Reserved.
+#
+# 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.
+
+"""
+Server group interface.
+"""
+
+from novaclient import base
+
+
+class ServerGroup(base.Resource):
+ """
+ A server group.
+ """
+ NAME_ATTR = 'server_group_name'
+
+ def __repr__(self):
+ return '<ServerGroup: %s>' % self.id
+
+ def delete(self):
+ self.manager.delete(self)
+
+
+class ServerGroupsManager(base.ManagerWithFind):
+ """
+ Manage :class:`ServerGroup` resources.
+ """
+ resource_class = ServerGroup
+
+ def list(self):
+ """Get a list of all server groups.
+
+ :rtype: list of :class:`ServerGroup`.
+ """
+ return self._list('/os-server-groups', 'server_groups')
+
+ def get(self, id):
+ """Get a specific server group.
+
+ :param id: The ID of the :class:`ServerGroup` to get.
+ :rtype: :class:`ServerGroup`
+ """
+ return self._get('/os-server-groups/%s' % id,
+ 'server_group')
+
+ def delete(self, id):
+ """Delete a specific server group.
+
+ :param id: The ID of the :class:`ServerGroup` to delete.
+ """
+ self._delete('/os-server-groups/%s' % id)
+
+ def create(self, **kwargs):
+ """Create (allocate) a server group.
+
+ :rtype: list of :class:`ServerGroup`
+ """
+ body = {'server_group': kwargs}
+ return self._create('/os-server-groups', body, 'server_group')
diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py
index a043d3c..c1754c9 100644
--- a/novaclient/v1_1/shell.py
+++ b/novaclient/v1_1/shell.py
@@ -3512,3 +3512,42 @@ def do_availability_zone_list(cs, _args):
_translate_availability_zone_keys(result)
utils.print_list(result, ['Name', 'Status'],
sortby_index=None)
+
+
+def _print_server_group_details(server_group):
+ columns = ['Id', 'Name', 'Policies', 'Members', 'Metadata']
+ utils.print_list(server_group, columns)
+
+
+def do_server_group_list(cs, args):
+ """Print a list of all server groups."""
+ server_groups = cs.server_groups.list()
+ _print_server_group_details(server_groups)
+
+
+@utils.arg('name', metavar='<name>', help='Server group name.')
+@utils.arg('--policy', metavar='<policy>', action='append',
+ dest='policies', default=[], type=str,
+ help='Policies for the server groups')
+def do_server_group_create(cs, args):
+ """Create a new server group with the specified details."""
+ kwargs = {'name': args.name,
+ 'policies': args.policies}
+ server_group = cs.server_groups.create(**kwargs)
+ _print_server_group_details([server_group])
+
+
+@utils.arg('id', metavar='<id>',
+ help="Unique ID of the server group to delete")
+def do_server_group_delete(cs, args):
+ """Delete a specific server group."""
+ cs.server_groups.delete(args.id)
+ print("Instance group %s has been successfully deleted." % args.id)
+
+
+@utils.arg('id', metavar='<id>',
+ help="Unique ID of the server group to get")
+def do_server_group_get(cs, args):
+ """Get a specific server group."""
+ server_group = cs.server_groups.get(args.id)
+ _print_server_group_details([server_group])