-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
60f2a2b
0a34be5
7819113
6fd7142
fac2a1d
cba79af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
||
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) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming the
Suggested change
|
||||||||||
|
||||||||||
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<>(); | ||||||||||
|
||||||||||
|
@@ -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; | ||||||||||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming the
Suggested change
|
||||||||||
// The role to be granted. | ||||||||||
String role = "roles/logging.logWriter"; | ||||||||||
|
||||||||||
|
@@ -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; | ||||||||||
|
@@ -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); | ||||||||||
} | ||||||||||
} | ||||||||||
|
@@ -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. | ||||||||||
|
@@ -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)) { | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider renaming the
Suggested change
|
||||||||||
|
||||||||||
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(); | ||||||||||
|
@@ -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); | ||||||||||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
// Adding all remaining members to create new binding | ||||||||||
// Adding all remaining principals to create new binding | ||||||||||
Binding newBinding = binding.toBuilder() | ||||||||||
.clearMembers() | ||||||||||
.addAllMembers(newMemberList) | ||||||||||
|
@@ -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()); | ||||||||||
iennae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
return updatedPolicy; | ||||||||||
} | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better consistency with the updated terminology, consider renaming the
members
variable toprincipals
. 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.