/** * Returns the IAM access control policy for this project. Returns {@code null} if the resource * does not exist or if you do not have adequate permission to view the project or get the policy. * * @return the IAM policy for the project * @throws ResourceManagerException upon failure * @see <a href= * "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/getIamPolicy"> * Resource Manager getIamPolicy</a> */ public Policy getPolicy() { return resourceManager.getPolicy(getProjectId()); }
@Test public void testGetPolicy() { expect(resourceManager.getOptions()).andReturn(mockOptions).times(1); expect(resourceManager.getPolicy(PROJECT_ID)).andReturn(POLICY); replay(resourceManager); initializeProject(); assertEquals(POLICY, project.getPolicy()); }
@Test public void testGetPolicy() { assertNull(RESOURCE_MANAGER.getPolicy(COMPLETE_PROJECT.getProjectId())); RESOURCE_MANAGER.create(COMPLETE_PROJECT); RESOURCE_MANAGER.replacePolicy(COMPLETE_PROJECT.getProjectId(), POLICY); Policy retrieved = RESOURCE_MANAGER.getPolicy(COMPLETE_PROJECT.getProjectId()); assertEquals(POLICY.getBindings(), retrieved.getBindings()); assertNotNull(retrieved.getEtag()); assertEquals(0, retrieved.getVersion()); }
@Test public void testReplacePolicy() { try { RESOURCE_MANAGER.replacePolicy("nonexistent-project", POLICY); fail("Project doesn't exist."); } catch (ResourceManagerException e) { assertEquals(403, e.getCode()); assertTrue(e.getMessage().endsWith("project was not found.")); } RESOURCE_MANAGER.create(PARTIAL_PROJECT); Policy oldPolicy = RESOURCE_MANAGER.getPolicy(PARTIAL_PROJECT.getProjectId()); RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.getProjectId(), POLICY); try { RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.getProjectId(), oldPolicy); fail("Policy with an invalid etag didn't cause error."); } catch (ResourceManagerException e) { assertEquals(409, e.getCode()); assertTrue(e.getMessage().contains("Policy etag mismatch")); } String originalEtag = RESOURCE_MANAGER.getPolicy(PARTIAL_PROJECT.getProjectId()).getEtag(); Policy newPolicy = RESOURCE_MANAGER.replacePolicy(PARTIAL_PROJECT.getProjectId(), POLICY); assertEquals(POLICY.getBindings(), newPolicy.getBindings()); assertNotNull(newPolicy.getEtag()); assertNotEquals(originalEtag, newPolicy.getEtag()); }
/** * Returns the IAM access control policy for this project. Returns {@code null} if the resource * does not exist or if you do not have adequate permission to view the project or get the policy. * * @return the IAM policy for the project * @throws ResourceManagerException upon failure * @see <a href= * "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/getIamPolicy"> * Resource Manager getIamPolicy</a> */ public Policy getPolicy() { return resourceManager.getPolicy(projectId()); }