Search code examples
google-cloud-platformgcloud

What modifications do I need to make to my gcloud command to get the list of enabled services in all GCP projects in the below tabular format?


I have the following code block to enumerate the enabled services in all GCP projects:

for project in  $(gcloud projects list --format="value(projectId)"); \
do gcloud services list --project $project --format="table[box]($project,config.name,config.title)"; \
done;

It gives me the output in this format:

enter image description here

But I would like the output to be in this format:

enter image description here

How do I accomplish that? Please advise. Thanks.


Solution

  • You can't using gcloud because you need to assemble values from multiple commands: Projects.List and Services.List. gcloud --format applies only to the output from a single command.

    You're grabbing all the data you need (Project, Name and Title). I recommend you use bash and the characters output by gcloud to form the table, and synthesize the table output for yourself.

    Update

    Here's a script that'll get you CSV output that matches your rows:

    PROJECTS=$(\
      gcloud projects list \
      --format="value(projectId)")
    for PROJECT in ${PROJECTS}
    do
      CONFIGS=$(gcloud services list \
      --project=${PROJECT} \
      --format="csv[no-heading](config.name,config.title.encode(base64))")
      for CONFIG in ${CONFIGS}
      do
        IFS=, read NAME TITLE <<< ${CONFIG}
        TITLE=$(echo ${TITLE} | base64 --decode)
        echo "${PROJECT},${NAME},\"${TITLE}\""
      done
    done
    

    NOTE encode(base64) to avoid the unquote title values from being split. base64 --decode reverses this at output. It should be possible to use format but I don't understand the syntax. --format='csv[no-heading](config.name,format("\"{0}\"",config.title))' didn't work.

    Then, in the best *nix tradition, you can pipe that output into awk or columns or some other script that formats the CSV as a pretty-printed table. This is the more difficult challenge because you'll want to determine the longest value in each field in order to layout correctly. I'll leave that to you.