Skip to content

Commit aaed4b3

Browse files
committed
Network: Add tag support for security group
Change-Id: Icccb23429913724c6a8bd15d4737672b47a5f13a Closes-Bug: #1750983
1 parent 7505831 commit aaed4b3

6 files changed

Lines changed: 295 additions & 6 deletions

File tree

doc/source/cli/command-objects/security-group.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Create a new security group
1919
openstack security group create
2020
[--description <description>]
2121
[--project <project> [--project-domain <project-domain>]]
22+
[--tag <tag> | --no-tag]
2223
<name>
2324
2425
.. option:: --description <description>
@@ -38,6 +39,18 @@ Create a new security group
3839
3940
*Network version 2 only*
4041
42+
.. option:: --tag <tag>
43+
44+
Tag to be added to the security group (repeat option to set multiple tags)
45+
46+
*Network version 2 only*
47+
48+
.. option:: --no-tag
49+
50+
No tags associated with the security group
51+
52+
*Network version 2 only*
53+
4154
.. describe:: <name>
4255
4356
New security group name
@@ -68,6 +81,8 @@ List security groups
6881
openstack security group list
6982
[--all-projects]
7083
[--project <project> [--project-domain <project-domain>]]
84+
[--tags <tag>[,<tag>,...]] [--any-tags <tag>[,<tag>,...]]
85+
[--not-tags <tag>[,<tag>,...]] [--not-any-tags <tag>[,<tag>,...]]
7186
7287
.. option:: --all-projects
7388
@@ -89,6 +104,30 @@ List security groups
89104
90105
*Network version 2 only*
91106
107+
.. option:: --tags <tag>[,<tag>,...]
108+
109+
List security groups which have all given tag(s)
110+
111+
*Network version 2 only*
112+
113+
.. option:: --any-tags <tag>[,<tag>,...]
114+
115+
List security groups which have any given tag(s)
116+
117+
*Network version 2 only*
118+
119+
.. option:: --not-tags <tag>[,<tag>,...]
120+
121+
Exclude security groups which have all given tag(s)
122+
123+
*Network version 2 only*
124+
125+
.. option:: --not-any-tags <tag>[,<tag>,...]
126+
127+
Exclude security groups which have any given tag(s)
128+
129+
*Network version 2 only*
130+
92131
security group set
93132
------------------
94133
@@ -100,6 +139,7 @@ Set security group properties
100139
openstack security group set
101140
[--name <new-name>]
102141
[--description <description>]
142+
[--tag <tag>] [--no-tag]
103143
<group>
104144
105145
.. option:: --name <new-name>
@@ -110,6 +150,15 @@ Set security group properties
110150
111151
New security group description
112152
153+
.. option:: --tag <tag>
154+
155+
Tag to be added to the security group (repeat option to set multiple tags)
156+
157+
.. option:: --no-tag
158+
159+
Clear tags associated with the security group. Specify both --tag
160+
and --no-tag to overwrite current tags
161+
113162
.. describe:: <group>
114163
115164
Security group to modify (name or ID)
@@ -128,3 +177,28 @@ Display security group details
128177
.. describe:: <group>
129178
130179
Security group to display (name or ID)
180+
181+
security group unset
182+
--------------------
183+
184+
Unset security group properties
185+
186+
.. program:: security group unset
187+
.. code:: bash
188+
189+
openstack security group unset
190+
[--tag <tag> | --all-tag]
191+
<group>
192+
193+
.. option:: --tag <tag>
194+
195+
Tag to be removed from the security group
196+
(repeat option to remove multiple tags)
197+
198+
.. option:: --all-tag
199+
200+
Clear all tags associated with the security group
201+
202+
.. describe:: <group>
203+
204+
Security group to modify (name or ID)

openstackclient/network/v2/security_group.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import argparse
1717

18+
from osc_lib.command import command
1819
from osc_lib import utils
1920
import six
2021

@@ -23,6 +24,7 @@
2324
from openstackclient.network import common
2425
from openstackclient.network import sdk_utils
2526
from openstackclient.network import utils as network_utils
27+
from openstackclient.network.v2 import _tag
2628

