Search in sources :

Example 1 with Bindings

use of com.google.api.services.storage.model.Policy.Bindings in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testSetIamPolicy.

@Test
public void testSetIamPolicy() {
    com.google.api.services.storage.model.Policy preCommitApiPolicy = new com.google.api.services.storage.model.Policy().setBindings(ImmutableList.of(new Bindings().setMembers(ImmutableList.of("allUsers")).setRole("roles/storage.objectViewer"), new Bindings().setMembers(ImmutableList.of("user:test1@gmail.com", "user:test2@gmail.com")).setRole("roles/storage.objectAdmin"), new Bindings().setMembers(ImmutableList.of("group:test-group@gmail.com")).setRole("roles/storage.admin"))).setEtag(POLICY_ETAG1);
    // postCommitApiPolicy is identical but for the etag, which has been updated.
    com.google.api.services.storage.model.Policy postCommitApiPolicy = new com.google.api.services.storage.model.Policy().setBindings(ImmutableList.of(new Bindings().setMembers(ImmutableList.of("allUsers")).setRole("roles/storage.objectViewer"), new Bindings().setMembers(ImmutableList.of("user:test1@gmail.com", "user:test2@gmail.com")).setRole("roles/storage.objectAdmin"), new Bindings().setMembers(ImmutableList.of("group:test-group@gmail.com")).setRole("roles/storage.admin"))).setEtag(POLICY_ETAG2);
    Policy postCommitLibPolicy = Policy.newBuilder().addIdentity(StorageRoles.objectViewer(), Identity.allUsers()).addIdentity(StorageRoles.objectAdmin(), Identity.user("test1@gmail.com"), Identity.user("test2@gmail.com")).addIdentity(StorageRoles.admin(), Identity.group("test-group@gmail.com")).setEtag(POLICY_ETAG2).build();
    EasyMock.expect(storageRpcMock.getIamPolicy(BUCKET_NAME1)).andReturn(API_POLICY1);
    EasyMock.expect(storageRpcMock.setIamPolicy(eq(BUCKET_NAME1), eqApiPolicy(preCommitApiPolicy))).andReturn(postCommitApiPolicy);
    EasyMock.replay(storageRpcMock);
    initializeService();
    Policy currentPolicy = storage.getIamPolicy(BUCKET_NAME1);
    Policy updatedPolicy = storage.setIamPolicy(BUCKET_NAME1, currentPolicy.toBuilder().addIdentity(StorageRoles.admin(), Identity.group("test-group@gmail.com")).build());
    assertEquals(updatedPolicy, postCommitLibPolicy);
}
Also used : ApiPolicyMatcher.eqApiPolicy(com.google.cloud.storage.testing.ApiPolicyMatcher.eqApiPolicy) Policy(com.google.cloud.Policy) Bindings(com.google.api.services.storage.model.Policy.Bindings) Test(org.junit.Test)

Example 2 with Bindings

use of com.google.api.services.storage.model.Policy.Bindings in project google-cloud-java by GoogleCloudPlatform.

the class PolicyHelper method convertToApiPolicy.

static com.google.api.services.storage.model.Policy convertToApiPolicy(Policy policy) {
    List<Bindings> bindings = new ArrayList<>(policy.getBindings().size());
    for (Map.Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
        List<String> members = new ArrayList<>(entry.getValue().size());
        for (Identity identity : entry.getValue()) {
            members.add(identity.strValue());
        }
        bindings.add(new Bindings().setMembers(members).setRole(entry.getKey().getValue()));
    }
    return new com.google.api.services.storage.model.Policy().setBindings(bindings).setEtag(policy.getEtag());
}
Also used : Policy(com.google.cloud.Policy) Set(java.util.Set) ArrayList(java.util.ArrayList) Bindings(com.google.api.services.storage.model.Policy.Bindings) Role(com.google.cloud.Role) Identity(com.google.cloud.Identity) Map(java.util.Map)

Example 3 with Bindings

use of com.google.api.services.storage.model.Policy.Bindings in project google-cloud-java by GoogleCloudPlatform.

the class PolicyHelperTest method testEquivalence.

@Test
public void testEquivalence() {
    Policy libPolicy = Policy.newBuilder().addIdentity(StorageRoles.objectViewer(), Identity.allUsers()).addIdentity(StorageRoles.objectAdmin(), Identity.user("test1@gmail.com"), Identity.user("test2@gmail.com")).setEtag(ETAG).build();
    com.google.api.services.storage.model.Policy apiPolicy = new com.google.api.services.storage.model.Policy().setBindings(ImmutableList.of(new Bindings().setMembers(ImmutableList.of("allUsers")).setRole("roles/storage.objectViewer"), new Bindings().setMembers(ImmutableList.of("user:test1@gmail.com", "user:test2@gmail.com")).setRole("roles/storage.objectAdmin"))).setEtag(ETAG);
    Policy actualLibPolicy = PolicyHelper.convertFromApiPolicy(apiPolicy);
    com.google.api.services.storage.model.Policy actualApiPolicy = PolicyHelper.convertToApiPolicy(libPolicy);
    assertEquals(libPolicy, actualLibPolicy);
    assertTrue(new ApiPolicyMatcher(apiPolicy).matches(actualApiPolicy));
}
Also used : Policy(com.google.cloud.Policy) ApiPolicyMatcher(com.google.cloud.storage.testing.ApiPolicyMatcher) Bindings(com.google.api.services.storage.model.Policy.Bindings) Test(org.junit.Test)

Aggregations

Bindings (com.google.api.services.storage.model.Policy.Bindings)3 Policy (com.google.cloud.Policy)3 Test (org.junit.Test)2 Identity (com.google.cloud.Identity)1 Role (com.google.cloud.Role)1 ApiPolicyMatcher (com.google.cloud.storage.testing.ApiPolicyMatcher)1 ApiPolicyMatcher.eqApiPolicy (com.google.cloud.storage.testing.ApiPolicyMatcher.eqApiPolicy)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1