<commit_before>#! /usr/bin/env python
# Python script for the Interoute Virtual Data Centre API:
#   Name: networks_member_listing.py
#   Purpose: List the networks in a VDC with the VMs belonging to each network
#   Requires: class VDCApiCall in the file vdc_api_call.py
# See the repo: https://github.com/Interoute/API-fun-and-education
#
# You can pass options via the command line: type 'python networks_member_listing.py -h'
# for usage information
#
# Copyright (C) Interoute Communications Limited, 2016

from __future__ import print_function
import vdc_api_call as vdc
import getpass
import json
import os
import string
import datetime
import argparse
import re

if __name__ == '__main__':
    # STEP 1: Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--config", default=os.path.join(os.path.expanduser('~'), '.vdcapi'),
                    help="path/name of the config file to be used for the API URL and API keys (default is ~/.vdcapi)")
    parser.add_argument("-r", "--region", choices=['Europe','europe','USA','usa','Asia','asia'],
                    default='Europe', help="specify the VDC region: Europe, USA or Asia (default Europe)")
    parser.add_argument("-f", "--diagfile", default='VDC-network-data.diag',
                    help="name of the output diag file for use with nwdiag")
    parser.add_argument("-z", "--zone", help="filter results by zone name (match by initial characters) ")
    parser.add_argument("-v", "--vmstate", action='store_true', help="display VM state by text colour")
    vdcRegion = parser.parse_args().region
    diagfileName = parser.parse_args().diagfile
    config_file = parser.parse_args().config
    zonenameFilter = parser.parse_args().zone
    showVmState =  parser.parse_args().vmstate

    # STEP 2: If config file is found, read its content,
    # else query user for the URL, API key, Secret key
    if os.path.isfile(config_file):
        with open(config_file) as fh:
            data = fh.read()
            config = json.loads(data)
            api_url = config['api_url']
            apiKey = config['api_key']
            secret = config['api_secret']
    else:
        print('API url (e.g. http://10.220.18.115:8080/client/api):', end='')
        api_url = raw_input()
        print('API key:', end='')
        apiKey = raw_input()
        secret = getpass.getpass(prompt='API secret:')

    # STEP 3: Create the api access object
    api = vdc.VDCApiCall(api_url, apiKey, secret)

    # STEP 4: API calls to get the information about networks and VMs
    request = {'region': vdcRegion}
    try:
       networksList = api.listNetworks(request)
    except KeyError:
       print("Error: No networks found", file=sys.stderr)
    try: 
       vmList = api.listVirtualMachines(request)
    except KeyError:
       print("Note: No VMs found for this account", file=sys.stderr)
       vmList = {}
       pass 
    try:
       portForwardingRulesList = api.listPortForwardingRules(request)
    except KeyError:
       print("Note: No port-forwarding rules found for this account")
       portForwardingRulesList = {}
       pass 

    if zonenameFilter:
       networksList['network'] = [network for network in networksList['network'] if re.search('\A'+zonenameFilter,network['zonename'])]

    # STEP 5: Process the information from the API calls
    nameStringSubs = string.maketrans(" -","__")
    try:
        diagfile = open(diagfileName, 'w')<commit_msg>Fix bug when -z filter option leads to empty network list<commit_after>#! /usr/bin/env python
# Python script for the Interoute Virtual Data Centre API:
#   Name: networks_member_listing.py
#   Purpose: List the networks in a VDC with the VMs belonging to each network
#   Requires: class VDCApiCall in the file vdc_api_call.py
# See the repo: https://github.com/Interoute/API-fun-and-education
#
# You can pass options via the command line: type 'python networks_member_listing.py -h'
# for usage information
#
# Copyright (C) Interoute Communications Limited, 2016

from __future__ import print_function
import vdc_api_call as vdc
import sys
import getpass
import json
import os
import string
import datetime
import argparse
import re

if __name__ == '__main__':
    # STEP 1: Parse command line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--config", default=os.path.join(os.path.expanduser('~'), '.vdcapi'),
                    help="path/name of the config file to be used for the API URL and API keys (default is ~/.vdcapi)")
    parser.add_argument("-r", "--region", choices=['Europe','europe','USA','usa','Asia','asia'],
                    default='Europe', help="specify the VDC region: Europe, USA or Asia (default Europe)")
    parser.add_argument("-f", "--diagfile", default='VDC-network-data.diag',
                    help="name of the output diag file for use with nwdiag")
    parser.add_argument("-z", "--zone", help="filter results by zone name (match by initial characters) ")
    parser.add_argument("-v", "--vmstate", action='store_true', help="display VM state by text colour")
    vdcRegion = parser.parse_args().region
    diagfileName = parser.parse_args().diagfile
    config_file = parser.parse_args().config
    zonenameFilter = parser.parse_args().zone
    showVmState =  parser.parse_args().vmstate

    # STEP 2: If config file is found, read its content,
    # else query user for the URL, API key, Secret key
    if os.path.isfile(config_file):
        with open(config_file) as fh:
            data = fh.read()
            config = json.loads(data)
            api_url = config['api_url']
            apiKey = config['api_key']
            secret = config['api_secret']
    else:
        print('API url (e.g. http://10.220.18.115:8080/client/api):', end='')
        api_url = raw_input()
        print('API key:', end='')
        apiKey = raw_input()
        secret = getpass.getpass(prompt='API secret:')

    # STEP 3: Create the api access object
    api = vdc.VDCApiCall(api_url, apiKey, secret)

    # STEP 4: API calls to get the information about networks and VMs
    request = {'region': vdcRegion}
    try:
       networksList = api.listNetworks(request)
       if zonenameFilter:
          networksList['network'] = [network for network in networksList['network'] if re.search('\A'+zonenameFilter,network['zonename'])]
          if networksList['network'] == []:
             print("ERROR: No networks found when using the zonename filter \'%s\'." % zonenameFilter)
             sys.exit("FATAL: Program terminating")
    except KeyError:
       print("Error: No networks found", file=sys.stderr)
    try: 
       vmList = api.listVirtualMachines(request)
    except KeyError:
       print("Note: No VMs found for this account", file=sys.stderr)
       vmList = {}
       pass 
    try:
       portForwardingRulesList = api.listPortForwardingRules(request)
    except KeyError:
       print("Note: No port-forwarding rules found for this account")
       portForwardingRulesList = {}
       pass 


    # STEP 5: Process the information from the API calls
    nameStringSubs = string.maketrans(" -","__")
    try:
        diagfile = open(diagfileName, 'w')
{'fraction_non_alphanumeric': 0.11674986134220743, 'fraction_numerical': 0.006378258458125347, 'mean_word_length': 3.1430212521539347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3485416', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2108, 'n_words': 657}