2018-03-08 16:02:07 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
|
|
|
* Copyright (c) VMware, Inc. 2018. All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
* *******************************************************
|
|
|
|
*
|
|
|
|
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
|
|
|
|
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
|
|
|
|
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
|
|
|
|
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'VMware, Inc.'
|
|
|
|
|
|
|
|
import argparse
|
2018-11-22 02:51:21 -05:00
|
|
|
|
|
|
|
from com.vmware.vmc.model_client import (L2Extension, SddcNetwork,
|
|
|
|
SddcNetworkAddressGroups,
|
|
|
|
SddcNetworkAddressGroup,
|
|
|
|
SddcNetworkDhcpConfig,
|
|
|
|
SddcNetworkDhcpIpPool)
|
2018-03-08 16:02:07 -05:00
|
|
|
from vmware.vapi.vmc.client import create_vmc_client
|
|
|
|
|
|
|
|
|
|
|
|
class LogicalNetworkCrud(object):
|
|
|
|
"""
|
|
|
|
Demonstrates logical network CRUD operations
|
|
|
|
|
|
|
|
Sample Prerequisites:
|
|
|
|
- An organization associated with the calling user.
|
|
|
|
- A SDDC in the organization
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
2018-11-22 02:51:21 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'-r',
|
|
|
|
'--refresh-token',
|
|
|
|
required=True,
|
|
|
|
help='VMware Cloud API refresh token')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-o', '--org-id', required=True, help='Organization identifier.')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-s', '--sddc-id', required=True, help='Sddc Identifier.')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--network-name',
|
|
|
|
default='Sample Logical Network',
|
|
|
|
help='Name of the new logical network')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--subnet',
|
|
|
|
default='192.168.100.1/24',
|
|
|
|
help='Logical network subnet')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'--dhcp-range',
|
|
|
|
default='192.168.100.2-192.168.100.254',
|
|
|
|
help='DHCP IP range for the logical network')
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-c',
|
|
|
|
'--cleardata',
|
|
|
|
action='store_true',
|
|
|
|
help='Clean up after sample run')
|
2018-03-08 16:02:07 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
self.network_id = None
|
|
|
|
self.org_id = args.org_id
|
|
|
|
self.sddc_id = args.sddc_id
|
|
|
|
self.network_name = args.network_name
|
|
|
|
self.primary_address, self.prefix_length = args.subnet.split('/')
|
|
|
|
self.dhcp_range = args.dhcp_range
|
|
|
|
self.cleanup = args.cleardata
|
|
|
|
self.vmc_client = create_vmc_client(args.refresh_token)
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# Check if the organization exists
|
|
|
|
orgs = self.vmc_client.Orgs.list()
|
|
|
|
if self.org_id not in [org.id for org in orgs]:
|
2018-11-22 02:51:21 -05:00
|
|
|
raise ValueError("Org with ID {} doesn't exist".format(
|
|
|
|
self.org_id))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
# Check if the SDDC exists
|
|
|
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
|
|
|
if self.sddc_id not in [sddc.id for sddc in sddcs]:
|
2018-11-22 02:51:21 -05:00
|
|
|
raise ValueError("SDDC with ID {} doesn't exist in org {}".format(
|
|
|
|
self.sddc_id, self.org_id))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
# Delete logical networks with same name
|
|
|
|
networks = self.vmc_client.orgs.sddcs.networks.Logical.get_0(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id).data
|
2018-03-08 16:02:07 -05:00
|
|
|
for network in networks:
|
|
|
|
if network.name == self.network_name:
|
|
|
|
self.vmc_client.orgs.sddcs.networks.Logical.delete(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, network_id=network.id)
|
2018-03-08 16:02:07 -05:00
|
|
|
print('\n# Setup: Logical network "{}" '
|
|
|
|
'with the same name is deleted'.format(network.id))
|
|
|
|
|
|
|
|
def create_logical_network(self):
|
|
|
|
print('\n# Example: Add a logical network to the compute gateway')
|
|
|
|
edges = self.vmc_client.orgs.sddcs.networks.Edges.get(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id,
|
2018-03-08 16:02:07 -05:00
|
|
|
edge_type='gatewayServices').edge_page.data
|
|
|
|
print(' Compute Gateway ID: {}'.format(edges[1].id))
|
|
|
|
edge_id = edges[1].id
|
|
|
|
|
|
|
|
# Construct a new NSX logical network spec
|
|
|
|
network = SddcNetwork(
|
2018-11-22 02:51:21 -05:00
|
|
|
subnets=SddcNetworkAddressGroups(address_groups=[
|
|
|
|
SddcNetworkAddressGroup(
|
2018-03-08 16:02:07 -05:00
|
|
|
prefix_length=self.prefix_length,
|
2018-11-22 02:51:21 -05:00
|
|
|
primary_address=self.primary_address)
|
|
|
|
]),
|
2018-03-08 16:02:07 -05:00
|
|
|
name=self.network_name,
|
|
|
|
cgw_id=edge_id,
|
2018-11-22 02:51:21 -05:00
|
|
|
dhcp_configs=SddcNetworkDhcpConfig(ip_pools=[
|
|
|
|
SddcNetworkDhcpIpPool(
|
|
|
|
ip_range=self.dhcp_range, domain_name=None)
|
|
|
|
]))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
self.vmc_client.orgs.sddcs.networks.Logical.create(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, sddc_network=network)
|
2018-03-08 16:02:07 -05:00
|
|
|
|
2018-11-22 02:51:21 -05:00
|
|
|
print('\n# New logical network "{}" is added'.format(
|
|
|
|
self.network_name))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
def get_logical_network(self):
|
|
|
|
print('\n# Example: List all logical networks')
|
|
|
|
networks = self.vmc_client.orgs.sddcs.networks.Logical.get_0(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id).data
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
self.print_output(networks)
|
|
|
|
|
|
|
|
for network in networks:
|
|
|
|
if network.name == self.network_name:
|
|
|
|
self.network_id = network.id
|
|
|
|
break
|
|
|
|
else:
|
2018-11-22 02:51:21 -05:00
|
|
|
raise Exception("Can't find logical network with name {}".format(
|
|
|
|
self.network_name))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
print('\n# Get the new logical network specs')
|
|
|
|
network = self.vmc_client.orgs.sddcs.networks.Logical.get(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, network_id=self.network_id)
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
self.print_output([network])
|
|
|
|
|
|
|
|
def update_logical_network(self):
|
|
|
|
print('\n# Example: Update the logical network')
|
|
|
|
network = self.vmc_client.orgs.sddcs.networks.Logical.get(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, network_id=self.network_id)
|
2018-03-08 16:02:07 -05:00
|
|
|
network.l2_extension = L2Extension(123)
|
|
|
|
network.subnets = None
|
|
|
|
network.dhcp_configs = None
|
|
|
|
|
|
|
|
self.vmc_client.orgs.sddcs.networks.Logical.update(
|
|
|
|
org=self.org_id,
|
|
|
|
sddc=self.sddc_id,
|
|
|
|
network_id=self.network_id,
|
|
|
|
sddc_network=network)
|
|
|
|
|
|
|
|
network = self.vmc_client.orgs.sddcs.networks.Logical.get(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, network_id=self.network_id)
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
print('# List the updated logical network specs')
|
|
|
|
self.print_output([network])
|
|
|
|
|
|
|
|
def delete_logical_network(self):
|
|
|
|
if self.cleanup:
|
|
|
|
self.vmc_client.orgs.sddcs.networks.Logical.delete(
|
2018-11-22 02:51:21 -05:00
|
|
|
org=self.org_id, sddc=self.sddc_id, network_id=self.network_id)
|
|
|
|
print('\n# Example: Logical network "{}" is deleted'.format(
|
|
|
|
self.network_name))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
def print_output(self, networks):
|
|
|
|
for network in networks:
|
2018-11-22 02:51:21 -05:00
|
|
|
print('Gateway: {}, Network ID: {}, Network Name: {}, Subnets: {}'.
|
|
|
|
format(
|
|
|
|
network.cgw_name, network.id, network.name,
|
|
|
|
'{}/{}'.format(
|
|
|
|
network.subnets.address_groups[0].primary_address,
|
|
|
|
network.subnets.address_groups[0].prefix_length)))
|
2018-03-08 16:02:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
logical_network_crud = LogicalNetworkCrud()
|
|
|
|
logical_network_crud.setup()
|
|
|
|
logical_network_crud.create_logical_network()
|
|
|
|
logical_network_crud.get_logical_network()
|
|
|
|
# TODO: figure out the requirements for updating logical network
|
|
|
|
# logical_network_crud.update_logical_network()
|
|
|
|
logical_network_crud.delete_logical_network()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|