← 返回首页
Merge "Low-level Compute v2 API: floating ip" · stackhpc/python-openstackclient@b0ce957 · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Commit b0ce957

Browse files
authored andcommitted
Merge "Low-level Compute v2 API: floating ip"
2 parents 18206a9 + e6ea45b commit b0ce957

5 files changed

Lines changed: 277 additions & 93 deletions

File tree

‎openstackclient/api/compute_v2.py‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,88 @@ def find(
9393

9494
return ret
9595

96+
# Flaoting IPs
97+
98+
def floating_ip_create(
99+
self,
100+
pool=None,
101+
):
102+
"""Create a new floating ip
103+
104+
https://developer.openstack.org/api-ref/compute/#create-allocate-floating-ip-address
105+
106+
:param pool: Name of floating IP pool
107+
"""
108+
109+
url = "/os-floating-ips"
110+
111+
try:
112+
return self.create(
113+
url,
114+
json={'pool': pool},
115+
)['floating_ip']
116+
except (
117+
ksa_exceptions.NotFound,
118+
ksa_exceptions.BadRequest,
119+
):
120+
msg = _("%s not found") % pool
121+
raise exceptions.NotFound(msg)
122+
123+
def floating_ip_delete(
124+
self,
125+
floating_ip_id=None,
126+
):
127+
"""Delete a floating IP
128+
129+
https://developer.openstack.org/api-ref/compute/#delete-deallocate-floating-ip-address
130+
131+
:param string security_group:
132+
Floating IP ID
133+
"""
134+
135+
url = "/os-floating-ips"
136+
137+
if floating_ip_id is not None:
138+
return self.delete('/%s/%s' % (url, floating_ip_id))
139+
140+
return None
141+
142+
def floating_ip_find(
143+
self,
144+
floating_ip=None,
145+
):
146+
"""Return a security group given name or ID
147+
148+
https://developer.openstack.org/api-ref/compute/#list-floating-ip-addresses
149+
150+
:param string floating_ip:
151+
Floating IP address
152+
:returns: A dict of the floating IP attributes
153+
"""
154+
155+
url = "/os-floating-ips"
156+
157+
return self.find(
158+
url,
159+
attr='ip',
160+
value=floating_ip,
161+
)
162+
163+
def floating_ip_list(
164+
self,
165+
):
166+
"""Get floating IPs
167+
168+
https://developer.openstack.org/api-ref/compute/#show-floating-ip-address-details
169+
170+
:returns:
171+
list of security groups names
172+
"""
173+
174+
url = "/os-floating-ips"
175+
176+
return self.list(url)["floating_ips"]
177+
96178
# Security Groups
97179

98180
def security_group_create(

‎openstackclient/network/v2/floating_ip.py‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ def take_action_network(self, client, parsed_args):
190190
return (display_columns, data)
191191

192192
def take_action_compute(self, client, parsed_args):
193-
obj = client.floating_ips.create(parsed_args.network)
194-
columns = _get_columns(obj._info)
195-
data = utils.get_dict_properties(obj._info, columns)
193+
obj = client.api.floating_ip_create(parsed_args.network)
194+
columns = _get_columns(obj)
195+
data = utils.get_dict_properties(obj, columns)
196196
return (columns, data)
197197

198198

@@ -245,13 +245,7 @@ def take_action_network(self, client, parsed_args):
245245
client.delete_ip(obj)
246246

247247
def take_action_compute(self, client, parsed_args):
248-
obj = utils.find_resource(client.floating_ips, self.r)
249-
client.floating_ips.delete(obj.id)
250-
251-
def take_action(self, parsed_args):
252-
"""Implements a naive cache for the list of floating IPs"""
253-
254-
super(DeleteFloatingIP, self).take_action(parsed_args)
248+
client.api.floating_ip_delete(self.r)
255249

256250

257251
class DeleteIPFloating(DeleteFloatingIP):
@@ -414,10 +408,10 @@ def take_action_compute(self, client, parsed_args):
414408
'Pool',
415409
)
416410

417-
data = client.floating_ips.list()
411+
data = client.api.floating_ip_list()
418412

419413
return (headers,
420-
(utils.get_item_properties(
414+
(utils.get_dict_properties(
421415
s, columns,
422416
formatters={},
423417
) for s in data))
@@ -510,12 +504,9 @@ def take_action_network(self, client, parsed_args):
510504
return (display_columns, data)
511505

512506
def take_action_compute(self, client, parsed_args):
513-
obj = utils.find_resource(
514-
client.floating_ips,
515-
parsed_args.floating_ip,
516-
)
517-
columns = _get_columns(obj._info)
518-
data = utils.get_dict_properties(obj._info, columns)
507+
obj = client.api.floating_ip_find(parsed_args.floating_ip)
508+
columns = _get_columns(obj)
509+
data = utils.get_dict_properties(obj, columns)
519510
return (columns, data)
520511

521512

‎openstackclient/tests/unit/api/test_compute_v2.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,117 @@ def setUp(self):
3434
self.requests_mock = self.useFixture(fixture.Fixture())
3535

3636

37+
class TestFloatingIP(TestComputeAPIv2):
38+
39+
FAKE_FLOATING_IP_RESP = {
40+
'id': 1,
41+
'ip': '203.0.113.11', # TEST-NET-3
42+
'fixed_ip': '198.51.100.11', # TEST-NET-2
43+
'pool': 'nova',
44+
'instance_id': None,
45+
}
46+
FAKE_FLOATING_IP_RESP_2 = {
47+
'id': 2,
48+
'ip': '203.0.113.12', # TEST-NET-3
49+
'fixed_ip': '198.51.100.12', # TEST-NET-2
50+
'pool': 'nova',
51+
'instance_id': None,
52+
}
53+
LIST_FLOATING_IP_RESP = [
54+
FAKE_FLOATING_IP_RESP,
55+
FAKE_FLOATING_IP_RESP_2,
56+
]
57+
58+
def test_floating_ip_create(self):
59+
self.requests_mock.register_uri(
60+
'POST',
61+
FAKE_URL + '/os-floating-ips',
62+
json={'floating_ip': self.FAKE_FLOATING_IP_RESP},
63+
status_code=200,
64+
)
65+
ret = self.api.floating_ip_create('nova')
66+
self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret)
67+
68+
def test_floating_ip_create_not_found(self):
69+
self.requests_mock.register_uri(
70+
'POST',
71+
FAKE_URL + '/os-floating-ips',
72+
status_code=404,
73+
)
74+
self.assertRaises(
75+
osc_lib_exceptions.NotFound,
76+
self.api.floating_ip_create,
77+
'not-nova',
78+
)
79+
80+
def test_floating_ip_delete(self):
81+
self.requests_mock.register_uri(
82+
'DELETE',
83+
FAKE_URL + '/os-floating-ips/1',
84+
status_code=202,
85+
)
86+
ret = self.api.floating_ip_delete('1')
87+
self.assertEqual(202, ret.status_code)
88+
self.assertEqual("", ret.text)
89+
90+
def test_floating_ip_delete_none(self):
91+
ret = self.api.floating_ip_delete()
92+
self.assertIsNone(ret)
93+
94+
def test_floating_ip_find_id(self):
95+
self.requests_mock.register_uri(
96+
'GET',
97+
FAKE_URL + '/os-floating-ips/1',
98+
json={'floating_ip': self.FAKE_FLOATING_IP_RESP},
99+
status_code=200,
100+
)
101+
ret = self.api.floating_ip_find('1')
102+
self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret)
103+
104+
def test_floating_ip_find_ip(self):
105+
self.requests_mock.register_uri(
106+
'GET',
107+
FAKE_URL + '/os-floating-ips/' + self.FAKE_FLOATING_IP_RESP['ip'],
108+
status_code=404,
109+
)
110+
self.requests_mock.register_uri(
111+
'GET',
112+
FAKE_URL + '/os-floating-ips',
113+
json={'floating_ips': self.LIST_FLOATING_IP_RESP},
114+
status_code=200,
115+
)
116+
ret = self.api.floating_ip_find(self.FAKE_FLOATING_IP_RESP['ip'])
117+
self.assertEqual(self.FAKE_FLOATING_IP_RESP, ret)
118+
119+
def test_floating_ip_find_not_found(self):
120+
self.requests_mock.register_uri(
121+
'GET',
122+
FAKE_URL + '/os-floating-ips/1.2.3.4',
123+
status_code=404,
124+
)
125+
self.requests_mock.register_uri(
126+
'GET',
127+
FAKE_URL + '/os-floating-ips',
128+
json={'floating_ips': self.LIST_FLOATING_IP_RESP},
129+
status_code=200,
130+
)
131+
self.assertRaises(
132+
osc_lib_exceptions.NotFound,
133+
self.api.floating_ip_find,
134+
'1.2.3.4',
135+
)
136+
137+
def test_floating_ip_list(self):
138+
self.requests_mock.register_uri(
139+
'GET',
140+
FAKE_URL + '/os-floating-ips',
141+
json={'floating_ips': self.LIST_FLOATING_IP_RESP},
142+
status_code=200,
143+
)
144+
ret = self.api.floating_ip_list()
145+
self.assertEqual(self.LIST_FLOATING_IP_RESP, ret)
146+
147+
37148
class TestSecurityGroup(TestComputeAPIv2):
38149

39150
FAKE_SECURITY_GROUP_RESP = {

‎openstackclient/tests/unit/compute/v2/fakes.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,11 +1016,7 @@ def create_one_floating_ip(attrs=None):
10161016
# Overwrite default attributes.
10171017
floating_ip_attrs.update(attrs)
10181018

1019-
floating_ip = fakes.FakeResource(
1020-
info=copy.deepcopy(floating_ip_attrs),
1021-
loaded=True)
1022-
1023-
return floating_ip
1019+
return floating_ip_attrs
10241020

10251021
@staticmethod
10261022
def create_floating_ips(attrs=None, count=2):

0 commit comments

Comments
 (0)

Footer

© 2026 GitHub, Inc.