Skip to content

Commit

Permalink
Add NAT gateways to Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
sshnaidm committed Sep 13, 2023
1 parent 3564ee3 commit aff0b2a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions test_data/az_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,19 @@ vms:
vm_id: eb3229e8-26a9-4a46-94ac-f8261b11bc6b
extensions_time_budget: PT1H30M
time_created: '2023-09-08T05:48:05.560671Z'
nat_gateways:
- id: /subscriptions/26b9eb0b-2d3b-49e8-b07f-ce08716a1fc6/resourceGroups/ManagedByParacloud/providers/Microsoft.Network/natGateways/natgw2
name: natgw2
type: Microsoft.Network/natGateways
location: eastus
tags: {}
sku:
name: Standard
etag: W/"062b3468-b080-4ec5-b7f0-b519f5bda734"
idle_timeout_in_minutes: 4
public_ip_addresses:
- id: /subscriptions/26b9eb0b-2d3b-49e8-b07f-ce08716a1fc6/resourceGroups/ManagedByParacloud/providers/Microsoft.Network/publicIPAddresses/priv-publicIpAddress
subnets:
- id: /subscriptions/26b9eb0b-2d3b-49e8-b07f-ce08716a1fc6/resourceGroups/ManagedByParacloud/providers/Microsoft.Network/virtualNetworks/main/subnets/priv
resource_guid: 90953e6b-5cea-4af0-82f2-15611e2a617d
provisioning_state: Succeeded
29 changes: 28 additions & 1 deletion transible/plugins/az_ansible/azure_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from transible.utils import read_yaml, optimize



class AzureAnsible:
"""Main class to generate Ansible playbooks from Amazon
Expand Down Expand Up @@ -79,6 +78,7 @@ def retrieve_cloud_data(self, data_type):
const.FILE_LBS: ('networks', self.az_calc.create_load_balancers),
const.FILE_SERVERS: ('compute', self.az_calc.create_servers),
const.FILE_AVAIL_SETS: ('compute', self.az_calc.create_availability_sets),
const.FILE_NAT_GWS: ('networks', self.az_calc.create_nat_gateways),

}
for file_name, (path, func) in cloud_funcs.items():
Expand Down Expand Up @@ -358,6 +358,31 @@ def create_network_interfaces(self, force_optimize=conf.VARS_OPT_NETINTS,
netints.append(optimized)
return netints

def create_nat_gateways(self, force_optimize=conf.VARS_OPT_NAT_GWS,
vars_file=True):
ngws = []
pre_optimized = []
for ng in self.data['nat_gateways']:
n = {'state': '{{ state }}'}
n['resource_group'] = self.resource_group_name
n['name'] = ng['name']
n['public_ip_addresses'] = [i['id'].split("/publicIPAddresses/")[1] for i in ng['public_ip_addresses']]
n['idle_timeout_in_minutes'] = ng['idle_timeout_in_minutes']
n['sku'] = ng['sku']['name']
ngw = {'azure.azcollection.azure_rm_natgateway': n}
if force_optimize:
pre_optimized.append(ngw)
else:
ngws.append(ngw)
if force_optimize:
optimized = optimize(
pre_optimized,
use_vars=vars_file,
var_name="nat_gateways")
if optimized:
ngws.append(optimized)
return ngws

def create_app_secgroups(self, force_optimize=conf.VARS_OPT_APPSECGROUPS,
vars_file=True):
appscgps = []
Expand Down Expand Up @@ -642,6 +667,8 @@ def get_info(self):
self.resource_group_name), const.FILE_LBS),
"subnets": (conf.DUMP_NETWORKS, self.network_client.virtual_networks.list(
self.resource_group_name), const.FILE_SUBNETS),
"nat_gateways": (conf.DUMP_NETWORKS, self.network_client.nat_gateways.list(
self.resource_group_name), const.FILE_NAT_GWS),
"availability_sets": (conf.DUMP_SERVERS, self.compute_client.availability_sets.list(
self.resource_group_name), const.FILE_KEYPAIRS),
"vms": (conf.DUMP_SERVERS, self.compute_client.virtual_machines.list(
Expand Down
1 change: 1 addition & 0 deletions transible/plugins/az_ansible/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
VARS_OPT_NETINTS = False
VARS_OPT_RESOURCEGRPS = True
VARS_OPT_AVAILSETS = False
VARS_OPT_NAT_GWS = False
2 changes: 2 additions & 0 deletions transible/plugins/az_ansible/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- import_tasks: networks/subnets.yml
- import_tasks: networks/security_groups.yml
- import_tasks: networks/eips.yml
- import_tasks: networks/nat_gateways.yml
- import_tasks: networks/load_balancers.yml
- import_tasks: networks/application_security_groups.yml
- import_tasks: networks/network_interfaces.yml
Expand Down Expand Up @@ -59,6 +60,7 @@
- import_tasks: networks/network_interfaces.yml
- import_tasks: networks/security_groups.yml
- import_tasks: networks/application_security_groups.yml
- import_tasks: networks/nat_gateways.yml
- import_tasks: networks/load_balancers.yml
- import_tasks: networks/eips.yml
- import_tasks: networks/subnets.yml
Expand Down

0 comments on commit aff0b2a

Please sign in to comment.