Skip to content

Commit e273691

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add fixed-ip and floating-ip commands"
2 parents c5e9048 + e9021c2 commit e273691

5 files changed

Lines changed: 315 additions & 1 deletion

File tree

openstackclient/common/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,16 @@ def find_resource(manager, name_or_id):
3838
except (ValueError, exceptions.NotFound):
3939
pass
4040

41+
kwargs = {}
42+
if 'NAME_ATTR' in manager.resource_class.__dict__:
43+
# novaclient does this for oddball resources
44+
kwargs[manager.resource_class.NAME_ATTR] = name_or_id
45+
else:
46+
kwargs['name'] = name_or_id
47+
4148
# finally try to find entity by name
4249
try:
43-
return manager.find(name=name_or_id)
50+
return manager.find(**kwargs)
4451
# FIXME(dtroyer): The exception to catch here is dependent on which
4552
# client library the manager passed in belongs to.
4653
# Eventually this should be pulled from a common set
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"""Fixed IP action implementations"""
17+
18+
import logging
19+
20+
from cliff import command
21+
22+
from openstackclient.common import utils
23+
24+
25+
class AddFixedIP(command.Command):
26+
"""Add fixed-ip command"""
27+
28+
api = "compute"
29+
log = logging.getLogger(__name__ + ".AddFixedIP")
30+
31+
def get_parser(self, prog_name):
32+
parser = super(AddFixedIP, self).get_parser(prog_name)
33+
parser.add_argument(
34+
"network",
35+
metavar="<network>",
36+
help="Name of the network to fetch an IP address from",
37+
)
38+
parser.add_argument(
39+
"server",
40+
metavar="<server>",
41+
help="Name of the server to receive the IP address",
42+
)
43+
return parser
44+
45+
def take_action(self, parsed_args):
46+
self.log.debug("take_action(%s)" % parsed_args)
47+
compute_client = self.app.client_manager.compute
48+
49+
network = utils.find_resource(
50+
compute_client.networks, parsed_args.network)
51+
52+
server = utils.find_resource(
53+
compute_client.servers, parsed_args.server)
54+
55+
server.add_fixed_ip(network.id)
56+
return
57+
58+
59+
class RemoveFixedIP(command.Command):
60+
"""Remove fixed-ip command"""
61+
62+
api = "compute"
63+
log = logging.getLogger(__name__ + ".RemoveFixedIP")
64+
65+
def get_parser(self, prog_name):
66+
parser = super(RemoveFixedIP, self).get_parser(prog_name)
67+
parser.add_argument(
68+
"ip_address",
69+
metavar="<ip-address>",
70+
help="IP address to remove from server",
71+
)
72+
parser.add_argument(
73+
"server",
74+
metavar="<server>",
75+
help="Name of the server to remove the IP address from",
76+
)
77+
return parser
78+
79+
def take_action(self, parsed_args):
80+
self.log.debug("take_action(%s)" % parsed_args)
81+
compute_client = self.app.client_manager.compute
82+
83+
server = utils.find_resource(
84+
compute_client.servers, parsed_args.server)
85+
86+
server.remove_fixed_ip(parsed_args.ip_address)
87+
return
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Pool action implementations"""
17+
18+
import logging
19+
20+
from cliff import lister
21+
22+
from openstackclient.common import utils
23+
24+
25+
class ListFloatingIPPool(lister.Lister):
26+
"""List floating-ip-pool command"""
27+
28+
api = 'compute'
29+
log = logging.getLogger(__name__ + '.ListFloatingIPPool')
30+
31+
def take_action(self, parsed_args):
32+
self.log.debug('take_action(%s)' % parsed_args)
33+
compute_client = self.app.client_manager.compute
34+
35+
columns = ('Name',)
36+
37+
data = compute_client.floating_ip_pools.list()
38+
39+
return (columns,
40+
(utils.get_item_properties(
41+
s, columns,
42+
formatters={},
43+
) for s in data))

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,28 @@ def read(fname):
193193
'set_compute-service='
194194
'openstackclient.compute.v2.service:SetService',
195195

196+
'add_fixed-ip=openstackclient.compute.v2.fixedip:AddFixedIP',
197+
'remove_fixed-ip=openstackclient.compute.v2.fixedip:RemoveFixedIP',
198+
196199
'create_flavor=openstackclient.compute.v2.flavor:CreateFlavor',
197200
'delete_flavor=openstackclient.compute.v2.flavor:DeleteFlavor',
198201
'list_flavor=openstackclient.compute.v2.flavor:ListFlavor',
199202
'show_flavor=openstackclient.compute.v2.flavor:ShowFlavor',
200203

204+
'add_floating-ip='
205+
'openstackclient.compute.v2.floatingip:AddFloatingIP',
206+
'create_floating-ip='
207+
'openstackclient.compute.v2.floatingip:CreateFloatingIP',
208+
'delete_floating-ip='
209+
'openstackclient.compute.v2.floatingip:DeleteFloatingIP',
210+
'list_floating-ip='
211+
'openstackclient.compute.v2.floatingip:ListFloatingIP',
212+
'remove_floating-ip='
213+
'openstackclient.compute.v2.floatingip:RemoveFloatingIP',
214+
215+
'list_floating-ip-pool='
216+
'openstackclient.compute.v2.floatingippool:ListFloatingIPPool',
217+
201218
'list_host=openstackclient.compute.v2.host:ListHost',
202219
'show_host=openstackclient.compute.v2.host:ShowHost',
203220

0 commit comments

Comments
 (0)