2729

2830
def _format_network_security_group_rules(sg_rules):
@@ -106,6 +108,7 @@ def update_parser_network(self, parser):
106108
help=_("Owner's project (name or ID)")
107109
)
108110
identity_common.add_project_domain_option_to_parser(parser)
111+
_tag.add_tag_option_to_parser_for_create(parser, _('security group'))
109112
return parser
110113

111114
def _get_description(self, parsed_args):
@@ -130,6 +133,8 @@ def take_action_network(self, client, parsed_args):
130133

131134
# Create the security group and display the results.
132135
obj = client.create_security_group(**attrs)
136+
# tags cannot be set when created, so tags need to be set later.
137+
_tag.update_tags_for_set(client, obj, parsed_args)
133138
display_columns, property_columns = _get_columns(obj)
134139
data = utils.get_item_properties(
135140
obj,
@@ -198,6 +203,7 @@ def update_parser_network(self, parser):
198203
"(name or ID)")
199204
)
200205
identity_common.add_project_domain_option_to_parser(parser)
206+
_tag.add_tag_filtering_option_to_parser(parser, _('security group'))
201207
return parser
202208

203209
def update_parser_compute(self, parser):
@@ -220,19 +226,23 @@ def take_action_network(self, client, parsed_args):
220226
).id
221227
filters['tenant_id'] = project_id
222228
filters['project_id'] = project_id
229+
230+
_tag.get_tag_filtering_args(parsed_args, filters)
223231
data = client.security_groups(**filters)
224232

225233
columns = (
226234
"ID",
227235
"Name",
228236
"Description",
229-
"Project ID"
237+
"Project ID",
238+
"tags"
230239
)
231240
column_headers = (
232241
"ID",
233242
"Name",
234243
"Description",
235-
"Project"
244+
"Project",
245+
"Tags"
236246
)
237247
return (column_headers,
238248
(utils.get_item_properties(
@@ -282,6 +292,10 @@ def update_parser_common(self, parser):
282292
)
283293
return parser
284294

295+
def update_parser_network(self, parser):
296+
_tag.add_tag_option_to_parser_for_set(parser, _('security group'))
297+
return parser
298+
285299
def take_action_network(self, client, parsed_args):
286300
obj = client.find_security_group(parsed_args.group,
287301
ignore_missing=False)
@@ -295,6 +309,9 @@ def take_action_network(self, client, parsed_args):
295309
# the update.
296310
client.update_security_group(obj, **attrs)
297311

312+
# tags is a subresource and it needs to be updated separately.
313+
_tag.update_tags_for_set(client, obj, parsed_args)
314+
298315
def take_action_compute(self, client, parsed_args):
299316
data = client.api.security_group_find(parsed_args.group)
300317

@@ -344,3 +361,25 @@ def take_action_compute(self, client, parsed_args):
344361
formatters=_formatters_compute
345362
)
346363
return (display_columns, data)
364+
365+
366+
class UnsetSecurityGroup(command.Command):
367+
_description = _("Unset security group properties")
368+
369+
def get_parser(self, prog_name):
370+
parser = super(UnsetSecurityGroup, self).get_parser(prog_name)
371+
parser.add_argument(
372+
'group',
373+
metavar="<group>",
374+
help=_("Security group to modify (name or ID)")
375+
)
376+
_tag.add_tag_option_to_parser_for_unset(parser, _('security group'))
377+
return parser
378+
379+
def take_action(self, parsed_args):
380+
client = self.app.client_manager.network
381+
obj = client.find_security_group(parsed_args.group,
382+
ignore_missing=False)
383+
384+
# tags is a subresource and it needs to be updated separately.
385+
_tag.update_tags_for_unset(client, obj, parsed_args)

openstackclient/tests/unit/network/v2/fakes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ def create_one_security_group(attrs=None):
11311131
'description': 'security-group-description-' + uuid.uuid4().hex,
11321132
'project_id': 'project-id-' + uuid.uuid4().hex,
11331133
'security_group_rules': [],
1134+
'tags': []
11341135
}
11351136

11361137
# Overwrite default attributes.

0 commit comments

Comments
 (0)