def create_resource_group(location, resource_name, subscription): check_if_resource_group_exists(resource_name=resource_name) create_resouce_group_command = [ "group", "create", "-l", "{}".format(location), "-n", "{}RG".format(resource_name), "--tags", "Component={}".format(resource_name), "--subscription", "{}".format(subscription) ] execute_az_command(command_to_execute=create_resouce_group_command)
def connect_vnet_to_service_endpoint(resource_group, service_endpoint, subscription, vnet_name): service_endpoint_connector_command = [ "network", "vnet", "subnet", "update", "--resource-group", "{}".format(resource_group), "--name", "{}Subnet".format(vnet_name), "--vnet-name", "{}".format(vnet_name), "--subscription", "{}".format(subscription), "--service-endpoints", service_endpoint ] print(service_endpoint_connector_command) execute_az_command(command_to_execute=service_endpoint_connector_command)
def create_vnet_peering(resource_group, subscription, target_vnet, target_vnet_resource_id, vnet): vnet_peering_command = [ "network", "vnet", "peering", "create", "--name", "{}To{}".format(vnet, target_vnet), "--remote-vnet", "{}".format(target_vnet_resource_id), "--resource-group", "{}".format(resource_group), "--vnet-name", "{}".format(vnet), "--subscription", "{}".format(subscription) ] execute_az_command(command_to_execute=vnet_peering_command)
def create_vnet(ipv4_range, location, resource_group, service_endpoint, subnet_range, subscription, vnet_name): check_if_vnet_exists(vnet_name=vnet_name) create_vnet_command = [ "network", "vnet", "create", "--name", "{}".format(vnet_name), "--resource-group", "{}".format(resource_group), "--location", "{}".format(location), "--address-prefix", "{}".format(ipv4_range), "--subnet-name", "{}Subnet".format(vnet_name), "--subnet-prefix", "{}".format(subnet_range), "--tags", "Component={}".format(vnet_name), "--subscription", "{}".format(subscription) ] execute_az_command(command_to_execute=create_vnet_command) if service_endpoint: connect_vnet_to_service_endpoint(resource_group=resource_group, service_endpoint=service_endpoint, subscription=subscription, vnet_name=vnet_name)
def get_vnet_resource_id(resource_group, vnet_name): get_vnet_resource_id_command = [ "network", "vnet", "show", "--resource-group", "{}".format(resource_group), "--name", "{}".format(vnet_name) ] vnet_resource_id = execute_az_command( command_to_execute=get_vnet_resource_id_command) return vnet_resource_id['id']
def check_if_vnet_exists(vnet_name): vnet_list_command = ["network", "vnet", "list"] vnet_list = execute_az_command(command_to_execute=vnet_list_command) if vnet_list: for vnet_record in vnet_list: vnet_name_record = vnet_record['name'] if vnet_name_record == "{}".format(vnet_name): raise Exception( "VNET name {} already exists".format(vnet_name_record)) else: print("No VNets found, continuing...")
def check_if_resource_group_exists(resource_name): check_if_resource_group_exists_command = [ "group", "exists", "-n", "{}RG".format(resource_name) ] resource_group_exists = execute_az_command( command_to_execute=check_if_resource_group_exists_command) if resource_group_exists: raise Exception( "Resource Group {}RG Already Exists, Exiting...".format( resource_name))
def check_if_vnet_peering_exists(resource_group, subscription, target_vnet, vnet): vnet_peering_list_command = [ "network", "vnet", "peering", "list", "--resource-group", "{}".format(resource_group), "--vnet-name", "{}".format(vnet), "--subscription", "{}".format(subscription) ] vnet_peerings = execute_az_command( command_to_execute=vnet_peering_list_command) if vnet_peerings: for vnet_peering in vnet_peerings: vnet_peering_name = vnet_peering['name'] if vnet_peering_name == "{}To{}".format(vnet, target_vnet): raise Exception( "VNet Peering {} already exists".format(vnet_peering_name)) else: print("No VNet Peerings for {} found in {}".format( vnet, resource_group)) print("The VNet Peering is ready to be created")