Ganesh Jagdale
Ganesh Jagdale

Reputation: 51

GCP API for getting list of labels

Is there an GCP API for getting labels for resources? I want to get a list of all labels for GCP Project. How to get all labels in GCP? Basically GCP Resource Manager API return the Lebel object for every resource. But I want to get list of all labels using one API call.

Upvotes: 4

Views: 4126

Answers (3)

JuanToroMarty
JuanToroMarty

Reputation: 516

You could list for all the resources you want and filter what you need as @Bumeranghc said with the proper --format like (with jq for further manipulation): gcloud projects list --format=json | jq -r '.[].labels'

Without jq: gcloud projects list —-format=“json(projectId, labels)”

Upvotes: 3

Bumeranghc
Bumeranghc

Reputation: 61

I tried the next workaround to get all the labels:

gcloud projects describe <YOUR-PROJECT-NAME> --format=json | jq -r '.labels|to_entries|map("\(.key)=\(.value|tostring)")|.[]'

You will receive a key=value list.

Upvotes: 3

PjoterS
PjoterS

Reputation: 14112

Currently it's not possible to list all labels within a GCP project.

Beside what you mention about GCP Resource Manager API, you could get some information from Asset Inventory, however it won't fulfill your requirements.

To achieve what you want, you can fill a Feature Request in GCP IssueTracker to create this possibility.

Upvotes: 0

Related Questions