From 2fceab2a588366565d7b3a13b51f3eefaa580fc5 Mon Sep 17 00:00:00 2001 From: KC Wang Date: Thu, 13 Jun 2013 00:07:03 -0700 Subject: [PATCH] FWaaS Client and Cli (Work in Progress) Change-Id: Ib59a8a2b12b2286954c0d8ae56d7be78dfcbace7 --- quantumclient/quantum/v2_0/firewall.py | 90 +++++++++++++ quantumclient/quantum/v2_0/firewallpolicy.py | 92 ++++++++++++++ quantumclient/quantum/v2_0/firewallrule.py | 109 ++++++++++++++++ quantumclient/shell.py | 30 +++++ quantumclient/v2_0/client.py | 125 ++++++++++++++++++- 5 files changed, 445 insertions(+), 1 deletion(-) create mode 100644 quantumclient/quantum/v2_0/firewall.py create mode 100644 quantumclient/quantum/v2_0/firewallpolicy.py create mode 100644 quantumclient/quantum/v2_0/firewallrule.py diff --git a/quantumclient/quantum/v2_0/firewall.py b/quantumclient/quantum/v2_0/firewall.py new file mode 100644 index 000000000..04c1cfcf4 --- /dev/null +++ b/quantumclient/quantum/v2_0/firewall.py @@ -0,0 +1,90 @@ +# Copyright 2013 Big Switch Networks +# 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. +# +# @author: KC Wang, Big Switch Networks +# +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +import logging + +from quantumclient.quantum import v2_0 as quantumv20 + + +class ListFirewall(quantumv20.ListCommand): + """List firewalls that belong to a given tenant.""" + + resource = 'firewall' + log = logging.getLogger(__name__ + '.ListFirewall') + list_columns = ['id', 'name', 'description', + 'shared', 'admin_state_up', 'status', + 'firewall _policy_id'] + _formatters = {} + pagination_support = True + sorting_support = True + + +class ShowFirewall(quantumv20.ShowCommand): + """Show information of a given firewall.""" + + resource = 'firewall' + log = logging.getLogger(__name__ + '.ShowFirewall') + + +class CreateFirewall(quantumv20.CreateCommand): + """Create a firewall.""" + + resource = 'firewall' + log = logging.getLogger(__name__ + '.CreateFirewall') + + def add_known_arguments(self, parser): + parser.add_argument( + '--name', + help='name for the firewall') + parser.add_argument( + '--description', + help='description for the firewall rule') + parser.add_argument( + '--shared', + action='store_true', + help='shared (default True)') + parser.add_argument( + '--admin_state_up', + help='action (default True)') + parser.add_argument( + '--firewall_policy_id' + help='firewall policy id') + + def args2body(self, parsed_args): + body = { + self.resource: {}, + } + quantumv20.update_dict(parsed_args, body[self.resource], + ['name', 'description', 'shared', + 'tenant_id']) + return body + + +class UpdateFirewall(quantumv20.UpdateCommand): + """Update a given firewall.""" + + resource = 'firewall' + log = logging.getLogger(__name__ + '.UpdateFirewall') + + +class DeleteFirewall(quantumv20.DeleteCommand): + """Delete a given firewall.""" + + resource = 'firewall' + log = logging.getLogger(__name__ + '.DeleteFirewall') diff --git a/quantumclient/quantum/v2_0/firewallpolicy.py b/quantumclient/quantum/v2_0/firewallpolicy.py new file mode 100644 index 000000000..82c00b2e5 --- /dev/null +++ b/quantumclient/quantum/v2_0/firewallpolicy.py @@ -0,0 +1,92 @@ +# Copyright 2013 Big Switch Networks +# 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. +# +# @author: KC Wang, Big Switch Networks +# +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +import logging + +from quantumclient.quantum import v2_0 as quantumv20 + + +class ListFirewallPolicy(quantumv20.ListCommand): + """List firewall policies that belong to a given tenant.""" + + resource = 'firewall_policy' + log = logging.getLogger(__name__ + '.ListFirewallPolicy') + list_columns = ['id', 'name', 'description', 'shared', + 'firewall_rules_list', 'firewalls_list', + 'audited'] + _formatters = {} + pagination_support = True + sorting_support = True + + +class ShowFirewallPolicy(quantumv20.ShowCommand): + """Show information of a given firewall policy.""" + + resource = 'firewall_policy' + log = logging.getLogger(__name__ + '.ShowFirewallPolicy') + + +class CreateFirewallPolicy(quantumv20.CreateCommand): + """Create a firewall policy.""" + + resource = 'firewall_policy' + log = logging.getLogger(__name__ + '.CreateFirewallPolicy') + + def add_known_arguments(self, parser): + parser.add_argument( + '--name', + help='name for the firewall policy') + parser.add_argument( + '--description', + help='description for the firewall policy') + parser.add_argument( + '--shared', + action='store_true', + help='shared (default True)' + parser.add_argument( + '--firewall_rules_list', + help='ordered list of firewall rules for the policy') + parser.add_argument( + '--audited', + action='store_false', + help='audited (default False)') + + def args2body(self, parsed_args): + body = { + self.resource: {}, + } + quantumv20.update_dict(parsed_args, body[self.resource], + ['name', 'description', 'shared', + 'firewall_rules_list', 'audited', + 'tenant_id']) + return body + + +class UpdateFirewallPolicy(quantumv20.UpdateCommand): + """Update a given firewall policy.""" + + resource = 'firewall_policy' + log = logging.getLogger(__name__ + '.UpdateFirewallPolicy') + + +class DeleteFirewallPolicy(quantumv20.DeleteCommand): + """Delete a given firewall policy.""" + + resource = 'firewall_policy' + log = logging.getLogger(__name__ + '.DeleteFirewallPolicy') diff --git a/quantumclient/quantum/v2_0/firewallrule.py b/quantumclient/quantum/v2_0/firewallrule.py new file mode 100644 index 000000000..60c4a9c94 --- /dev/null +++ b/quantumclient/quantum/v2_0/firewallrule.py @@ -0,0 +1,109 @@ +# Copyright 2013 Big Switch Networks +# 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. +# +# @author: KC Wang, Big Switch Networks +# +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +import logging + +from quantumclient.quantum import v2_0 as quantumv20 + + +class ListFirewallRule(quantumv20.ListCommand): + """List firewall rules that belong to a given tenant.""" + + resource = 'firewall_rule' + log = logging.getLogger(__name__ + '.ListFirewallRule') + list_columns = ['id', 'name', 'description', 'firewall_policy_id', + 'shared', 'protocol', 'source_ip_address', + 'destination_ip_address', 'source_port', + 'destination_port', 'action', 'enabled'] + _formatters = {} + pagination_support = True + sorting_support = True + + +class ShowFirewallRule(quantumv20.ShowCommand): + """Show information of a given firewall rule.""" + + resource = 'firewall_rule' + log = logging.getLogger(__name__ + '.ShowFirewallRule') + + +class CreateFirewallRule(quantumv20.CreateCommand): + """Create a firewall rule.""" + + resource = 'firewall_rule' + log = logging.getLogger(__name__ + '.CreateFirewallRule') + + def add_known_arguments(self, parser): + parser.add_argument( + '--name', + help='name for the firewall rule') + parser.add_argument( + '--description', + help='description for the firewall rule') + parser.add_argument( + '--shared', + action='store_true', + help='shared (default True)') + parser.add_argument( + '--protcol', + help='protocol for the firewall rule: {tcp, udp, icmp}') + parser.add_argument( + '--source-ip-address', + help='source ip address') + parser.add_argument( + '--destination-ip-address', + help='destination ip address') + parser.add_argument( + '--source-port', + help='source port') + parser.add_argument( + '--destination-port', + help='destination port') + parser.add_argument( + '--action', + help='action (default deny)') + parser.add_argument( + '--enabled', + action='store_true', + help='enabled (default True)') + + def args2body(self, parsed_args): + body = { + self.resource: {}, + } + quantumv20.update_dict(parsed_args, body[self.resource], + ['name', 'description', 'shared', 'protocol', + 'source_ip_address', 'destination_ip_address', + 'source_port', 'destination_port', + 'action', 'enabled', 'tenant_id']) + return body + + +class UpdateFirewallRule(quantumv20.UpdateCommand): + """Update a given firewall rule.""" + + resource = 'firewall_rule' + log = logging.getLogger(__name__ + '.UpdateFirewallRule') + + +class DeleteFirewallRule(quantumv20.DeleteCommand): + """Delete a given firewall rule.""" + + resource = 'firewall_rule' + log = logging.getLogger(__name__ + '.DeleteFirewallRule') diff --git a/quantumclient/shell.py b/quantumclient/shell.py index afcabb7a0..9673e67ad 100644 --- a/quantumclient/shell.py +++ b/quantumclient/shell.py @@ -257,6 +257,36 @@ def env(*_vars, **kwargs): 'quantumclient.quantum.v2_0.agentscheduler.ListRoutersOnL3Agent'), 'l3-agent-list-hosting-router': utils.import_class( 'quantumclient.quantum.v2_0.agentscheduler.ListL3AgentsHostingRouter'), + 'firewall-rule-list': utils.import_class( + 'quantumclient.quantum.v2_0.firewallrule.ListFirewallRule'), + 'firewall-rule-show': utils.import_class( + 'quantumclient.quantum.v2_0.firewallrule.ShowFirewallRule'), + 'firewall-rule-create': utils.import_class( + 'quantumclient.quantum.v2_0.firewallrule.CreateFirewallRule'), + 'firewall-rule-update': utils.import_class( + 'quantumclient.quantum.v2_0.firewallrule.UpdateFirewallRule'), + 'firewall-rule-delete': utils.import_class( + 'quantumclient.quantum.v2_0.firewallrule.DeleteFirewallRule'), + 'firewall-policy-list': utils.import_class( + 'quantumclient.quantum.v2_0.firewallpolicy.ListFirewallPolicy'), + 'firewall-policy-show': utils.import_class( + 'quantumclient.quantum.v2_0.firewallpolicy.ShowFirewallPolicy'), + 'firewall-policy-create': utils.import_class( + 'quantumclient.quantum.v2_0.firewallpolicy.CreateFirewallPolicy'), + 'firewall-policy-update': utils.import_class( + 'quantumclient.quantum.v2_0.firewallpolicy.UpdateFirewallPolicy'), + 'firewall-policy-delete': utils.import_class( + 'quantumclient.quantum.v2_0.firewallpolicy.DeleteFirewallPolicy'), + 'firewall-list': utils.import_class( + 'quantumclient.quantum.v2_0.firewall.ListFirewall'), + 'firewall-show': utils.import_class( + 'quantumclient.quantum.v2_0.firewall.ShowFirewall'), + 'firewall-create': utils.import_class( + 'quantumclient.quantum.v2_0.firewall.CreateFirewall'), + 'firewall-update': utils.import_class( + 'quantumclient.quantum.v2_0.firewall.UpdateFirewall'), + 'firewall-delete': utils.import_class( + 'quantumclient.quantum.v2_0.firewall.DeleteFirewall'), } COMMANDS = {'2.0': COMMAND_V2} diff --git a/quantumclient/v2_0/client.py b/quantumclient/v2_0/client.py index fb55b1e4e..bc91add01 100644 --- a/quantumclient/v2_0/client.py +++ b/quantumclient/v2_0/client.py @@ -181,11 +181,17 @@ class Client(object): agent_path = "/agents/%s" network_gateways_path = "/network-gateways" network_gateway_path = "/network-gateways/%s" - DHCP_NETS = '/dhcp-networks' DHCP_AGENTS = '/dhcp-agents' L3_ROUTERS = '/l3-routers' L3_AGENTS = '/l3-agents' + firewall_rules_path = "/firewall_rules" + firewall_rule_path = "/firewall_rules/%s" + firewall_policies_path = "/firewall_policies" + firewall_policy_path = "/firewall_policies/%s" + firewalls_path = "/firewalls" + firewall_path = "/firewalls/%s" + # API has no way to report plurals, so we have to hard code them EXTED_PLURALS = {'routers': 'router', 'floatingips': 'floatingip', @@ -198,6 +204,9 @@ class Client(object): 'members': 'member', 'health_monitors': 'health_monitor', 'quotas': 'quota', + 'firewall_rules': 'firewall_rule', + 'firewall_policies': 'firewall_policy', + 'firewalls': 'firewall' } # 8192 Is the default max URI len for eventlet.wsgi.server MAX_URI_LEN = 8192 @@ -876,6 +885,120 @@ def add_router_to_l3_agent(self, l3_agent, body): return self.post((self.agent_path + self.L3_ROUTERS) % l3_agent, body=body) + @APIParamsCall + def list_firewall_rules(self, retrieve_all=True, **_params): + """ + Fetches a list of all firewall rules for a tenant + """ + # Pass filters in "params" argument to do_request + + return self.list('firewall_rules', self.firewall_rules_path, retrieve_all, + **_params) + + @APIParamsCall + def show_firewall_rule(self, firewall_rule, **_params): + """ + Fetches information of a certain firewall rule + """ + return self.get(self.firewall_rule_path % (firewall_rule), params=_params) + + @APIParamsCall + def create_firewall_rule(self, body=None): + """ + Creates a new firewall rule + """ + return self.post(self.firewall_rules_path, body=body) + + @APIParamsCall + def update_firewall_rule(self, firewall_rule, body=None): + """ + Updates a firewall rule + """ + return self.put(self.firewall_rule_path % (firewall_rule), body=body) + + @APIParamsCall + def delete_firewall_rule(self, firewall_rule): + """ + Deletes the specified firewall rule + """ + return self.delete(self.firewall_rule_path % (firewall_rule)) + + @APIParamsCall + def list_firewall_policies(self, retrieve_all=True, **_params): + """ + Fetches a list of all firewall policies for a tenant + """ + # Pass filters in "params" argument to do_request + + return self.list('firewall_policies', self.firewall_policies_path, retrieve_all, + **_params) + + @APIParamsCall + def show_firewall_policy(self, firewall_policy, **_params): + """ + Fetches information of a certain firewall policy + """ + return self.get(self.firewall_policy_path % (firewall_policy), params=_params) + + @APIParamsCall + def create_firewall_policy(self, body=None): + """ + Creates a new firewall policy + """ + return self.post(self.firewall_policies_path, body=body) + + @APIParamsCall + def update_firewall_policy(self, firewall_policy, body=None): + """ + Updates a firewall policy + """ + return self.put(self.firewall_policy_path % (firewall_policy), body=body) + + @APIParamsCall + def delete_firewall_policy(self, firewall_policy): + """ + Deletes the specified firewall policy + """ + return self.delete(self.firewall_policy_path % (firewall_policy)) + + @APIParamsCall + def list_firewalls(self, retrieve_all=True, **_params): + """ + Fetches a list of all firewals for a tenant + """ + # Pass filters in "params" argument to do_request + + return self.list('firewalls', self.firewalls_path, retrieve_all, + **_params) + + @APIParamsCall + def show_firewall(self, firewall, **_params): + """ + Fetches information of a certain firewall + """ + return self.get(self.firewall_path % (firewall), params=_params) + + @APIParamsCall + def create_firewall(self, body=None): + """ + Creates a new firewall + """ + return self.post(self.firewalls_path, body=body) + + @APIParamsCall + def update_firewall(self, firewall, body=None): + """ + Updates a firewall + """ + return self.put(self.firewall_path % (firewall), body=body) + + @APIParamsCall + def delete_firewall(self, firewall): + """ + Deletes the specified firewall + """ + return self.delete(self.firewall_path % (firewall)) + @APIParamsCall def remove_router_from_l3_agent(self, l3_agent, router_id): """