From 4555085289b087c6fc8eb6c0e7f902c2a8d8706c Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Wed, 3 May 2017 19:31:17 +0800 Subject: [PATCH] Add floating IP associate/disassociate support Change-Id: Ia3fb63e78a52f76b9c1d42c795ea1a3abfef6562 --- moganclient/osc/v1/server.py | 65 ++++++++++++++++++++++++++++++++++++ moganclient/v1/server.py | 11 ++++++ setup.cfg | 2 ++ 3 files changed, 78 insertions(+) diff --git a/moganclient/osc/v1/server.py b/moganclient/osc/v1/server.py index 20754af..9cf7e6c 100644 --- a/moganclient/osc/v1/server.py +++ b/moganclient/osc/v1/server.py @@ -537,3 +537,68 @@ class ShowServerNetworkInfo(command.Lister): return (columns, (utils.get_item_properties( nic, columns, formatters=formatters) for nic in data)) + + +class AddFloatingIP(command.Command): + _description = _("Add floating IP address to server") + + def get_parser(self, prog_name): + parser = super(AddFloatingIP, self).get_parser(prog_name) + parser.add_argument( + "server", + metavar="", + help=_("Server to receive the floating IP address (name or ID)"), + ) + parser.add_argument( + "ip_address", + metavar="", + help=_("Floating IP address to assign to server (IP only)"), + ) + parser.add_argument( + "--fixed-ip-address", + metavar="", + help=_("Fixed IP address to associate with this floating IP " + "address"), + ) + return parser + + def take_action(self, parsed_args): + bc_client = self.app.client_manager.baremetal_compute + server = utils.find_resource( + bc_client.server, + parsed_args.server, + ) + + bc_client.server.add_floating_ip(server.uuid, + parsed_args.ip_address, + parsed_args.fixed_ip_address) + + +class RemoveFloatingIP(command.Command): + _description = _("Remove floating IP address from server") + + def get_parser(self, prog_name): + parser = super(RemoveFloatingIP, self).get_parser(prog_name) + parser.add_argument( + "server", + metavar="", + help=_( + "Server to remove the floating IP address from (name or ID)" + ), + ) + parser.add_argument( + "ip_address", + metavar="", + help=_("Floating IP address to remove from server (IP only)"), + ) + return parser + + def take_action(self, parsed_args): + bc_client = self.app.client_manager.baremetal_compute + server = utils.find_resource( + bc_client.server, + parsed_args.server, + ) + + bc_client.server.remove_floating_ip(server.uuid, + parsed_args.ip_address) diff --git a/moganclient/v1/server.py b/moganclient/v1/server.py index fd16172..7d23e7c 100644 --- a/moganclient/v1/server.py +++ b/moganclient/v1/server.py @@ -126,3 +126,14 @@ class ServerManager(base.ManagerWithFind): def get_server_nics(self, server_id): url = '/servers/%s/networks' % base.getid(server_id) return self._list(url, response_key='nics') + + def add_floating_ip(self, server_id, ip_address, fixed_ip_address): + url = '/servers/%s/networks/floatingips' % base.getid(server_id) + data = {'address': ip_address, + 'fixed_address': fixed_ip_address} + return self._create(url, data=data) + + def remove_floating_ip(self, server_id, ip_address): + url = '/servers/%(server)s/networks/floatingips/%(ip)s' % { + 'server': base.getid(server_id), 'ip': ip_address} + return self._delete(url) diff --git a/setup.cfg b/setup.cfg index d2fa163..234e947 100644 --- a/setup.cfg +++ b/setup.cfg @@ -48,6 +48,8 @@ openstack.baremetal_compute.v1 = baremetal_server_update = moganclient.osc.v1.server:UpdateServer baremetal_server_unlock = moganclient.osc.v1.server:UnLockServer baremetal_server_netinfo = moganclient.osc.v1.server:ShowServerNetworkInfo + baremetal_server_add_floating_ip = moganclient.osc.v1.server:AddFloatingIP + baremetal_server_remove_floating_ip = moganclient.osc.v1.server:RemoveFloatingIP baremetal_availability_zone_list = moganclient.osc.v1.availability_zone:ListAvailabilityZone baremetal_keypair_create = moganclient.osc.v1.keypair:CreateKeyPair baremetal_keypair_show = moganclient.osc.v1.keypair:ShowKeyPair