ERROR: (gcloud.services.enable) User does not have permission to access project (or it may not exist): The caller does not have permission
Asked Answered
C

2

6

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
Coraliecoraline answered 19/2, 2019 at 0:47 Comment(1)
You specified cloudresourcemanager.googleapis.com AND list as services. "list" is not a service!Forfeiture
H
5

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.

Headwork answered 19/2, 2019 at 6:48 Comment(0)
F
0

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.

Forfeiture answered 29/7, 2020 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.