Any list command in gcloud shell is returning in an unexpected, harder to read format.
Example: From doc: https://cloud.google.com/blog/products/it-ops/filtering-and-formatting-fun-with
Let's start off by formatting a simple command that you are already
familiar with that lists the projects to which you have access:
gcloud projects list
PROJECT_ID NAME PROJECT_NUMBER
canvas-syntax-130823 scesproject2 346904393285
windy-bearing-129522 scesproject1 222844913538
When I try this I always get format:
PROJECT_ID: anvas-syntax-130823
NAME: scesproject2
PROJECT_NUMBER: 346904393285
PROJECT_ID: windy-bearing-129522
NAME: scesproject1
PROJECT_NUMBER: 222844913538
This format is harder to read and navigate especially for longer outputs.
Any idea why I am getting output in this format?
I tried with different instances, different accounts, it is not tied to any config I would have.
I know about the --format options, I would just like to understand why and where is the "tabular" default view gone?
The CLI has an option to make output easier for screen readers to process.
To disable that feature:
gcloud config set accessibility/screen_reader false
To enable that feature:
gcloud config set accessibility/screen_reader true
This link provides more information:
gcloud config set
Related
I need to find out which resources (Asset-Types) in entire GCP organization can be labelled.
In short, i do not want resources which doesn't have a column Label in the schema. Is there a way to find columns of every asset-type ? or any other way to extract only resources that have column/attribute Label?
gcloud asset search-all-resources --scope=organizations/Org-ID
--filter=-labels:* --format='csv(name, assetType, labels)' --sort-by=name > notLabels.csv
i use this command to get the resources but it returns also the resources that can't be labelled.
You can find the list of services that support labels in GCP in this documentation.
And you can filter it with the following format below as an example:
gcloud asset search-all-resources --filter labels.env:*
The above command lists the services that has env as key and anything that has value on it.
gcloud asset search-all-resources --filter=-labels.*
The second sample command above lists the resources with no labels value by adding - before the label parameter.
You can find more information on using filter searches using labels here.
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:
But I would like the output to be in this format:
How do I accomplish that? Please advise. Thanks.
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.
I'm trying to automate the deletion of SSL certificates that end with a certain text pattern on GCP projects.
For this I use the command:
gcloud compute ssl-certificates list --filter="name~'819$'" --format="(name)"
Which output displays exactly this format:
NAME
certname1-1602160819
certname2-1602160819
certname3-1602160819
...and so on
The thing is that if I want to use the results from this command to then use it to input another gcloud command that deletes each certificate, I get the first variable as NAME which is the field title and obviously not a certificate.
Here is my script:
#!/bin/bash
for oldcert in $( gcloud compute ssl-certificates list --filter="name~'819$'" --format="(NAME)")
do
gcloud compute ssl-certificates delete $oldcert
done
Do you know how I could get the field name NAME out of my output so I could treat each result in another command directly.
Thanks for your precious advices
#Hitobat thanks very much for your comment
I used the csv[no-heading] option even though the tails -n +2 otion also does the job
the following commands did the job great:
#!/bin/bash
for oldcert in $( gcloud compute ssl-certificates list --filter="name~'819$'" --format="csv[no-heading](name)")
do
gcloud compute ssl-certificates delete $oldcert --quiet
done
The right format to use is --format=value[](name).
According to the docs:
value
CSV with no heading and <TAB> separator instead of <COMMA>. Used
to retrieve individual resource values.
So it's equivalent to the --format="csv[no-heading](name) that you used, but "more correct" (and a little more legible).
Is there any command to list all the GCP project quota in a single excel file with only top headers. I tried to apply FOR loop on quota management however it gives me output with header included every time with new projects when appended.
gcloud compute project-info describe --flatten=quotas -- format='csv(quotas.metric,quotas.limit,quotas.usage)' will provide for one project. However require for all project on Org level and folder level in a single excel file.
I crafted this bash code that can help you in order to iterate all projects related with the account used with GCloud feel free to modify this code according your use case
#!/bin/bash
#unique header
echo "ProjectId,Metric,Quota,Usage"
gcloud projects list --format="csv[no-heading](projectId,name)" |\
while IFS="," read -r ID NAME
do
RESULT=$(\
gcloud compute project-info describe --project ${ID} \
--flatten=quotas \
--format="csv[no-heading](quotas.metric,quotas.limit,quotas.usage)")
# Prefix ${ID} to each line in the result
for LINE in ${RESULT}
do
echo ${ID},${LINE}
done
done
it is important that the account authenticated has the role project/viewer over all projects associated, also Compute Engine API must be enabled in the projects.
Having said that, you can create a service account associated per organization or by folder in order to get all necessary information.
In colab, if I try to execute a simple bigquery query without explicitly providing the project-id like this:
%%bigquery
SELECT 1 AS test
then I get the following error:
OSError: Project was not passed and could not be determined from the environment.
I realise I can mitigate this error by specifying the --project parameter in the %%bq magic:
%%bigquery --project my-project-id
SELECT 1 AS test
but the text of this error suggest that perhaps it's possible to specify the project-id by setting an environment variable instead. Is this possible?
I'd prefer to be able to set up the env var at the top of the notebook then not specify the project id each time. I tried a guess of GCP_PROJECT and googled around the issue but to no avail. I couldn't turn up any docs or answers as to which env var I can set for the projectid.
Use the GCLOUD_PROJECT like this:
%env GCLOUD_PROJECT=your_project_id
Or, if you want to type a bit longer
%env GOOGLE_CLOUD_PROJECT=your_project_id
I didn't find any document either. So, I dug up the code and found it here