bsam
bsam

Reputation: 930

Using the Google Cloud Platform SDK CLI to List all Active Resources Under a Given Project

Is it possible to list, through the Google Cloud Platform (GCP) SDK CLI (gcloud), all active resources under a given GCP project?

Upvotes: 33

Views: 20600

Answers (3)

Fahad Khan
Fahad Khan

Reputation: 31

If you want to list the resources on basis of their state then you can use --filter= option and this will the active state resources

Use Case:- If you want to list all the projects with pending deletion state then you will use:

gcloud  projects  list --filter='lifecycleState:DELETE_REQUESTED'

Upvotes: 0

Circy
Circy

Reputation: 1194

You can use search-all-resources to search all the resources across services (or APIs) and projects for a given organization, folder, or project.

To search all the resources in a project with number 123:

$ gcloud asset search-all-resources --scope=projects/123

See the other post for more details: How to find, list, or search resources across services (APIs) and projects in Google Cloud Platform?

Upvotes: 29

DazWilkin
DazWilkin

Reputation: 40426

IIUC there's no general-purpose type for "things that live in projects" so you'd need to enumerate all the types (that interest you) specifically.

Also, some resources (e.g. keys) are owned by service accounts that are owned by projects.

for PROJECT in $(\
  gcloud projects list \
  --format="value(projectId)")
do
  echo "Project: ${PROJECT}"
  echo "Services"
  gcloud services list --project=${PROJECT}
  echo "Kubernetes Clusters"
  gcloud container clusters list --project=${PROJECT}
  echo "Compute Engine instances"
  gcloud compute instances list --project=${PROJECT}
  echo "Service Accounts"
  for ACCOUNT in $(\
    gcloud iam service-accounts list \
    --project=${PROJECT} \
    --format="value(email)")
  do
    echo "Service Account keys: ${ACCOUNT}"
    gcloud iam service-accounts keys list --iam-account=${ACCOUNT} --project=${PROJECT}
  done
done

Various challenges with this approach though:

  • Some enumerations may require more details (e.g. regions|zones)
  • You'd need to be exhaustive (it won't list what you don't request)
  • it gets nested|messy quickly
  • Some services prompt if they're not enabled (e.g. Compute Engine)

NB

  • You can apply --filter=... to each of the above commands
  • You could wrap the entire loop into one that enumerates gcloud auth list accounts

Upvotes: 6

Related Questions