Skip to content

docs(iam): Update comments and terminology in IAM samples #9887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions iam/snippets/src/main/java/AddBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public static void main(String[] args) {
Policy policy = Policy.newBuilder().build();
// TODO: Replace with your role.
String role = "roles/role-to-add";
// TODO: Replace with your members.
List<String> members = Collections.singletonList("user:[email protected]");
// TODO: Replace with your principals.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
List<String> members = Collections.singletonList("principal-id");
Comment on lines +30 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

For better consistency with the updated terminology, consider renaming the members variable to principals. This aligns the variable name with the concept of principals and makes the code easier to understand. This change adheres to the Google Java Style Guide's recommendation for clear and descriptive variable names.

Suggested change
// TODO: Replace with your principals.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
List<String> members = Collections.singletonList("principal-id");
List<String> principals = Collections.singletonList("principal-id");


addBinding(policy, role, members);
}

// Adds a member to a role.
// Adds a principals to a role.
public static Policy addBinding(Policy policy, String role, List<String> members) {
Binding binding = Binding.newBuilder()
.setRole(role)
Expand Down
11 changes: 6 additions & 5 deletions iam/snippets/src/main/java/AddMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ public static void main(String[] args) {
Policy policy = Policy.newBuilder().build();
// TODO: Replace with your role.
String role = "roles/existing-role";
// TODO: Replace with your member.
String member = "user:[email protected]";
// TODO: Replace with your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "principal-id";
Comment on lines +29 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Consider renaming the member variable to principal for consistency with the updated terminology. This aligns the variable name with the concept of principals and makes the code easier to understand. This change adheres to the Google Java Style Guide's recommendation for clear and descriptive variable names.

Suggested change
// TODO: Replace with your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "principal-id";
String principal = "principal-id";


addMember(policy, role, member);
}

// Adds a member to a pre-existing role.
// Adds a principal to a pre-existing role.
public static Policy addMember(Policy policy, String role, String member) {
List<Binding> newBindingsList = new ArrayList<>();

Expand All @@ -44,13 +45,13 @@ public static Policy addMember(Policy policy, String role, String member) {
}
}

// Update the policy to add the member.
// Update the policy to add the principal.
Policy updatedPolicy = policy.toBuilder()
.clearBindings()
.addAllBindings(newBindingsList)
.build();

System.out.println("Added member: " + updatedPolicy.getBindingsList());
System.out.println("Added principal: " + updatedPolicy.getBindingsList());

return updatedPolicy;
}
Expand Down
17 changes: 9 additions & 8 deletions iam/snippets/src/main/java/Quickstart.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public static void main(String[] args) throws IOException {
String projectId = "your-project";
// TODO: Replace with your service account name.
String serviceAccount = "your-service-account";
// TODO: Replace with the ID of your member in the form "user:[email protected]"
String member = "your-member";
// TODO: Replace with the ID of your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "your-principal";
Comment on lines +37 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Consider renaming the member variable to principal for consistency with the updated terminology. This aligns the variable name with the concept of principals and makes the code easier to understand. This change adheres to the Google Java Style Guide's recommendation for clear and descriptive variable names.

Suggested change
// TODO: Replace with the ID of your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "your-principal";
String principal = "your-principal";

// The role to be granted.
String role = "roles/logging.logWriter";

Expand All @@ -56,10 +57,10 @@ public static void quickstart(String projectId, String serviceAccount,
// This client only needs to be created once, and can be reused for multiple requests.
try (IAMClient iamClient = IAMClient.create()) {

// Grants your member the "Log writer" role for your project.
// Grants your principal the "Log writer" role for your project.
addBinding(iamClient, projectId, serviceAccount, member, role);

// Get the project's policy and print all members with the "Log Writer" role
// Get the project's policy and print all principals with the "Log Writer" role
Policy policy = getPolicy(iamClient, projectId, serviceAccount);

Binding binding = null;
Expand All @@ -73,14 +74,14 @@ public static void quickstart(String projectId, String serviceAccount,
}

System.out.println("Role: " + binding.getRole());
System.out.print("Members: ");
System.out.print("Principals: ");

for (String m : binding.getMembersList()) {
System.out.print("[" + m + "] ");
}
System.out.println();

// Removes member from the "Log writer" role.
// Removes principal from the "Log writer" role.
removeMember(iamClient, projectId, serviceAccount, member, role);
}
}
Expand All @@ -107,7 +108,7 @@ public static void addBinding(IAMClient iamClient, String projectId, String serv
}

if (binding != null) {
// If binding already exists, adds member to binding.
// If binding already exists, adds principal to binding.
binding.getMembersList().add(member);
} else {
// If binding does not exist, adds binding to policy.
Expand All @@ -127,7 +128,7 @@ public static void removeMember(IAMClient iamClient, String projectId, String se
// Gets the project's policy.
Policy.Builder policy = getPolicy(iamClient, projectId, serviceAccount).toBuilder();

// Removes the member from the role.
// Removes the principal from the role.
Binding binding = null;
for (Binding b : policy.getBindingsList()) {
if (b.getRole().equals(role)) {
Expand Down
15 changes: 8 additions & 7 deletions iam/snippets/src/main/java/RemoveMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public static void main(String[] args) throws IOException {
Policy policy = Policy.newBuilder().build();
// TODO: Replace with your role.
String role = "roles/existing-role";
// TODO: Replace with your member.
String member = "user:[email protected]";
// TODO: Replace with your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "principal-id";
Comment on lines +30 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Consider renaming the member variable to principal for consistency with the updated terminology. This aligns the variable name with the concept of principals and makes the code easier to understand. This change adheres to the Google Java Style Guide's recommendation for clear and descriptive variable names.

Suggested change
// TODO: Replace with your principal.
// For examples, see https://cloud.google.com/iam/docs/principal-identifiers
String member = "principal-id";
String principal = "principal-id";


removeMember(policy, role, member);
}

// Removes member from a role; removes binding if binding contains no members.
// Removes principal from a role; removes binding if binding contains no members.
public static Policy removeMember(Policy policy, String role, String member) {
// Creating new builder with all values copied from origin policy
Policy.Builder policyBuilder = policy.toBuilder();
Expand All @@ -49,12 +50,12 @@ public static Policy removeMember(Policy policy, String role, String member) {

if (binding != null && binding.getMembersList().contains(member)) {
List<String> newMemberList = new ArrayList<>(binding.getMembersList());
// Removing member from a role
// Removing principal from the role
newMemberList.remove(member);

System.out.println("Member " + member + " removed from " + role);

// Adding all remaining members to create new binding
// Adding all remaining principals to create new binding
Binding newBinding = binding.toBuilder()
.clearMembers()
.addAllMembers(newMemberList)
Expand All @@ -70,14 +71,14 @@ public static Policy removeMember(Policy policy, String role, String member) {
newBindingList.add(newBinding);
}

// Update the policy to remove the member.
// Update the policy to remove the principal.
policyBuilder.clearBindings()
.addAllBindings(newBindingList);
}

Policy updatedPolicy = policyBuilder.build();

System.out.println("Exising members: " + updatedPolicy.getBindingsList());
System.out.println("Exising principals: " + updatedPolicy.getBindingsList());

return updatedPolicy;
}
Expand Down
Loading