| 1 |
# Copyright 2016 Radware LTD. |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
|
| 17 |
|
| 18 |
from neutronclient._i18n import _ |
| 19 |
from neutronclient.common import utils |
| 20 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 21 |
|
| 22 |
|
| 23 |
def _get_policy_id(client, policy_id_or_name): |
| 24 |
return neutronV20.find_resourceid_by_name_or_id( |
| 25 |
client, 'l7policy', policy_id_or_name, |
| 26 |
cmd_resource='lbaas_l7policy') |
| 27 |
|
| 28 |
|
| 29 |
class LbaasL7RuleMixin(object): |
| 30 |
|
| 31 |
def set_extra_attrs(self, parsed_args): |
| 32 |
self.parent_id = _get_policy_id(self.get_client(), |
| 33 |
parsed_args.l7policy) |
| 34 |
|
| 35 |
def add_known_arguments(self, parser): |
| 36 |
parser.add_argument( |
| 37 |
'l7policy', metavar='L7POLICY', |
| 38 |
help=_('ID or name of L7 policy this rule belongs to.')) |
| 39 |
|
| 40 |
|
| 41 |
def _add_common_args(parser, is_create=True): |
| 42 |
parser.add_argument( |
| 43 |
'--type', |
| 44 |
required=is_create, |
| 45 |
type=utils.convert_to_uppercase, |
| 46 |
choices=['HOST_NAME', 'PATH', 'FILE_TYPE', 'HEADER', 'COOKIE'], |
| 47 |
help=_('Rule type.')) |
| 48 |
parser.add_argument( |
| 49 |
'--compare-type', |
| 50 |
required=is_create, |
| 51 |
type=utils.convert_to_uppercase, |
| 52 |
choices=['REGEX', 'STARTS_WITH', 'ENDS_WITH', |
| 53 |
'CONTAINS', 'EQUAL_TO'], |
| 54 |
help=_('Rule compare type.')) |
| 55 |
parser.add_argument( |
| 56 |
'--invert-compare', |
| 57 |
dest='invert', |
| 58 |
action='store_true', |
| 59 |
help=_('Invert the compare type.')) |
| 60 |
parser.add_argument( |
| 61 |
'--key', |
| 62 |
help=_('Key to compare.' |
| 63 |
' Relevant for HEADER and COOKIE types only.')) |
| 64 |
parser.add_argument( |
| 65 |
'--value', |
| 66 |
required=is_create, |
| 67 |
help=_('Value to compare.')) |
| 68 |
|
| 69 |
|
| 70 |
def _common_args2body(client, parsed_args, is_create=True): |
| 71 |
attributes = ['type', 'compare_type', |
| 72 |
'invert', 'key', 'value', 'admin_state_up'] |
| 73 |
if is_create: |
| 74 |
attributes.append('tenant_id') |
| 75 |
body = {} |
| 76 |
neutronV20.update_dict(parsed_args, body, attributes) |
| 77 |
return {'rule': body} |
| 78 |
|
| 79 |
|
| 80 |
class ListL7Rule(LbaasL7RuleMixin, neutronV20.ListCommand): |
| 81 |
"""LBaaS v2 List L7 rules that belong to a given L7 policy.""" |
| 82 |
|
| 83 |
resource = 'rule' |
| 84 |
shadow_resource = 'lbaas_l7rule' |
| 85 |
pagination_support = True |
| 86 |
sorting_support = True |
| 87 |
|
| 88 |
list_columns = [ |
| 89 |
'id', 'type', 'compare_type', 'invert', 'key', 'value', |
| 90 |
'admin_state_up', 'status' |
| 91 |
] |
| 92 |
|
| 93 |
def take_action(self, parsed_args): |
| 94 |
self.parent_id = _get_policy_id(self.get_client(), |
| 95 |
parsed_args.l7policy) |
| 96 |
self.values_specs.append('--l7policy_id=%s' % self.parent_id) |
| 97 |
return super(ListL7Rule, self).take_action(parsed_args) |
| 98 |
|
| 99 |
|
| 100 |
class ShowL7Rule(LbaasL7RuleMixin, neutronV20.ShowCommand): |
| 101 |
"""LBaaS v2 Show information of a given rule.""" |
| 102 |
|
| 103 |
resource = 'rule' |
| 104 |
shadow_resource = 'lbaas_l7rule' |
| 105 |
|
| 106 |
|
| 107 |
class CreateL7Rule(LbaasL7RuleMixin, neutronV20.CreateCommand): |
| 108 |
"""LBaaS v2 Create L7 rule.""" |
| 109 |
|
| 110 |
resource = 'rule' |
| 111 |
shadow_resource = 'lbaas_l7rule' |
| 112 |
|
| 113 |
def add_known_arguments(self, parser): |
| 114 |
super(CreateL7Rule, self).add_known_arguments(parser) |
| 115 |
_add_common_args(parser) |
| 116 |
parser.add_argument( |
| 117 |
'--admin-state-down', |
| 118 |
dest='admin_state_up', |
| 119 |
action='store_false', |
| 120 |
help=_('Set admin state up to false')) |
| 121 |
|
| 122 |
def args2body(self, parsed_args): |
| 123 |
return _common_args2body(self.get_client(), parsed_args) |
| 124 |
|
| 125 |
|
| 126 |
class UpdateL7Rule(LbaasL7RuleMixin, neutronV20.UpdateCommand): |
| 127 |
"""LBaaS v2 Update a given L7 rule.""" |
| 128 |
|
| 129 |
resource = 'rule' |
| 130 |
shadow_resource = 'lbaas_l7rule' |
| 131 |
|
| 132 |
def add_known_arguments(self, parser): |
| 133 |
super(UpdateL7Rule, self).add_known_arguments(parser) |
| 134 |
_add_common_args(parser, False) |
| 135 |
utils.add_boolean_argument( |
| 136 |
parser, '--admin-state-up', |
| 137 |
help=_('Specify the administrative state of the rule' |
| 138 |
' (True meaning "Up").')) |
| 139 |
|
| 140 |
def args2body(self, parsed_args): |
| 141 |
return _common_args2body(self.get_client(), parsed_args, False) |
| 142 |
|
| 143 |
|
| 144 |
class DeleteL7Rule(LbaasL7RuleMixin, neutronV20.DeleteCommand): |
| 145 |
"""LBaaS v2 Delete a given L7 rule.""" |
| 146 |
|
| 147 |
resource = 'rule' |
| 148 |
shadow_resource = 'lbaas_l7rule' |