Skip to content

Instantly share code, notes, and snippets.

@JacobCallahan
Last active February 4, 2021 16:27
Show Gist options
  • Save JacobCallahan/1c948ca2466aaeb5de395743f1555e19 to your computer and use it in GitHub Desktop.
Save JacobCallahan/1c948ca2466aaeb5de395743f1555e19 to your computer and use it in GitHub Desktop.
generate virt-who esx json data
import argparse
import json
import uuid
def gen_json(hypervisors, guests):
virtwho = {}
hypervisor_list = []
for i in range(hypervisors):
guest_list = []
for c in range(guests):
guest_list.append(
{
"guestId": str(uuid.uuid4()),
"state": 1,
"attributes": {"active": 1, "virtWhoType": "esx"},
}
)
_id = str(uuid.uuid4()).replace("-", ".")
hypervisor_list.append({
"hypervisorId": {"hypervisorId": _id},
"name": str(uuid.uuid4()).replace("-", "."),
"guestIds": guest_list,
"facts": {
"hypervisor.type": "VMware ESXi",
"dmi.system.uuid": _id,
"cpu.cpu_socket(s)": "2",
}
})
return {"hypervisors": hypervisor_list}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"hypervisors", type=int, help="The number of hypervisors to create."
)
parser.add_argument(
"guests", type=int, help="The number of guests per hypervisor to create."
)
parser.add_argument("path", type=str, help="The file path to save the output to.")
args = parser.parse_args()
data = gen_json(args.hypervisors, args.guests)
with open(args.path, "w") as f:
json.dump(data, f)
@JacobCallahan
Copy link
Author

Usage examples

# python2.7 genvirt.py -h
usage: genvirt.py [-h] hypervisors guests path

positional arguments:
  hypervisors  The number of hypervisors to create.
  guests       The number of guests per hypervisor to create.
  path         The file path to save the output to.

optional arguments:
  -h, --help   show this help message and exit

# python2.7 genvirt.py 500 30 test1.json
# python2.7 genvirt.py 500 30 test2.json
# python2.7 genvirt.py 500 30 test3.json
# ll
-rw-r--r--. 1 root root 1.7M Nov 17 03:31 test1.json
-rw-r--r--. 1 root root 1.7M Nov 17 03:31 test2.json
-rw-r--r--. 1 root root 1.7M Nov 17 03:31 test3.json

@JacobCallahan
Copy link
Author

To submit this data to satellite you can use curl.

curl -k -X POST --cert /etc/pki/consumer/cert.pem --key /etc/pki/consumer/key.pem https://<sathost>/rhsm/hypervisors -H "Content-Type: application/json" -d @"test1.json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment