use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest in project java-docs-samples by GoogleCloudPlatform.
the class RemoveMember method removeMember.
// Removes member from a role; removes binding if binding contains 0 members.
public static void removeMember(Policy policy) {
// policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
String role = "roles/existing-role";
String member = "user:member-to-remove@example.com";
List<Binding> bindings = policy.getBindings();
Binding binding = null;
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
binding = b;
}
}
if (binding.getMembers().contains(member)) {
binding.getMembers().remove(member);
System.out.println("Member " + member + " removed from " + role);
if (binding.getMembers().isEmpty()) {
policy.getBindings().remove(binding);
}
return;
}
System.out.println("Role not found in policy; member not removed");
return;
}
use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest in project java-docs-samples by GoogleCloudPlatform.
the class SetPolicy method setPolicy.
// Sets a project's policy.
public static void setPolicy(Policy policy, String projectId) {
// policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
// projectId = "my-project-id"
CloudResourceManager service = null;
try {
service = createCloudResourceManagerService();
} catch (IOException | GeneralSecurityException e) {
System.out.println("Unable to initialize service: \n" + e.toString());
return;
}
try {
SetIamPolicyRequest request = new SetIamPolicyRequest();
request.setPolicy(policy);
Policy response = service.projects().setIamPolicy(projectId, request).execute();
System.out.println("Policy set: " + response.toString());
} catch (IOException e) {
System.out.println("Unable to set policy: \n" + e.toString());
}
}
use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest in project java-docs-samples by GoogleCloudPlatform.
the class GetPolicy method getPolicy.
// Gets a project's policy.
public static Policy getPolicy(String projectId) {
// projectId = "my-project-id"
Policy policy = null;
CloudResourceManager service = null;
try {
service = createCloudResourceManagerService();
} catch (IOException | GeneralSecurityException e) {
System.out.println("Unable to initialize service: \n" + e.toString());
return policy;
}
try {
GetIamPolicyRequest request = new GetIamPolicyRequest();
policy = service.projects().getIamPolicy(projectId, request).execute();
System.out.println("Policy retrieved: " + policy.toString());
return policy;
} catch (IOException e) {
System.out.println("Unable to get policy: \n" + e.toString());
return policy;
}
}
use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method getPolicy.
public static Policy getPolicy(CloudResourceManager crmService, String projectId) {
// Gets the project's policy by calling the
// Cloud Resource Manager Projects API.
Policy policy = null;
try {
GetIamPolicyRequest request = new GetIamPolicyRequest();
policy = crmService.projects().getIamPolicy(projectId, request).execute();
} catch (IOException e) {
System.out.println("Unable to get policy: \n" + e.getMessage() + e.getStackTrace());
}
return policy;
}
use of com.google.api.services.cloudresourcemanager.v3.model.GetIamPolicyRequest in project java-docs-samples by GoogleCloudPlatform.
the class AddBinding method addBinding.
// Adds a member to a role with no previous members.
public static void addBinding(Policy policy) {
// policy = service.Projects.GetIAmPolicy(new GetIamPolicyRequest(), your-project-id).Execute();
String role = "roles/role-to-add";
List<String> members = new ArrayList<String>();
members.add("user:member-to-add@example.com");
Binding binding = new Binding();
binding.setRole(role);
binding.setMembers(members);
policy.getBindings().add(binding);
System.out.println("Added binding: " + binding.toString());
}
Aggregations