GCLOUD: Getting the name of a snapshot list - google-cloud-platform

Question:
How do i get a Value from a list into a Variable (String) in Gcloud?
Wanted:
I need to get the name of the Snapshot. With a Powershell list i would just do:
$os_disk_snapshot= (gcloud compute snapshots list --limit=1 --filter="creationTimestamp.date('%Y-%m-%d', Z)='$date'" --filter="sourceDisk=$source_disk"").name
But this doesn't work.
The following just gives me a list with the parameters name,disk_size_gb, sourcedisk and status. But i only need the name.
gcloud compute snapshots list --limit=1 --filter="creationTimestamp.date('%Y-%m-%d', Z)='$date'" --filter="sourceDisk=$source_disk""
Greetings and thanks in advance :)

you can use --format "value(name)" in the command to get only name.
--format "value(name)"
Read more about gcloud Global Flags here

Related

GCP Data Catalog - BigQuery gcloud Command

How to change between enumerated values of a field in a Data Catalog Tag Attached to a BigQuery Dataset using gcloud command?
I am able to manually change the value, let us say the enumerated values are Happy and sad, I am able to swap between sad and Happy manually but, I want to execute it using gcloud command.
The basic syntax is:
gcloud beta data-catalog entries update [ENTRY_ID] --location=[LOCATION] --update-mask=[FIELD_NAME] --type=[TAG_TYPE] [FIELD_NAME]=[NEW_VALUE]
Where:
LOCATION is the location of the Data Catalog instance that contains the entry
TAG_TYPE is the type of the tag you want to update
NEW_VALUE is the new value you want to set for the field
FIELD_NAME is the name of the field you want to update
field named emotion from "sad" to "happy", you would use the following command:
gcloud beta data-catalog entries update [ENTRY_ID] --location=[LOCATION] --update-mask=emotion --type=[TAG_TYPE] emotion=happy

How to list "labels" and "in use by" along with instances in a project?

I am currently using the following piece of code to get instance list from a project (which seems to work ok):
gcloud compute instances list
--format="csv(name,description,machineType,status,zone)"
However, looking at the response body for instances.list, I found labels but couldnt find where "In Use By" values are listed. I've tried the following, but it didn't work.
gcloud compute instances list \
--format="csv(name,description,machineType,status,zone,items.labels.list())"
If it helps, I am looking for the values in red to be listed along with my instances.list output:
https://imgur.com/FFeDHoW
You can use the below commands to get the details using gcloud compute instances list --topic format:
gcloud compute instances list --format='csv(name,description,machineType,status,zone,labels,inUseBy,instanceTemplate.list())'
or
gcloud compute instances list --format='table(name,description,machineType,status,zone,labels,inUseBy,instanceTemplate.list())'
Sample Output:

GCP, is there a way to find which Asset-type can be labelled and which are not?

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.

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:
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.

gcloud command output formatting to use results in another gcloud command

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).