Skip to content

Commit 8549ee9

Browse files
authored
Add get private gateway endpoint (#635)
Add get private gateway endpoint Reviewed-by: Anton Sidelnikov
1 parent 1234eb9 commit 8549ee9

12 files changed

Lines changed: 184 additions & 2 deletions

File tree

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ Reik Keutterling <spielkind@gmail.com>
2020
Rodion Gyrbu <rodion.gyrbu@t-systems.com>
2121
RusselSand <48719909+RusselSand@users.noreply.github.com>
2222
SebastianGode <70581801+SebastianGode@users.noreply.github.com>
23+
Shushanik Hovhannesyan <shhovhan@gmail.com>
2324
T. Schreiber <tino.schreiber@t-systems.com>
2425
Tino Schr <tino.schreiber@t-systems.com>
2526
Tino Schreiber <Tino.Schreiber@t-systems.com>
2627
U-EMEA1\A58358743 <A58358743@BE1BT361.emea1.cds.t-internal.com>
28+
Valeriia Ziukina <48719909+RusselSand@users.noreply.github.com>
2729
Vineet Pruthi <48789821+vineet-pruthi@users.noreply.github.com>
2830
Vladimir Hasko <vladimirhasko@gmail.com>
2931
Vladimir Vshivkov <32225815+enrrou@users.noreply.github.com>

doc/source/sdk/guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Open Telekom Cloud related User Guides
2828
modelarts
2929
nat
3030
obs
31+
privatenat
3132
rds
3233
sfsturbo
3334
smn
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Network Address Translation (NAT)
2+
=================================
3+
4+
.. contents:: Table of Contents
5+
:local:
6+
7+
NAT Gateway
8+
-----------
9+
10+
The NAT Gateway service provides the network address translation (NAT)
11+
function for servers, such as Elastic Cloud Servers (ECSs), Bare Metal
12+
Servers (BMSs), and Workspace desktops, in a Virtual Private Cloud (VPC)
13+
or servers that connect to a VPC through Direct Connect or Virtual
14+
Private Network (VPN) in local data centers, allowing these servers to
15+
share elastic IP addresses (EIPs) to access the Internet or to provide
16+
services accessible from the Internet.
17+
18+
List Private NAT Gateways
19+
^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
This interface is used to query Private NAT gateway list and to filter
22+
the output with query parameters.
23+
:class:`~otcextensions.sdk.natv3.v3.gateway.Gateway`.
24+
25+
.. literalinclude:: ../examples/natv3/list_private_gateways.py
26+
:lines: 16-23
27+
28+
Get Private NAT Gateway
29+
^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
This interface is used to get a Private NAT gateway by ID
32+
:class:`~otcextensions.sdk.natv3.v3.gateway.Gateway`.
33+
34+
.. literalinclude:: ../examples/natv3/get_private_gateway.py
35+
:lines: 16-24

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
19+
:members: private_nat_gateways, get_private_nat_gateway
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
Get details of Private NAT Gateway by gateway_id
15+
"""
16+
17+
import openstack
18+
19+
openstack.enable_logging(True)
20+
conn = openstack.connect(cloud="otc")
21+
22+
gateway = "gateway_id"
23+
gateway = conn.natv3.get_private_nat_gateway(gateway)
24+
print(gateway)

otcextensions/osclient/privatenat/v3/private_nat_gateway.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@
1717
from osc_lib import utils
1818
from osc_lib.command import command
1919

20+
from otcextensions.common import sdk_utils
2021
from otcextensions.i18n import _
2122

2223
LOG = logging.getLogger(__name__)
2324

2425

26+
def _get_columns(item):
27+
column_map = {}
28+
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
29+
30+
2531
class ListPrivateNatGateways(command.Lister):
2632

2733
_description = _("List Private NAT Gateways.")
@@ -149,3 +155,25 @@ def take_action(self, parsed_args):
149155
self.columns,
150156
(utils.get_item_properties(s, self.columns) for s in data),
151157
)
158+
159+
160+
class ShowPrivateNatGateway(command.ShowOne):
161+
_description = _("Show Private NAT Gateway details")
162+
163+
def get_parser(self, prog_name):
164+
parser = super(ShowPrivateNatGateway, self).get_parser(prog_name)
165+
parser.add_argument(
166+
"gateway",
167+
metavar="<gateway>",
168+
help=_("Specifies the private NAT gateway ID."),
169+
)
170+
return parser
171+
172+
def take_action(self, parsed_args):
173+
client = self.app.client_manager.privatenat
174+
obj = client.get_private_nat_gateway(parsed_args.gateway)
175+
176+
display_columns, columns = _get_columns(obj)
177+
data = utils.get_item_properties(obj, columns)
178+
179+
return display_columns, data

otcextensions/sdk/nat/v2/_proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def wait_for_gateway(
5858
wait=300,
5959
attribute="status",
6060
):
61-
"""Wait for an gateway to be in a particular status.
61+
"""Wait for a gateway to be in a particular status.
6262
6363
:param gateway:
6464
The :class:`~otcextensions.sdk.nat.v2.gateway.Gateway`

otcextensions/sdk/natv3/v3/_proxy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ def private_nat_gateways(self, **query):
3030
:returns: A generator of private NAT gateway objects.
3131
"""
3232
return self._list(_gateway.PrivateNatGateway, **query)
33+
34+
def get_private_nat_gateway(self, gateway):
35+
"""Get a single Private Nat gateway
36+
37+
:param gateway: The value can be the ID of a NAT Gatway
38+
39+
:returns: One :class:`~otcextensions.sdk.natv3.v3.gateway.PrivateNatGateway`
40+
41+
:raises: :class:`~openstack.exceptions.ResourceNotFound`
42+
when no resource can be found.
43+
"""
44+
return self._get(_gateway.PrivateNatGateway, gateway)

otcextensions/sdk/natv3/v3/gateway.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class PrivateNatGateway(resource.Resource):
3838
base_path = "/private-nat/gateways"
3939

4040
allow_list = True
41+
allow_fetch = True
4142

4243
_query_mapping = resource.QueryParameters(
4344
"description",

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# under the License.
1212
#
1313
import mock
14+
from openstackclient.tests.unit import utils as tests_utils
15+
from osc_lib import exceptions
1416

1517
from otcextensions.osclient.privatenat.v3 import private_nat_gateway
1618
from otcextensions.tests.unit.osclient.privatenat.v3 import fakes
@@ -138,3 +140,76 @@ def test_list_args(self):
138140
virsubnet_id=["s1"],
139141
enterprise_project_id=["ep1"],
140142
)
143+
144+
145+
class TestGetPrivateNatGateway(fakes.TestPrivateNat):
146+
147+
_data = fakes.FakePrivateNatGateway.create_one()
148+
149+
columns = (
150+
"id",
151+
"name",
152+
"spec",
153+
"status",
154+
"project_id",
155+
"enterprise_project_id",
156+
)
157+
158+
data = fakes.gen_data(_data, columns)
159+
160+
def setUp(self):
161+
super(TestGetPrivateNatGateway, self).setUp()
162+
163+
self.cmd = private_nat_gateway.ShowPrivateNatGateway(self.app, None)
164+
165+
self.client.get_private_nat_gateway = mock.Mock(return_value=self._data)
166+
167+
def test_get_no_options(self):
168+
arglist = []
169+
verifylist = []
170+
171+
# Testing that a call without the required argument will fail and
172+
# throw a "ParserExecption"
173+
self.assertRaises(
174+
tests_utils.ParserException,
175+
self.check_parser,
176+
self.cmd,
177+
arglist,
178+
verifylist,
179+
)
180+
181+
def test_get(self):
182+
arglist = [
183+
self._data.id,
184+
]
185+
186+
verifylist = [
187+
("gateway", self._data.id),
188+
]
189+
190+
# Verify cm is triggered with default parameters
191+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
192+
193+
# Trigger the action
194+
columns, data = self.cmd.take_action(parsed_args)
195+
self.client.get_private_nat_gateway.assert_called_with(self._data.id)
196+
197+
self.assertEqual(tuple(sorted(self.columns)), tuple(sorted(columns)))
198+
self.assertEqual(tuple(sorted(self.data)), tuple(sorted(data)))
199+
200+
def test_get_non_existing(self):
201+
arglist = ["unexist_nat_gateway"]
202+
verifylist = [("gateway", "unexist_nat_gateway")]
203+
204+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
205+
206+
self.client.get_private_nat_gateway.side_effect = exceptions.CommandError(
207+
"Resource Not Found"
208+
)
209+
210+
with self.assertRaises(exceptions.CommandError):
211+
self.cmd.take_action(parsed_args)
212+
213+
self.client.get_private_nat_gateway.assert_called_once_with(
214+
"unexist_nat_gateway"
215+
)

0 commit comments

Comments
 (0)