From 45496feee6ae781dc0c1df4a0e5bd71b05653748 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Wed, 3 May 2017 15:17:10 +0800 Subject: Create server with security group ID and name Both resource ID and name are supported to identify an object in openstackclient to make user easy to input, for security group, nova only support security group name in API when launch a new server, this patch convert ID to name, then pass name to nova API, and check the security group exist before creating server. Change-Id: I1ed4a967fb9de3f91c8945a1ef63f6c7b6b2dfb2 Closes-Bug: #1687814 --- openstackclient/compute/v2/server.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'openstackclient/compute') diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py index 60dc605c..7ed7c7f6 100644 --- a/openstackclient/compute/v2/server.py +++ b/openstackclient/compute/v2/server.py @@ -407,7 +407,7 @@ class CreateServer(command.ShowOne): ) parser.add_argument( '--security-group', - metavar='', + metavar='', action='append', default=[], help=_('Security group to assign to this server (name or ID) ' @@ -677,6 +677,22 @@ class CreateServer(command.ShowOne): # decide the default behavior. nics = [] + # Check security group exist and convert ID to name + security_group_names = [] + if self.app.client_manager.is_network_endpoint_enabled(): + network_client = self.app.client_manager.network + for each_sg in parsed_args.security_group: + sg = network_client.find_security_group(each_sg, + ignore_missing=False) + # Use security group ID to avoid multiple security group have + # same name in neutron networking backend + security_group_names.append(sg.id) + else: + # Handle nova-network case + for each_sg in parsed_args.security_group: + sg = compute_client.api.security_group_find(each_sg) + security_group_names.append(sg['name']) + hints = {} for hint in parsed_args.hint: key, _sep, value = hint.partition('=') @@ -706,7 +722,7 @@ class CreateServer(command.ShowOne): reservation_id=None, min_count=parsed_args.min, max_count=parsed_args.max, - security_groups=parsed_args.security_group, + security_groups=security_group_names, userdata=userdata, key_name=parsed_args.key_name, availability_zone=parsed_args.availability_zone, -- cgit v1.2.1