Bash Script for GCP to fetch data | C2C Community
Question

Bash Script for GCP to fetch data

  • 7 January 2022
  • 7 replies
  • 198 views

Userlevel 1

script to fetch the GCP project details as mention below.

projectid, name, projectnumber, date&time, folderpath, projectowner, folderpath, costcentre, label.

I have written belwo script and it is working for- projectid, name, projectnumber, date&time.

#!/bin/bash

> project_data.csv

gcloud projects list --format="value(project_id, name, project_number, project_owner, tag, createTime.date(tz=LOCAL))" >whole_project_details.txt

echo project_id, project_name, project_number, project_owner, project_tag, project_time >project_data.csv

while read project_details

do

project_id=`echo $project_details | awk '{print $1}'`

project_name=`echo  $project_details | awk '{print $2}'`

project_number=`echo $project_details | awk '{print $3}'`

project_time=`echo $project_details | awk '{print $4}'`

project_owner=`echo $project_details | awk '{print $5}'`

project_tag=`echo $project_details | awk '{print $6}'`

echo $project_id, $project_name, $project_number, $project_owner, $project_tag, $project_time  >> project_data.csv

done < whole_project_details.txt

 

But not able to fetch date of - owner, folderpath, label, costcentre.

 

please help me. Thank you in advance.


7 replies

Userlevel 3
Badge +3

From the documentation : 

 

To refer to a list of fields you can sort, filter, and format by for each resource, you can run a list command with the format set to text or json. For example, $ gcloud compute instances list --limit=1 --format=text.

 

In your case, try gcloud projects list --format=text to confirm all the information you are after is available for the project resource. 

 

 

Userlevel 1

Thank you so much for suggestion, But I am able to fetch -projectId, Name, ProjectNumber, date and time. but do not know how to fetch - owner, folderpath, costcenter, environment details.

Please suggest me if any idea.

Userlevel 3
Badge +3

check 

gcloud organizations 

gcloud resource-manager 

 

comand options

Userlevel 7
Badge +56

Hi @vikash kumar vikal! Did @sathyaram_s’s answers help you?

Userlevel 1

@ilias it does not work in my case..I am expecting data in csv file, as attached below.

 

Able to fetch data - projectid,nane, project number, date and time, but rest all came blank.

Userlevel 7
Badge +56

Have you checked @boredabdel’s answer on your other post?

Also have you checked this link?

Userlevel 1

#!/bin/bash

> project_data.csv

gcloud projects list --format="value(project_id, name, project_number, project_owner, tag, createTime.date(tz=LOCAL))" >whole_project_details.txt

echo project_id, project_name, project_number, project_owner, project_tag, project_time >project_data.csv

while read project_details

do

project_id=`echo $project_details | awk '{print $1}'`

project_name=`echo  $project_details | awk '{print $2}'`

project_number=`echo $project_details | awk '{print $3}'`

project_time=`echo $project_details | awk '{print $4}'`

project_owner=`gcloud projects get-iam-policy  ${project_id} --flatten="bindings[].members[]" --filter="bindings.role=roles/owner" --format="value(bindings.members)" `

project_tag=`echo $project_details | awk '{print $6}'`

echo $project_id, $project_name, $project_number, $project_owner, $project_tag, $project_time  >> project_data.csv

done < whole_project_details.txt

Reply