use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.
the class QuickstartTests method testQuickstart.
@Test
public void testQuickstart() throws Exception {
String member = "serviceAccount:" + serviceAccount.getEmail();
String role = "roles/logging.logWriter";
// Tests initializeService()
CloudResourceManager crmService = Quickstart.initializeService();
// Tests addBinding()
Quickstart.addBinding(crmService, "projects/" + PROJECT_ID, member, role);
// Get the project's polcy and confirm that the member is in the policy
Policy policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
Binding binding = null;
List<Binding> bindings = policy.getBindings();
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
assertThat(binding.getMembers(), hasItem(member));
// Tests removeMember()
Quickstart.removeMember(crmService, "projects/" + PROJECT_ID, member, role);
// Confirm that the member has been removed
policy = Quickstart.getPolicy(crmService, "projects/" + PROJECT_ID);
binding = null;
bindings = policy.getBindings();
for (Binding b : bindings) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
if (binding != null) {
assertThat(binding.getMembers(), not(hasItem(member)));
}
}
use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method setPolicy.
private static void setPolicy(CloudResourceManager crmService, String projectId, Policy policy) {
// Cloud Resource Manager Projects API.
try {
SetIamPolicyRequest request = new SetIamPolicyRequest();
request.setPolicy(policy);
crmService.projects().setIamPolicy(projectId, request).execute();
} catch (IOException e) {
System.out.println("Unable to set policy: \n" + e.getMessage() + e.getStackTrace());
}
}
use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method removeMember.
public static void removeMember(CloudResourceManager crmService, String projectId, String member, String role) {
// Gets the project's policy.
Policy policy = getPolicy(crmService, projectId);
// Removes the member from the role.
Binding binding = null;
for (Binding b : policy.getBindings()) {
if (b.getRole().equals(role)) {
binding = b;
break;
}
}
if (binding.getMembers().contains(member)) {
binding.getMembers().remove(member);
if (binding.getMembers().isEmpty()) {
policy.getBindings().remove(binding);
}
}
// Sets the updated policy.
setPolicy(crmService, projectId, policy);
}
use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager in project java-docs-samples by GoogleCloudPlatform.
the class TestPermissions method testPermissions.
// Tests if the caller has the listed permissions.
public static void testPermissions(String projectId) {
// 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;
}
List<String> permissionsList = Arrays.asList("resourcemanager.projects.get", "resourcemanager.projects.delete");
TestIamPermissionsRequest requestBody = new TestIamPermissionsRequest().setPermissions(permissionsList);
try {
TestIamPermissionsResponse testIamPermissionsResponse = service.projects().testIamPermissions(projectId, requestBody).execute();
System.out.println("Of the permissions listed in the request, the caller has the following: " + testIamPermissionsResponse.getPermissions().toString());
} catch (IOException e) {
System.out.println("Unable to test permissions: \n" + e.toString());
}
}
use of com.google.api.services.cloudresourcemanager.v3.CloudResourceManager 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());
}
}
Aggregations