| 1 |
# Copyright 2013 Mirantis Inc. |
| 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.neutron import v2_0 as neutronV20 |
| 19 |
|
| 20 |
|
| 21 |
class ListHealthMonitor(neutronV20.ListCommand): |
| 22 |
"""List health monitors that belong to a given tenant.""" |
| 23 |
|
| 24 |
resource = 'health_monitor' |
| 25 |
list_columns = ['id', 'type', 'admin_state_up'] |
| 26 |
pagination_support = True |
| 27 |
sorting_support = True |
| 28 |
|
| 29 |
|
| 30 |
class ShowHealthMonitor(neutronV20.ShowCommand): |
| 31 |
"""Show information of a given health monitor.""" |
| 32 |
|
| 33 |
resource = 'health_monitor' |
| 34 |
allow_names = False |
| 35 |
|
| 36 |
|
| 37 |
class CreateHealthMonitor(neutronV20.CreateCommand): |
| 38 |
"""Create a health monitor.""" |
| 39 |
|
| 40 |
resource = 'health_monitor' |
| 41 |
|
| 42 |
def add_known_arguments(self, parser): |
| 43 |
parser.add_argument( |
| 44 |
'--admin-state-down', |
| 45 |
dest='admin_state', action='store_false', |
| 46 |
help=_('Set admin state up to false.')) |
| 47 |
parser.add_argument( |
| 48 |
'--expected-codes', |
| 49 |
help=_('The list of HTTP status codes expected in ' |
| 50 |
'response from the member to declare it healthy. This ' |
| 51 |
'attribute can contain one value, ' |
| 52 |
'or a list of values separated by comma, ' |
| 53 |
'or a range of values (e.g. "200-299"). If this attribute ' |
| 54 |
'is not specified, it defaults to "200".')) |
| 55 |
parser.add_argument( |
| 56 |
'--http-method', |
| 57 |
help=_('The HTTP method used for requests by the monitor of type ' |
| 58 |
'HTTP.')) |
| 59 |
parser.add_argument( |
| 60 |
'--url-path', |
| 61 |
help=_('The HTTP path used in the HTTP request used by the monitor' |
| 62 |
' to test a member health. This must be a string ' |
| 63 |
'beginning with a / (forward slash).')) |
| 64 |
parser.add_argument( |
| 65 |
'--delay', |
| 66 |
required=True, |
| 67 |
help=_('The time in seconds between sending probes to members.')) |
| 68 |
parser.add_argument( |
| 69 |
'--max-retries', |
| 70 |
required=True, |
| 71 |
help=_('Number of permissible connection failures before changing ' |
| 72 |
'the member status to INACTIVE. [1..10]')) |
| 73 |
parser.add_argument( |
| 74 |
'--timeout', |
| 75 |
required=True, |
| 76 |
help=_('Maximum number of seconds for a monitor to wait for a ' |
| 77 |
'connection to be established before it times out. The ' |
| 78 |
'value must be less than the delay value.')) |
| 79 |
parser.add_argument( |
| 80 |
'--type', |
| 81 |
required=True, choices=['PING', 'TCP', 'HTTP', 'HTTPS'], |
| 82 |
help=_('One of the predefined health monitor types.')) |
| 83 |
|
| 84 |
def args2body(self, parsed_args): |
| 85 |
body = {'admin_state_up': parsed_args.admin_state, |
| 86 |
'delay': parsed_args.delay, |
| 87 |
'max_retries': parsed_args.max_retries, |
| 88 |
'timeout': parsed_args.timeout, |
| 89 |
'type': parsed_args.type} |
| 90 |
neutronV20.update_dict(parsed_args, body, |
| 91 |
['expected_codes', 'http_method', 'url_path', |
| 92 |
'tenant_id']) |
| 93 |
return {self.resource: body} |
| 94 |
|
| 95 |
|
| 96 |
class UpdateHealthMonitor(neutronV20.UpdateCommand): |
| 97 |
"""Update a given health monitor.""" |
| 98 |
|
| 99 |
resource = 'health_monitor' |
| 100 |
allow_names = False |
| 101 |
|
| 102 |
|
| 103 |
class DeleteHealthMonitor(neutronV20.DeleteCommand): |
| 104 |
"""Delete a given health monitor.""" |
| 105 |
|
| 106 |
resource = 'health_monitor' |
| 107 |
allow_names = False |
| 108 |
|
| 109 |
|
| 110 |
class AssociateHealthMonitor(neutronV20.NeutronCommand): |
| 111 |
"""Create a mapping between a health monitor and a pool.""" |
| 112 |
|
| 113 |
resource = 'health_monitor' |
| 114 |
|
| 115 |
def get_parser(self, prog_name): |
| 116 |
parser = super(AssociateHealthMonitor, self).get_parser(prog_name) |
| 117 |
parser.add_argument( |
| 118 |
'health_monitor_id', metavar='HEALTH_MONITOR_ID', |
| 119 |
help=_('Health monitor to associate.')) |
| 120 |
parser.add_argument( |
| 121 |
'pool_id', metavar='POOL', |
| 122 |
help=_('ID of the pool to be associated with the health monitor.')) |
| 123 |
return parser |
| 124 |
|
| 125 |
def take_action(self, parsed_args): |
| 126 |
neutron_client = self.get_client() |
| 127 |
body = {'health_monitor': {'id': parsed_args.health_monitor_id}} |
| 128 |
pool_id = neutronV20.find_resourceid_by_name_or_id( |
| 129 |
neutron_client, 'pool', parsed_args.pool_id) |
| 130 |
neutron_client.associate_health_monitor(pool_id, body) |
| 131 |
print((_('Associated health monitor ' |
| 132 |
'%s') % parsed_args.health_monitor_id), |
| 133 |
file=self.app.stdout) |
| 134 |
|
| 135 |
|
| 136 |
class DisassociateHealthMonitor(neutronV20.NeutronCommand): |
| 137 |
"""Remove a mapping from a health monitor to a pool.""" |
| 138 |
|
| 139 |
resource = 'health_monitor' |
| 140 |
|
| 141 |
def get_parser(self, prog_name): |
| 142 |
parser = super(DisassociateHealthMonitor, self).get_parser(prog_name) |
| 143 |
parser.add_argument( |
| 144 |
'health_monitor_id', metavar='HEALTH_MONITOR_ID', |
| 145 |
help=_('Health monitor to disassociate.')) |
| 146 |
parser.add_argument( |
| 147 |
'pool_id', metavar='POOL', |
| 148 |
help=_('ID of the pool to be disassociated with the health ' |
| 149 |
'monitor.')) |
| 150 |
return parser |
| 151 |
|
| 152 |
def take_action(self, parsed_args): |
| 153 |
neutron_client = self.get_client() |
| 154 |
pool_id = neutronV20.find_resourceid_by_name_or_id( |
| 155 |
neutron_client, 'pool', parsed_args.pool_id) |
| 156 |
neutron_client.disassociate_health_monitor(pool_id, |
| 157 |
parsed_args |
| 158 |
.health_monitor_id) |
| 159 |
print((_('Disassociated health monitor ' |
| 160 |
'%s') % parsed_args.health_monitor_id), |
| 161 |
file=self.app.stdout) |