Search code examples
google-cloud-platformgcloudgoogle-cloud-sdk

How to output every possible field for a resource using --format=table in gcloud


According to the documentation for the Cloud SDK - gcloud topic formats, it supplies an option --format that effectively controls the way your output looks.

I noticed, that when I use --format=json or --format=yaml with no additional arguments, gcloud produces an output that concludes every available field to be retrieved with this API, however, I was unable to figure out, how to reach the same kind of behavior with --table without the need of enumerating every single field of service.

Consider the following example:

gcloud app regions list --format=json --limit 1
[
  {
    "flexible": true,
    "region": "asia-east1",
    "search_api": false,
    "standard": true
  }
]

It shows an exhaustive enumeration of available fields for AppEngine region resource, yet when I want to output pretty much the same information, especially where I don't know exactly the full scope of fields or the exact naming, I ought to enumerate each of them individually like as follows.

gcloud app regions list \
--format="table[box,all-box,title='AppEngine APAC regions'](region,standard,flexible,search_api)" \
--limit 9
┌────────────────────────────────────────────────────────────────────────────────────┐
│                               AppEngine APAC regions                               │
├──────────────────────┬───────────────────┬───────────────────┬─────────────────────┤
│        REGION        │ SUPPORTS STANDARD │ SUPPORTS FLEXIBLE │ SUPPORTS GAE SEARCH │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-east1           │ YES               │ YES               │ NO                  │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-east2           │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-northeast1      │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-northeast2      │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-northeast3      │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-south1          │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-southeast1      │ YES               │ YES               │ NO                  │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ asia-southeast2      │ YES               │ YES               │ YES                 │
├──────────────────────┼───────────────────┼───────────────────┼─────────────────────┤
│ australia-southeast1 │ YES               │ YES               │ YES                 │

In summary, is there a simpler way to achieve mentioned behaviour with --table format something like this

gcloud app regions list --format="table" --limit 9 # Won't work

For a reference, here is an example using tabular view representation according to AWS table output format with no additional parameters being supplied

aws iam list-users --output table

Produces the following output

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                 ListUsers                                                                     |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
||                                                                                  Users                                                                      ||
|+----------------------------------------------------+---------------------------+---------------------------+----------+-----------------------+-------------+|
||                         Arn                        |       CreateDate          |    PasswordLastUsed       |   Path   |        UserId         |   UserName  ||
|+----------------------------------------------------+---------------------------+---------------------------+----------+-----------------------+-------------+|
||  arn:aws:iam::123456789012:user/Admin              | 2014-10-16T16:03:09+00:00 | 2016-06-03T18:37:29+00:00 | /        | AIDA1111111111EXAMPLE | Admin       ||
||  arn:aws:iam::123456789012:user/backup/backup-user | 2019-09-17T19:30:40+00:00 |                           | /backup/ | AIDA2222222222EXAMPLE | backup-user ||
||  arn:aws:iam::123456789012:user/cli-user           | 2019-09-17T19:11:39+00:00 |                           | /        | AIDA3333333333EXAMPLE | cli-user    ||
+---------------------------------------------------------------------------------------------------------------------------------------------------------------+

Solution

  • No.

    The documentation for --table explains "This format requires a projection to define the table columns".

    It's not an unreasonable request but I suspect the reason is that tables are better|best-suited to flat|non-hierarchical data. It's possible that nested types could be output as nested tables but this could become messy.

    What are you trying to achieve that you're unable to do as-is?

    You can determine the API calls that underlie any gcloud command by appending the flag --log-http.

    Then, using APIs Explorer, you can locate these methods e.g. App Engine Admin API, here's apps.locations.list and ... finally... the response type. In this case ListLocationsResponse.

    And, this type corresponds to the output of e.g. gcloud app regions list.