<commit_before># VIP swap test script
# requires 2 load balancers, in the same resource group, with public IP addresses
# can be used to swap a staging scale application into production

import azurerm
import argparse
import json
import sys
from haikunator import Haikunator # used to create a random ip name for the floating ip
import time

def handle_bad_update(operation, ret):
    print("Error " + operation)
    print('Return code: ' + str(ret.status_code) + ' Error: ' + ret.text)
    sys.exit(1)


def main():
    # create parser
    argParser = argparse.ArgumentParser()

    # arguments: resource group lb name 1, 2
    argParser.add_argument('--resourcegroup', '-r', required=True, dest='resource_group', action='store', help='Resource group name')
    argParser.add_argument('--lb1', '-1', required=True, action='store', help='Load balancer 1 name')
    argParser.add_argument('--lb2', '-2', required=True, action='store', help='Load balancer 2 name')

    argParser.add_argument('--verbose', '-v', action='store_true', default=False, help='Show additional information')
    argParser.add_argument('-y', dest='noprompt', action='store_true', default=False, help='Do not prompt for confirmation')

    args = argParser.parse_args()

    verbose = args.verbose  # print extra status information when True

    resource_group = args.resource_group
    lb1 = args.lb1
    lb2 = args.lb2

    # Load Azure app defaults
    try:
        with open('azurermconfig.json') as configFile:
            configdata = json.load(configFile)
    except FileNotFoundError:
        print("Error: Expecting lbconfig.json in current folder")
        sys.exit()

    tenant_id = configdata['tenantId']
    app_id = configdata['appId']
    app_secret = configdata['appSecret']
    subscription_id = configdata['subscriptionId']

    access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)

    # figure out location of resource group and use that for the float ip
    rg = azurerm.get_resource_group(access_token, subscription_id, resource_group)
    location = rg['location']

    # Create a spare public IP address
    ip_name = Haikunator.haikunate(delimiter='')
    dns_label = ip_name + 'dns'
    ip_ret = azurerm.create_public_ip(access_token, subscription_id, resource_group, ip_name, dns_label, location)
    floatip_id = ip_ret.json()['id']
    print('Float ip id = ' + floatip_id)
    
    # 1. Get lb 2
    print('Getting load balancer: ' + lb2)
    lbmodel2 = azurerm.get_load_balancer(access_token, subscription_id, resource_group, lb2)
    lb2_ip_id = lbmodel2['properties']['frontendIPConfigurations'][0]['properties']['publicIPAddress']['id']
    print(lb2 + ' ip id: ' + lb2_ip_id)
    if verbose == True:
        print(lb2 + ' model:')
        print(json.dumps(lbmodel2, sort_keys=False, indent=2, separators=(',', ': ')))

    # 2. Assign new ip to lb 2
    print('Updating ' + lb2 + ' ip to float id')<commit_msg>Fix Haikunator in vip_swap example<commit_after># VIP swap test script
# requires 2 load balancers, in the same resource group, with public IP addresses
# can be used to swap a staging scale application into production

import azurerm
import argparse
import json
import sys
from haikunator import Haikunator # used to create a random ip name for the floating ip
import time

def handle_bad_update(operation, ret):
    print("Error " + operation)
    print('Return code: ' + str(ret.status_code) + ' Error: ' + ret.text)
    sys.exit(1)


def main():
    # create parser
    argParser = argparse.ArgumentParser()

    # arguments: resource group lb name 1, 2
    argParser.add_argument('--resourcegroup', '-r', required=True, dest='resource_group', action='store', help='Resource group name')
    argParser.add_argument('--lb1', '-1', required=True, action='store', help='Load balancer 1 name')
    argParser.add_argument('--lb2', '-2', required=True, action='store', help='Load balancer 2 name')

    argParser.add_argument('--verbose', '-v', action='store_true', default=False, help='Show additional information')
    argParser.add_argument('-y', dest='noprompt', action='store_true', default=False, help='Do not prompt for confirmation')

    args = argParser.parse_args()

    verbose = args.verbose  # print extra status information when True

    resource_group = args.resource_group
    lb1 = args.lb1
    lb2 = args.lb2

    # Load Azure app defaults
    try:
        with open('azurermconfig.json') as configFile:
            configdata = json.load(configFile)
    except FileNotFoundError:
        print("Error: Expecting lbconfig.json in current folder")
        sys.exit()

    tenant_id = configdata['tenantId']
    app_id = configdata['appId']
    app_secret = configdata['appSecret']
    subscription_id = configdata['subscriptionId']

    access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)

    # figure out location of resource group and use that for the float ip
    rg = azurerm.get_resource_group(access_token, subscription_id, resource_group)
    location = rg['location']

    # Create a spare public IP address
    ip_name = Haikunator().haikunate(delimiter='')
    dns_label = ip_name + 'dns'
    ip_ret = azurerm.create_public_ip(access_token, subscription_id, resource_group, ip_name, dns_label, location)
    floatip_id = ip_ret.json()['id']
    print('Float ip id = ' + floatip_id)
    
    # 1. Get lb 2
    print('Getting load balancer: ' + lb2)
    lbmodel2 = azurerm.get_load_balancer(access_token, subscription_id, resource_group, lb2)
    lb2_ip_id = lbmodel2['properties']['frontendIPConfigurations'][0]['properties']['publicIPAddress']['id']
    print(lb2 + ' ip id: ' + lb2_ip_id)
    if verbose == True:
        print(lb2 + ' model:')
        print(json.dumps(lbmodel2, sort_keys=False, indent=2, separators=(',', ': ')))

    # 2. Assign new ip to lb 2
    print('Updating ' + lb2 + ' ip to float id')
{'fraction_non_alphanumeric': 0.11806615776081425, 'fraction_numerical': 0.010178117048346057, 'mean_word_length': 4.026427962489343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2807467', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1802, 'n_words': 503}