Network Address Translation (NAT)

NAT Gateway

The NAT Gateway service provides the network address translation (NAT) function for servers, such as Elastic Cloud Servers (ECSs), Bare Metal Servers (BMSs), and Workspace desktops, in a Virtual Private Cloud (VPC) or servers that connect to a VPC through Direct Connect or Virtual Private Network (VPN) in local data centers, allowing these servers to share elastic IP addresses (EIPs) to access the Internet or to provide services accessible from the Internet.

List NAT Gateways

This interface is used to query an NAT gateway list and to filter the output with query parameters. Gateway.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

for gateway in conn.nat.gateways():
    print(gateway)

Create NAT Gateway

This interface is used to create a NAT Ggateway with parameters. Gateway.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

attrs = {
    "name": "nat_001",
    "description": "my nat gateway 01",
    "router_id": "d84f345c-80a1-4fa2-a39c-d0d397c3f09a",
    "internal_network_id": "89d66639-aacb-4929-969d-07080b0f9fd9",
    "spec": "1"
}

gateway = conn.nat.create_gateway(**attrs)
print(gateway)

Get NAT Gateway

This interface is used to get a NAT gateway by ID or an instance of class Gateway.

import openstack

openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

gateway = 'gateway_id'
gateway = conn.nat.get_gateway(gateway)
print(gateway)

Find NAT Gateway

This interface is used to find a NAT gateway by id or name. Gateway.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

name_or_id = 'gateway_name_or_id'
gateway = conn.nat.find_gateway(name_or_id, ignore_missing=False)
print(gateway)

Update NAT Gateway

This interface is used to update NAT gateway parameters by id or an instance of class Gateway.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

attrs = {
    "name": "new_name",
    "description": "new description",
    "spec": "1"
}

name_or_id = 'gateway_name_or_id'
gateway = conn.nat.find_gateway(name_or_id, ignore_missing=False)
response = conn.nat.update_gateway(gateway, **attrs)
print(response)

Delete NAT Gateway

This interface is used to delete a NAT gateway by ID or an instance of class Gateway.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

name_or_id = 'gateway_name_or_id'
gateway = conn.nat.find_gateway(name_or_id=name_or_id, ignore_missing=False)
conn.nat.delete_gateway(gateway)

NAT Gateway supports source NAT (SNAT) and destination NAT (DNAT) functions.

SNAT

The SNAT function translates a private IP address to a public IP address by binding EIPs to servers in a VPC, providing secure and efficient access to the Internet.

List SNAT Rules

This interface is used to query an SNAT rule list and to filter the output with query parameters. Snat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

for snat_rule in conn.nat.snat_rules():
    print(snat_rule)

Create SNAT Rule

This interface is used to create a SNAT rule with parameters. Snat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

nat_gateway_id = 'nat_gateway_id'
network_id = 'network_id'
floating_ip_id = 'floating_ip_id'

attrs = {
    "nat_gateway_id": nat_gateway_id,
    "network_id": network_id,
    "floating_ip_id": floating_ip_id
}

snat_rule = conn.nat.create_snat_rule(**attrs)
print(snat_rule)

Get SNAT Rule

This interface is used to get a SNAT rule by ID or an instance of class Snat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

snat_rule_id = 'snat_rule_id'
snat_rule = conn.nat.get_snat_rule(snat_rule_id)
print(snat_rule)

Delete SNAT Rule

This interface is used to delete a SNAT Rule by ID or an instance of class Snat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

snat_rule_id = 'snat_rule_id'
conn.nat.delete_snat_rule(snat_rule_id)

DNAT

The DNAT function enables servers that share the same EIPs in a VPC to provide services accessible from the Internet through the IP address mapping and port mapping.

List DNAT Rules

This interface is used to query an DNAT rule list and to filter the output with query parameters. Dnat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

for dnat_rule in conn.nat.dnat_rules():
    print(dnat_rule)

Create DNAT Rule

This interface is used to create a DNAT rule with parameters. Dnat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

nat_gateway_id = 'nat_gateway_id'
port_id = 'network_id'
private_ip = '192.168.199.3'
floating_ip_id = 'floating_ip_id'
protocol = 'TCP'
internal_service_port = 80
external_service_port = 80


attrs = {
    "nat_gateway_id": nat_gateway_id,
    "private_ip": private_ip,
    "port_id": port_id,
    "protocol": protocol,
    "internal_service_port": internal_service_port,
    "external_service_port": external_service_port,
    "floating_ip_id": floating_ip_id
}

dnat_rule = conn.nat.create_dnat_rule(**attrs)
print(dnat_rule)

Get DNAT Rule

This interface is used to get a DNAT rule by ID or an instance of class Dnat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

dnat_rule_id = 'dnat_rule_id'
dnat_rule = conn.nat.get_dnat_rule(dnat_rule_id)
print(dnat_rule)

Delete DNAT Rule

This interface is used to delete a DNAT Rule by ID or an instance of class Dnat.

import openstack


openstack.enable_logging(True)
conn = openstack.connect(cloud='otc')

snat_rule_id = 'snat_rule_id'
conn.nat.delete_snat_rule(snat_rule_id)