| 1 |
# Copyright 2012 OpenStack Foundation. |
| 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 |
import argparse |
| 18 |
|
| 19 |
from neutronclient._i18n import _ |
| 20 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 21 |
from neutronclient.neutron.v2_0 import dns |
| 22 |
|
| 23 |
|
| 24 |
class ListFloatingIP(neutronV20.ListCommand): |
| 25 |
"""List floating IPs that belong to a given tenant.""" |
| 26 |
|
| 27 |
resource = 'floatingip' |
| 28 |
list_columns = ['id', 'fixed_ip_address', 'floating_ip_address', |
| 29 |
'port_id'] |
| 30 |
pagination_support = True |
| 31 |
sorting_support = True |
| 32 |
|
| 33 |
|
| 34 |
class ShowFloatingIP(neutronV20.ShowCommand): |
| 35 |
"""Show information of a given floating IP.""" |
| 36 |
|
| 37 |
resource = 'floatingip' |
| 38 |
allow_names = False |
| 39 |
|
| 40 |
|
| 41 |
class CreateFloatingIP(neutronV20.CreateCommand): |
| 42 |
"""Create a floating IP for a given tenant.""" |
| 43 |
|
| 44 |
resource = 'floatingip' |
| 45 |
|
| 46 |
def add_known_arguments(self, parser): |
| 47 |
parser.add_argument( |
| 48 |
'floating_network_id', metavar='FLOATING_NETWORK', |
| 49 |
help=_('ID or name of the network from which ' |
| 50 |
'the floating IP is allocated.')) |
| 51 |
parser.add_argument( |
| 52 |
'--description', |
| 53 |
help=_('Description of the floating IP.')) |
| 54 |
parser.add_argument( |
| 55 |
'--port-id', |
| 56 |
help=_('ID of the port to be associated with the floating IP.')) |
| 57 |
parser.add_argument( |
| 58 |
'--port_id', |
| 59 |
help=argparse.SUPPRESS) |
| 60 |
parser.add_argument( |
| 61 |
'--fixed-ip-address', |
| 62 |
help=_('IP address on the port (only required if port has ' |
| 63 |
'multiple IPs).')) |
| 64 |
parser.add_argument( |
| 65 |
'--fixed_ip_address', |
| 66 |
help=argparse.SUPPRESS) |
| 67 |
parser.add_argument( |
| 68 |
'--floating-ip-address', |
| 69 |
help=_('IP address of the floating IP')) |
| 70 |
parser.add_argument( |
| 71 |
'--subnet', |
| 72 |
dest='subnet_id', |
| 73 |
help=_('Subnet ID on which you want to create the floating IP.')) |
| 74 |
dns.add_dns_argument_create(parser, self.resource, 'domain') |
| 75 |
dns.add_dns_argument_create(parser, self.resource, 'name') |
| 76 |
|
| 77 |
def args2body(self, parsed_args): |
| 78 |
_network_id = neutronV20.find_resourceid_by_name_or_id( |
| 79 |
self.get_client(), 'network', parsed_args.floating_network_id) |
| 80 |
body = {'floating_network_id': _network_id} |
| 81 |
neutronV20.update_dict(parsed_args, body, |
| 82 |
['port_id', 'tenant_id', |
| 83 |
'fixed_ip_address', 'description', |
| 84 |
'floating_ip_address', 'subnet_id']) |
| 85 |
dns.args2body_dns_create(parsed_args, body, 'domain') |
| 86 |
dns.args2body_dns_create(parsed_args, body, 'name') |
| 87 |
return {self.resource: body} |
| 88 |
|
| 89 |
|
| 90 |
class DeleteFloatingIP(neutronV20.DeleteCommand): |
| 91 |
"""Delete a given floating IP.""" |
| 92 |
|
| 93 |
resource = 'floatingip' |
| 94 |
allow_names = False |
| 95 |
|
| 96 |
|
| 97 |
class AssociateFloatingIP(neutronV20.NeutronCommand): |
| 98 |
"""Create a mapping between a floating IP and a fixed IP.""" |
| 99 |
|
| 100 |
resource = 'floatingip' |
| 101 |
|
| 102 |
def get_parser(self, prog_name): |
| 103 |
parser = super(AssociateFloatingIP, self).get_parser(prog_name) |
| 104 |
parser.add_argument( |
| 105 |
'floatingip_id', metavar='FLOATINGIP_ID', |
| 106 |
help=_('ID of the floating IP to associate.')) |
| 107 |
parser.add_argument( |
| 108 |
'port_id', metavar='PORT', |
| 109 |
help=_('ID or name of the port to be associated with the ' |
| 110 |
'floating IP.')) |
| 111 |
parser.add_argument( |
| 112 |
'--fixed-ip-address', |
| 113 |
help=_('IP address on the port (only required if port has ' |
| 114 |
'multiple IPs).')) |
| 115 |
parser.add_argument( |
| 116 |
'--fixed_ip_address', |
| 117 |
help=argparse.SUPPRESS) |
| 118 |
return parser |
| 119 |
|
| 120 |
def take_action(self, parsed_args): |
| 121 |
neutron_client = self.get_client() |
| 122 |
update_dict = {} |
| 123 |
neutronV20.update_dict(parsed_args, update_dict, |
| 124 |
['port_id', 'fixed_ip_address']) |
| 125 |
neutron_client.update_floatingip(parsed_args.floatingip_id, |
| 126 |
{'floatingip': update_dict}) |
| 127 |
print(_('Associated floating IP %s') % parsed_args.floatingip_id, |
| 128 |
file=self.app.stdout) |
| 129 |
|
| 130 |
|
| 131 |
class DisassociateFloatingIP(neutronV20.NeutronCommand): |
| 132 |
"""Remove a mapping from a floating IP to a fixed IP.""" |
| 133 |
|
| 134 |
resource = 'floatingip' |
| 135 |
|
| 136 |
def get_parser(self, prog_name): |
| 137 |
parser = super(DisassociateFloatingIP, self).get_parser(prog_name) |
| 138 |
parser.add_argument( |
| 139 |
'floatingip_id', metavar='FLOATINGIP_ID', |
| 140 |
help=_('ID of the floating IP to disassociate.')) |
| 141 |
return parser |
| 142 |
|
| 143 |
def take_action(self, parsed_args): |
| 144 |
neutron_client = self.get_client() |
| 145 |
neutron_client.update_floatingip(parsed_args.floatingip_id, |
| 146 |
{'floatingip': {'port_id': None}}) |
| 147 |
print(_('Disassociated floating IP %s') % parsed_args.floatingip_id, |
| 148 |
file=self.app.stdout) |