/** * Sets the IAM access control policy for this project. Replaces any existing policy. It is * recommended that you use the read-modify-write pattern. See code samples and important details * of replacing policies in the documentation for {@link ResourceManager#replacePolicy}. * * @return the newly set IAM policy for this project * @throws ResourceManagerException upon failure * @see <a href= * "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/setIamPolicy"> * Resource Manager setIamPolicy</a> */ public Policy replacePolicy(Policy newPolicy) { return resourceManager.replacePolicy(getProjectId(), newPolicy); }
@Test public void testReplacePolicy() { expect(resourceManager.getOptions()).andReturn(mockOptions).times(1); expect(resourceManager.replacePolicy(PROJECT_ID, POLICY)).andReturn(POLICY); replay(resourceManager); initializeProject(); assertEquals(POLICY, project.replacePolicy(POLICY)); }
@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()); }
@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()); }
/** * Sets the IAM access control policy for this project. Replaces any existing policy. It is * recommended that you use the read-modify-write pattern. See code samples and important details * of replacing policies in the documentation for {@link ResourceManager#replacePolicy}. * * @return the newly set IAM policy for this project * @throws ResourceManagerException upon failure * @see <a href= * "https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/setIamPolicy"> * Resource Manager setIamPolicy</a> */ public Policy replacePolicy(Policy newPolicy) { return resourceManager.replacePolicy(projectId(), newPolicy); }