'Add user for resource using gcloud iam tools not via the user interface?

I want to create a user via gcloud tools in terminal not using the user interface.

I can create a service account using gcloud tools like this: gcloud iam service-accounts create my-service-account --display-name="My Service Account" and give it permissions like this gcloud projects add-iam-policy-binding project-name --member="serviceAccount:[email protected]" --role="roles/editor"

I can't find the way to do this for a user instead of a service account. The closest I could find was to run: gcloud projects add-iam-policy-binding my-project --member="user:[email protected]" --role="roles/editor" which results in the error ERROR: Policy modification failed because the user does not exist. If I create the user manually in the user interface then adding roles works.

How do I create the user via gcloud? This is what it looks like in the user interface..

enter image description here enter image description here



Solution 1:[1]

Turns out gcloud projects add-iam-policy-binding my-project --member="user:[email protected]" --role="roles/editor" does 'add' the user.

Re: Kolban's comment. There might be a misunderstanding since I don't want to create a gmail user I just want to do the equivalent of adding them (+ADD button above) and type into the "new principals" textbox and add a role. Above code works.

Solution 2:[2]

There are many ways to read commands from a file. A lot depends on the format of commands and if they have parameters or not.

Here are possible solutions.

One command per row in a text file

Save in the file row by row the sequence of commands. Read the file row by row and check each row with a list of commands.

Pro:

  • Easy to implement

Cons:

  • Not easy to handle parameters
  • Difficult to handle blocks of commands
  • Difficult to handle jumps between commands

Commands saved as json objects

Hold the file as a text file having a single json array where each item holds a command, eventually with parameters.

Pro

  • Quite easy using libraries to parse json files
  • Easy to handle parameters

Cons

  • A list of commands as json array is less readable than a structured programming language

Create a parser and your own programming language

You can create your own programming language having only the details that you need.

Pro

  • This solution fit very well any need that you can have
  • Easy to read because you can decide the structure that you like more
  • Speed of code
  • Is possible to handle typical programming construct like loops, conditional statements, blocks of code...

Cons

  • Very hard to implement, you need to define your own language and implement it using a custom parser (example using ANTLR4)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 d3f2
Solution 2 Davide Lorenzo MARINO