Automatically Rename Remote Worker Agents based on the employee’s HR profile

I wanted to share a useful script for those of you managing more than 100 remote worker agents on your NetBeez BeezKeeper. This script was developed from a customer request who manages over 10000 remote worker agents. They needed to update the agent names with the employee’s first name, last name, and office number.

The remote worker agent software reports the employee’s ID, which is the same as the laptop’s name, to the NetBeez BeezKeeper server. This script, which runs on AWS Lambda, periodically checks for new agents with a regular expression that matches the employee’s ID format, using the NetBeez API. Once it finds the new agents, it cross-references their ID with the employee’s profile on the HR software and generates a string with the ID, first name, last name, and office number for each agent. Finally, the updated names get submitted back to the NetBeez server API.

Here’s a diagram of the workflow:

To implement this functionality, you will need to set up your script for NetBeez BeezKeeper API access. You can use the following code to initialize everything needed. The script is in Python and should go into your Lambda function code:

## CHANGE THIS TO YOUR INSTANCE FQDN
beezkeeper_fqdn="my-beezkeeper.netbeezcloud.net"
baseUrl='https://' + beezkeeper_fqdn

# CHANGE THIS TO YOUR KEY
apiKey="XXXXXXXXXXXXXXXXXXXXXX"

import requests
import urllib3
import json

urllib3.disable_warnings()

nbHeaders = {
    'Cache-Control': 'no-store',
    'Content-type': 'application/json',
    'Authorization': 'Bearer ' + apiKey
}

Next, you’ll need to implement the following functions in your script:

  1. getNBAgents(): This function retrieves the agents that only have the employee ID in their name using the NetBeez API.
def getNBAgents():
    payload = ''
    url = baseUrl + '/agents?filter[name][regex]=id[0-9]{7}
&stubbed=true&page[offset]=1&page[limit]=1000&type=beta'

response = requests.request("GET", url, headers=nbHeaders, verify=False)
return response.json()['data']
print(getNBAgents())

Example output:

[{
  'id': '395',
  'type': 'agent_stub',
  'attributes': {
    'description': None,
    'agent_class': 'mac',
    'active': True,
    'category': 'remote_worker_agent',
    'agent_type_name': 'mac',
    'open_incident_id': None,
    'reached_target_test_limit': False,
    'reached_scheduled_test_limit': None,
    'name': 'id5647382'
  }
}]
  1. getEmployeeName(employeeId): This function performs an HR database lookup. For the sake of demonstration, we use a public API that generates random names. However, you should check your software’s documentation for information on how to get employee names from your HR software.
def getEmployeeName(employeeId):
    url = 'https://namey.muffinlabs.com/name.json?count=1&with_surname=true&frequency=common&employee_id=' + employeeId
    response = requests.get(url)
    return response.json()[0]

print(getEmployeeName('id1234567'))

Example output:

John Evans
  1. generatePayloadFromAgents(agents): This function generates an array of objects with agent IDs and names that will be used as the data pushed back to the NetBeez server to update the agent record names.
def generatePayloadFromAgents(agents):
    agentsToUpdate = []
    for agent in agents:
        employeeName = getEmployeeName(agent['attributes']['name'])
        agentId = int(agent['id'])
        newAgentName = agent['attributes']['name'] + ' - ' + employeeName
        agentsToUpdate.append({ 'id': agentId, 'name': newAgentName})
    payload = { 'agents': agentsToUpdate }
    return payload

print(generatePayloadFromAgents(getNBAgents()))

Example output:

{'agents': [{'id': 395, 'name': 'id5647382 - Kimberly Green'}]}
  1. pushNBAgentsWithEmployeeNames(newAgentNamesPayload): This function performs a PUT request back to the NetBeez API with the output from the generatePayloadFromAgents() function.
def pushNBAgentsWithEmployeeNames(newAgentNamesPayload):
    url = baseUrl + '/agents?type=beta'
    response = requests.request('PUT', url, headers=nbHeaders, data=json.dumps(newAgentNamesPayload), verify=False)
    return response

Then to glue it all together you run this in your Lambda function:

def lambda_handler(event, context):
    pushNBAgentsWithEmployeeNames(generatePayloadFromAgents(getNBAgents()))


if __name__ == '__main__':
    lambda_handler('', '')

That’s it! If you have any questions or need further assistance, feel free to ask.

Here are some references to the documentation: https://api.netbeez.net

1 Like