Skip to content

Commit feda2e1

Browse files
authored
Add create endpoint of transit ip addresses (#659)
Add create endpoint of transit ip addresses Reviewed-by: Anton Sidelnikov
1 parent bfbad83 commit feda2e1

10 files changed

Lines changed: 173 additions & 2 deletions

File tree

doc/source/sdk/guides/privatenat.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,12 @@ This interface is used to query details about a specified transit IP address.
190190

191191
.. literalinclude:: ../examples/natv3/get_private_transit_ip.py
192192
:lines: 16-23
193+
194+
Create Private Transit IP Address
195+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196+
197+
This interface is used to assign a transit IP address.
198+
:class:`~otcextensions.sdk.natv3.v3.transit_ip.PrivateTransitIp`.
199+
200+
.. literalinclude:: ../examples/natv3/create_private_transit_ip.py
201+
:lines: 16-28

doc/source/sdk/proxies/privatenat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Private NAT Gateway Operations
1616

1717
.. autoclass:: otcextensions.sdk.natv3.v3._proxy.Proxy
1818
:noindex:
19-
:members: private_nat_gateways, get_private_nat_gateway, private_dnat_rules, create_private_dnat_rule, update_private_dnat_rule, delete_private_dnat_rule, private_snat_rules, create_private_snat_rule, update_private_snat_rule, delete_private_snat_rule, private_transit_ips, get_private_transit_ip
19+
:members: private_nat_gateways, get_private_nat_gateway, private_dnat_rules, create_private_dnat_rule, update_private_dnat_rule, delete_private_dnat_rule, private_snat_rules, create_private_snat_rule, update_private_snat_rule, delete_private_snat_rule, private_transit_ips, get_private_transit_ip, create_private_transit_ip
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
"""
14+
Assign a private transit IP address.
15+
"""
16+
17+
import openstack
18+
19+
openstack.enable_logging(True)
20+
conn = openstack.connect(cloud="otc")
21+
22+
transit_ip = conn.natv3.create_private_transit_ip(
23+
virsubnet_id="2759da7b-8015-404c-ae0a-a389007b0e2a",
24+
ip_address="192.168.1.68",
25+
enterprise_project_id="2759da7b-8015-404c-ae0a-a389007b0e2a",
26+
tags=[{"key": "key1", "value": "value1"}],
27+
)
28+
print(transit_ip)

otcextensions/osclient/privatenat/v3/transit_ip.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from osc_lib.command import command
1919

2020
from otcextensions.common import sdk_utils
21+
from otcextensions.common.utils import normalize_tags
2122
from otcextensions.i18n import _
2223

2324
LOG = logging.getLogger(__name__)
@@ -204,3 +205,60 @@ def take_action(self, parsed_args):
204205
data = utils.get_item_properties(obj, columns)
205206

206207
return display_columns, data
208+
209+
210+
class CreatePrivateTransitIp(command.ShowOne):
211+
_description = _("Assign a private transit IP address.")
212+
213+
def get_parser(self, prog_name):
214+
parser = super(CreatePrivateTransitIp, self).get_parser(prog_name)
215+
parser.add_argument(
216+
"--virsubnet-id",
217+
metavar="<virsubnet_id>",
218+
required=True,
219+
help=_("Specifies the subnet ID of the current project."),
220+
)
221+
parser.add_argument(
222+
"--ip-address",
223+
metavar="<ip_address>",
224+
help=_("Specifies the transit IP address."),
225+
)
226+
parser.add_argument(
227+
"--enterprise-project-id",
228+
metavar="<enterprise_project_id>",
229+
help=_(
230+
"Specifies the enterprise project ID associated with the "
231+
"transit IP address."
232+
),
233+
)
234+
parser.add_argument(
235+
"--tags",
236+
metavar="<tags>",
237+
action="append",
238+
help=_(
239+
"Specifies the tag list in KEY=VALUE format. "
240+
"Repeat for multiple values."
241+
),
242+
)
243+
return parser
244+
245+
def take_action(self, parsed_args):
246+
client = self.app.client_manager.privatenat
247+
248+
attrs = {
249+
"virsubnet_id": parsed_args.virsubnet_id,
250+
}
251+
252+
if parsed_args.ip_address:
253+
attrs["ip_address"] = parsed_args.ip_address
254+
if parsed_args.enterprise_project_id:
255+
attrs["enterprise_project_id"] = parsed_args.enterprise_project_id
256+
if parsed_args.tags:
257+
attrs["tags"] = normalize_tags(parsed_args.tags)
258+
259+
obj = client.create_private_transit_ip(**attrs)
260+
261+
display_columns, columns = _get_columns(obj)
262+
data = utils.get_item_properties(obj, columns)
263+
264+
return display_columns, data

otcextensions/sdk/natv3/v3/_proxy.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,16 @@ def get_private_transit_ip(self, transit_ip):
264264
when no resource can be found.
265265
"""
266266
return self._get(_transit_ip.PrivateTransitIp, transit_ip)
267+
268+
def create_private_transit_ip(self, **attrs):
269+
"""Assign a private NAT transit IP address.
270+
271+
:param dict attrs: Keyword arguments which will be used to assign
272+
a :class:`~otcextensions.sdk.natv3.v3.transit_ip.PrivateTransitIp`,
273+
comprised of the properties on the PrivateTransitIp class.
274+
275+
:returns: The assigned private transit IP address.
276+
277+
:rtype: :class:`~otcextensions.sdk.natv3.v3.transit_ip.PrivateTransitIp`
278+
"""
279+
return self._create(_transit_ip.PrivateTransitIp, **attrs)

otcextensions/sdk/natv3/v3/transit_ip.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PrivateTransitIp(resource.Resource):
2525
# capabilities
2626
allow_fetch = True
2727
allow_list = True
28+
allow_create = True
2829

2930
_query_mapping = resource.QueryParameters(
3031
"limit",

otcextensions/tests/unit/osclient/privatenat/v3/test_transit_ip.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,58 @@ def test_show(self):
148148
self.client.get_private_transit_ip.assert_called_once_with(self._data.id)
149149
self.assertEqual(len(columns), len(data))
150150
self.assertIn("id", columns)
151+
152+
153+
class TestCreatePrivateTransitIp(fakes.TestPrivateNat):
154+
_data = fakes.FakePrivateTransitIp.create_one()
155+
156+
def setUp(self):
157+
super(TestCreatePrivateTransitIp, self).setUp()
158+
self.cmd = transit_ip.CreatePrivateTransitIp(self.app, None)
159+
self.client.create_private_transit_ip = mock.Mock(return_value=self._data)
160+
161+
def test_create(self):
162+
arglist = [
163+
"--virsubnet-id",
164+
self._data.virsubnet_id,
165+
"--ip-address",
166+
self._data.ip_address,
167+
"--enterprise-project-id",
168+
self._data.enterprise_project_id,
169+
"--tags",
170+
"key1=value1",
171+
]
172+
verifylist = [
173+
("virsubnet_id", self._data.virsubnet_id),
174+
("ip_address", self._data.ip_address),
175+
("enterprise_project_id", self._data.enterprise_project_id),
176+
("tags", ["key1=value1"]),
177+
]
178+
179+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
180+
columns, data = self.cmd.take_action(parsed_args)
181+
182+
self.client.create_private_transit_ip.assert_called_once_with(
183+
virsubnet_id=self._data.virsubnet_id,
184+
ip_address=self._data.ip_address,
185+
enterprise_project_id=self._data.enterprise_project_id,
186+
tags=[{"key": "key1", "value": "value1"}],
187+
)
188+
self.assertEqual(len(columns), len(data))
189+
self.assertIn("id", columns)
190+
191+
def test_create_minimal(self):
192+
arglist = [
193+
"--virsubnet-id",
194+
self._data.virsubnet_id,
195+
]
196+
verifylist = [
197+
("virsubnet_id", self._data.virsubnet_id),
198+
]
199+
200+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
201+
self.cmd.take_action(parsed_args)
202+
203+
self.client.create_private_transit_ip.assert_called_once_with(
204+
virsubnet_id=self._data.virsubnet_id,
205+
)

otcextensions/tests/unit/sdk/natv3/v3/test_proxy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ def test_private_transit_ips(self):
9191

9292
def test_get_private_transit_ip(self):
9393
self.verify_get(self.proxy.get_private_transit_ip, transit_ip.PrivateTransitIp)
94+
95+
def test_create_private_transit_ip(self):
96+
self.verify_create(
97+
self.proxy.create_private_transit_ip, transit_ip.PrivateTransitIp
98+
)

otcextensions/tests/unit/sdk/natv3/v3/test_transit_ip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def test_basic(self):
4242
self.assertEqual("transit_ip", sot.resource_key)
4343
self.assertEqual("transit_ips", sot.resources_key)
4444
self.assertEqual("/private-nat/transit-ips", sot.base_path)
45-
self.assertTrue(sot.allow_fetch)
4645
self.assertTrue(sot.allow_list)
46+
self.assertTrue(sot.allow_fetch)
47+
self.assertTrue(sot.allow_create)
4748

4849
def test_make_it(self):
4950
sot = transit_ip.PrivateTransitIp(**EXAMPLE)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ openstack.privatenat.v3 =
198198
privatenat_dnat_rule_delete = otcextensions.osclient.privatenat.v3.dnat:DeletePrivateDnatRule
199199
privatenat_transit_ip_list = otcextensions.osclient.privatenat.v3.transit_ip:ListPrivateTransitIps
200200
privatenat_transit_ip_show = otcextensions.osclient.privatenat.v3.transit_ip:ShowPrivateTransitIp
201+
privatenat_transit_ip_create = otcextensions.osclient.privatenat.v3.transit_ip:CreatePrivateTransitIp
201202

202203
openstack.auto_scaling.v1 =
203204
as_group_list = otcextensions.osclient.auto_scaling.v1.group:ListAutoScalingGroup

0 commit comments

Comments
 (0)