Reputation: 65
I'm looking to put together a script that I can run from Cloud Shell to enable an API in all projects. It is successfully going through each project, but I am getting a permission denied message for every one. I am the owner so there shouldn't be any permission issues.
As a permission test, if I run just "gcloud services enable cloudresourcemanager.googleapis.com", the API successfully enables.
What am I missing?
#!/bin/bash
for project in $(gcloud projects list --format="value(projectId)")
do
echo "ProjectId: $project"
for enableapi in $(gcloud services enable cloudresourcemanager.googleapis.com list --project $project --format=list)
do
echo " -> Enabled $enableapi"
done
done
Upvotes: 6
Views: 14660
Reputation: 14669
You do not have to set the project config if you use the --project flag to enable a service. The real problem is that you want to enable multiple services in your bash script, including a service called "list", which does not exists. Here is how to properly enable multiple api at the same time:
#!/bin/bash
# Make sure this is a valid bash array!
services=(service1 service2 service3)
# Option 1
for project in $(gcloud projects list --format="value(projectId)")
do
gcloud services enable $services --project $project
done
# Option 2
for project in $(gcloud projects list --format="value(projectId)")
do
for service in $services
do
gcloud services enable $service --project $project
done
done
See this documentation.
Upvotes: 0
Reputation: 1301
Lucas, this way could work:
#!/bin/bash
for project in $(gcloud projects list --format="value(projectId)")
do
echo "ProjectId: $project"
gcloud config set project $project
gcloud services enable cloudresourcemanager.googleapis.com --project $project
done
I'm following this doc.
Upvotes: 5