From 2d2f32b92321c50adbb2155cd51f674a8d8dd73b Mon Sep 17 00:00:00 2001 From: Anusha Hegde Date: Tue, 20 Aug 2019 12:24:24 +0530 Subject: [PATCH] =?UTF-8?q?differentiating=20required=20and=20optional=20a?= =?UTF-8?q?rguments=20in=20the=20help=20text=20for=20=E2=80=A6=20(#175)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * differentiating required and optional arguments in the help text for vmc samples Signed-off-by: Anusha Hegde --- samples/vmc/helpers/sample_cli.py | 48 +++++++++++++++++++ .../vmc/networks_nsxt/cgw_firewall_crud.py | 17 +------ .../vmc/networks_nsxt/dfw_firewall_crud.py | 17 +------ samples/vmc/networks_nsxt/hello_world.py | 17 +------ samples/vmc/networks_nsxt/l3_vpn_crud.py | 21 ++------ samples/vmc/networks_nsxt/nat_crud.py | 18 +------ .../networks_nsxt/security_group_create.py | 26 +++------- .../networks_nsxt/security_group_delete.py | 21 ++------ .../vmc/networks_nsxt/security_group_list.py | 19 +------- .../networks_nsxt/security_group_update.py | 23 ++------- .../networks_nsxt/segments_firewall_crud.py | 18 +------ samples/vmc/networks_nsxv/dns_crud.py | 21 ++------ samples/vmc/networks_nsxv/expose_public_ip.py | 26 +++------- .../vmc/networks_nsxv/firewall_rules_crud.py | 24 ++-------- samples/vmc/networks_nsxv/ipsec_vpns_crud.py | 34 ++++--------- .../vmc/networks_nsxv/logical_network_crud.py | 26 ++-------- samples/vmc/networks_nsxv/nat_rule_crud.py | 25 ++-------- samples/vmc/networks_nsxv/public_ip_crud.py | 21 ++------ samples/vmc/orgs/organization_operations.py | 8 ++-- samples/vmc/sddc/add_remove_hosts.py | 19 +------- ...t_to_vsphere_with_default_sddc_password.py | 14 +----- samples/vmc/sddc/deploy_ovf_template.py | 14 +++--- samples/vmc/sddc/sddc_crud.py | 41 ++++++++-------- samples/vmc/tasks/cancel_task.py | 27 ++++++----- samples/vmc/tasks/list_tasks.py | 27 ++++++----- samples/vmc/tasks/list_tasks_stg.py | 30 +++++++----- samples/vsphere/common/sample_cli.py | 11 +++-- 27 files changed, 201 insertions(+), 412 deletions(-) create mode 100644 samples/vmc/helpers/sample_cli.py diff --git a/samples/vmc/helpers/sample_cli.py b/samples/vmc/helpers/sample_cli.py new file mode 100644 index 00000000..a2a07f49 --- /dev/null +++ b/samples/vmc/helpers/sample_cli.py @@ -0,0 +1,48 @@ +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2019. 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.' +__copyright__ = 'Copyright 2019 VMware, Inc. All rights reserved.' + +import argparse + +""" +Builds a standard argument parser with required and optional argument groups + +Most of the VMC samples require these three standard required arguments. +If any of these arguments are not required, then build your own parser + +--refresh_token +--org_id +--sddc_id + +""" +parser = argparse.ArgumentParser() + +required_args = parser.add_argument_group( + 'required arguments') +optional_args = parser.add_argument_group( + 'optional arguments') + +required_args.add_argument( + '--refresh_token', + required=True, + help='Refresh token obtained from CSP') +required_args.add_argument( + '--org_id', + required=True, + help='Orgization ID') +required_args.add_argument( + '--sddc_id', + required=True, + help='SDDC ID') diff --git a/samples/vmc/networks_nsxt/cgw_firewall_crud.py b/samples/vmc/networks_nsxt/cgw_firewall_crud.py index 2e3db326..d3bfb124 100644 --- a/samples/vmc/networks_nsxt/cgw_firewall_crud.py +++ b/samples/vmc/networks_nsxt/cgw_firewall_crud.py @@ -16,8 +16,8 @@ __author__ = 'VMware, Inc.' __vcenter_version__ = '6.8.0+' -import argparse import requests +from samples.vmc.helpers.sample_cli import parser from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from com.vmware.nsx_policy.model_client import IPAddressExpression from com.vmware.nsx_policy.model_client import Group @@ -36,21 +36,6 @@ class NSXPolicyCGWFirewall(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - - parser.add_argument('--org_id', - required=True, - help='Orgization ID') - - parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - args = parser.parse_args() self.nsx_client = create_nsx_policy_client_for_vmc( diff --git a/samples/vmc/networks_nsxt/dfw_firewall_crud.py b/samples/vmc/networks_nsxt/dfw_firewall_crud.py index 97ef4c72..101751b0 100644 --- a/samples/vmc/networks_nsxt/dfw_firewall_crud.py +++ b/samples/vmc/networks_nsxt/dfw_firewall_crud.py @@ -16,8 +16,8 @@ __author__ = 'VMware, Inc.' __vcenter_version__ = '6.8.0+' -import argparse import requests +from samples.vmc.helpers.sample_cli import parser from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from com.vmware.nsx_policy.model_client import IPAddressExpression from com.vmware.nsx_policy.model_client import Group @@ -38,21 +38,6 @@ class NSXPolicyDFWFirewall(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - - parser.add_argument('--org_id', - required=True, - help='Orgization ID') - - parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - args = parser.parse_args() self.nsx_client = create_nsx_policy_client_for_vmc( diff --git a/samples/vmc/networks_nsxt/hello_world.py b/samples/vmc/networks_nsxt/hello_world.py index d920917e..21481db7 100644 --- a/samples/vmc/networks_nsxt/hello_world.py +++ b/samples/vmc/networks_nsxt/hello_world.py @@ -15,9 +15,8 @@ __author__ = 'VMware, Inc.' -import argparse import pprint - +from samples.vmc.helpers.sample_cli import parser from com.vmware.nsx_policy_client_for_vmc import ( create_nsx_policy_client_for_vmc) @@ -33,20 +32,6 @@ class AuthExample(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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.') - args = parser.parse_args() self.org_id = args.org_id diff --git a/samples/vmc/networks_nsxt/l3_vpn_crud.py b/samples/vmc/networks_nsxt/l3_vpn_crud.py index 96b4ef6b..e1162096 100644 --- a/samples/vmc/networks_nsxt/l3_vpn_crud.py +++ b/samples/vmc/networks_nsxt/l3_vpn_crud.py @@ -15,7 +15,7 @@ __author__ = 'VMware, Inc.' -import argparse +from samples.vmc.helpers.sample_cli import parser, required_args from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from vmware.vapi.bindings.struct import PrettyPrinter as NsxPrettyPrinter from com.vmware.nsx_policy.model_client import ApiError @@ -39,26 +39,11 @@ class NSXPolicyL3VPN(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - - parser.add_argument('--org_id', - required=True, - help='Orgization ID') - - parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - - parser.add_argument('--remote_endpoint_public_ip', + required_args.add_argument('--remote_endpoint_public_ip', required=True, help='L3 VPN Remote end point\'s public ip') - parser.add_argument('--passphrase', + required_args.add_argument('--passphrase', required=True, help='Passphrase used for VPN') diff --git a/samples/vmc/networks_nsxt/nat_crud.py b/samples/vmc/networks_nsxt/nat_crud.py index 66fcfe7d..85d8d5c3 100644 --- a/samples/vmc/networks_nsxt/nat_crud.py +++ b/samples/vmc/networks_nsxt/nat_crud.py @@ -16,8 +16,9 @@ __author__ = 'VMware, Inc.' __vcenter_version__ = '6.8.1+' -import argparse import requests + +from samples.vmc.helpers.sample_cli import parser from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from com.vmware.nsx_vmc_app_client_for_vmc import create_nsx_vmc_app_client_for_vmc from com.vmware.nsx_vmc_app.model_client import PublicIp @@ -36,21 +37,6 @@ class NSXPolicyNAT(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - - parser.add_argument('--org_id', - required=True, - help='Orgization ID') - - parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - args = parser.parse_args() self.nsx_client = create_nsx_policy_client_for_vmc( diff --git a/samples/vmc/networks_nsxt/security_group_create.py b/samples/vmc/networks_nsxt/security_group_create.py index 7337f32b..dab65011 100644 --- a/samples/vmc/networks_nsxt/security_group_create.py +++ b/samples/vmc/networks_nsxt/security_group_create.py @@ -15,10 +15,11 @@ __author__ = 'VMware, Inc' __vcenter_version__ = 'VMware Cloud on AWS' -import argparse import random import requests + +from samples.vmc.helpers.sample_cli import parser, required_args, optional_args from com.vmware.nsx_policy.infra_client import Domains from com.vmware.nsx_policy.model_client import (Expression, Group, IPAddressExpression) @@ -35,34 +36,19 @@ Create a new NSX-T Group on MGW or CGW Sample Prerequisites: - SDDC deployed in VMware Cloud on AWS """ -parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - -parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - -parser.add_argument('--org_id', - required=True, - help='Orgization ID') - -parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - -parser.add_argument('--gateway_type', +optional_args.add_argument('--gateway_type', default='mgw', help='Gateway type. Either mgw or cgw') -parser.add_argument('--name', +required_args.add_argument('--name', required=True, help='Name of the security group to be created') -parser.add_argument('--ip_address', +optional_args.add_argument('--ip_address', default='172.31.0.0/24', help='IP address for the expression') -parser.add_argument('--group_id', +optional_args.add_argument('--group_id', help='ID of the group. A random ID will be used by default') args = parser.parse_args() diff --git a/samples/vmc/networks_nsxt/security_group_delete.py b/samples/vmc/networks_nsxt/security_group_delete.py index 9f1079cf..2af72759 100644 --- a/samples/vmc/networks_nsxt/security_group_delete.py +++ b/samples/vmc/networks_nsxt/security_group_delete.py @@ -16,10 +16,10 @@ __author__ = 'VMware, Inc' __vcenter_version__ = 'VMware Cloud on AWS' -import argparse import random import requests +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.nsx_policy.infra_client import Domains from com.vmware.nsx_policy.model_client import (Expression, Group, IPAddressExpression) @@ -39,26 +39,11 @@ Sample Prerequisites: - SDDC deployed in VMware Cloud on AWS - A NSX-T security group """ -parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - -parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - -parser.add_argument('--org_id', - required=True, - help='Orgization ID') - -parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - -parser.add_argument('--gateway_type', +optional_args.add_argument('--gateway_type', default='mgw', help='Gateway type. Either mgw or cgw') -parser.add_argument('--group_id', +optional_args.add_argument('--group_id', help='ID of the group to be deleted') args = parser.parse_args() diff --git a/samples/vmc/networks_nsxt/security_group_list.py b/samples/vmc/networks_nsxt/security_group_list.py index f235f8d3..658415ab 100644 --- a/samples/vmc/networks_nsxt/security_group_list.py +++ b/samples/vmc/networks_nsxt/security_group_list.py @@ -17,8 +17,8 @@ __author__ = 'VMware, Inc' __vcenter_version__ = 'VMware Cloud on AWS' import requests -import argparse +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.nsx_policy.infra_client import Domains from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from vmware.vapi.bindings.struct import PrettyPrinter @@ -34,22 +34,7 @@ List all Network Security Groups Sample Prerequisites: - SDDC deployed in VMware Cloud on AWS """ -parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - -parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - -parser.add_argument('--org_id', - required=True, - help='Orgization ID') - -parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - -parser.add_argument('--gateway_type', +optional_args.add_argument('--gateway_type', default='mgw', help='Gateway type. Either mgw or cgw') diff --git a/samples/vmc/networks_nsxt/security_group_update.py b/samples/vmc/networks_nsxt/security_group_update.py index a3bce5c5..945fb7f6 100644 --- a/samples/vmc/networks_nsxt/security_group_update.py +++ b/samples/vmc/networks_nsxt/security_group_update.py @@ -16,10 +16,10 @@ __author__ = 'VMware, Inc' __vcenter_version__ = 'VMware Cloud on AWS' -import argparse import random import requests +from samples.vmc.helpers.sample_cli import parser, required_args, optional_args from com.vmware.nsx_policy.infra_client import Domains from com.vmware.nsx_policy.model_client import (Expression, Group, IPAddressExpression) @@ -40,29 +40,14 @@ Sample Prerequisites: - SDDC deployed in VMware Cloud on AWS - A NSX-T security group """ -parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - -parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - -parser.add_argument('--org_id', - required=True, - help='Orgization ID') - -parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - -parser.add_argument('--gateway_type', +optional_args.add_argument('--gateway_type', default='mgw', help='Gateway type. Either mgw or cgw') -parser.add_argument('--group_id', +optional_args.add_argument('--group_id', help='ID of the group to be updated') -parser.add_argument('--name', +required_args.add_argument('--name', required=True, help='New name of the security group to be updated') diff --git a/samples/vmc/networks_nsxt/segments_firewall_crud.py b/samples/vmc/networks_nsxt/segments_firewall_crud.py index 8a5b3cab..0402684d 100644 --- a/samples/vmc/networks_nsxt/segments_firewall_crud.py +++ b/samples/vmc/networks_nsxt/segments_firewall_crud.py @@ -15,9 +15,8 @@ __author__ = 'VMware, Inc.' - -import argparse import requests +from samples.vmc.helpers.sample_cli import parser from com.vmware.nsx_policy_client_for_vmc import create_nsx_policy_client_for_vmc from com.vmware.nsx_policy.model_client import Rule from vmware.vapi.bindings.struct import PrettyPrinter as NsxPrettyPrinter @@ -34,21 +33,6 @@ class NSXPolicySegmentFirewall(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('--refresh_token', - required=True, - help='Refresh token obtained from CSP') - - parser.add_argument('--org_id', - required=True, - help='Orgization ID') - - parser.add_argument('--sddc_id', - required=True, - help='Sddc ID') - args = parser.parse_args() self.nsx_client = create_nsx_policy_client_for_vmc( diff --git a/samples/vmc/networks_nsxv/dns_crud.py b/samples/vmc/networks_nsxv/dns_crud.py index 4ce8fc2e..85ca0d9d 100644 --- a/samples/vmc/networks_nsxv/dns_crud.py +++ b/samples/vmc/networks_nsxv/dns_crud.py @@ -15,8 +15,7 @@ __author__ = 'VMware, Inc.' -import argparse - +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import DnsForwarders from vmware.vapi.vmc.client import create_vmc_client @@ -31,27 +30,13 @@ class DNSCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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('--use-compute-gateway', + optional_args.add_argument('--use-compute-gateway', action='store_true', default=False, help='Use compute gateway. Default is using ' 'management gateway') - parser.add_argument('-c', '--cleardata', + optional_args.add_argument('--cleardata', action='store_true', help='Clean up after sample run') args = parser.parse_args() diff --git a/samples/vmc/networks_nsxv/expose_public_ip.py b/samples/vmc/networks_nsxv/expose_public_ip.py index 1ee64bd1..4035ccd7 100644 --- a/samples/vmc/networks_nsxv/expose_public_ip.py +++ b/samples/vmc/networks_nsxv/expose_public_ip.py @@ -16,9 +16,9 @@ __author__ = 'VMware, Inc.' __vcenter_version__ = 'VMware Cloud on AWS' -import argparse import random +from samples.vmc.helpers.sample_cli import parser, required_args, optional_args from com.vmware.vmc.model_client import * from vmware.vapi.vmc.client import create_vmc_client from samples.vmc.helpers.vmc_task_helper import wait_for_task @@ -36,37 +36,23 @@ class ExposePublicIP(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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('--notes', + optional_args.add_argument('--notes', default='Sample public IP ' + str(random.randint(0, 100)), help='Notes of the new public IP') - parser.add_argument('--fw-rule-name', + optional_args.add_argument('--fw-rule-name', default='Sample firewall rule ' + str(random.randint(0, 100)), help='Name of the compute gae') - parser.add_argument('--nat-rule-description', + optional_args.add_argument('--nat-rule-description', default='Sample NAT rule ' + str(random.randint(0, 100)), help='Description for the NAT rule') - parser.add_argument('--internal-ip', + required_args.add_argument('--internal-ip', required=True, help='Private IP of the VM') - parser.add_argument('-c', '--cleardata', + optional_args.add_argument('--cleardata', action='store_true', help='Clean up after sample run') args = parser.parse_args() diff --git a/samples/vmc/networks_nsxv/firewall_rules_crud.py b/samples/vmc/networks_nsxv/firewall_rules_crud.py index b42fa6a9..05baf749 100644 --- a/samples/vmc/networks_nsxv/firewall_rules_crud.py +++ b/samples/vmc/networks_nsxv/firewall_rules_crud.py @@ -14,8 +14,7 @@ __author__ = 'VMware, Inc.' -import argparse - +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import (AddressFWSourceDestination, Application, FirewallRules, Nsxfirewallrule, Nsxfirewallservice) @@ -32,34 +31,19 @@ class FirewallRulesCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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( + optional_args.add_argument( '--rule-name', default='Sample Firewall Rule', help='Name of the new firewall rule') - parser.add_argument( + optional_args.add_argument( '--use-compute-gateway', action='store_true', default=False, help='Use compute gateway. Default is using ' 'management gateway') - parser.add_argument( - '-c', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/networks_nsxv/ipsec_vpns_crud.py b/samples/vmc/networks_nsxv/ipsec_vpns_crud.py index e80046b0..7a53526c 100644 --- a/samples/vmc/networks_nsxv/ipsec_vpns_crud.py +++ b/samples/vmc/networks_nsxv/ipsec_vpns_crud.py @@ -14,8 +14,7 @@ __author__ = 'VMware, Inc.' -import argparse - +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import Ipsec, IpsecSite, IpsecSites, Subnets from vmware.vapi.vmc.client import create_vmc_client @@ -30,59 +29,44 @@ class IpsecVPNsCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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( + optional_args.add_argument( '--use-compute-gateway', action='store_true', default=False, help='Use compute gateway. Default is using ' 'management gateway') - parser.add_argument( + optional_args.add_argument( '--vpn-name', default='Sample IPsec VPN', help='Name of the new VPN') - parser.add_argument( + optional_args.add_argument( '--public-ip', default='10.10.10.10', help='IP (IPv4) address or FQDN of the Peer') - parser.add_argument( + optional_args.add_argument( '--private-ip', default='192.168.10.10', help='Local IP of the IPsec Site') - parser.add_argument( + optional_args.add_argument( '--remote-networks', default='192.168.20.10/24', help='Peer subnets for which VPN is configured') - parser.add_argument( + optional_args.add_argument( '--local-networks', default='192.168.30.10/24', help='Local subnets for which VPN is configured') - parser.add_argument( + optional_args.add_argument( '--key', default='00000000', help='Pre Shared Key for the IPsec Site') - parser.add_argument( - '-c', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/networks_nsxv/logical_network_crud.py b/samples/vmc/networks_nsxv/logical_network_crud.py index 6294463d..06e35f85 100644 --- a/samples/vmc/networks_nsxv/logical_network_crud.py +++ b/samples/vmc/networks_nsxv/logical_network_crud.py @@ -14,8 +14,7 @@ __author__ = 'VMware, Inc.' -import argparse - +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import (L2Extension, SddcNetwork, SddcNetworkAddressGroups, SddcNetworkAddressGroup, @@ -34,37 +33,22 @@ class LogicalNetworkCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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( + optional_args.add_argument( '--network-name', default='Sample Logical Network', help='Name of the new logical network') - parser.add_argument( + optional_args.add_argument( '--subnet', default='192.168.100.1/24', help='Logical network subnet') - parser.add_argument( + optional_args.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', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/networks_nsxv/nat_rule_crud.py b/samples/vmc/networks_nsxv/nat_rule_crud.py index 80179fd4..bac48e9f 100644 --- a/samples/vmc/networks_nsxv/nat_rule_crud.py +++ b/samples/vmc/networks_nsxv/nat_rule_crud.py @@ -14,7 +14,7 @@ __author__ = 'VMware, Inc.' -import argparse +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import Nsxnatrule, NatRules from vmware.vapi.vmc.client import create_vmc_client @@ -29,35 +29,20 @@ class NatRuleCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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( + optional_args.add_argument( '--public-ip', help='Public IP range for the NAT rule') - parser.add_argument( + optional_args.add_argument( '--rule-description', default='Sample NAT rule', help='Description for the rule') - parser.add_argument( + optional_args.add_argument( '--internal-ip', default='192.168.200.1/24', help='NAT rule subnet') - parser.add_argument( - '-c', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/networks_nsxv/public_ip_crud.py b/samples/vmc/networks_nsxv/public_ip_crud.py index 768f0035..7006bd89 100644 --- a/samples/vmc/networks_nsxv/public_ip_crud.py +++ b/samples/vmc/networks_nsxv/public_ip_crud.py @@ -14,7 +14,7 @@ __author__ = 'VMware, Inc.' -import argparse +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import SddcAllocatePublicIpSpec from vmware.vapi.vmc.client import create_vmc_client @@ -31,27 +31,12 @@ class PublicIPsCrud(object): """ def __init__(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - 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( + optional_args.add_argument( '--notes', default='Sample public IP', help='Notes of the new public IP') - parser.add_argument( - '-c', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/orgs/organization_operations.py b/samples/vmc/orgs/organization_operations.py index 6f8707dd..0c7e76a2 100644 --- a/samples/vmc/orgs/organization_operations.py +++ b/samples/vmc/orgs/organization_operations.py @@ -38,10 +38,10 @@ class OperationsOnOrganizations(object): def options(self): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument( - '-r', - '--refresh-token', + required_args = parser.add_argument_group( + 'required arguments') + required_args.add_argument( + '--refresh_token', required=True, help='VMware Cloud API refresh token') diff --git a/samples/vmc/sddc/add_remove_hosts.py b/samples/vmc/sddc/add_remove_hosts.py index 954e4fb8..e15efd49 100644 --- a/samples/vmc/sddc/add_remove_hosts.py +++ b/samples/vmc/sddc/add_remove_hosts.py @@ -15,10 +15,10 @@ __author__ = 'VMware, Inc.' -import argparse import atexit import requests +from samples.vmc.helpers.sample_cli import parser, optional_args from com.vmware.vmc.model_client import EsxConfig, ErrorResponse from com.vmware.vapi.std.errors_client import InvalidRequest from vmware.vapi.vmc.client import create_vmc_client @@ -43,22 +43,7 @@ class AddRemoveHosts(object): self.interval_sec = None def options(self): - parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - 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('-i', '--interval-sec', + optional_args.add_argument('--interval-sec', default=60, help='Task pulling interval in sec') diff --git a/samples/vmc/sddc/connect_to_vsphere_with_default_sddc_password.py b/samples/vmc/sddc/connect_to_vsphere_with_default_sddc_password.py index 292ab9fa..b6dcc7fb 100644 --- a/samples/vmc/sddc/connect_to_vsphere_with_default_sddc_password.py +++ b/samples/vmc/sddc/connect_to_vsphere_with_default_sddc_password.py @@ -15,11 +15,11 @@ __author__ = 'VMware, Inc.' __vcenter_version__ = 'VMware Cloud on AWS' -import argparse from com.vmware.vapi.std.errors_client import NotFound from com.vmware.vmc.model_client import ErrorResponse from six.moves.urllib import parse +from samples.vmc.helpers.sample_cli import parser from vmware.vapi.vmc.client import create_vmc_client from vmware.vapi.vsphere.client import create_vsphere_client @@ -35,18 +35,6 @@ class ConnectTovSphereWithDefaultCredentials(object): """ def __init__(self): - parser = argparse.ArgumentParser() - 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.') args = parser.parse_args() self.refresh_token = args.refresh_token diff --git a/samples/vmc/sddc/deploy_ovf_template.py b/samples/vmc/sddc/deploy_ovf_template.py index faf55276..4ec5ccb6 100644 --- a/samples/vmc/sddc/deploy_ovf_template.py +++ b/samples/vmc/sddc/deploy_ovf_template.py @@ -41,28 +41,30 @@ class DeployOvfTemplate: self.vm_name = 'deployed-vm-' + str(generate_random_uuid()) parser = sample_cli.build_arg_parser() - parser.add_argument('--libitemname', + required_args = parser.add_argument_group( + 'required arguments') + required_args.add_argument('--libitemname', required=True, - help='(Required) The name of the library item to deploy. ' + help='The name of the library item to deploy. ' 'The library item should contain an OVF package.') parser.add_argument('--vmname', default=self.vm_name, - help='(Optional) The name of the Virtual Machine to be deployed. ' + help='The name of the Virtual Machine to be deployed. ' 'Default: "{}"'.format(self.vm_name)) parser.add_argument('--resourcepoolname', default='Compute-ResourcePool', - help='(Optional) The name of the resource pool to be used. ' + help='The name of the resource pool to be used. ' 'Default: "Compute-ResourcePool"') parser.add_argument('--foldername', default='Workloads', - help='(Optional) The name of the folder to be used. ' + help='The name of the folder to be used. ' 'Default: "Workloads"') parser.add_argument('--opaquenetworkname', - help='(Optional) The name of the opaque network to be added ' + help='The name of the opaque network to be added ' 'to the deployed vm') args = sample_util.process_cli_args(parser.parse_args()) diff --git a/samples/vmc/sddc/sddc_crud.py b/samples/vmc/sddc/sddc_crud.py index 1ef118c0..d7f3ad88 100644 --- a/samples/vmc/sddc/sddc_crud.py +++ b/samples/vmc/sddc/sddc_crud.py @@ -14,14 +14,13 @@ __author__ = 'VMware, Inc.' -import argparse import os from random import randrange +import argparse from com.vmware.vapi.std.errors_client import InvalidRequest from com.vmware.vmc.model_client import AwsSddcConfig, ErrorResponse, AccountLinkSddcConfig, SddcConfig from vmware.vapi.vmc.client import create_vmc_client - from samples.vmc.helpers.vmc_task_helper import wait_for_task @@ -36,49 +35,49 @@ class CreateDeleteSDDC(object): def __init__(self): parser = argparse.ArgumentParser() - parser.add_argument( - '-r', - '--refresh-token', + required_args = parser.add_argument_group( + 'required arguments') + optional_args = parser.add_argument_group( + 'optional arguments') + + required_args.add_argument( + '--refresh_token', required=True, - help='VMware Cloud API refresh token') + help='Refresh token obtained from CSP') - parser.add_argument( - '-o', '--org-id', required=True, help='Organization identifier.') + required_args.add_argument( + '--org_id', + required=True, + help='Organization identifier.') - parser.add_argument( - '-sn', + optional_args.add_argument( '--sddc-name', help="Name of the SDDC to be created. " "Default is 'Sample SDDC xx'") - parser.add_argument('--region', default='US_WEST_2', help='AWS Region') + optional_args.add_argument('--region', default='US_WEST_2', help='AWS Region') - parser.add_argument( - '-i', + optional_args.add_argument( '--interval-sec', default=60, help='Task pulling interval in sec') - parser.add_argument( - '-ls', + optional_args.add_argument( '--listsddc', action='store_true', help='List SDDCs in the specified Org') - parser.add_argument( - '-cs', + optional_args.add_argument( '--createsddc', action='store_true', help='Create an SDDC in the specified Org') - parser.add_argument( - '-ds', + optional_args.add_argument( '--deletesddc', action='store_true', help='Deletes the SDDC in the specified Org ') - parser.add_argument( - '-c', + optional_args.add_argument( '--cleardata', action='store_true', help='Clean up after sample run') diff --git a/samples/vmc/tasks/cancel_task.py b/samples/vmc/tasks/cancel_task.py index b167be6c..3f551f9b 100644 --- a/samples/vmc/tasks/cancel_task.py +++ b/samples/vmc/tasks/cancel_task.py @@ -14,7 +14,6 @@ __author__ = 'VMware, Inc.' import argparse - from com.vmware.vmc.model_client import Task from vmware.vapi.vmc.client import create_vmc_client @@ -27,19 +26,25 @@ Sample Prerequisites: """ parser = argparse.ArgumentParser( - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('--refresh-token', - required=True, - help='VMware Cloud API refresh token') +required_args = parser.add_argument_group( + 'required arguments') -parser.add_argument('--org-id', - required=True, - help='Organization identifier.') +required_args.add_argument( + '--refresh_token', + required=True, + help='Refresh token obtained from CSP') -parser.add_argument('--task-id', - required=True, - help='Task ID to be cancelled') +required_args.add_argument( + '--org-id', + required=True, + help='Organization identifier.') + +required_args.add_argument( + '--task-id', + required=True, + help='Task ID to be cancelled') args = parser.parse_args() diff --git a/samples/vmc/tasks/list_tasks.py b/samples/vmc/tasks/list_tasks.py index 9c9f6bc5..6546b4ae 100644 --- a/samples/vmc/tasks/list_tasks.py +++ b/samples/vmc/tasks/list_tasks.py @@ -14,7 +14,6 @@ __author__ = 'VMware, Inc.' import argparse - from com.vmware.vmc.model_client import Task from vmware.vapi.vmc.client import create_vmc_client @@ -28,19 +27,25 @@ Sample Prerequisites: accepted = [Task.STATUS_STARTED, Task.STATUS_CANCELING, Task.STATUS_FINISHED, Task.STATUS_FAILED, Task.STATUS_CANCELED] -parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser = argparse.ArgumentParser( + formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('--refresh-token', - required=True, - help='VMware Cloud API refresh token') +required_args = parser.add_argument_group( + 'required arguments') -parser.add_argument('--org-id', - required=True, - help='Organization identifier.') +required_args.add_argument( + '--refresh_token', + required=True, + help='Refresh token obtained from CSP') +required_args.add_argument( + '--org-id', + required=True, + help='Organization identifier.') -parser.add_argument('--task-status', - help='Task status to filter. Possible values are: {} \ - Show all tasks if no value is passed'.format(accepted)) +required_args.add_argument( + '--task-status', + help='Task status to filter. Possible values are: {} \ + Show all tasks if no value is passed'.format(accepted)) args = parser.parse_args() diff --git a/samples/vmc/tasks/list_tasks_stg.py b/samples/vmc/tasks/list_tasks_stg.py index 9c9f6bc5..ea17d352 100644 --- a/samples/vmc/tasks/list_tasks_stg.py +++ b/samples/vmc/tasks/list_tasks_stg.py @@ -14,7 +14,6 @@ __author__ = 'VMware, Inc.' import argparse - from com.vmware.vmc.model_client import Task from vmware.vapi.vmc.client import create_vmc_client @@ -28,19 +27,28 @@ Sample Prerequisites: accepted = [Task.STATUS_STARTED, Task.STATUS_CANCELING, Task.STATUS_FINISHED, Task.STATUS_FAILED, Task.STATUS_CANCELED] -parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser = argparse.ArgumentParser( + formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('--refresh-token', - required=True, - help='VMware Cloud API refresh token') +required_args = parser.add_argument_group( + 'required arguments') +optional_args = parser.add_argument_group( + 'optional arguments') -parser.add_argument('--org-id', - required=True, - help='Organization identifier.') +required_args.add_argument( + '--refresh_token', + required=True, + help='Refresh token obtained from CSP') -parser.add_argument('--task-status', - help='Task status to filter. Possible values are: {} \ - Show all tasks if no value is passed'.format(accepted)) +required_args.add_argument( + '--org-id', + required=True, + help='Organization identifier.') + +optional_args.add_argument( + '--task-status', + help='Task status to filter. Possible values are: {} \ + Show all tasks if no value is passed'.format(accepted)) args = parser.parse_args() diff --git a/samples/vsphere/common/sample_cli.py b/samples/vsphere/common/sample_cli.py index 41122dbb..6bf3c026 100644 --- a/samples/vsphere/common/sample_cli.py +++ b/samples/vsphere/common/sample_cli.py @@ -31,16 +31,21 @@ def build_arg_parser(): parser = argparse.ArgumentParser( description='Standard Arguments for talking to vCenter') - parser.add_argument('-s', '--server', + required_args = parser.add_argument_group( + 'required arguments') + required_args.add_argument('-s', '--server', action='store', + required=True, help='vSphere service IP to connect to') - parser.add_argument('-u', '--username', + required_args.add_argument('-u', '--username', action='store', + required=True, help='Username to use when connecting to vc') - parser.add_argument('-p', '--password', + required_args.add_argument('-p', '--password', action='store', + required=True, help='Password to use when connecting to vc') parser.add_argument('-c', '--cleardata',