From d2037abd48c9061b57134dcdc9489cf6e76b26b6 Mon Sep 17 00:00:00 2001 From: Irwin Kennedy Date: Fri, 28 Jan 2022 12:50:51 +0000 Subject: [PATCH 1/4] RAIN-27045 Script to generate a GCP resource count and breakdown by asset-type. --- resource_management/gcp/README.md | 65 +++++++++++++++++++ .../gcp/gcp_asset_breakdown.sh | 53 +++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 resource_management/gcp/README.md create mode 100755 resource_management/gcp/gcp_asset_breakdown.sh diff --git a/resource_management/gcp/README.md b/resource_management/gcp/README.md new file mode 100644 index 0000000..a1a45f5 --- /dev/null +++ b/resource_management/gcp/README.md @@ -0,0 +1,65 @@ +# Overview + +BASH script to produce a GCP resource breakdown and total resource count. +It examines every project in the current org. that the executing user has access to (see Roles below). +If a project request receives permission denied, an error will be displayed on screen, but the remaining projects will still be examined. + +# Pre-requisites + +A Unix like shell (MacOS/Linux) with gcloud, sed, bc utilities installed is the required execution environment. +We recommend using the [gcp cloud shell](https://console.cloud.google.com/home/dashboard?cloudshell=true) - it has all the dependencies. + +# Roles + +The user executing the script must have roles/cloudassset.viewer and roles/serviceusage.serviceUsageConsumer on the parent of the resources to be examined. + +### A) We recommend granting at the org level: + +`gcloud organizations add-iam-policy-binding TARGET_ORGANIZATION_ID \ + --member user:USER_ACCOUNT_EMAIL \ + --role roles/cloudasset.viewer` + +`gcloud organizations add-iam-policy-binding TARGET_ORGANIZATION_ID \ + --member user:USER_ACCOUNT_EMAIL \ + --role roles/serviceusage.serviceUsageConsumer` + +### B) Alternative is granting for each project to be examined: + +`gcloud projects add-iam-policy-binding TARGET_PROJECT_ID \ + --member user:USER_ACCOUNT_EMAIL \ + --role roles/cloudasset.viewer` + +`gcloud projects add-iam-policy-binding TARGET_PROJECT_ID \ + --member user:USER_ACCOUNT_EMAIL \ + --role roles/serviceusage.serviceUsageConsumer` + +# API enablement + +Script requires access to cloudasset API. + +### A) We recommend granting at the org level: + +`gcloud --organization services enable cloudasset.googleapis.com` + +### B) Alternative is granting for each project to be examined: + +`gcloud --project services enable cloudasset.googleapis.com` + +# Usage + +1. Download the script: + +wget