|
| 1 | +# Copyright 2013 OpenStack Foundation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Floating IP action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from cliff import command |
| 21 | +from cliff import lister |
| 22 | +from cliff import show |
| 23 | + |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class AddFloatingIP(command.Command): |
| 28 | + """Add floating-ip command""" |
| 29 | + |
| 30 | + api = "compute" |
| 31 | + log = logging.getLogger(__name__ + ".AddFloatingIP") |
| 32 | + |
| 33 | + def get_parser(self, prog_name): |
| 34 | + parser = super(AddFloatingIP, self).get_parser(prog_name) |
| 35 | + parser.add_argument( |
| 36 | + "ip_address", |
| 37 | + metavar="<ip-address>", |
| 38 | + help="IP address to add to server", |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "server", |
| 42 | + metavar="<server>", |
| 43 | + help="Name of the server to receive the IP address", |
| 44 | + ) |
| 45 | + return parser |
| 46 | + |
| 47 | + def take_action(self, parsed_args): |
| 48 | + self.log.debug("take_action(%s)" % parsed_args) |
| 49 | + compute_client = self.app.client_manager.compute |
| 50 | + |
| 51 | + server = utils.find_resource( |
| 52 | + compute_client.servers, parsed_args.server) |
| 53 | + |
| 54 | + server.add_floating_ip(parsed_args.ip_address) |
| 55 | + return |
| 56 | + |
| 57 | + |
| 58 | +class CreateFloatingIP(show.ShowOne): |
| 59 | + """Create floating-ip command""" |
| 60 | + |
| 61 | + api = 'compute' |
| 62 | + log = logging.getLogger(__name__ + '.CreateFloatingIP') |
| 63 | + |
| 64 | + def get_parser(self, prog_name): |
| 65 | + parser = super(CreateFloatingIP, self).get_parser(prog_name) |
| 66 | + parser.add_argument( |
| 67 | + 'pool', |
| 68 | + metavar='<pool>', |
| 69 | + help='Pool to fetch floating IP from', |
| 70 | + ) |
| 71 | + return parser |
| 72 | + |
| 73 | + def take_action(self, parsed_args): |
| 74 | + self.log.debug('take_action(%s)' % parsed_args) |
| 75 | + compute_client = self.app.client_manager.compute |
| 76 | + floating_ip = compute_client.floating_ips.create(parsed_args.pool) |
| 77 | + |
| 78 | + info = {} |
| 79 | + info.update(floating_ip._info) |
| 80 | + return zip(*sorted(info.iteritems())) |
| 81 | + |
| 82 | + |
| 83 | +class DeleteFloatingIP(command.Command): |
| 84 | + """Delete floating-ip command""" |
| 85 | + |
| 86 | + api = 'compute' |
| 87 | + log = logging.getLogger(__name__ + '.DeleteFloatingIP') |
| 88 | + |
| 89 | + def get_parser(self, prog_name): |
| 90 | + parser = super(DeleteFloatingIP, self).get_parser(prog_name) |
| 91 | + parser.add_argument( |
| 92 | + "ip_address", |
| 93 | + metavar="<ip-address>", |
| 94 | + help="IP address to add to server", |
| 95 | + ) |
| 96 | + return parser |
| 97 | + |
| 98 | + def take_action(self, parsed_args): |
| 99 | + self.log.debug('take_action(%s)' % parsed_args) |
| 100 | + compute_client = self.app.client_manager.compute |
| 101 | + |
| 102 | + floating_ip = utils.find_resource( |
| 103 | + compute_client.floating_ips, |
| 104 | + parsed_args.ip_address, |
| 105 | + ) |
| 106 | + |
| 107 | + compute_client.floating_ips.delete(floating_ip) |
| 108 | + return |
| 109 | + |
| 110 | + |
| 111 | +class ListFloatingIP(lister.Lister): |
| 112 | + """List floating-ip command""" |
| 113 | + |
| 114 | + api = 'compute' |
| 115 | + log = logging.getLogger(__name__ + '.ListFloatingIP') |
| 116 | + |
| 117 | + def take_action(self, parsed_args): |
| 118 | + self.log.debug('take_action(%s)' % parsed_args) |
| 119 | + compute_client = self.app.client_manager.compute |
| 120 | + |
| 121 | + columns = ('ID', 'Pool', 'IP', 'Fixed IP', 'Instance ID') |
| 122 | + |
| 123 | + data = compute_client.floating_ips.list() |
| 124 | + |
| 125 | + return (columns, |
| 126 | + (utils.get_item_properties( |
| 127 | + s, columns, |
| 128 | + formatters={}, |
| 129 | + ) for s in data)) |
| 130 | + |
| 131 | + |
| 132 | +class RemoveFloatingIP(command.Command): |
| 133 | + """Remove floating-ip command""" |
| 134 | + |
| 135 | + api = "compute" |
| 136 | + log = logging.getLogger(__name__ + ".RemoveFloatingIP") |
| 137 | + |
| 138 | + def get_parser(self, prog_name): |
| 139 | + parser = super(RemoveFloatingIP, self).get_parser(prog_name) |
| 140 | + parser.add_argument( |
| 141 | + "ip_address", |
| 142 | + metavar="<ip-address>", |
| 143 | + help="IP address to remove from server", |
| 144 | + ) |
| 145 | + parser.add_argument( |
| 146 | + "server", |
| 147 | + metavar="<server>", |
| 148 | + help="Name of the server to remove the IP address from", |
| 149 | + ) |
| 150 | + return parser |
| 151 | + |
| 152 | + def take_action(self, parsed_args): |
| 153 | + self.log.debug("take_action(%s)" % parsed_args) |
| 154 | + compute_client = self.app.client_manager.compute |
| 155 | + |
| 156 | + server = utils.find_resource( |
| 157 | + compute_client.servers, parsed_args.server) |
| 158 | + |
| 159 | + server.remove_floating_ip(parsed_args.ip_address) |
| 160 | + return |
0 commit comments