I am very new to gcloud command line and new to scripting altogether. I'm cleaning up a GCP org with multiple stray projects. I am trying to run a gcloud command to find the creator of all my projects so I can reach out to each project creator and ask them to clean up a few things.
I found a command to search logs for a project and find the original project creator, provided the project isn't older than 400 days.
gcloud logging read --project [PROJECT]
--order=asc --limit=1
--format='table(protoPayload.methodName, protoPayload.authenticationInfo.principalEmail)'
My problem is this: I have over 300 projects in my org currently. I have a .csv of all project names and IDs via (gcloud projects list).
Using the above command. How can I make [project] a variable and call/import the project name field from my .csv as the variable.
What I hope to accomplish is this: The gcloud command line provided the output for each project name in the .csv file and outputs it all to a another .csv file. I hope this all made sense.
Thanks.
I haven't tried anything yet. I don't want to run the same command for each of the 300 projects manually.
I think we are solidly in the domain of "BASH script writing in Linux". At the highest level, what you want to do is loop through each of the rows in your CSV, parse out the column you are interested in, put that in a BASH variable and then reference the variable in your data. Here is a recipe on parsing CSV in BASH. However, I'm tempted to suggest a different strategy. In your gcloud command where you used --format="table(...)", consider outputing JSON such as --format="json". The reason that helps us is that JSON is (in my opinion) easier to parse using the 'jq' command.
Thanks so much kolban. I apologize if I posted my question in the wrong "domain". Unfortunately, I am VERY new to scripting as a whole and have no prior experience with BASH script writing in Linux. I was hoping there is a solution to my use case using native gcp gcloud.
I don't have an immediate answer, but one thought might be to create a new project ... say XYZ. Then we can look in the logs and find the log entry that is written when a new project is created. Ideally, that record will also contain identity information of the principal that ran the project creation command. Now that we know what an examplar log entry looks like, maybe we could write a gcloud logging read with a filter that would only return those kinds of records. That would then seem to say that we would end up with a list of the identities that created the projects.
Another thought would be to run a gcloud command that gives us a list of all the projects in your organization. From there, for each project, we could now run a gcloud command to retrieve the project IAM policy. In theory, we could then look for principals which have Owner role and any of these should be able to affect cleanups.