def test_login(): controller_ip = os.environ.get("CONTROLLER") username = os.environ.get("CONTROLLER_USERNAME") password = os.environ.get("CONTROLLER_PASSWORD") controller = Aviatrix(controller_ip) controller.login(username, password) assert controller.results.find("authorized successfully") != -1
def fw_policies_example(controller_ip, username, password, gw_name): """ Performs actions related to FW policies Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password gw_name - string - name of a gateway to use for testing """ controller = Aviatrix(controller_ip) controller.login(username, password) gwy = controller.get_gateway_by_name('admin', gw_name) if not gwy: print 'Gateway %s not found\n' % (gw_name) return current = controller.get_fw_policy_full(gw_name) print 'CURRENT POLICY: %s' % (current) rules = current['security_rules'] rules.append({ 'protocol': 'all', 's_ip': '192.168.1.0/24', 'd_ip': '10.0.0.0/24', 'deny_allow': 'allow', 'port': '', 'log_enable': 'off' }) controller.set_fw_policy_security_rules(gw_name, rules) current = controller.get_fw_policy_full(gw_name) print 'CURRENT POLICY: %s' % (current)
def get_controller_from_argument(arg): """ Gets an Aviatrix Controller object from a string of the format: USER::PASSWD@@HOST """ userpw, controller_host = arg.split('@@') username, password = userpw.split('::') logging.debug('Connecting to Aviatrix @ %s', controller_host) controller = Aviatrix(controller_host) controller.login(username, password) return controller
def get_diagnostics(controller_ip, username, password, gwy_name): """ Prints the statistics to stdout Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password gwy_name - string - the gateway name """ controller = Aviatrix(controller_ip) controller.login(username, password) print 'Getting diagnostics for gateway %s ...' % (gwy_name) diagnostics = controller.get_gateway_diagnostics(gwy_name) print '%s' % (json.dumps(diagnostics, indent=2))
def get_stats(controller_ip, username, password, gwy_name): """ Prints the statistics to stdout Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password gwy_name - string - the gateway name """ controller = Aviatrix(controller_ip) controller.login(username, password) print 'Getting stats for gateway %s ...' % (gwy_name) end = datetime.now() start = end - timedelta(days=1) stats = controller.get_gateway_statistic_over_time( gwy_name, start, end, Aviatrix.StatName.DATA_AVG_TOTAL) print '%s' % (json.dumps(stats, indent=2))
def main(): """ main() interface to this script """ if len(sys.argv) != 4: print ('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) # connect to the controller controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] controller = Aviatrix(controller_ip) controller.login(username, password) create_profiles(controller) attach_users(controller) detach_users(controller) cleanup_profiles(controller)
def test(controller_ip, username, password): """ Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password """ print 'Connecting to %s' % controller_ip controller = Aviatrix(controller_ip) controller.login(username, password) print controller.get_controller_public_ip()
def print_stats(controller_ip, username, password): """ Prints the statistics to stdout Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password """ controller = Aviatrix(controller_ip) controller.login(username, password) gws = controller.list_gateways('admin') for gateway in gws: data = controller.get_current_gateway_statistics(gateway['vpc_name']) for gw_data in data: current = gw_data['mpstats']['stats_current'] cpu = current['cpu'] cpu_load = cpu['ks'] + cpu['us'] cpu_idle = cpu['idle'] memory = current['memory'] memory_free = memory['free'] disk_free = long(gw_data['hdisk_free']) network = gw_data['ifstats'] total_bytes_in = network['Cumulative (sent/received/total)'][1] total_bytes_out = network['Cumulative (sent/received/total)'][0] print( '%s:\n\tCPU Load: %d%%\n\tCPU Idle: %d%%\n\tFree Memory: %d kb\n\t' 'Free Diskspace: %d kb\n\tBytes in: %s\n\tBytes out: %s\n\t' % ( gw_data['gw_name'], cpu_load, cpu_idle, memory_free, disk_free, total_bytes_in, total_bytes_out, )) peers = controller.list_peers() for pair in peers: is_down = (pair['peering_state'].lower() != 'up') print '%s%s <==> %s %s' % ('!!!!! ' if is_down else '', pair['vpc_name1'], pair['vpc_name2'], pair['peering_state'].upper())
def fw_tag_example(controller_ip, username, password): """ Performs actions related to FW policy tags Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password gw_name - string - name of a gateway """ controller = Aviatrix(controller_ip) controller.login(username, password) current = controller.list_fw_tags() print "1. CURRENT tags: %s" % (current) controller.add_fw_tag('TEST_ME') current = controller.list_fw_tags() if 'TEST_ME' not in current: print 'ERROR: TEST_ME tag is missing: %s' % (current) return print '2. Added TEST_ME tag' members = controller.get_fw_tag_members('TEST_ME') print '3. current members: %s' % (members) members.append({'name': 'fwtag1', 'cidr': '192.168.1.0/24'}) controller.set_fw_tag_members('TEST_ME', members) members = controller.get_fw_tag_members('TEST_ME') print 'current members: %s' % (members) members.append({'name': 'fwtag2', 'cidr': '192.168.2.0/24'}) members.append({'name': 'fwtag3', 'cidr': '192.168.3.0/24'}) members.append({'name': 'fwtag4', 'cidr': '192.168.4.0/24'}) members.append({'name': 'fwtag5', 'cidr': '192.168.5.0/24'}) members.append({'name': 'fwtag6', 'cidr': '192.168.6.0/24'}) controller.set_fw_tag_members('TEST_ME', members) members = controller.get_fw_tag_members('TEST_ME') print 'current members: %s' % (members) controller.set_fw_tag_members('TEST_ME', []) controller.delete_fw_tag('TEST_ME') current = controller.list_fw_tags() if 'TEST_ME' in current: print 'ERROR: TEST_ME tag is in %s' % (current) return print 'Removed TEST_ME tag'
# #------------------------------------------------------------------------- from aviatrix import Aviatrix import logging import sys if len(sys.argv) != 4: print ('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) controller = Aviatrix(controller_ip) controller.login(username, password) controller.create_gateway('demoteam', # account_name Aviatrix.CloudType.AWS, # cloud_type 'paris_test', # gateway name 'vpc-0f04ff66', # VPC ID 'eu-west-3', # region 't2.micro', # size '172.31.0.0/16', # public subnet ignore_me=True)
def fqdn_example(controller_ip, username, password, gw_name): """ Performs actions related to FQDN filters Arguments: controller_ip - string - the controller host or IP username - string - the controller login username password - string - the controller login password gw_name - string - name of a gateway """ controller = Aviatrix(controller_ip) controller.login(username, password) gwy = controller.get_gateway_by_name('admin', gw_name) if not gwy: print 'Gateway %s not found\n' % (gw_name) return controller.enable_snat(gw_name) controller.add_fqdn_filter_tag('TEST_TAG') tags = controller.list_fqdn_filters() if 'TEST_TAG' not in tags: print 'TEST_TAG not found!\n' return controller.delete_fqdn_filter_tag('TEST_TAG') tags = controller.list_fqdn_filters() if 'TEST_TAG' in tags: print 'TEST_TAG found!\n' return controller.add_fqdn_filter_tag('TEST_TAG') try: controller.set_fqdn_filter_domain_list('TEST_TAG', ['*.google.com', 'cnn.com', '*.aviatrix.com']) except BaseException, e: print str(e) pass
if len(sys.argv) != 4: print('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) controller = Aviatrix(controller_ip) controller.login(username, password) controller.create_gateway( 'demoteam', # account_name Aviatrix.CloudType.AWS, # cloud_type 'vpn', # gateway name 'vpc-abcd0000', # VPC ID 'us-east-1', # region 't2.micro', # size '172.16.44.0/28', # public subnet vpn_access='yes', enable_elb='yes', cidr='192.168.43.0/24', max_conn=100, split_tunnel='yes', enable_ldap='no')
# $5 - CONN_NAME - string - connection name # $6 - NEW_SUBNET - string - new CIDR # #------------------------------------------------------------------------- from aviatrix import Aviatrix import logging import sys if len(sys.argv) != 7: print ('usage: %s <HOST> <USER> <PASSWORD> <VPC_ID> <CONN_NAME> <NEW_SUBNET>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' ' VPC_ID the VPC ID\n' ' CONN_NAME the s2c connection name\n' ' NEW_SUBNET the new CIDR for remote side\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] vpc_id = sys.argv[4] conn_name = sys.argv[5] new_subnet = sys.argv[6] #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) controller = Aviatrix(controller_ip) controller.login(username, password) controller.set_site2cloud_remote_subnet(vpc_id, conn_name, new_subnet)
# # INPUTS: # $1 - HOST - string - host/ip of the controller # $2 - USER - string - the username used to authenticate with controller # $3 - PASSWORD - string - the password of the given USER # # OUTPUTS: # count - int - number of gateways configured #------------------------------------------------------------------------- from aviatrix import Aviatrix import logging import sys if len(sys.argv) != 4: print ('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] controller = Aviatrix(controller_ip) controller.login(username, password) gws = controller.list_gateways('admin') print len(gws)
# # INPUTS: # $1 - HOST - string - host/ip of the controller # $2 - USER - string - the username used to authenticate with controller # $3 - PASSWORD - string - the password of the given USER # # OUTPUTS: # count - int - number of peers defined in Aviatrix #------------------------------------------------------------------------- from aviatrix import Aviatrix import logging import sys if len(sys.argv) != 4: print ('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] controller = Aviatrix(controller_ip) controller.login(username, password) peers = controller.list_peers_vpc_pairs() print len(peers)
VPC_ID = 'vpc-12345678' CONNECTION_NAME_ACTIVE = 'Tunnel_Name' CONNECTION_NAME_STANDBY = 'Tunnel_Name_Standby' REAL_CIDR = '10.0.0.0/16' FAKE_CIDR_ACTIVE = '100.64.0.0/16' FAKE_CIDR_STANDBY = '100.64.1.0/16' if len(sys.argv) != 4: print('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] controller = Aviatrix(controller_ip) controller.login(username, password) # first, change the active cidr to a fake cidr (so it won't conflict) controller.set_site2cloud_remote_subnet(VPC_ID, CONNECTION_NAME_ACTIVE, FAKE_CIDR_ACTIVE) # now the active tunnel and the standby tunnel both have a "fake" CIDR for remote subnet # next, change the standby to the real CIDR controller.set_site2cloud_remote_subnet(VPC_ID, CONNECTION_NAME_STANDBY, REAL_CIDR)
from aviatrix import Aviatrix if len(sys.argv) != 4: print('usage: %s <HOST> <USER> <PASSWORD>\n' ' where\n' ' HOST Aviatrix Controller hostname or IP\n' ' USER Aviatrix Controller login username\n' ' PASSWORD Aviatrix Controller login password\n' % sys.argv[0]) sys.exit(1) controller_ip = sys.argv[1] username = sys.argv[2] password = sys.argv[3] #logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) controller = Aviatrix(controller_ip) controller.login(username, password) users = controller.list_vpn_users() for user in users: if user['attached']: controller.detach_vpn_user(user['vpc_id'], user['_id']) controller.attach_vpn_user( 'Aviatrix-vpc', # TODO: ELB (from controller UI) 'vpc-abcd0000', # TODO: VPC ID user['_id'], # username user['email'], # email None, 'TODO') # TODO