diff --git a/gbpclient/gbp/v2_0/groupbasedpolicy.py b/gbpclient/gbp/v2_0/groupbasedpolicy.py index 5eec7f6..155d068 100644 --- a/gbpclient/gbp/v2_0/groupbasedpolicy.py +++ b/gbpclient/gbp/v2_0/groupbasedpolicy.py @@ -179,7 +179,8 @@ class ListPolicyTargetGroup(neutronV20.ListCommand): resource = 'policy_target_group' log = logging.getLogger(__name__ + '.ListPolicyTargetGroup') - list_columns = ['id', 'name', 'description', 'l2_policy_id', 'subnets'] + list_columns = ['id', 'name', 'description', 'application_policy_group_id', + 'l2_policy_id', 'subnets'] pagination_support = True sorting_support = True @@ -205,6 +206,11 @@ class CreatePolicyTargetGroup(neutronV20.CreateCommand): 'name', metavar='NAME', help=_('Name of Policy Target Group to create ' '(required argument)')) + parser.add_argument( + '--application-policy-group', metavar='APPLICATION_POLICY_GROUP', + default='', + help=_('Application Policy Group UUID (optional, default ' + 'is None)')) parser.add_argument( '--l2-policy', metavar='L2_POLICY', default='', @@ -241,6 +247,12 @@ class CreatePolicyTargetGroup(neutronV20.CreateCommand): self.get_client(), 'l2_policy', parsed_args.l2_policy) + if parsed_args.application_policy_group: + body[self.resource]['application_policy_group_id'] = ( + neutronV20.find_resourceid_by_name_or_id( + self.get_client(), 'application_policy_group', + parsed_args.application_policy_group)) + if parsed_args.network_service_policy: body[self.resource]['network_service_policy_id'] = \ neutronV20.find_resourceid_by_name_or_id( @@ -292,6 +304,9 @@ class UpdatePolicyTargetGroup(neutronV20.UpdateCommand): parser.add_argument( '--l2-policy', metavar='L2_POLICY', help=_('New L2 policy')) + parser.add_argument( + '--application-policy-group', metavar='APPLICATION_POLICY_GROUP', + help=_('New Application Policy Group')) parser.add_argument( '--network-service-policy', metavar='NETWORK_SERVICE_POLICY', help=_('New Network Service Policy')) @@ -326,6 +341,14 @@ class UpdatePolicyTargetGroup(neutronV20.UpdateCommand): self.get_client(), 'l2_policy', parsed_args.l2_policy) + if parsed_args.application_policy_group == '': + body[self.resource]['application_policy_group_id'] = None + elif parsed_args.application_policy_group: + body[self.resource]['application_policy_group_id'] = ( + neutronV20.find_resourceid_by_name_or_id( + self.get_client(), 'application_policy_group', + parsed_args.application_policy_group)) + if parsed_args.network_service_policy == '': body[self.resource]['network_service_policy_id'] = None elif parsed_args.network_service_policy: @@ -674,6 +697,85 @@ class UpdateL3Policy(neutronV20.UpdateCommand): return body +class ListApplicationPolicyGroup(neutronV20.ListCommand): + """List l3_policies that belong to a given tenant.""" + + resource = 'application_policy_group' + log = logging.getLogger(__name__ + '.ListApplicationPolicyGroup') + _formatters = {} + list_columns = ['id', 'name', 'description', 'shared'] + pagination_support = True + sorting_support = True + + +class ShowApplicationPolicyGroup(neutronV20.ShowCommand): + """Show information of a given Application Policy Group.""" + + resource = 'application_policy_group' + log = logging.getLogger(__name__ + '.ShowApplicationPolicyGroup') + + +class CreateApplicationPolicyGroup(neutronV20.CreateCommand): + """Create a Application Policy Group for a given tenant.""" + + resource = 'application_policy_group' + log = logging.getLogger(__name__ + '.CreateApplicationPolicyGroup') + + def add_known_arguments(self, parser): + parser.add_argument( + '--description', + help=_('Description of the Application Policy Group')) + parser.add_argument( + 'name', metavar='NAME', + help=_('Name of L3 policy to create (required argument)')) + n_utils.add_boolean_argument( + parser, '--shared', dest='shared', + help=_('Enable or disable resource sharing, default is False')) + + def args2body(self, parsed_args): + body = {self.resource: {}, } + + neutronV20.update_dict(parsed_args, body[self.resource], + ['name', 'tenant_id', 'description', + 'shared']) + + return body + + +class DeleteApplicationPolicyGroup(neutronV20.DeleteCommand): + """Delete a given Application Policy Group.""" + + resource = 'application_policy_group' + log = logging.getLogger(__name__ + '.DeleteApplicationPolicyGroup') + + +class UpdateApplicationPolicyGroup(neutronV20.UpdateCommand): + """Update Application Policy Group's information.""" + + resource = 'application_policy_group' + log = logging.getLogger(__name__ + '.UpdateApplicationPolicyGroup') + + def add_known_arguments(self, parser): + parser.add_argument( + '--description', + help=_('New description of the Application Policy Group')) + parser.add_argument( + '--name', + help=_('New name of the Application Policy Group')) + n_utils.add_boolean_argument( + parser, '--shared', dest='shared', + help=_('Enable or disable resource sharing')) + + def args2body(self, parsed_args): + body = {self.resource: {}, } + + neutronV20.update_dict(parsed_args, body[self.resource], + ['name', 'tenant_id', 'description', + 'shared']) + + return body + + class ListNetworkServicePolicy(neutronV20.ListCommand): """List Network Service Policies that belong to a given tenant.""" diff --git a/gbpclient/gbpshell.py b/gbpclient/gbpshell.py index 56b3fdb..875976a 100644 --- a/gbpclient/gbpshell.py +++ b/gbpclient/gbpshell.py @@ -102,6 +102,11 @@ COMMAND_V2 = { 'group-update': gbp.UpdatePolicyTargetGroup, 'group-list': gbp.ListPolicyTargetGroup, 'group-show': gbp.ShowPolicyTargetGroup, + 'application-policy-group-create': gbp.CreateApplicationPolicyGroup, + 'application-policy-group-delete': gbp.DeleteApplicationPolicyGroup, + 'application-policy-group-update': gbp.UpdateApplicationPolicyGroup, + 'application-policy-group-list': gbp.ListApplicationPolicyGroup, + 'application-policy-group-show': gbp.ShowApplicationPolicyGroup, 'l2policy-create': gbp.CreateL2Policy, 'l2policy-delete': gbp.DeleteL2Policy, 'l2policy-update': gbp.UpdateL2Policy, @@ -192,6 +197,11 @@ COMMAND_V2 = { 'ptg-update': gbp.UpdatePolicyTargetGroup, 'ptg-list': gbp.ListPolicyTargetGroup, 'ptg-show': gbp.ShowPolicyTargetGroup, + 'apg-create': gbp.CreateApplicationPolicyGroup, + 'apg-delete': gbp.DeleteApplicationPolicyGroup, + 'apg-update': gbp.UpdateApplicationPolicyGroup, + 'apg-list': gbp.ListApplicationPolicyGroup, + 'apg-show': gbp.ShowApplicationPolicyGroup, 'l2p-create': gbp.CreateL2Policy, 'l2p-delete': gbp.DeleteL2Policy, 'l2p-update': gbp.UpdateL2Policy, diff --git a/gbpclient/tests/unit/test_cli20_apg.py b/gbpclient/tests/unit/test_cli20_apg.py new file mode 100644 index 0000000..1ebd449 --- /dev/null +++ b/gbpclient/tests/unit/test_cli20_apg.py @@ -0,0 +1,111 @@ +# 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. +# + +import logging +import sys + +from gbpclient.gbp.v2_0 import groupbasedpolicy as gbp +from gbpclient.tests.unit import test_cli20 + + +class CLITestV20ApplicationPolicyGroupJSON(test_cli20.CLITestV20Base): + + LOG = logging.getLogger(__name__) + + def setUp(self): + super(CLITestV20ApplicationPolicyGroupJSON, self).setUp() + + def test_create_application_policy_group_with_mandatory_params(self): + resource = 'application_policy_group' + cmd = gbp.CreateApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + name = 'my-name' + tenant_id = 'my-tenant' + my_id = 'my-id' + args = ['--tenant-id', tenant_id, + name] + position_names = ['name', ] + position_values = [name, ] + self._test_create_resource(resource, cmd, name, my_id, args, + position_names, position_values, + tenant_id=tenant_id) + + def test_create_application_policy_group_with_all_params(self): + """application-policy-group-create with all params.""" + resource = 'application_policy_group' + cmd = gbp.CreateApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + name = 'myname' + tenant_id = 'mytenant' + description = 'My Application Policy Group' + my_id = 'someid' + shared = 'true' + args = ['--tenant-id', tenant_id, + '--description', description, + '--shared', shared, + name] + position_names = ['name', ] + position_values = [name, ] + self._test_create_resource(resource, cmd, name, my_id, args, + position_names, position_values, + tenant_id=tenant_id, + description=description, shared=shared) + + def test_list_application_policy_groups(self): + resource = 'application_policy_groups' + cmd = gbp.ListApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + self._test_list_resources(resource, cmd, True) + + def test_show_application_policy_group(self): + resource = 'application_policy_group' + cmd = gbp.ShowApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + args = ['--fields', 'id', self.test_id] + self._test_show_resource(resource, cmd, self.test_id, args, ['id']) + + def test_update_application_policy_group(self): + resource = 'application_policy_group' + cmd = gbp.UpdateApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + self._test_update_resource(resource, cmd, 'myid', + ['myid', '--name', 'myname', + '--tags', 'a', 'b'], + {'name': 'myname', 'tags': ['a', 'b'], }) + + def test_update_application_policy_group_with_all_params(self): + resource = 'application_policy_group' + cmd = gbp.UpdateApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + name = 'myname' + description = 'My Application Policy Group' + my_id = 'someid' + shared = 'true' + args = ['--name', name, + '--description', description, + '--shared', shared, + my_id] + params = { + 'name': name, + 'description': description, + 'shared': shared + } + self._test_update_resource(resource, cmd, my_id, args, params) + + def test_delete_application_policy_group_name(self): + resource = 'application_policy_group' + cmd = gbp.DeleteApplicationPolicyGroup(test_cli20.MyApp(sys.stdout), + None) + my_id = 'my-id' + args = [my_id] + self._test_delete_resource(resource, cmd, my_id, args) diff --git a/gbpclient/tests/unit/test_cli20_policytargetgroup.py b/gbpclient/tests/unit/test_cli20_policytargetgroup.py index c2c45d1..b37c693 100644 --- a/gbpclient/tests/unit/test_cli20_policytargetgroup.py +++ b/gbpclient/tests/unit/test_cli20_policytargetgroup.py @@ -48,6 +48,7 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): tenant_id = 'my-tenant' name = 'my-name' description = 'ptg description' + application_policy_group_id = 'application_policy_group_id' l2_policy_id = 'l2_policy_id' provided_prs = "icmp-prs=false,web-prs=true" consumed_prs = "ssh-prs=true,ftp-prs=false" @@ -57,13 +58,15 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): args = [name, '--tenant-id', tenant_id, '--description', description, + '--application-policy-group-id', application_policy_group_id, '--l2-policy-id', l2_policy_id, '--provided-policy-rule-sets', provided_prs, '--consumed-policy-rule-sets', consumed_prs, '--network-service-policy-id', network_service_policy_id, '--shared', shared, '--intra-ptg-allow', intra_ptg_allow] - position_names = ['name', 'description', 'l2_policy_id', + position_names = ['name', 'description', 'application_policy_group_id', + 'l2_policy_id', 'provided_policy_rule_sets', 'consumed_policy_rule_sets', 'network_service_policy_id'] @@ -73,7 +76,8 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): consumed_policy_rule_sets = { 'ssh-prs': 'true', 'ftp-prs': 'false'} - position_values = [name, description, l2_policy_id, + position_values = [name, description, application_policy_group_id, + l2_policy_id, provided_policy_rule_sets, consumed_policy_rule_sets, network_service_policy_id] @@ -111,6 +115,7 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): my_id = 'my-id' name = 'ptg' description = 'ptg description' + application_policy_group_id = 'application_policy_group_id' l2_policy_id = 'l2_policy_id' provided_prs = "icmp-prs=false,web-prs=true" consumed_prs = "ssh-prs=true,ftp-prs=false" @@ -120,6 +125,7 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): args = [my_id, '--name', name, '--description', description, + '--application-policy-group-id', application_policy_group_id, '--l2-policy-id', l2_policy_id, '--provided-policy-rule-sets', provided_prs, '--consumed-policy-rule-sets', consumed_prs, @@ -135,6 +141,7 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): params = { 'name': name, 'description': description, + 'application_policy_group_id': application_policy_group_id, 'l2_policy_id': l2_policy_id, 'provided_policy_rule_sets': provided_policy_rule_sets, 'consumed_policy_rule_sets': consumed_policy_rule_sets, @@ -172,6 +179,17 @@ class CLITestV20PolicyTargetGroupJSON(test_cli20.CLITestV20Base): params = {'network_service_policy_id': None} self._test_update_resource(resource, cmd, my_id, args, params) + def test_update_policy_target_group_unset_apg(self): + """policy-target-group-update.""" + resource = 'policy_target_group' + cmd = gbp.UpdatePolicyTargetGroup(test_cli20.MyApp(sys.stdout), None) + my_id = 'my-id' + application_policy_group_id = '' + args = [my_id, + '--application-policy-group', application_policy_group_id] + params = {'application_policy_group_id': None} + self._test_update_resource(resource, cmd, my_id, args, params) + def test_delete_policy_target_group_name(self): """policy-target-group-delete.""" resource = 'policy_target_group' diff --git a/gbpclient/v2_0/client.py b/gbpclient/v2_0/client.py index e87407b..5aa084e 100644 --- a/gbpclient/v2_0/client.py +++ b/gbpclient/v2_0/client.py @@ -149,6 +149,8 @@ class Client(object): policy_target_path = "/grouppolicy/policy_targets/%s" policy_target_groups_path = "/grouppolicy/policy_target_groups" policy_target_group_path = "/grouppolicy/policy_target_groups/%s" + application_policy_groups_path = "/grouppolicy/application_policy_groups" + application_policy_group_path = "/grouppolicy/application_policy_groups/%s" l2_policies_path = "/grouppolicy/l2_policies" l2_policy_path = "/grouppolicy/l2_policies/%s" l3_policies_path = "/grouppolicy/l3_policies" @@ -181,6 +183,7 @@ class Client(object): # API has no way to report plurals, so we have to hard code them EXTED_PLURALS = {'policy_targets': 'policy_target', 'policy_target_groups': 'policy_target_group', + 'application_policy_groups': 'application_policy_group', 'l2_policies': 'l2_policy', 'l3_policies': 'l3_policy', 'network_service_policies': 'network_service_policy', @@ -218,6 +221,40 @@ class Client(object): return self.get(self.policy_target_path % (policy_target), params=_params) + @APIParamsCall + def list_application_policy_groups(self, retrieve_all=True, **_params): + """Fetches a list of all application_policy_groups for a tenant.""" + # Pass filters in "params" argument to do_request + return self.list('application_policy_groups', + self.application_policy_groups_path, + retrieve_all, **_params) + + @APIParamsCall + def show_application_policy_group( + self, application_policy_group, **_params): + """Fetches information of a certain application_policy_group.""" + return self.get(self.application_policy_group_path % ( + application_policy_group), params=_params) + + @APIParamsCall + def create_application_policy_group(self, body=None): + """Creates a new application_policy_group.""" + return self.post(self.application_policy_groups_path, body=body) + + @APIParamsCall + def update_application_policy_group( + self, application_policy_group, body=None): + """Updates a application_policy_group.""" + return self.put( + self.application_policy_group_path % (application_policy_group), + body=body) + + @APIParamsCall + def delete_application_policy_group(self, application_policy_group): + """Deletes the specified application_policy_group.""" + return self.delete( + self.application_policy_group_path % (application_policy_group)) + @APIParamsCall def create_policy_target(self, body=None): """Creates a new policy target."""