0

gcloud supports scripting with --format option and gcloud services enable --async returns a command that could be used to wait till the operation is completed.

E.g. the following call gcloud services disable servicenetworking.googleapis.com --async may return smth like:

Asynchronous operation is in progress... Use the following command to wait for its completion:
 gcloud beta services operations wait operations/acf.<UID>

The problem here is that the output is returned as 2 rows and is not that easy to include into automated scripts. The first idea is to use the --format option with smth like --format=json and use jq afterward, but the --format=json just does nothing for the gcloud services enable/disable, it always returns [].

xSAVIKx
  • 463
  • 4
  • 12
  • I think that is because the returned commands are on beta, but you can try to merge all lines of the output like is explained on this [question](https://stackoverflow.com/a/8748084/11529321) – Jan Hernandez Jan 22 '20 at 18:53
  • It's not about the returned command, I believe. It's more about the command output in general. The `--async` flag is not a `beta` option. Thx for the link, I do believe I can figure out how to parse the output manually, but maybe someone from *gcloud* team is around. – xSAVIKx Jan 22 '20 at 19:13

1 Answers1

1

So, I found out that gcloud services enable/disable has no actual output, but instead, the output we receive with --async goes to the error stream.

So, I've created this small script that allows grabbing the operation ID from the output, store it in a file and then process in whatever way we want:

wait_operation_id_file="$(mktemp /tmp/enable_service_operation.XXXXXXX)"
gcloud services enable "servicenetworking.googleapis.com" --async 2>&1 \
  | grep 'gcloud beta services operations wait' \
  | sed 's/.*wait //' \
  >> "${wait_operation_id_file}"
wait_id="$(cat "${wait_operation_id_file}")"
gcloud services operations wait "${wait_id}"
rm --force "${wait_operation_id_file}"
xSAVIKx
  • 463
  • 4
  • 12