|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +* ******************************************************* |
| 5 | +* Copyright (c) VMware, Inc. 2018. All Rights Reserved. |
| 6 | +* SPDX-License-Identifier: MIT |
| 7 | +* ******************************************************* |
| 8 | +* |
| 9 | +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT |
| 10 | +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, |
| 11 | +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED |
| 12 | +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, |
| 13 | +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | +""" |
| 15 | + |
| 16 | +__author__ = 'VMware, Inc.' |
| 17 | +__vcenter_version__ = 'VMware Cloud on AWS' |
| 18 | + |
| 19 | +import argparse |
| 20 | +import random |
| 21 | + |
| 22 | +from com.vmware.vmc.model_client import * |
| 23 | +from vmware.vapi.vmc.client import create_vmc_client |
| 24 | +from samples.vmc.helpers.vmc_task_helper import wait_for_task |
| 25 | + |
| 26 | + |
| 27 | +class ExposePublicIP(object): |
| 28 | + """ |
| 29 | + Demo steps required to expose a VM to public internet |
| 30 | + 1. Request a public IP address |
| 31 | + 2. Add a firewall rule on compute gateway to access to the VM |
| 32 | + 3. Create a NAT rule to forward traffic from public IP to private IP |
| 33 | +
|
| 34 | + Sample Prerequisites: |
| 35 | + - A VM deployed inside the SDDC with private IP address |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self): |
| 39 | + parser = argparse.ArgumentParser( |
| 40 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 41 | + parser.add_argument('-r', '--refresh-token', |
| 42 | + required=True, |
| 43 | + help='VMware Cloud API refresh token') |
| 44 | + |
| 45 | + parser.add_argument('-o', '--org-id', |
| 46 | + required=True, |
| 47 | + help='Organization identifier.') |
| 48 | + |
| 49 | + parser.add_argument('-s', '--sddc-id', |
| 50 | + required=True, |
| 51 | + help='Sddc Identifier.') |
| 52 | + |
| 53 | + parser.add_argument('--notes', |
| 54 | + default='Sample public IP ' + str(random.randint(0, 100)), |
| 55 | + help='Notes of the new public IP') |
| 56 | + |
| 57 | + parser.add_argument('--fw-rule-name', |
| 58 | + default='Sample firewall rule ' + str(random.randint(0, 100)), |
| 59 | + help='Name of the compute gae') |
| 60 | + |
| 61 | + parser.add_argument('--nat-rule-description', |
| 62 | + default='Sample NAT rule ' + str(random.randint(0, 100)), |
| 63 | + help='Description for the NAT rule') |
| 64 | + |
| 65 | + parser.add_argument('--internal-ip', |
| 66 | + required=True, |
| 67 | + help='Private IP of the VM') |
| 68 | + |
| 69 | + parser.add_argument('-c', '--cleardata', |
| 70 | + action='store_true', |
| 71 | + help='Clean up after sample run') |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + self.network_id = None |
| 75 | + self.edge_id = None |
| 76 | + self.nat_rule_id = None |
| 77 | + self.public_ip = None |
| 78 | + self.nfwr = None |
| 79 | + self.org_id = args.org_id |
| 80 | + self.sddc_id = args.sddc_id |
| 81 | + self.notes = args.notes |
| 82 | + self.fw_rule_name = args.fw_rule_name |
| 83 | + self.nat_rule_description = args.nat_rule_description |
| 84 | + self.internal_ip = args.internal_ip |
| 85 | + self.cleardata = args.cleardata |
| 86 | + self.vmc_client = create_vmc_client(args.refresh_token) |
| 87 | + |
| 88 | + def setup(self): |
| 89 | + # Check if the organization exists |
| 90 | + orgs = self.vmc_client.Orgs.list() |
| 91 | + if self.org_id not in [org.id for org in orgs]: |
| 92 | + raise ValueError("Org with ID {} doesn't exist".format(self.org_id)) |
| 93 | + |
| 94 | + # Check if the SDDC exists |
| 95 | + sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id) |
| 96 | + if self.sddc_id not in [sddc.id for sddc in sddcs]: |
| 97 | + raise ValueError("SDDC with ID {} doesn't exist in org {}". |
| 98 | + format(self.sddc_id, self.org_id)) |
| 99 | + |
| 100 | + edges = self.vmc_client.orgs.sddcs.networks.Edges.get( |
| 101 | + org=self.org_id, |
| 102 | + sddc=self.sddc_id, |
| 103 | + edge_type='gatewayServices').edge_page.data |
| 104 | + print('\n# Setup: Compute Gateway ID: {}'.format(edges[1].id)) |
| 105 | + self.edge_id = edges[1].id |
| 106 | + |
| 107 | + def request_public_ip(self): |
| 108 | + print('\n# Example: Request a new IP for SDDC') |
| 109 | + ip_spec = SddcAllocatePublicIpSpec(names=[self.notes], count=1) |
| 110 | + task = self.vmc_client.orgs.sddcs.Publicips.create( |
| 111 | + org=self.org_id, |
| 112 | + sddc=self.sddc_id, |
| 113 | + spec=ip_spec) |
| 114 | + |
| 115 | + wait_for_task(task_client=self.vmc_client.orgs.Tasks, |
| 116 | + org_id=self.org_id, |
| 117 | + task_id=task.id, |
| 118 | + interval_sec=2) |
| 119 | + |
| 120 | + ips = self.vmc_client.orgs.sddcs.Publicips.list( |
| 121 | + org=self.org_id, |
| 122 | + sddc=self.sddc_id) |
| 123 | + |
| 124 | + for ip in ips: |
| 125 | + if ip.name == self.notes: |
| 126 | + self.ip_id = ip.allocation_id |
| 127 | + self.public_ip = ip.public_ip |
| 128 | + print('# Successfully requested public IP {}'. |
| 129 | + format(ip.public_ip)) |
| 130 | + break |
| 131 | + else: |
| 132 | + raise Exception("Can't find public IP with notes {}". |
| 133 | + format(self.notes)) |
| 134 | + |
| 135 | + def create_firewall_rule_on_cgw(self): |
| 136 | + |
| 137 | + print('\n# Example: Add a firewall rule to the compute gateway') |
| 138 | + |
| 139 | + # Construct a new NSX firewall rule object |
| 140 | + # which allow any to any traffic |
| 141 | + self.nfwr = Nsxfirewallrule(rule_type='user', |
| 142 | + name=self.fw_rule_name, |
| 143 | + enabled=True, |
| 144 | + action='accept', |
| 145 | + source=AddressFWSourceDestination( |
| 146 | + exclude=False, |
| 147 | + ip_address=['any'], |
| 148 | + grouping_object_id=[], |
| 149 | + vnic_group_id=[]), |
| 150 | + destination=AddressFWSourceDestination( |
| 151 | + exclude=False, |
| 152 | + ip_address=['any'], |
| 153 | + grouping_object_id=[], |
| 154 | + vnic_group_id=[]), |
| 155 | + logging_enabled=False, |
| 156 | + application=None) |
| 157 | + |
| 158 | + self.vmc_client.orgs.sddcs.networks.edges.firewall.config.Rules.add( |
| 159 | + org=self.org_id, |
| 160 | + sddc=self.sddc_id, |
| 161 | + edge_id=self.edge_id, |
| 162 | + firewall_rules=FirewallRules([self.nfwr])) |
| 163 | + |
| 164 | + print(' # New firewall rule "{}" is added'.format(self.fw_rule_name)) |
| 165 | + |
| 166 | + def create_net_rule(self): |
| 167 | + |
| 168 | + print('\n# Example: Add a NAT rule to the compute gateway') |
| 169 | + |
| 170 | + # Construct a new NSX NAT rule spec |
| 171 | + rule = Nsxnatrule(vnic='0', |
| 172 | + rule_type='user', |
| 173 | + action='dnat', # Supported types are DNAT|SNAT |
| 174 | + protocol='any', |
| 175 | + description=self.nat_rule_description, |
| 176 | + original_address=self.public_ip, |
| 177 | + original_port='any', |
| 178 | + translated_address=self.internal_ip, |
| 179 | + translated_port='any', |
| 180 | + enabled=True) |
| 181 | + |
| 182 | + self.vmc_client.orgs.sddcs.networks.edges.nat.config.Rules.add( |
| 183 | + org=self.org_id, |
| 184 | + sddc=self.sddc_id, |
| 185 | + edge_id=self.edge_id, |
| 186 | + nat_rules=NatRules([rule])) |
| 187 | + |
| 188 | + print(' # New NAT rule "{}" is added'.format(self.nat_rule_description)) |
| 189 | + |
| 190 | + def cleanup(self): |
| 191 | + if self.cleardata: |
| 192 | + |
| 193 | + # Delete the firewall rule |
| 194 | + fw_rules = self.vmc_client.orgs.sddcs.networks.edges.firewall.Config.get( |
| 195 | + org=self.org_id, |
| 196 | + sddc=self.sddc_id, |
| 197 | + edge_id=self.edge_id).firewall_rules.firewall_rules |
| 198 | + |
| 199 | + for r in fw_rules: |
| 200 | + if r.name == self.fw_rule_name: |
| 201 | + self.vmc_client.orgs.sddcs.networks.edges.firewall.config.Rules.delete( |
| 202 | + org=self.org_id, |
| 203 | + sddc=self.sddc_id, |
| 204 | + edge_id=self.edge_id, |
| 205 | + rule_id=r.rule_id) |
| 206 | + break |
| 207 | + print('\n# Cleanup: Firewall rule {} is deleted'. |
| 208 | + format(self.fw_rule_name)) |
| 209 | + |
| 210 | + # Delete the NAT rule |
| 211 | + rules = self.vmc_client.orgs.sddcs.networks.edges.nat.Config.get( |
| 212 | + org=self.org_id, |
| 213 | + sddc=self.sddc_id, |
| 214 | + edge_id=self.edge_id).rules.nat_rules_dtos |
| 215 | + for rule in rules: |
| 216 | + if rule.description == self.nat_rule_description: |
| 217 | + self.vmc_client.orgs.sddcs.networks.edges.nat.config.Rules.delete( |
| 218 | + org=self.org_id, |
| 219 | + sddc=self.sddc_id, |
| 220 | + edge_id=self.edge_id, |
| 221 | + rule_id=rule.rule_id) |
| 222 | + print('\n# Cleanup: NAT rule "{}" is deleted'. |
| 223 | + format(self.nat_rule_description)) |
| 224 | + |
| 225 | + # Release the public IP address |
| 226 | + self.vmc_client.orgs.sddcs.Publicips.delete( |
| 227 | + org=self.org_id, |
| 228 | + sddc=self.sddc_id, |
| 229 | + id=self.ip_id) |
| 230 | + print('\n# Cleanup: Public IP "{}" is released'. |
| 231 | + format(self.public_ip)) |
| 232 | + |
| 233 | + |
| 234 | +def main(): |
| 235 | + expose_public_ip = ExposePublicIP() |
| 236 | + expose_public_ip.setup() |
| 237 | + expose_public_ip.request_public_ip() |
| 238 | + expose_public_ip.create_firewall_rule_on_cgw() |
| 239 | + expose_public_ip.create_net_rule() |
| 240 | + expose_public_ip.cleanup() |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == '__main__': |
| 244 | + main() |
0 commit comments