Skip to content
Advertisement

How do I use the Google Cloud APIs (directly or using the GCP PHP Client SDK) to List, Add, and Remove Users from a GCP project programmatically?

A brief summary: I have a PHP web application which uses the Google Cloud PHP Client SDK to integrate with Dialogflow. That’s working no problem. I have a need now to let my users access the GCP Dialogflow dashboard directly, and part of that means that they need to be able to add/remove the Dialogflow permissions to users from within my application. (For obvious reasons I don’t want to give them full project IAM permissions and access).

Apparently, permissions are handled by a separate “Google APIs Client Library for PHP”.

This appears to be the API for listing the existing project user policies: https://cloud.google.com/resource-manager/reference/rest/v1/projects/listOrgPolicies

I believe that this is the API for the add/remove users part, but it’s not that obvious: https://cloud.google.com/resource-manager/reference/rest/v1/projects/setIamPolicy

===

The problem:

However, when I run the example code provided in the above referenced listOrgPolicies API, I get this error:

JavaScript

I tried both composer require google/apiclient-services and composer require google/apiclient-services:dev-master. And got the same result.

===

My questions are:

Are these even the correct APIs for the task?

Where can I find the SDK libraries for these actions? (or alternatively, if anyone has suggestions on how to perform these tasks directly to the API using PHP cURL without the SDK)

I’d prefer if all the action happens in PHP, but if you have a working solution using some other method or language or console command line, as long as it can be executed by my application I can work with that.

Thanks in advance!

Advertisement

Answer

Before getting to the answer, please make sure the Resource Manager API is enabled. This is necessary for the REST approach and the programmatic one.

To modify the IAM bindings for a user in a given project there are two endpoints that need to be called, which are documented here. Those would be:

  1. To get the current IAM configuration: https://cloud.google.com/resource-manager/reference/rest/v1/projects/getIamPolicy
  2. To set the new IAM policy: https://cloud.google.com/resource-manager/reference/rest/v1/projects/setIamPolicy

In between steps 1 and 2 it would be needed to modify the json returned by the first endpoint either adding the user with the role of your choice or removing the user entry from it. To see what’s the json structure required to add/remove users I would suggest examining the link shared above as well as examining the payload returned by the first command.

While directly calling the REST api is one option at your disposal you may also use the gcloud CLI to perform such operations. As it seems users are going to be added/removed one by one the command line would be a simple choice and it’s the one requiring less setup. The commands for both actions would be:

JavaScript

The third option would be to do it programmatically. Although I would like to provide a PHP sample I don’t have enough experience with the language, nonetheless below is a Python script that allows to add/remove roles. It has been bootstrapped from the Quickstart with just some minor changes and from the tests I ran it should work fine.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement