| 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 |
from neutronclient._i18n import _ |
| 18 |
from neutronclient.common import exceptions |
| 19 |
from neutronclient.common import utils |
| 20 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 21 |
|
| 22 |
|
| 23 |
def _get_listener_id(client, listener_id_or_name): |
| 24 |
return neutronV20.find_resourceid_by_name_or_id( |
| 25 |
client, 'listener', listener_id_or_name) |
| 26 |
|
| 27 |
|
| 28 |
def _get_pool_id(client, pool_id_or_name): |
| 29 |
return neutronV20.find_resourceid_by_name_or_id( |
| 30 |
client, 'pool', pool_id_or_name, cmd_resource='lbaas_pool') |
| 31 |
|
| 32 |
|
| 33 |
def _add_common_args(parser, is_create=True): |
| 34 |
parser.add_argument( |
| 35 |
'--name', |
| 36 |
help=_('Name of the policy.')) |
| 37 |
parser.add_argument( |
| 38 |
'--description', |
| 39 |
help=_('Description of the policy.')) |
| 40 |
parser.add_argument( |
| 41 |
'--action', |
| 42 |
required=is_create, |
| 43 |
metavar='ACTION', |
| 44 |
type=utils.convert_to_uppercase, |
| 45 |
choices=['REJECT', 'REDIRECT_TO_POOL', 'REDIRECT_TO_URL'], |
| 46 |
help=_('Action type of the policy.')) |
| 47 |
parser.add_argument( |
| 48 |
'--redirect-pool', |
| 49 |
help=_('ID or name of the pool for REDIRECT_TO_POOL action type.')) |
| 50 |
parser.add_argument( |
| 51 |
'--redirect-url', |
| 52 |
help=_('URL for REDIRECT_TO_URL action type. ' |
| 53 |
'This should be a valid URL string.')) |
| 54 |
parser.add_argument( |
| 55 |
'--position', |
| 56 |
type=int, |
| 57 |
help=_('L7 policy position in ordered policies list. ' |
| 58 |
'This must be an integer starting from 1. ' |
| 59 |
'Not specifying the position will place the policy ' |
| 60 |
'at the tail of existing policies list.')) |
| 61 |
|
| 62 |
|
| 63 |
def _common_args2body(client, parsed_args, is_create=True): |
| 64 |
if parsed_args.redirect_url: |
| 65 |
if parsed_args.action != 'REDIRECT_TO_URL': |
| 66 |
raise exceptions.CommandError(_('Action must be REDIRECT_TO_URL')) |
| 67 |
if parsed_args.redirect_pool: |
| 68 |
if parsed_args.action != 'REDIRECT_TO_POOL': |
| 69 |
raise exceptions.CommandError(_('Action must be REDIRECT_TO_POOL')) |
| 70 |
parsed_args.redirect_pool_id = _get_pool_id( |
| 71 |
client, parsed_args.redirect_pool) |
| 72 |
if (parsed_args.action == 'REDIRECT_TO_URL' and |
| 73 |
not parsed_args.redirect_url): |
| 74 |
raise exceptions.CommandError(_('Redirect URL must be specified')) |
| 75 |
if (parsed_args.action == 'REDIRECT_TO_POOL' and |
| 76 |
not parsed_args.redirect_pool): |
| 77 |
raise exceptions.CommandError(_('Redirect pool must be specified')) |
| 78 |
|
| 79 |
attributes = ['name', 'description', |
| 80 |
'action', 'redirect_pool_id', 'redirect_url', |
| 81 |
'position', 'admin_state_up'] |
| 82 |
if is_create: |
| 83 |
parsed_args.listener_id = _get_listener_id( |
| 84 |
client, parsed_args.listener) |
| 85 |
attributes.extend(['listener_id', 'tenant_id']) |
| 86 |
body = {} |
| 87 |
neutronV20.update_dict(parsed_args, body, attributes) |
| 88 |
return {'l7policy': body} |
| 89 |
|
| 90 |
|
| 91 |
class ListL7Policy(neutronV20.ListCommand): |
| 92 |
"""LBaaS v2 List L7 policies that belong to a given listener.""" |
| 93 |
|
| 94 |
resource = 'l7policy' |
| 95 |
shadow_resource = 'lbaas_l7policy' |
| 96 |
pagination_support = True |
| 97 |
sorting_support = True |
| 98 |
list_columns = [ |
| 99 |
'id', 'name', 'action', 'redirect_pool_id', 'redirect_url', |
| 100 |
'position', 'admin_state_up', 'status' |
| 101 |
] |
| 102 |
|
| 103 |
|
| 104 |
class ShowL7Policy(neutronV20.ShowCommand): |
| 105 |
"""LBaaS v2 Show information of a given L7 policy.""" |
| 106 |
|
| 107 |
resource = 'l7policy' |
| 108 |
shadow_resource = 'lbaas_l7policy' |
| 109 |
|
| 110 |
|
| 111 |
class CreateL7Policy(neutronV20.CreateCommand): |
| 112 |
"""LBaaS v2 Create L7 policy.""" |
| 113 |
|
| 114 |
resource = 'l7policy' |
| 115 |
shadow_resource = 'lbaas_l7policy' |
| 116 |
|
| 117 |
def add_known_arguments(self, parser): |
| 118 |
_add_common_args(parser) |
| 119 |
parser.add_argument( |
| 120 |
'--admin-state-down', |
| 121 |
dest='admin_state_up', |
| 122 |
action='store_false', |
| 123 |
help=_('Set admin state up to false.')) |
| 124 |
parser.add_argument( |
| 125 |
'--listener', |
| 126 |
required=True, |
| 127 |
metavar='LISTENER', |
| 128 |
help=_('ID or name of the listener this policy belongs to.')) |
| 129 |
|
| 130 |
def args2body(self, parsed_args): |
| 131 |
return _common_args2body(self.get_client(), parsed_args) |
| 132 |
|
| 133 |
|
| 134 |
class UpdateL7Policy(neutronV20.UpdateCommand): |
| 135 |
"""LBaaS v2 Update a given L7 policy.""" |
| 136 |
|
| 137 |
resource = 'l7policy' |
| 138 |
shadow_resource = 'lbaas_l7policy' |
| 139 |
|
| 140 |
def add_known_arguments(self, parser): |
| 141 |
_add_common_args(parser, is_create=False) |
| 142 |
utils.add_boolean_argument( |
| 143 |
parser, '--admin-state-up', |
| 144 |
help=_('Specify the administrative state of the policy' |
| 145 |
' (True meaning "Up").')) |
| 146 |
|
| 147 |
def args2body(self, parsed_args): |
| 148 |
return _common_args2body(self.get_client(), parsed_args, False) |
| 149 |
|
| 150 |
|
| 151 |
class DeleteL7Policy(neutronV20.DeleteCommand): |
| 152 |
"""LBaaS v2 Delete a given L7 policy.""" |
| 153 |
|
| 154 |
resource = 'l7policy' |
| 155 |
shadow_resource = 'lbaas_l7policy' |