0

I use govc to create resources in VMware. Before creating a new resource pool I want to verify if a resource pool exists with the same name. Running govc pool.info on an existing pool returns information about the existing pool and the command exits with a value of 0.

Executing the same command on a non existing resource pool returns nothing, but also exits with a value of 0.

➜  govc pool.info existing-pool
Name:               existing-pool
  Path:             /TEST/host/server/Resources/existing-pool
  CPU Usage:        0MHz (0.0%)
  CPU Shares:       normal
  CPU Reservation:  0MHz (expandable=true)
  CPU Limit:        -1MHz
  Mem Usage:        0MB (0.0%)
  Mem Shares:       normal
  Mem Reservation:  0MB (expandable=true)
  Mem Limit:        -1MB
➜  echo $?                       
0
➜  govc pool.info test
➜  echo $?
0

How do you verify existing resource pools?

Erik
  • 133
  • 15

2 Answers2

0

As pool.info does not handle exit status if no pool exists I handle the error in pool.create instead.

➜  govc pool.create test
govc: ServerFaultCode: The name 'test' already exists.
➜  echo $?                             
1

When exit code != 0, handle error and alert user.

Erik
  • 133
  • 15
0

You can use the -json option: govc pool.info -json=true PATH

Pipe the output to jq and check for an empty ResourcePools list/array.

A non-existent resource pool returns a JSON object that has a null ResourcePools array: govc pool.info -json=true /MYDC/host/MYCLUSTER/Resources/foo/bar-pool-1 { "ResourcePools": null }

So something like this, where path is an input var:

pools=$(govc pool.info -json=true $path | jq '.ResourcePools | length')

if [ $pools -eq 0 ]; then govc pool.create $path else echo "Resource pool: $path already exists" fi